MQTT help using ESP32

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Mohammedabujarad
Posts: 14
Joined: Wed Mar 25, 2020 12:35 am

MQTT help using ESP32

Post by Mohammedabujarad » Wed Mar 25, 2020 12:39 am

hello everyone,

i have an Esp32 that is connected to a dht22 sensor which publish the data to an mqtt server, also i have another esp32 that subscribe to the topic where the data are published, but the esp32 that subscribe to the topic it disply an error says ( mqtt: OSError(-1,) ). i am using micro python for the esp32 and both of them are connected to a raspberry pi 3 as server. also both of the board have two script. the first one connect too the WIFI, and for the second script. the first one publish the data and the second one subscribe to the topic.

here are the codes:

the code that publish the data:

Code: Select all

from time import sleep
from umqtt.robust import MQTTClient
from machine import Pin
from dht import DHT22

SERVER = '172.20.10.11' # MQTT Server Address 
CLIENT_ID = 'ESP32_DHT22_Sensor'
TOPIC = b'temp_humidity'

client = MQTTClient(CLIENT_ID, SERVER)
client.connect() # Connect to MQTT broker

sensor = DHT22(Pin(15, Pin.IN, Pin.PULL_UP)) # DHT-22 on GPIO 15 (input with internal pull-up resistor)

while True:
try:
sensor.measure() # Poll sensor
t = sensor.temperature()
h = sensor.humidity()
if isinstance(t, float) and isinstance(h, float): # Confirm sensor results are numeric
msg = (b'{0:3.1f},{1:3.1f}'.format(t, h))
client.publish(TOPIC, msg) # Publish sensor data to MQTT topic
print(msg)
else:
print('Invalid sensor readings.')
except OSError:
print('Failed to read sensor.')
sleep(2)
the code that subscribe to the topic:

Code: Select all

import time
from umqtt.robust import MQTTClient

SERVER = '172.20.10.11' # MQTT Server Address 
CLIENT_ID = 'ESP32_DHT22_Sensor'
TOPIC = b'temp_humidity'

def sub_cb(topic, msg):
print((topic, msg))

c = MQTTClient(CLIENT_ID, SERVER)

c.DEBUG = True
c.set_callback(sub_cb)

if not c.connect(clean_session=False):
print("New session being set up")
c.subscribe(TOPIC)

while 1:
c.wait_msg()

c.disconnect()
here is the error that i get whenever i ran the second code:

mqtt: OSError(-1,)
I (27817) wifi: ampdu: ignore deleting tx BA0
mqtt: OSError(-1,)
mqtt: OSError(-1,)
mqtt: OSError(-1,)
I (41297) wifi: ampdu: ignore deleting tx BA0
mqtt: OSError(-1,)
mqtt: OSError(-1,)
mqtt: OSError(-1,)
I (55127) wifi: ampdu: ignore deleting tx BA0
mqtt: OSError(-1,)
mqtt: OSError(-1,)
mqtt: OSError(-1,)
mqtt reconnect: OSError(23,)
I (68337) wifi: ampdu: ignore deleting tx BA0
mqtt reconnect: OSError(23,)
mqtt reconnect: OSError(23,)
mqtt reconnect: OSError(23,)
mqtt reconnect: OSError(23,)
mqtt reconnect: OSError(23,)
mqtt reconnect: OSError(23,)
mqtt reconnect: OSError(23,)
mqtt reconnect: OSError(23,)
mqtt reconnect: OSError(23,)
mqtt reconnect: OSError(23,)
mqtt reconnect: OSError(23,)
mqtt reconnect: OSError(23,)
mqtt reconnect: OSError(23,)
mqtt reconnect: OSError(23,)
mqtt reconnect: OSError(23,)
mqtt reconnect: OSError(23,)
mqtt reconnect: OSError(23,)
mqtt reconnect: OSError(23,)
mqtt reconnect: OSError(23,)
mqtt reconnect: OSError(23,)

Online
kevinkk525
Posts: 733
Joined: Sat Feb 03, 2018 7:02 pm

