No llamamos a este artículo un tutorial ya que no se explicarán todos los pasos de la configuración del sensor. Puede ser la idea de este artículo mostrar el panorama general de la instalación de sensores en una Raspberry Pi bajo el framework Istsos que implementa el estándar SOS.
Página de github de Istsos:
Video
La bitácora esta separada en varias partes
Parte física
Instale el sensor como se explica en las figuras y fotos.
En un terminal de Raspbian
Cuando instale Raspian, establezca el nombre de usuario en "istsos"
Importa el paquete para leer el sensor de temperatura.
pip install python3-w1thermsensor
pip install python-dotenv
Habilitar el acceso a los pines GPIO
sudo raspi-config
3 Interface Options > I8 Remote GPIO > Yes
Habilitar la superposición de sensores
Necesitas actualizar tu archivo de arranque
sudo nano /boot/config.txt
Luego al final agrega lo siguiente
dtoverlay=w1-gpio, gpiopin=4
Grabe el archivo
Finalmente reinicie
sudo reboot
Obtener el id del sensor
cd /sys/bus/w1/devices
ls
Verifique la identificación del sensor como 28-XXXXXXXXXXXX donde X.. es la identificación del sensor
En la carpeta /opt/istsos, cree un archivo tempInsertObservation.py
sudo mkdir /opt/istsos
sudo nano /opt/istsos/tempInsertObservation.py
Pegue este código
#!/usr/bin/env python # coding: utf-8 from requests.auth import HTTPBasicAuth from requests.packages.urllib3.exceptions import InsecureRequestWarning import requests, json, os from datetime import datetime from dotenv import load_dotenv from dateutil.tz import tzlocal from dotenv import load_dotenv from w1thermsensor import W1ThermSensor, Sensor sensor = W1ThermSensor(Sensor.DS18B20, '0000071dacba') #create a file env.txt with #USER='XXXX' #PASSWORD='XXXX' load_dotenv('/opt/istsos/env.txt') user = os.getenv('ISTSOSUSER') password = os.getenv('ISTSOSPASSWORD') requests.packages.urllib3.disable_warnings(InsecureRequestWarning) auth = HTTPBasicAuth(user, password) url = 'https://apps.hatarilabs.com/istsos' service = 'temperature' procedure = 'genericSensor' off = 'tempnetwork' # Requesting a describe sensor mainly to store the assignedSensorId resProcedure = requests.get( '%s/wa/istsos/services/%s/procedures/%s' % ( url, service, procedure), auth=auth) print(resProcedure) procedureData = resProcedure.json()['data'] # Preparing "io" object to send observedProperties = requests.get( '%s/wa/istsos/services/%s/operations/getobservation/offerings/' '%s/procedures/%s/observedproperties/:/eventtime/last' % ( url, service, off, procedure), params={ "qualityIndex": "False" }, auth=auth) io = { "AssignedSensorId": procedureData['assignedSensorId'], "ForceInsert": "true", "Observation": observedProperties.json()['data'][0] } #humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN) temperature = sensor.get_temperature() print("Recorded temperature of %s celsius" % temperature) eventtime = datetime.now(tzlocal()) io["Observation"]['samplingTime'] = { "beginPosition": eventtime.isoformat(), "endPosition": eventtime.isoformat() } ob = [eventtime.isoformat(),str(temperature)] io["Observation"]['result']['DataArray']['values'] = [ob] resInsert = requests.post('%s/wa/istsos/services/%s/operations/insertobservation'%(url,service), data=json.dumps(io), auth=auth)
Defina un trabajo de cron:
crontab -e
Añada la siguiente línea:
* * * * * /usr/bin/python /opt/istsos/tempInsertObservation.py
Ejecute el comando una vez y verifique que se haya realizado un registro:
/usr/bin/python /opt/istsos/tempInsertObservation.py
Compruebe si el cronjob se está ejecutando:
sudo tail -100 /var/log/syslog
En otra computadora
Recupere los datos de salida como un dataframe de pandas con este cuaderno de Ipython:
from requests.auth import HTTPBasicAuth
from requests.packages.urllib3.exceptions import InsecureRequestWarning
import matplotlib.pyplot as plt
import requests, json, os
from dotenv import load_dotenv
import pandas as pd
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
#create a file envViewer.txt with
#USER='XXXX'
#PASSWORD='XXXX'
load_dotenv('envViewer.txt')
user = os.getenv('ISTSOSUSER')
password = os.getenv('ISTSOSPASSWORD')
auth = HTTPBasicAuth(user, password)
url = 'https://apps.hatarilabs.com/istsos'
service = 'temperature'
procedure = 'genericSensor'
off = 'tempnetwork'
beginTime = "2023-06-01T00:00:00-05:00"
endTime = "2023-06-01T20:00:00-05:00"
# Preparing "io" object to send
observedProperties = requests.get(
'%s/wa/istsos/services/%s/operations/getobservation/offerings/'
'%s/procedures/%s/observedproperties/:/eventtime/%s/%s' % (
url, service, off, procedure,beginTime,endTime),
params={
"qualityIndex": "False"
}, auth=auth)
observedList = observedProperties.json()['data'][0]['result']['DataArray']['values']
observedList[:5]
[['2023-06-01T10:49:04.650563-05:00', 23.625],
['2023-06-01T10:50:04.170496-05:00', 23.625],
['2023-06-01T10:51:05.050523-05:00', 30.5],
['2023-06-01T10:52:04.490570-05:00', 29.125],
['2023-06-01T10:53:04.810532-05:00', 28.0]]
observedDf = pd.DataFrame(columns=['Date','Temp'])
observedDf
Date | Temp |
---|
observedDf['Date'] = pd.to_datetime([i[0] for i in observedList])
observedDf['Temp'] = [i[1] for i in observedList]
observedDf = observedDf.set_index('Date')
observedDf.head()
Temp | |
---|---|
Date | |
2023-06-01 10:49:04.650563-05:00 | 23.625 |
2023-06-01 10:50:04.170496-05:00 | 23.625 |
2023-06-01 10:51:05.050523-05:00 | 30.500 |
2023-06-01 10:52:04.490570-05:00 | 29.125 |
2023-06-01 10:53:04.810532-05:00 | 28.000 |
observedDf.plot()
<Axes: xlabel='Date'>