-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSRC04-MQTT.py
52 lines (47 loc) · 1.74 KB
/
SRC04-MQTT.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
#!/usr/bin/python
import RPi.GPIO as GPIO
import time
import paho.mqtt.client as mqttClient
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected to broker")
global Connected #Use global variable
Connected = True #Signal connection
else:
print("Connection failed")
Connected = False #global variable for the state of the connection
def distancesensortomqtt(TRIGGERIO,ECHOIO,TOPIC,BROKER,USERNAME,PASSWORD):
GPIO.setmode(GPIO.BOARD)
PIN_TRIGGER = TRIGGERIO
PIN_ECHO = ECHOIO
GPIO.setup(PIN_TRIGGER, GPIO.OUT)
GPIO.setup(PIN_ECHO, GPIO.IN)
GPIO.output(PIN_TRIGGER, GPIO.LOW)
pulse_start_time = 0
pulse_end_time = 0
print("Waiting for sensor to settle")
time.sleep(2)
print("Calculating distance")
GPIO.output(PIN_TRIGGER, GPIO.HIGH)
time.sleep(0.00001)
GPIO.output(PIN_TRIGGER, GPIO.LOW)
while GPIO.input(PIN_ECHO)==0:
pulse_start_time = time.time()
while GPIO.input(PIN_ECHO)==1:
pulse_end_time = time.time()
pulse_duration = pulse_end_time - pulse_start_time
distance = round(pulse_duration * 17150, 2)
print("Distance:",distance,"cm")
GPIO.cleanup()
broker_address= BROKER
port = 1883
user = USERNAME
password = PASSWORD
client = mqttClient.Client("Python")
client.username_pw_set(user, password=password)
client.on_connect= on_connect
client.connect(broker_address, port=port)
client.loop_start()
client.publish(TOPIC,distance,retain=False)
distancesensortomqtt(7,11,"rpi/distance/sensor1","smartwala.azim.network",'username','password')
distancesensortomqtt(38,40,"rpi/distance/sensor2","smartwala.azim.network",'username','password')