Last active
June 26, 2020 19:42
-
-
Save atchoo78/fa3fbe5b06d38380058b5eb38c1372a7 to your computer and use it in GitHub Desktop.
HTTP server on Raspberry Pi providing data from 5 different temperature & humidity sensors. Run "pip3 install -r requirements.txt" first.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"id": 1, | |
"name": "DS18B20", | |
"model": "ds18b20", | |
"battery": 254, | |
"deviceType": "0000000D-0001-1000-2005-ACCA54000000", | |
"uuid": "0000000D-0001-1000-2005-ACCA54000000", | |
"data": [ | |
{ | |
"name": "Temperature", | |
"type": "temperature", | |
"value": "29.062", | |
"unit": "°C" | |
} | |
] | |
}, | |
{ | |
"id": 2, | |
"name": "DHT-11", | |
"model": "dht-11", | |
"battery": 254, | |
"deviceType": "0000000D-0001-1000-2005-ACCA54000000", | |
"uuid": "0000000D-0001-1000-2005-ACCA54000000", | |
"data": [ | |
{ | |
"name": "Temperature", | |
"type": "temperature", | |
"value": "29.0", | |
"unit": "°C" | |
}, | |
{ | |
"name": "Humidity", | |
"type": "humidity", | |
"value": "41.0", | |
"unit": "%" | |
} | |
] | |
}, | |
{ | |
"id": 3, | |
"name": "DHT-22", | |
"model": "dht-22", | |
"battery": 254, | |
"deviceType": "0000000D-0001-1000-2005-ACCA54000000", | |
"uuid": "0000000D-0001-1000-2005-ACCA54000000", | |
"data": [ | |
{ | |
"name": "Temperature", | |
"type": "temperature", | |
"value": "30.100000381469727", | |
"unit": "°C" | |
}, | |
{ | |
"name": "Humidity", | |
"type": "humidity", | |
"value": "34.400001525878906", | |
"unit": "%" | |
} | |
] | |
}, | |
{ | |
"id": 4, | |
"name": "AM-2320", | |
"model": "am2320", | |
"battery": 254, | |
"deviceType": "0000000D-0001-1000-2005-ACCA54000000", | |
"uuid": "0000000D-0001-1000-2005-ACCA54000000", | |
"data": [ | |
{ | |
"name": "Temperature", | |
"type": "temperature", | |
"value": 29.1, | |
"unit": "°C" | |
}, | |
{ | |
"name": "Humidity", | |
"type": "humidity", | |
"value": 44.6, | |
"unit": "%" | |
} | |
] | |
}, | |
{ | |
"id": 5, | |
"name": "RPi CPU Temp", | |
"model": "Raspberry Pi 4B", | |
"battery": 50, | |
"deviceType": "0000000D-0001-1000-2005-ACCA54000000", | |
"uuid": "0000000D-0001-1000-2005-ACCA54000000", | |
"data": [ | |
{ | |
"name": "CPU temperature", | |
"type": "temperature", | |
"value": "56.0", | |
"unit": "°C" | |
} | |
] | |
} | |
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Adafruit-Blinka==4.5.0 | |
adafruit-circuitpython-am2320==1.2.2 | |
adafruit-circuitpython-busdevice==4.3.1 | |
Adafruit-DHT==1.4.0 | |
Adafruit-PlatformDetect==2.6.1 | |
Adafruit-PureIO==1.1.4 | |
Adafruit-Python-DHT==1.4.0 | |
crcmod==1.7 | |
RPi.GPIO==0.7.0 | |
urllib3==1.24.1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from http.server import BaseHTTPRequestHandler,HTTPServer | |
from urllib.parse import urlparse | |
from time import * | |
import sys, json | |
import os, glob | |
import board | |
import busio | |
import adafruit_am2320 | |
import Adafruit_DHT | |
# Insert your preferred URL endpoint here: | |
endPoint = '/sensors' | |
sensors = [] | |
# Set to True to enable. Otherwise set to False | |
DS18B20 = True | |
DHT11 = True | |
DHT22 = True | |
AM2320 = True | |
# Section for reading DS18B20 type temperature sensors | |
if(DS18B20): | |
sys_dir = '/sys/bus/w1/devices/' | |
raw_sensor_dir = glob.glob(sys_dir + '28*')[0] | |
raw_sensor_file = raw_sensor_dir + '/w1_slave' | |
def read_temp_raw(): | |
f = open(raw_sensor_file, 'r') | |
lines = f.readlines() | |
f.close() | |
return lines | |
def DS18B20(): | |
lines = read_temp_raw() | |
while lines[0].strip()[-3:] != 'YES': | |
time.sleep(0.2) | |
lines = read_temp_raw() | |
equals_pos = lines[1].find('t=') | |
if equals_pos != -1: | |
temp_string = lines[1][equals_pos+2:] | |
temp_c = float(temp_string) / 1000.0 | |
temp_f = temp_c * 9.0 / 5.0 + 32.0 | |
return str(temp_c) | |
sensors.append( | |
{ | |
"id": 1, | |
"name": "DS18B20", | |
"model": "ds18b20", | |
"battery": 254, | |
"deviceType": "0000000D-0001-1000-2005-ACCA54000000", | |
"uuid": "0000000D-0001-1000-2005-ACCA54000000", | |
"data": | |
[ | |
{ | |
"name": "Temperature", | |
"type": "temperature", | |
"value": DS18B20(), | |
"unit": "\u00b0C" | |
} | |
] | |
}) | |
# Section for reading DHT-11 and/or DHT-22 temperature/humidity sensors | |
if(DHT11): | |
DHT11_GPIO = 16 # GPIO that the sensor is connected to | |
dht11_humidity, dht11_temperature = Adafruit_DHT.read_retry(11, DHT11_GPIO) | |
sensors.append( | |
{ | |
"id": 2, | |
"name": "DHT-11", | |
"model": "dht-11", | |
"battery": 254, | |
"deviceType": "0000000D-0001-1000-2005-ACCA54000000", | |
"uuid": "0000000D-0001-1000-2005-ACCA54000000", | |
"data": | |
[ | |
{ | |
"name": "Temperature", | |
"type": "temperature", | |
"value": "{0:0}".format(dht11_temperature, dht11_humidity), | |
"unit": "\u00b0C" | |
}, | |
{ | |
"name": "Humidity", | |
"type": "humidity", | |
"value": "{1:0}".format(dht11_temperature, dht11_humidity), | |
"unit": "%" | |
} | |
] | |
}) | |
if(DHT22): | |
DHT22_GPIO = 12 # GPIO that the sensor is connected to | |
dht22_humidity, dht22_temperature = Adafruit_DHT.read_retry(22, DHT22_GPIO) | |
# Temporary fix for the insanely high resolution on the DHT-22, resulting in 10 decimals as default: | |
dht22_temp = float("{0:0}".format(dht22_temperature, dht22_humidity)) | |
dht22_hum = float("{1:0}".format(dht22_temperature, dht22_humidity)) | |
dht22_tr = round(dht22_temp, 2) | |
dht22_hr = round(dht22_hum, 2) | |
sensors.append( | |
{ | |
"id": 3, | |
"name": "DHT-22", | |
"model": "dht-22", | |
"battery": 254, | |
"deviceType": "0000000D-0001-1000-2005-ACCA54000000", | |
"uuid": "0000000D-0001-1000-2005-ACCA54000000", | |
"data": | |
[ | |
{ | |
"name": "Temperature", | |
"type": "temperature", | |
"value": dht22_tr, | |
"unit": "\u00b0C" | |
}, | |
{ | |
"name": "Humidity", | |
"type": "humidity", | |
"value": dht22_hr, | |
"unit": "%" | |
} | |
] | |
}) | |
else: | |
pass | |
# Section for reading the AM2320 Temperature/Humidity sensor | |
if(AM2320): | |
i2c = busio.I2C(board.SCL, board.SDA) | |
am = adafruit_am2320.AM2320(i2c) | |
am2320_temp = am.temperature | |
am2320_humidity = am.relative_humidity | |
sensors.append( | |
{ | |
"id": 4, | |
"name": "AM-2320", | |
"model": "am2320", | |
"battery": 254, | |
"deviceType": "0000000D-0001-1000-2005-ACCA54000000", | |
"uuid": "0000000D-0001-1000-2005-ACCA54000000", | |
"data": | |
[ | |
{ | |
"name": "Temperature", | |
"type": "temperature", | |
"value": am2320_temp, | |
"unit": "\u00b0C" | |
}, | |
{ | |
"name": "Humidity", | |
"type": "humidity", | |
"value": am2320_humidity, | |
"unit": "%" | |
} | |
] | |
}) | |
else: | |
pass | |
# If you get a "VCHI initialization failed" error, you must add your user to the "video" group: | |
# sudo usermod -aG video <YOUR_USERNAME> | |
cputemp_ = os.popen("vcgencmd measure_temp").readline()[5:] | |
cputemp = cputemp_[:4] | |
sensors.append( | |
{ | |
"id": 5, | |
"name": "RPi CPU Temp", | |
"model": "Raspberry Pi 4B", # or whatever | |
"battery": 50, | |
"deviceType": "0000000D-0001-1000-2005-ACCA54000000", | |
"uuid": "0000000D-0001-1000-2005-ACCA54000000", | |
"data": | |
[ | |
{ | |
"name": "CPU temperature", | |
"type": "temperature", | |
"value": cputemp, | |
"unit": "\u00b0C" | |
} | |
] | |
}) | |
class SensorServer(BaseHTTPRequestHandler): | |
def do_GET(self): | |
self._set_headers() | |
if self.path == endPoint: | |
sensorData = json.dumps(sensors) | |
return (self.send_response(200), self.send_header("Content-type", "application/json"), self.end_headers(), self.wfile.write(sensorData.encode(encoding='utf-8'))) | |
def run(server_class=HTTPServer, handler_class=SensorServer, port=8080): | |
server_address = ('', port) | |
httpd = server_class(server_address, handler_class) | |
print('Starting Sensor server...') | |
try: | |
httpd.serve_forever() | |
except KeyboardInterrupt: | |
pass | |
httpd.server_close() | |
print('\n Stopping Sensor server...') | |
if __name__ == "__main__": | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment