MQTT
Micropython
Micropython is becoming popular nowadays. It uses python as the programming language for working with microcontrollers. It has support for Esp32,ESP8266,CC3200,STM 32,Raspberry Pi and PI boards etc.
It is a very fast to operate and very easy to handle. The developer can use the various libraries made available if the Micropython community.How to set up Micropython on ESP32 Board is explained in the youtube Video here.
Here we are implementing Mqtt client using ESP32 Devkit and Micropython.The complete implementation is explained in the Youtube video here.
Code Functionality
The necessary libraries are imported to implement the Mqtt over ESP32.
import network
import time
from umqtt.simple import MQTTClient
from machine import Pin
The network library is used for wifi connection related functionality.The time module is used to add waiting period.The library umqqt.simple is used to implement Mqtt.The machine library is used to access microcontrollers Resources such as GPIO pin. The pin 2 of ESp32 is used for Led operation.
The ESP32 first the connection with wifi using wifi Credentials such SSID and Password.Then the
connection is established with boker("mqtt-v2.thingspeak.com") with credentials as Client ID, Broker name,port(1883),Username and Password.
Publishing the Message
After the establishing connection with the broker, the message is published to thebroker. The counter value is incremented per 2 seconds and published using the topic (""topicOut = "channels/" + CHANNEL_ID + "/publish" ).The counter value is displayed on the dashboard of thingSpeak.com platform.
code snippet for publishing the message is as shown below.
import network import time from umqtt.simple import MQTTClient from machine import Pin WiFi_SSID = "" # Enter Wifi SSID WiFi_PASS = ""
# Enter Wifi Pasword
#SERVER = "mqtt-v2.thingspeak.com"
# Enter Mqtt Broker Name
SERVER = "mqtt3.thingspeak.com" # Enter Mqtt Broker Name
PORT = 1883 CHANNEL_ID = "" #Enter Channel Id here USER = "" # Enter User Id here CLIENT_ID = "" #Enter Client Id here PASSWORD = "" #Enter Password here counter = 0 # counter value initialised led = 0 #create topic to publish the message topicOut = "channels/" + CHANNEL_ID + "/publish" #create topic to publish the message #topicIn = "channels/" + CHANNEL_ID + "/subscribe/fields/field2" led = Pin(2, Pin.OUT) #Led pin is initialise as Output led.value(0) # Led pin is set low
# Function to implement wifi connection
def wifi_connect():
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
if not wlan.isconnected():
print('connecting to network...')
wlan.connect(WiFi_SSID, WiFi_PASS)
while not wlan.isconnected():
pass
print("Connected to Wifi Router")
#Call back function called when new message from the broker arrives
def calback(topic,msg):
print(topic)
print(msg)
if msg == b'1':
led.value(1)
else:
led.value(0)
#connect esp32 to wifi
wifi_connect()
#create a client and connect to the mqtt broker
#client = MQTTClient(CLIENT_ID, SERVER,PORT,USER,PASSWORD)
client = MQTTClient(CLIENT_ID, SERVER,PORT,USER,PASSWORD,60)
client.set_callback(calback)
client.connect()
#client.subscribe(topicIn)
print("Mqtt connected")
while True:
client.check_msg()
if counter>100:
counter = 0
#Publish the topic meaasge to the broker
client.publish(topicOut, "field1="+str(counter))
print(counter)
counter = counter + 10
time.sleep(2)
Subscribing to the message
The ESp32 board which acting as a Mqtt client is subscribed to the topic("topicIn = "channels/" + CHANNEL_ID + "/subscribe/fields/field2"").The led operation command is published from the thingspeak dashboard and the led on ESP32 board is operated.
code snippet for publishing the message is as shown below.
import network import time from umqtt.simple import MQTTClient from machine import Pin WiFi_SSID = "" # Enter Wifi SSID WiFi_PASS = ""
# Enter Wifi Pasword
#SERVER = "mqtt-v2.thingspeak.com"
# Enter Mqtt Broker Name
SERVER = "mqtt3.thingspeak.com" # Enter Mqtt Broker Name
PORT = 1883 CHANNEL_ID = "" #Enter Channel Id here USER = "" # Enter User Id here CLIENT_ID = "" #Enter Client Id here PASSWORD = "" #Enter Password here
counter = 0 led = 0 #create topic to publish the message #topicOut = "channels/" + CHANNEL_ID + "/publish" #create topic to publish the message topicIn = "channels/" + CHANNEL_ID + "/subscribe/fields/field2" led = Pin(2, Pin.OUT) led.value(0) def wifi_connect(): wlan = network.WLAN(network.STA_IF) wlan.active(True) if not wlan.isconnected(): print('connecting to network...') wlan.connect(WiFi_SSID, WiFi_PASS) while not wlan.isconnected(): pass print("Connected to Wifi Router") def calback(topic,msg): print(topic) print(msg) if msg == b'1': led.value(1) else: led.value(0) #connect esp32 to wifi wifi_connect() #create a client and connect to the mqtt broker #client = MQTTClient(CLIENT_ID, SERVER,PORT,USER,PASSWORD)
client = MQTTClient(CLIENT_ID, SERVER,PORT,USER,PASSWORD,60)
client.set_callback(calback)
client.connect()
client.subscribe(topicIn)
print("Mqtt connected")
while True:
client.check_msg()
time.sleep(2)
Others :
Use of Tasmota with ESP32+Mqtt
Implementing Mqtt Client in Python|Paho-Mqtt library.
Integrate Tasmota Device with Home Assistant+Mqtt
Integrate ESP Home Device with Home Assistant