Re: MQTT help using ESP32

Post by kevinkk525 » Wed Mar 25, 2020 7:40 pm

A simple mistake I once made too and therefore just remembered:
You are trying to connect both your devices with the same ID:

Code: Select all

CLIENT_ID = 'ESP32_DHT22_Sensor'
Change it to something unique and it should work.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

Mohammedabujarad
Posts: 14
Joined: Wed Mar 25, 2020 12:35 am

Re: MQTT help using ESP32

Post by Mohammedabujarad » Wed Mar 25, 2020 11:02 pm

Thank you so much, It did work. I have one more question if you don’t mind, let’s say that I want to add an if statement to the subscribe code that says if the temperature got higher than 50C, it should print a danger message. What is the easiest way to do it ?

Online
kevinkk525
Posts: 733
Joined: Sat Feb 03, 2018 7:02 pm

Re: MQTT help using ESP32

Post by kevinkk525 » Thu Mar 26, 2020 6:48 am

Basically just like you said it:

But you have to get the value from your message first, which was encoded like

Code: Select all

(b'{0:3.1f},{1:3.1f}'.format(t, h))

Code: Select all

def sub_cb(topic, msg):
    print((topic, msg))
    if float(msg.decode().split(",")[0])>50:
        print("danger")
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

Mohammedabujarad
Posts: 14
Joined: Wed Mar 25, 2020 12:35 am

Re: MQTT help using ESP32

Post by Mohammedabujarad » Thu Mar 26, 2020 2:00 pm

Thank you , I really appreciate it

Mohammedabujarad
Posts: 14
Joined: Wed Mar 25, 2020 12:35 am

Re: MQTT help using ESP32

Post by Mohammedabujarad » Thu Mar 26, 2020 11:08 pm

i tried to do something like this , but it gave me an error in line 20 which is the line with the msg

Code: Select all

import time
from umqtt.robust import MQTTClient

SERVER = '172.20.10.11'  # MQTT Server Address (Change to the IP address of your Pi)
CLIENT_ID = 'ESP32_2'
TOPIC = b'temp_humidity'

def sub_cb(topic, msg):
    print((topic, msg))


c = MQTTClient(CLIENT_ID, SERVER)

c.DEBUG = True
c.set_callback(sub_cb)

t = sensor.temperature()
h = sensor.humidity()
if isinstance(t, float) and isinstance(h, float): # Confirm sensor results are numeric
msg = (b'{0:3.1f},{1:3.1f}'.format(t, h))

def sub_cb(topic, msg):
    print((topic, msg))
    if float(msg.decode().split(",")[0])>30:
        
        print("danger")
if not c.connect(clean_session=False):
    print("New session being set up")
    c.subscribe(TOPIC)

while 1:
    c.wait_msg()


c.disconnect()

Online
kevinkk525
Posts: 733
Joined: Sat Feb 03, 2018 7:02 pm

Re: MQTT help using ESP32

Post by kevinkk525 » Fri Mar 27, 2020 6:43 am

What error? because that line works just fine when I test it
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

Mohammedabujarad
Posts: 14
Joined: Wed Mar 25, 2020 12:35 am

Re: MQTT help using ESP32

Post by Mohammedabujarad » Fri Mar 27, 2020 7:22 am

So whenever I ran The whole code , it says

File “Led_sub.py”, line 20
SyntaxError: invalid syntax

Online
kevinkk525
Posts: 733
Joined: Sat Feb 03, 2018 7:02 pm

Re: MQTT help using ESP32

Post by kevinkk525 » Fri Mar 27, 2020 7:28 am

So the code you posted is indeed what you have in your source file..

Well look again closely, you should easily find the syntax error.. hint: indentation
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

Mohammedabujarad
Posts: 14
Joined: Wed Mar 25, 2020 12:35 am

Re: MQTT help using ESP32

Post by Mohammedabujarad » Fri Mar 27, 2020 8:18 am

I got it to work , thank you. It is just a space before msg :D

Post Reply