-
Notifications
You must be signed in to change notification settings - Fork 2
/
awsiotpub.py
67 lines (50 loc) · 1.64 KB
/
awsiotpub.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
#!/usr/bin/python
import paho.mqtt.client as paho
import os
import socket
import ssl
import json
from time import sleep
from sense_hat import SenseHat
connflag = False
def on_connect(client, userdata, flags, rc):
global connflag
connflag = True
if rc==0:
print ("Connection status: successful")
elif rc==1:
print ("Connection status: Connection refused")
sense = SenseHat()
mqttc = paho.Client()
mqttc.on_connect = on_connect
awshost = "data.iot.us-west-2.amazonaws.com"
awsport = 8883
clientId = "rpisensehat-publisher"
thingName = "rpisensehat"
caPath = "rootCA.pem"
certPath = "3b60693e48-certificate.pem.crt"
keyPath = "3b60693e48-private.pem.key"
mqttc.tls_set(caPath, certfile=certPath, keyfile=keyPath, cert_reqs=ssl.CERT_REQUIRED, tls_version=ssl.PROTOCOL_TLSv1_2, ciphers=None)
mqttc.connect(awshost, awsport, keepalive=60)
mqttc.loop_start()
while True:
message = {}
device = {}
device['cpuTemperature'] = os.popen('vcgencmd measure_temp').readline().replace("temp=","").replace("'C\n","")
environment = {}
temp = {}
temp['basedOnHumidity'] = sense.get_temperature_from_humidity()
temp['basedOnPressure'] = sense.get_temperature_from_pressure()
environment['humidity'] = sense.get_humidity()
environment['pressure'] = sense.get_pressure()
environment['temperature'] = temp
message['device'] = device
message['environment'] = environment
jsonData = json.dumps(message)
if connflag == True:
mqttc.publish("environmentData", jsonData, qos=1)
print jsonData
sleep(10)
else:
print("waiting for connection...")
sleep(5)