Skip to content

Instantly share code, notes, and snippets.

@Fieel
Created November 19, 2019 09:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fieel/5f4d3a1b7eb24ad92cf3e34fe0bba486 to your computer and use it in GitHub Desktop.
Save Fieel/5f4d3a1b7eb24ad92cf3e34fe0bba486 to your computer and use it in GitHub Desktop.
Python script to read data from the ADC Expansion installed on a Onion Omega2 device. Due to space constraints (<6 MB) i had to use the included libraries such as urrlib for POSTing the data (in json format)).
#
# Script che legge le temperature dall'ADC expansion montata
# sull'Onion Omega2.
# Oltre a leggere i dati effettua pure le chiamate HTTP verso il webserver.
# https://docs.onion.io/omega2-docs/adc-expansion.html
# import vari
import math # x calcoli sensori
import json # x jsonificare prima del POST
from datetime import datetime # x loggare le date
# URLLIB per effettuare la chiamata POST
import urllib.request
import urllib.parse
# Libreria per leggere dall'espansione ADC Onion
from OmegaExpansion import AdcExp
adc = AdcExp.AdcExp()
# Costanti varie
API_ENDPOINT = 'http://web.server/iot/a8b9/'
# ###########################
# 1. Lettura valori sensori
# ###########################
# TMP36GZ sensore digitale temperatura
# Formula per ottenere Gradi Celsius
# (https://www.bc-robotics.com/tutorials/using-a-tmp36-temperature-sensor-with-arduino/)
a0 = adc.read_voltage(0)
temp_digital = (a0 - 0.5) * 100
# Fotoresistenza con resistenza da 10k ohm
# Ritorna un valore che aumenta con l'intensità della luce
a1 = adc.read_voltage(1)
# Sensore acqua, se bagnato ritorna != 0
a2 = adc.read_voltage(2)
# KY-013 sensore analogico temperatura
# Formula per calcolare i Gradi Celsuis partendo dalla resistenza del termistor
# (https://arduinomodules.info/ky-013-analog-temperature-sensor-module/)
Vo = adc.read_voltage(3)
# voltage to resistance, partendo dal fatto che ho un valore tra 0 e 5
a = (5 / Vo) -1
b = 10000 / a
# Equazione di Steinhart-hart
# https://en.wikipedia.org/wiki/Steinhart%E2%80%93Hart_equation
steinhart = b / 10000
steinhart = math.log(steinhart)
steinhart = steinhart / 3950
steinhart = steinhart + (1.0 / ( 25 + 273.15 ))
steinhart = 1.0 / steinhart
# kelvin -> celsius
temp_analog = steinhart - 273.15
# ###########################################################
# 2. Creazione dati da inserire nel body della richiesta POST
# ###########################################################
body = {
'temp_digital': temp_digital,
'temp_analog': temp_analog,
'lux': a1,
'water': a2
}
jsondataasbytes = json.dumps(body).encode('utf-8') # needs to be bytes
# ###############################################
# 3. Inviare i dati JSON via richiesta HTTP POST
# ###############################################
req = urllib.request.Request(API_ENDPOINT, data=jsondataasbytes)
req.add_header('Content-Type', 'application/json; charset=utf-8')
response = urllib.request.urlopen(req)
# ###################
# 4. Logga il risultato
# ##################
print("\n ########## ")
print(datetime.now().strftime(" %d/%m/%Y %H:%M:%S"), " :: ", response.getcode(), "\n", response.info())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment