forked from krohak/Protei_Rpi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbroker_pi.py
112 lines (93 loc) · 2.5 KB
/
broker_pi.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
from __future__ import print_function
import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish
import time
import hmac
import json
count=0
max=3
hostname = "iot.eclipse.org"
topic_near="krohak/near"
port=1883
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe("CHANNEL1")
def on_connect2(client, userdata, rc):
print("Connected with result code2 "+str(rc))
client.subscribe("CHANNEL2")
def on_message(client, userdata, msg):
if(msg.topic=="CHANNEL1"):
hash=(msg.payload.decode("utf-8")).split("_")[0]
print(("Received Hash: %s")%(hash))
data=(msg.payload.decode("utf-8")).split("_")[1]
digest_maker = hmac.new('PASSWORD')
digest_maker.update(data)
digest = digest_maker.hexdigest()
print(("Computed Hash: %s")%(digest))
if digest == hash:
print("Valid data")
with open('log.json', 'a') as f:
print(data,file=f)
global count
count+=1
print(count)
else:
print("Invalid data")
def on_message2(client, userdata, msg):
if(msg.topic=="CHANNEL2"):
print(("Received new threshold value: %s")%msg.payload.decode("utf-8"))
try:
global max
max=int(msg.payload.decode("utf-8"))
except Exception,e:
print(e)
client = mqtt.Client()
client2 = mqtt.Client()
client.connect("localhost",1883,60)
client2.connect("iot.eclipse.org",1883)
client.on_connect = on_connect
client.on_message = on_message
client2.on_connect = on_connect2
client2.on_message = on_message2
#client.loop_start()
while True:
client.loop_start()
client2.loop_start()
time.sleep(2)
global max
if count>=max:
client.loop_stop()
client2.loop_stop()
cord_list=[]
avg_temp=0
avg_hum=0
avg_pres=0
f=open('log.json','r+')
for a in f:
packet=json.loads(a)
cord_list+=[packet["coordinates"]]
avg_temp+=packet["properties"]["Temperature"]
avg_hum+=packet["properties"]["Humidity"]
avg_pres+=packet["properties"]["Pressure"]
avg_temp/=count
avg_hum/=count
avg_pres/=count
qstr=('{"coordinates":%s,"properties":{"Temperature":%s,"Pressure":%s,"Humidity":%s}}'%(cord_list,avg_temp,avg_pres,avg_hum))
print(qstr)
digest_maker = hmac.new('PASSWORD')
digest_maker.update(qstr)
digest = digest_maker.hexdigest()
to_send=digest+'_'+qstr
try:
publish.single(topic_near, payload=str(to_send),
qos=1,
hostname=hostname,
port=port)
f.seek(0)
f.truncate()
f.close()
count=0
print("Data sent")
except Exception,e:
print(e)
time.sleep(2)