| 1 |
1 |
Back to subject |
EDGE COMPUTING LAB - 23A39605 (Lab) |
Dec. 29, 2025 |
5. MQTT-Based Edge Communication
o Setup publisher/subscriber model for edge-to-cloud communication |
5. MQTT-Based Edge Communication o Setup publisher/subscriber model for edge-to-cloud communication
REQUIRED LIBRARY (on Raspberry Pi)
pip install Adafruit-DHT
FINAL IMPROVED CODE (LED + DHT11 via MQTT)
import time
import RPi.GPIO as GPIO
import paho.mqtt.client as mqtt
import board
import adafruit_dht
# ---------------- GPIO SETUP ----------------
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
LED_PIN = 18
GPIO.setup(LED_PIN, GPIO.OUT)
# ---------------- DHT11 SETUP ----------------
dhtDevice = adafruit_dht.DHT11(board.D4)
# ---------------- MQTT SETUP ----------------
BROKER = "192.168.0.161"
TOPIC_CMD = "led/cmd"
TOPIC_STATUS = "led/status"
TOPIC_DHT = "sensor/dht11"
# ---------------- STATE ----------------
led_state = False # change detector
# ---------------- CALLBACKS ----------------
def on_connect(client, userdata, flags, rc):
print("Connected:", rc)
client.subscribe(TOPIC_CMD)
def on_message(client, userdata, msg):
global led_state
payload = msg.payload.decode().lower()
print("MQTT:", payload)
# -------- OFF → ON (CHANGE SIGNAL) --------
if payload == "on" and not led_state:
led_state = True
GPIO.output(LED_PIN, GPIO.HIGH)
client.publish(TOPIC_STATUS, "ON")
# ---- READ DHT11 ONCE ----
try:
temperature = dhtDevice.temperature
humidity = dhtDevice.humidity
if temperature is not None and humidity is not None:
data = f"Temp={temperature}C Humidity={humidity}%"
client.publish(TOPIC_DHT, data)
print("DHT:", data)
except RuntimeError as error:
print("DHT error:", error)
# -------- ON → OFF (CHANGE SIGNAL) --------
elif payload == "off" and led_state:
led_state = False
GPIO.output(LED_PIN, GPIO.LOW)
client.publish(TOPIC_STATUS, "OFF")
def on_disconnect(client, userdata, rc):
print("Disconnected")
# ---------------- MQTT CLIENT ----------------
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.on_disconnect = on_disconnect
client.connect(BROKER, 1883, 60)
client.loop_forever()
Example Output
MQTT: on
DHT: Temp=28C Humidity=65%
DHT: Temp=28C Humidity=64%
MQTT: off
Case02
import time
from time import sleep
import os,sys
import RPi.GPIO as GPIO
import paho.mqtt.client as paho
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
LED_PIN=18 #define LED pin
GPIO.setup(LED_PIN,GPIO.OUT) # Set pin function as output
def on_connect(self, mosq, obj, rc):
self.subscribe("led", 0)
def on_message(mosq, obj, msg):
#print(msg.topic + " " + str(msg.qos) + " " + str(msg.payload))
if(msg.payload.decode().lower() == 'on'):
print("LED on")
#sleep(5)
GPIO.output(LED_PIN, 1) #LED ON
sleep(0.2)
elif(msg.payload.decode().lower() == 'off'):
print("LED off")
#sleep(5)
GPIO.output(LED_PIN,0) # LED OFF
sleep(0.2)
def on_publish(mosq, obj, mid):
#print("mid: " + str(mid))
pass
def on_subscribe(mosq, obj, mid, granted_qos):
print("Subscribed: " + str(mid) + " " + str(granted_qos))
mqttc = paho.Client() # object declaration
# Assign event callbacks
mqttc.on_message = on_message # called as callback
mqttc.on_connect = on_connect
mqttc.on_publish = on_publish
mqttc.on_subscribe = on_subscribe
mqttc.connect('192.168.0.161', 1883)
rc = 0
flag = 0
flag1 = 0
while True:
input_state=bool(GPIO.input(18))
rc = mqttc.loop()
if flag==0:
mqttc.publish("led",'Status :'+str(GPIO.input(18)))
flag=1
if input_state==False:
if flag1==0:
print('Button Not Pressed')
flag1=1
flag=0
elif input_state==True:
if flag1==1:
print('Button Pressed')
flag1=0
flag=0
Testing From OnBard raspberry Pi(192.168.0.161)
import time
from time import sleep
import os,sys
import RPi.GPIO as GPIO
import paho.mqtt.client as paho
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
LED_PIN=18 #define LED pin
GPIO.setup(LED_PIN,GPIO.OUT) # Set pin function as output
GPIO.output(LED_PIN,0)
GPIO.output(LED_PIN,1) #On
GPIO.output(LED_PIN,0) #OFF
Using MQTT Msg Publish
On Way Case
pi@raspberrypi:~ $ mosquitto_pub -h 192.168.0.161 -t led -m "on"
pi@raspberrypi:~ $ mosquitto_pub -h 192.168.0.161 -t led -m "oN"
pi@raspberrypi:~ $ mosquitto_pub -h 192.168.0.161 -t led -m "On"
pi@raspberrypi:~ $ mosquitto_pub -h 192.168.0.161 -t led -m "ON"
OFF Way Case
pi@raspberrypi:~ $ mosquitto_pub -h 192.168.0.161 -t led -m "off"
pi@raspberrypi:~ $ mosquitto_pub -h 192.168.0.161 -t led -m "Off"
pi@raspberrypi:~ $ mosquitto_pub -h 192.168.0.161 -t led -m "oFf"
pi@raspberrypi:~ $ mosquitto_pub -h 192.168.0.161 -t led -m "OFF"
|