MQTT Communication Extension for Sensor Implementation
Užbaigimo reikalavimai
MQTT Communication Extension for Sensor Implementation
We extend our last approach in Listing 1 with the MQTT communication protocol in Listing 2, so that we can transmit the sensor measurement data. You can find this version of the code in the repository with the filename distance_sensor_simple_with_MQTT.py.
import RPi.GPIO as GPIO
import time
import paho.mqtt.client as mqtt
HOST = "test.mosquitto.org"
PORT = 1883
client = mqtt.Client()
client.connect(HOST, PORT)
# use Raspberry Pi board pin numbers
GPIO.setmode(GPIO.BOARD)
# set GPIO Pins
PIN_TRIGGER = 7
PIN_ECHO = 11
print("Distance Measurment In Progress")
# set GPIO input and output channels
GPIO.setup(PIN_TRIGGER, GPIO.OUT)
GPIO.setup(PIN_ECHO, GPIO.IN)
while True:
# set Trigger to HIGH
GPIO.output(PIN_TRIGGER, True)
# set Trigger after 0.01ms to LOW
time.sleep(0.00001)
GPIO.output(PIN_TRIGGER, False)
startTime = time.time()
stopTime = time.time()
# save start time
while GPIO.input(PIN_ECHO) == 0:
pass
startTime = time.time()
# save time of arrival
while GPIO.input(PIN_ECHO) == 1:
pass
stopTime = time.time()
# time difference between start and arrival
TimeElapsed = stopTime - startTime
# multiply with the sonic speed (34300 cm/s)
# and divide by 2, because there and back
distance = (TimeElapsed * 34300) / 2
print ("Distance: %.1f cm" % distance)
client.publish("/test/distance", "Distance={0:0.1f} cm".format(distance))
client.subscribe("/test/distance")
time.sleep(1)
Listing 2: Simple
approach with MQTT communication protocol
To run the code in Listing 2, you first need to install a Mosquitto MQTT Broker:
sudo apt-get install mosquitto
Now you can run the
above code in Listing 2 simply by calling the following command:
python distance_sensor_simple_with_MQTT.py