-
Notifications
You must be signed in to change notification settings - Fork 1
/
iot.py
65 lines (48 loc) · 1.5 KB
/
iot.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
from twython import Twython
import json, requests
import RPi.GPIO as GPIO
import time
from datetime import date
from datetime import datetime
from datetime import timedelta
CONSUMER_KEY = 'xxxxxxxxxxxxxx'
CONSUMER_SECRET = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'
ACCESS_KEY = 'xxxxxxxxxxxxxx-xxxxxxxxxxxxxx'
ACCESS_SECRET = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'
api = Twython(CONSUMER_KEY,CONSUMER_SECRET,ACCESS_KEY,ACCESS_SECRET)
xxxxxxxxxxxxxx
GPIO.setwarnings(False)
url = 'http://192.168.1.76/gazou/site/controleur/Controleur-CalcDiff.php'
exec_time = 5 * 60 * 1000 #5 minutes
starttime = date(2013, 10, 1)
#setup GPIO
GPIO.setmode(GPIO.BOARD) # set board mode to Broadcom
GPIO.setup(7, GPIO.OUT) # set up pin 17
GPIO.setup(11, GPIO.OUT) # set up pin 18
GPIO.setup(13, GPIO.OUT) # set up pin 18
print("----start--- exec time: (ms)" + str(exec_time))
def execute(dt):
_url = url + "?date=" + str(dt)
#print "fetch data for " + str(dt)
resp = requests.get(url=_url)
result = json.loads(resp.text)
data = result["data"]
print (resp.text)
GPIO.output(13, data[0]) # turn on pin 17
GPIO.output(11, data[1]) # turn on pin 18
GPIO.output(7, data[2]) # turn on pin 18
try:
if (data[2] == 1):
msg = 'bravo! 10% de CO2 en moins, cc #HackTheDatas'
api.update_status(status=msg)
except:
pass
curr_time = 0
curr_date = starttime
while (curr_time < exec_time):
execute(curr_date)
time.sleep(3)
curr_date = curr_date + timedelta(days=5)
curr_time = curr_time + 3 * 1000
print("----stop---")
GPIO.cleanup()