-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththingspeak.py
executable file
·77 lines (60 loc) · 2.17 KB
/
thingspeak.py
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env python3
# https://www.raspberrypi-spy.co.uk/2015/06/basic-temperature-logging-to-the-internet-with-raspberry-pi/
import time, os, sys
import urllib.request, urllib.parse, urllib.error, urllib.request, urllib.error, urllib.parse
import bme280
from tsl2561 import TSL2561, TSL2561_GAIN_16X
tsl = TSL2561(gain=TSL2561_GAIN_16X, autogain=True)
import paho.mqtt.client as mqtt
client = mqtt.Client()
client.connect("localhost")
client.loop_start()
import json
import secrets
class Config:
interval = 10
url = 'https://api.thingspeak.com/update'
key = secrets.thingspeak
def postThingspeak(temp, pres, humi, lux, bb, ir):
values = { 'api_key': Config.key, 'field1': temp, 'field2': pres, 'field3': humi, 'field4': lux, 'field5': bb, 'field6': ir }
postdata = urllib.parse.urlencode(values).encode('ascii')
req = urllib.request.Request(Config.url, postdata)
log = time.strftime("%d-%m-%Y,%H:%M:%S") + ","
log += "{:.2f}C".format(temp) + ","
log += "{:.2f}mBar".format(pres) + ","
log += "{:.2f}per".format(humi) + ","
log += "%slux" % lux + ","
log += "%sbb" % bb + ","
log += "%sir" % ir + ","
try:
response = urllib.request.urlopen(req, None, 5)
html_string = response.read().decode('ascii')
response.close()
log += 'Update ' + html_string
except urllib.error.HTTPError as e:
log += 'Server could not fulfill the request. Error code: ' + str(e.code)
except urllib.error.URLError as e:
log += 'Failed to reach server. Reason: ' + str(e.reason)
except Exception as e:
log += type(e).__name__ + ': ' + str(e)
print(log)
sys.stdout.flush()
def main():
i = 6
while True:
temp,pres,humi = bme280.readBME280All()
r_bme280 = { 'temperature': temp, 'pressure': pres, 'humidity': humi }
bb, ir = tsl._get_luminosity()
lux = tsl._calculate_lux(bb, ir)
r_tsl2561 = { 'lux': lux, 'broadband': bb, 'infrared': ir }
# mqtt
client.publish("sensors/bme280", json.dumps(r_bme280))
client.publish("sensors/tsl2561", json.dumps(r_tsl2561))
if i == 6:
postThingspeak(temp, pres, humi, lux, bb, ir)
i = 1
else:
i += 1
time.sleep(Config.interval)
if __name__=="__main__":
main()