-
Notifications
You must be signed in to change notification settings - Fork 0
/
SPC_Publisher.py
288 lines (248 loc) · 9.77 KB
/
SPC_Publisher.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
"""
Project: Smart Park Capsule
Course: Smart City and IOT
Institute: Service Computing Department, IAAS, University of Stuttgart
---------------- Team Members -----------
Sl.No | Names
1 | Swathi Shridhar
2 | Badruddin Mukadam
3 | Suraj Sakpal
4 | Anish Krishna Navalgund
-----------------------------------------
File Description: SPC_Publisher.py is the main code present on the RaspberryPi. This code publishes
Raspberry Pi sensor data through MQTT protocol to AWS.
Timestamp: 10th July 2022
"""
import threading
import paho.mqtt.client as paho
import ssl
import grovepi
from grovepi import *
from grove_rgb_lcd import *
import RPi.GPIO as GPIO
import time
from time import sleep
import datetime
from random import uniform
import paho.mqtt.publish as publish
import ast
import math
#--------------Initializations--------------#
HostAddress="192.168.0.222"
PlanTopic = "temperatureAction"
#temperature & humidity sensor
TempPort = 7
TempSensor = 0
TempTimeout = 1
#light sensor
LightSensor = 0
# Connect the Grove LED to digital port D3
led = 4
pinMode(led,"OUTPUT")
# Connect the Grove LED to digital port D5 for resemblence of Led strip
led_strip = 3
pinMode(led_strip,"OUTPUT")
# Connect the Grove Buzzer to digital port D2
# SIG,NC,VCC,GND
buzzer = 2
grovepi.pinMode(buzzer,"OUTPUT")
#IR sensor
PIRSensor = 8
IR=0
#ultra-sonic sensor
Ultrasonic_ranger = 5
Ultrasonic_ranger2 = 6
#motor and IR
#mode = GPIO.getmode()
IRSensor = 18
Forward=24
Backward=23
SleepTime=1
#GPIO.setup(IRSensor,GPIO.IN)
notmax=1
count_flag = 0
# set I2C to use the hardware bus
grovepi.set_bus("RPI_1")
GPIO.setmode(GPIO.BCM)
GPIO.setup(Forward, GPIO.OUT)
GPIO.setup(Backward, GPIO.OUT)
#servo motor intidef init():
GPIO.setup(17,GPIO.OUT)
servo = GPIO.PWM(17,50)
servo.start(0)
def IR():
#GPIO.setmode(GPIO.BCM)
GPIO.setup(IRSensor, GPIO.IN)
if GPIO.input(IRSensor):
ir = 1
else:
ir = 0
#GPIO.cleanup()
return ir
def fanoff():
GPIO.output(Forward, GPIO.HIGH)
print("FAN Off")
time.sleep(0.9)
#GPIO.output(Forward, GPIO.LOW)
def fanon():
print("FAN On")
#time.sleep(0.2)
GPIO.output(Forward, GPIO.LOW)
#---------------Function Definitions----------#
def on_connect(client, userdata, flags, rc): # func for making connection
global connflag
print("Connected to AWS")
connflag = True
print("Connection returned result: " + str(rc) )
######on_message defined twice??????????????/
def on_message(client, userdata, msg): # Func for Sending msg
print(msg.topic+" "+str(msg.payload))
def pddlMQTTDataReceive():
print("MQTT PDDL Data receive thread started....... :)")
pddl_mqtt_client.subscribe(PlanTopic) # subscribe topic test
pddl_mqtt_client.loop_forever()
def on_message(client, userdata, message):
global writer
action = str(message.payload.decode("utf-8"))
print("Received msg is ", action)
# payload_data = json.loads(s)
payload_pddl = ast.literal_eval(action)
temp_output = None
light_output = None
pir_output = None
ultra_output = None
#Actuator actions
if 'temp_action' in payload_pddl and payload_pddl['temp_action'] is not None:
temp_output = payload_pddl['temp_action']
if 'light_action' in payload_pddl and payload_pddl['light_action'] is not None:
light_output = payload_pddl['light_action']
if 'pir_action' in payload_pddl and payload_pddl['pir_action'] is not None:
pir_output = payload_pddl['pir_action']
if 'ultra_action' in payload_pddl and payload_pddl['ultra_action'] is not None:
ultra_output = payload_pddl['ultra_action']
cooler_actuation(temp_output)
light_actuation(light_output)
gate_actuation(pir_output,ultra_output)
ultra_actuation(ultra_output)
def cooler_actuation(temp_output):
print(temp_output)
if temp_output is not None:
#add temp actions
if temp_output == '(increasetemperature':
fanoff()
else:
fanon()
def light_actuation(light_output):
if light_output is not None:
if light_output == '(switchonlight':
print("LIGHTS: ON")
digitalWrite(led_strip,1)
else:
print("LIGHTS: OFF")
digitalWrite(led_strip,0)
def gate_actuation(pir_output,ultra_output):
if pir_output is not None:
servo.start(0)
if pir_output == '(opendoor' and ultra_output == '(spotoccupied':
setText("Parking spots \n full! SORRY:(")
setRGB(0,0,64)
grovepi.digitalWrite(buzzer,1)
time.sleep(1)
# Stop buzzing for 1 second
grovepi.digitalWrite(buzzer,0)
time.sleep(0.5)
grovepi.digitalWrite(buzzer,1)
time.sleep(1)
grovepi.digitalWrite(buzzer,0)
print("Door is close")
elif pir_output == '(opendoor' and ultra_output != '(spotoccupied':
print ("Opening the door !")
setText(" Welcome!Free spots available.")
servo.ChangeDutyCycle(7)
time.sleep(0.5)
servo.ChangeDutyCycle(0)
time.sleep(3)
#turn back to 0 degrees
print ("Turning back to 0 degrees")
servo.ChangeDutyCycle(2)
time.sleep(0.5)
servo.ChangeDutyCycle(0)
setRGB(0,200,64)
setText("Welcome to Smart Park capsule")
setRGB(0,128,64)
else:
print("Door is close")
def ultra_actuation(ultra_output):
if ultra_output is not None:
if ultra_output == '(spotoccupied':
print("occupied: led off")
digitalWrite(led,0)
else:
print("Not occupied: led on")
digitalWrite(led,1) # Send HIGH to switch on LED
mqttc = paho.Client()
client= paho.Client()
client.connect("192.168.0.222",1883,60)
mqttc.on_connect = on_connect
mqttc.on_message = on_message
#subscribe from pddl
pddl_mqtt_client = paho.Client("PDDL") # create client object
pddl_mqtt_client.on_message = on_message
print("connecting to broker host", HostAddress)
pddl_mqtt_client.connect(HostAddress) # connection establishment with broker
def pddlMQTTDataReceive():
print("MQTT PDDL Data receive thread started....... :)")
pddl_mqtt_client.subscribe(PlanTopic) # subscribe topic test
pddl_mqtt_client.loop_forever()
def sensordatapublish():
prevTemp=26.0
prevHumid =50.0
setText("Welcome to Smart Park capsule")
setRGB(0,128,64)
digitalWrite(led_strip,0)
while 1:
sleep(5)
if connflag == True:
timeStamp = datetime.datetime.now()
tempreading = uniform(20.0,25.0)
# Generating Temperature Readings
[temperature, humidity] = grovepi.dht(TempPort, TempSensor)
if math.isnan(temperature)== True or math.isnan(humidity)== True:
temperature = prevTemp
humidity = prevHumid
else:
prevTemp = temperature
prevHumid = humidity
# Generating Ultrasonic Readings
us= grovepi.ultrasonicRead(Ultrasonic_ranger)
us2= grovepi.ultrasonicRead(Ultrasonic_ranger2)
# Generating Lightsensor Readings
light= grovepi.analogRead(LightSensor)
# Calculate resistance of sensor in K
if light is not 0 :
resistance = (float)(1024 - light) * 10 / light
else :
resistance = 1
# Generating the IR sensor readings
IR_value = IR()
message = '{"timeStamp":'+'"'+str(timeStamp)+'",'+'"temperature":'+str(temperature)+','+'"humidity":'+str(humidity) +','+'"Ultrasonic-1":'+str(us)+','+'"Ultrasonic-2":'+str(us2)+','+'"Lightsensor":'+str(light) +','+'"Light_resistance":'+str(resistance) +','+'"IR_sensor":'+str(IR_value) + '}'
mqttc.publish("temperatureTopic", message, 1) # topic: temperature # Publishing Temperature values
client.publish("temperatureTopic", message, 1)
print("msg sent: temperature " + "%.1f" % temperature +" , humidity " + "%.1f" % humidity +", Ultrasonic-1 " + "%.1f" % us +", Ultrasonic-2 " + "%.1f" % us2 +", Lightsensor :" + "%.1f" % light +", IR_sensor: " + "%.1f" % IR_value ) # Print sent temperature msg on console
else:
print("waiting for connection...")
awshost = "a2ngemuw19bbnm-ats.iot.eu-central-1.amazonaws.com" # Endpoint
awsport = 8883 # Port no.
clientId = "myLaptop" # Thing_Name
thingName = "Raspberrypi" # Thing_Name
caPath = "/home/pi/Public/AWS_IOT/AmazonRootCA1.pem" #Amazon's certificate from Third party # Root_CA_Certificate_Name
certPath = "/home/pi/Public/AWS_IOT/certificate.pem.crt" # <Thing_Name>.cert.pem.crt. Thing's certificate from Amazon
keyPath = "/home/pi/Public/AWS_IOT/private.pem.key" # <Thing_Name>.private.key Thing's private key from Amazon
mqttc.tls_set(caPath, certfile=certPath, keyfile=keyPath, cert_reqs=ssl.CERT_REQUIRED, tls_version=ssl.PROTOCOL_TLSv1_2, ciphers=None) # pass parameters
mqttc.connect(awshost, awsport, keepalive=60) # connect to aws server
mqttc.loop_start() # Start the loop
#---------Start threads to publish sensor data and recieve the plan ---------#
t1 = threading.Thread(target=sensordatapublish)
t2 = threading.Thread(target=pddlMQTTDataReceive)
t1.start()
t2.start()