forked from Thomas-Tsai/pms3003-g3
-
Notifications
You must be signed in to change notification settings - Fork 1
/
monitor.py
executable file
·46 lines (41 loc) · 1.11 KB
/
monitor.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
#!/usr/bin/python
import time
import datetime
import os
import httplib, urllib
import g5
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(16, GPIO.OUT)
p = GPIO.PWM(16, 50) # channel=16 frequency=50Hz
p.start(0)
air=g5.g5sensor()
try:
while True:
try:
pmdata=air.read("/dev/ttyAMA0")
except:
pmdata=[0,0,0,0,0,0,0,0,0,0,0,0]
continue
print pmdata
a = pmdata[5] * 2
if a > 100:
p.ChangeDutyCycle(100)
else:
p.ChangeDutyCycle(a)
# thingspeak
params = urllib.urlencode({'field1': pmdata[3], 'field2': pmdata[4], 'field3': pmdata[5], 'key':'YOUR_KEY'})
headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain"}
try:
tconn = httplib.HTTPConnection("api.thingspeak.com:80")
tconn.request("POST", "/update", params, headers)
response = tconn.getresponse()
data = response.read()
tconn.close()
except:
continue
time.sleep(300)
except KeyboardInterrupt:
pass
p.stop()
GPIO.cleanup()