Skip to content

Instantly share code, notes, and snippets.

@aadps
Last active July 3, 2021 16:46
Show Gist options
  • Save aadps/a76e7ddb6a9c732ca9b3b49f683b4ea7 to your computer and use it in GitHub Desktop.
Save aadps/a76e7ddb6a9c732ca9b3b49f683b4ea7 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import sys
import Adafruit_DHT
import json
import time
from datetime import datetime
def update(hum, temp, t):
with open('/var/www/html/data.json', 'r+') as f:
data = json.load(f)
data['hum'].append(hum);
data['temp'].append(temp);
data['t'].append(t);
if len(data['hum'])>500:
data['hum'].pop(0);
data['temp'].pop(0);
data['t'].pop(0);
f.seek(0);
f.truncate();
json.dump(data, f);
return
hsum, tsum, c=0, 0, 0
while True:
hum, temp = Adafruit_DHT.read_retry(22, 4)
if hum is None or temp is None or hum > 100:
continue
hsum+=hum; tsum+=temp; c+=1;
if c>9:
update(hsum/c, tsum/c, datetime.now().strftime('%Y/%m/%d %H:%M'));
hsum, tsum, c=0, 0, 0
time.sleep(60)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment