-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetenvmon.py
More file actions
executable file
·64 lines (47 loc) · 1.85 KB
/
netenvmon.py
File metadata and controls
executable file
·64 lines (47 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python3
# For general information on DHT sensors in Python, see Adafruit's helpful tutorials
# https://learn.adafruit.com/dht-humidity-sensing-on-raspberry-pi-with-gdocs-logging/python-setup
import Adafruit_DHT
import datetime
import json
import time
# Port 80 is easy but requires the script to be run as root
# If you'd prefer not to run as root, choose another port such as 8080
HTTP_PORT = 80
DHT_SENSOR = Adafruit_DHT.DHT11
DHT_0_PIN = 4
DHT_1_PIN = 18
# Occassionally the sensor will return null values, especially if polled repeatedly in less than 3 seconds.
# This code will retry up to the MAX_TRIES number of times.
MAX_TRIES = 10
from http.server import HTTPServer, BaseHTTPRequestHandler
class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
tries = 0
humidity_0 = None
temperature_0 = None
while (humidity_0 is None and temperature_0 is None and tries < MAX_TRIES):
humidity_0, temperature_0 = Adafruit_DHT.read(DHT_SENSOR, DHT_0_PIN)
tries += 1
tries = 0
humidity_1 = None
temperature_1 = None
while (humidity_1 is None and temperature_1 is None and tries < MAX_TRIES):
humidity_1, temperature_1 = Adafruit_DHT.read(DHT_SENSOR, DHT_1_PIN)
tries += 1
th_data = {
'sensor_0': {
'temperature': temperature_0,
'humidity': humidity_0
},
'sensor_1': {
'temperature': temperature_1,
'humidity': humidity_1
}
}
json_data = json.dumps(th_data)
self.send_response(200)
self.end_headers()
self.wfile.write(bytes(json_data, 'utf-8'))
httpd = HTTPServer(('', HTTP_PORT), SimpleHTTPRequestHandler)
httpd.serve_forever()