forked from krohak/Protei_Rpi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend_data.py
74 lines (54 loc) · 1.62 KB
/
send_data.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
import paho.mqtt.publish as publish
import sys
import datetime
import hmac
from sense_hat import SenseHat
sense = SenseHat()
port = 1883 # Default port for unencrypted MQTT
x_cord=float(sys.argv[1])
y_cord=float(sys.argv[2])
print(sys.argv[1])
print(sys.argv[2])
print(sys.argv[3])
def get_details():
if(sys.argv[3]=="1"):
hostname = "iot.eclipse.org" # Sandbox broker
topic_far="TOPIC1"
return hostname,topic_far
elif(sys.argv[3]=="2"):
hostname = "192.168.1.1"
topic_far="TOPIC2"
return hostname,topic_far
try:
device="DEVICE#"
#coord=[114.102757,22.280894]
coord=[x_cord,y_cord]
humidity = str(sense.get_humidity())
#print(humidity)
temp = str(sense.get_temperature())
#print(temp)
pressure = str(sense.get_pressure())
#print(pressure)
north = str(sense.get_compass())
#print(north)
date=str(datetime.datetime.now())
#print(date)
qstr=('{"coordinates":%s,"properties":{"Device":%s,"Time":"%s","Temperature":%s,"Pressure":%s,"Humidity":%s,"Magnetometer":%s}}'%(coord,device,date,temp,pressure,humidity,north))
digest_maker = hmac.new('PASSWORD')
digest_maker.update(qstr)
digest = digest_maker.hexdigest()
print(digest)
to_send=digest+'_'+qstr
#to_send="digest"+'_'+qstr
hostname,topic_far=get_details()
print(hostname)
print(topic_far)
publish.single(topic_far, payload=str(to_send),
qos=1,
hostname=hostname,
port=port)
print(to_send)
except Exception,e:
print("failed")
print(e)
sys.exit(1)