-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws_pubsub_readings.py
69 lines (58 loc) · 2.07 KB
/
aws_pubsub_readings.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
from AWSIoTPythonSDK.MQTTLib import AWSIoTMQTTClient
import serial
#from rpi_lcd import LCD
from time import sleep
# Get serial to fetch data from arduino
ser = serial.Serial('/dev/ttyUSB0', 9600)
#lcd = LCD()
def customCallback(client, userdata, message):
print("Received a new message: ")
print(message.payload)
print("from topic: ")
print(message.topic)
print("--------------\n\n")
host = "a290uc2ksy4m1j-ats.iot.us-west-2.amazonaws.com"
rootCAPath = "/home/pi/Templates/Project/smartgarden/rootca.pem"
certificatePath = "/home/pi/Templates/Project/smartgarden/certificate.pem.crt"
privateKeyPath = "/home/pi/Templates/Project/smartgarden/private.pem.key"
my_rpi = AWSIoTMQTTClient("basicPubSub")
my_rpi.configureEndpoint(host, 8883)
my_rpi.configureCredentials(rootCAPath, privateKeyPath, certificatePath)
my_rpi.configureOfflinePublishQueueing(-1) # Infinite offline Publish queueing
my_rpi.configureDrainingFrequency(2) # Draining: 2 Hz
my_rpi.configureConnectDisconnectTimeout(10) # 10 sec
my_rpi.configureMQTTOperationTimeout(5) # 5 sec
# Connect and subscribe to AWS IoT
my_rpi.connect()
my_rpi.subscribe("smartgarden/readings", 1, customCallback)
#lcd.text(" SMART GARDEN ", 1)
#lcd.text("* Welcome back *", 2)
sleep(2)
#lcd.clear()
# Publish to the same topic in a loop forever
loopCount = 0
while True:
temp = float(ser.readline())
hum = float(ser.readline())
soil = int(ser.readline())
#light = int(ser.readline())
# lcd.text('Humidity: {:.2f}%'.format(hum), 1)
# lcd.text('Temp: {:.2f} C'.format(temp), 2)
sleep(2)
# lcd.clear()
# lcd.text('Moisture: {:d}'.format(soil), 1)
# lcd.text('Light Level: {:d} C'.format(light), 2)
sleep(2)
# lcd.clear()
loopCount = loopCount+1
message = {}
message["id"] = "id_smartgarden"
import datetime as datetime
now = datetime.datetime.now()
message["datetimeid"] = now.isoformat()
message["temperature"] = temp
message["humidity"] = hum
message["moisture"] = soil
#message["light"] = light
import json
my_rpi.publish("smartgarden/readings", json.dumps(message), 1)