022 – ESP32 MicroPython: MQTT Part 2: Subscribe

In the previous tutorial, I demonstrate the use of MQTT protocol by sending DHT sensor readings to Thingspeak as MQTT broker while the ESP32 serves as MQTT client. The act of MQTT client sending data to MQTT broker is called MQTT publish.

Now in this tutorial, I will demonstrate the other direction of MQTT communication which is MQTT subscribe where a client is waiting for a message from a broker. A client that subscribes to a certain topic will receive a data update from the broker when a new publish data is received from a certain publishing client.

Now to demonstrate that, I will be needing 3 components which are:
- A publishing client
- A broker and
- A subscribe client

BILL OF MATERIALS:
- ESP32 development board.
- 0.96 OLED display
HARDWARE INSTRUCTION:
- Connect the OLED VCC pin to ESP32 3.3V pin.
- Connect the OLED GND pin to ESP32 GND pin.
- Connect the OLED SCL pin to ESP32 D22 pin.
- Connect the OLED SDA pin to ESP32 D21 pin.
VIDEO DEMONSTRATION:
CALL TO ACTION:
For any concern, write your message in the comment section.
You might also like to support my journey on Youtube by Subscribing. Click this to Subscribe to TechToTinker.
Thank you and have a good days ahead.
See you,
– George Bantique | tech.to.tinker@gmail.com
SOURCE CODE:
1. Basic demonstration of handling MQTT Subscribe message data payload:
# **************************************#
# MQTT in MicroPython with Thingspeak #
# **************************************#
# Author: George Bantique #
# TechToTinker Youtube Channel #
# TechToTinker.blogspot.com #
# tech.to.tinker@gmail.com #
# Date: Dec.13, 2020 #
# Please feel free to modify the code #
# according to your needs. #
# **************************************#
# **************************************#
# Load necessary libraries
import machine
import network
import wifi_credentials
from umqtt.robust import MQTTClient
import ssd1306
import time
import os
import sys
# **************************************#
# Objects:
led = machine.Pin(2,machine.Pin.OUT)
scl = machine.Pin(22, machine.Pin.OUT, machine.Pin.PULL_UP)
sda = machine.Pin(21, machine.Pin.OUT, machine.Pin.PULL_UP)
i2c = machine.I2C(scl=scl, sda=sda, freq=400000)
oled = ssd1306.SSD1306_I2C(128, 64, i2c, addr=0x3C)
# **************************************#
# Configure the ESP32 wifi as STAtion.
sta = network.WLAN(network.STA_IF)
if not sta.isconnected():
print('connecting to network...')
sta.active(True)
#sta.connect('wifi ssid', 'wifi password')
sta.connect(wifi_credentials.ssid, wifi_credentials.password)
while not sta.isconnected():
pass
print('network config:', sta.ifconfig())
# **************************************#
# Callback function, it is the function
# that will be called when a new msg
# is received from MQTT broker
def call_back_function(topic, msg):
oled.fill(0)
oled.text(msg.decode().strip("'n"), 5, 5)
oled.show()
print((topic, msg))
# **************************************#
# Global variables and constants
UNIQUE_CLIENT_ID = "ab06f0c1-1319-44bc-9534-04caea40af81"
THINGSPEAK_URL = b"mqtt.thingspeak.com"
THINGSPEAK_USERNAME = b'mwa0000020270097'
THINGSPEAK_MQTT_API_KEY = b'AWP9SRI6ED9ONGBO'
THINGSPEAK_CHANNEL_ID = b'1250656'
THINGSPEAK_CHANNEL_READ_API_KEY = b'TVRCWGZKKOVC2MQR'
TOPIC_SUB = bytes("channels/{:s}/subscribe/fields/field1/{:s}"
.format(THINGSPEAK_CHANNEL_ID,
THINGSPEAK_CHANNEL_READ_API_KEY),
'utf-8')
# **************************************#
# MQTT client configuration:
# 1.Create the MQTT client
client = MQTTClient(client_id = UNIQUE_CLIENT_ID,
server = THINGSPEAK_URL,
user = THINGSPEAK_USERNAME,
password = THINGSPEAK_MQTT_API_KEY,
ssl = False)
# 2.Set the callback function
client.set_callback(call_back_function)
# 3.Connect to MQTT broker
try:
client.connect()
except Exception as e:
machine.reset
# 4.Subscribe to a specific topic
client.subscribe(TOPIC_SUB)
# **************************************#
# Main loop
while True:
try:
#client.wait_msg() #blocking
client.check_msg() #non-blocking
except KeyboardInterrupt:
print('Ctrl-C pressed...exiting')
client.disconnect()
sys.exit()
2. How to use MQTT Subscribe message as command for controlling something:
# **************************************#
# MQTT in MicroPython with Thingspeak #
# **************************************#
# Author: George Bantique #
# TechToTinker Youtube Channel #
# TechToTinker.blogspot.com #
# tech.to.tinker@gmail.com #
# Date: Dec.13, 2020 #
# Please feel free to modify the code #
# according to your needs. #
# **************************************#
# **************************************#
# Load necessary libraries
import machine
import network
import wifi_credentials
from umqtt.robust import MQTTClient
import ssd1306
import time
import os
import sys
# **************************************#
# Objects:
led = machine.Pin(2,machine.Pin.OUT)
scl = machine.Pin(22, machine.Pin.OUT, machine.Pin.PULL_UP)
sda = machine.Pin(21, machine.Pin.OUT, machine.Pin.PULL_UP)
i2c = machine.I2C(scl=scl, sda=sda, freq=400000)
oled = ssd1306.SSD1306_I2C(128, 64, i2c, addr=0x3C)
# **************************************#
# Configure the ESP32 wifi as STAtion.
sta = network.WLAN(network.STA_IF)
if not sta.isconnected():
print('connecting to network...')
sta.active(True)
#sta.connect('wifi ssid', 'wifi password')
sta.connect(wifi_credentials.ssid, wifi_credentials.password)
while not sta.isconnected():
pass
print('network config:', sta.ifconfig())
# **************************************#
# Callback function, it is the function
# that will be called when a new msg
# is received from MQTT broker
def call_back_function(topic, msg):
global message
oled.fill(0)
message = msg.decode().strip("'n")
oled.text(message, 5, 5)
oled.show()
print((topic, msg))
# **************************************#
# Global variables and constants
UNIQUE_CLIENT_ID = "ab06f0c1-1319-44bc-9534-04caea40af81"
THINGSPEAK_URL = b"mqtt.thingspeak.com"
THINGSPEAK_USERNAME = b'mwa0000020270097'
THINGSPEAK_MQTT_API_KEY = b'AWP9SRI6ED9ONGBO'
THINGSPEAK_CHANNEL_ID = b'1250656'
THINGSPEAK_CHANNEL_READ_API_KEY = b'TVRCWGZKKOVC2MQR'
TOPIC_SUB = bytes("channels/{:s}/subscribe/fields/field1/{:s}"
.format(THINGSPEAK_CHANNEL_ID,
THINGSPEAK_CHANNEL_READ_API_KEY),
'utf-8')
message = ""
# **************************************#
# MQTT client configuration:
# 1.Create the MQTT client object
client = MQTTClient(client_id = UNIQUE_CLIENT_ID,
server = THINGSPEAK_URL,
user = THINGSPEAK_USERNAME,
password = THINGSPEAK_MQTT_API_KEY,
ssl = False)
# 2.Set the callback function
client.set_callback(call_back_function)
# 3.Connect to MQTT broker
try:
client.connect()
except Exception as e:
machine.reset
# 4.Subscribe to a specific topic
client.subscribe(TOPIC_SUB)
# **************************************#
# Main loop
while True:
try:
client.wait_msg() #blocking
#client.check_msg() #non-blocking
if message == 'led ON':
led.on()
print('led is now ON')
elif message == 'led OFF':
led.off()
print('led is now OFF')
elif message == 'oled WHITE':
oled.invert(1)
print('oled is now black on WHITE')
elif message == 'oled BLACK':
oled.invert(0)
print('oled is now white on BLACK')
except KeyboardInterrupt:
print('Ctrl-C pressed...exiting')
client.disconnect()
sys.exit()
Hi,
I try to set Thinkspeak. But there is not MQTT API KEY on My Profile page.
I try to find some document like https://ww2.mathworks.cn/help/thingspeak/use-desktop-mqtt-client-to-publish-to-a-channel.html
to set MQTT device on ThingSpeak, and key in the CLIENT_ID, password(MQTT_API_KEY)
But it still show error message like below:
Traceback (most recent call last):
File "", line 91, in
File "umqtt/simple.py", line 215, in check_msg
File "umqtt/robust.py", line 44, in wait_msg
File "umqtt/robust.py", line 24, in reconnect
File "umqtt/simple.py", line 110, in connect
MQTTException: 5
same problem do you get any solution?
same probelem for me as well….any solution