Hello, I follow “MicroPython – Getting Started with MQTT on ESP32/ESP8266” tutorial, but can not make ESP32 to publish message to MQTT. I have done simpler set up, where I use only one ESP32, that should publish message to topic ‘hello’ and then use Node-RED to listen to this topic. I am quite sure that both RsbPI and MQTT is in good order as I have another project that is posting successfully at the same time (based on different tutorial). I know that ESP connected to network successfully, since I see device on my router. I tried to manually execute following lines : >>> global client_id, mqtt_server, topic_sub
>>> client = MQTTClient(client_id, mqtt_server)
>>> client.set_callback(sub_cb)
>>> client.connect()
to wich I get error message:
Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
File “umqttsimple.py”, line 105, in connect
MQTTException: 5
Not sure what this message means and what is wrong, would appreaciate some quidance
Which code are you uploading to your board? Are you using my exact? I’ve never experienced that error before, so I don’t now what’s happening in your case…
I have used exact code for ESP1 (boot.py and main.py) and also umqttsimple.py that was linked in tutorial. I have just excluded ESP2 from the setup and used Node-Red instead.
Are you entering the correct MQTT broker IP address?
How does that line looks like in your code? Just to trying to figure out what might be missing…
Copied text of umqttsimple.py file, saved and downloaded to run on ESP32, just to avoid some accidental error. I get immediately error message:
——————-
download ok
os.listdir(‘.’)
Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
NameError: name ‘os’ isn’t defined
>>>
reflush tree false
Tried to connect to MQTT got same message as I posted earlier
Connection to MQTT server is in boot.py file the lines lookes like this:
mqtt_server = ‘192.168.1.29’
I believe port is added at a later stage, right?
If no port is passed in the connect method, it uses the default MQTT broker port:
port = 8883 ssl port = 1883
Are you using Mosquitto?
You can ignore that “error message” when you see the message “download ok”.
If it prints the message “download ok“, it means that your board received the full script, but sometimes afterwards it prints an OS error message that you can ignore…
I use Mosquitto and default port 1883. Have another ESP32 running different code that is connecting and publishing data successfully on 1883 port. Ok, I disregard OS error message after file download, reset ESP32 using onboard button. I get Connection successsful coming from boot.py, confirming that ESP is connected to wifi, then follows these error messages :
Traceback (most recent call last):
File “main.py”, line 26, in <module>
File “main.py”, line 24, in <module>
File “main.py”, line 13, in connect_and_subscribe
File “umqttsimple.py”, line 104, in connect
MQTTException: 5
I understand it is not able to connect to MQTT broker
I checked code on other ESP32 that is working fine. I use user name and password to connect to MQTT , so this must be the cause of failed connection. My line on another ESP looks like this:
client = MQTTClient(client_id, broker_address, port=1883, user=b”****”, password=b”****”)
Need to find right place and syntax to add user name and password to your script. Sorry, should have thought about it earlier
Exactly, that would be my next question. If you require username/password to establish an MQTT connection, you need to pass them as arguments in your script.
In the main.py file at line 10:
client = MQTTClient(client_id, mqtt_server)
It needs to be:
client = MQTTClient(client_id, mqtt_server, user=b'your_user', password=b'your_pass')
Does it work now?
Thank you for your quick responses. I finally got it to work I used format that I had in another ESP32. Which was:
client = MQTTClient(client_id, mqtt_server, port=1883, user=b”USER_ID”, password=b”PASSWORD”)
So by adding port, user name and password all worked out. I am just not sure if port is absolutely necessary, but it definitely did not hurt. Also, not sure what letter b does to user and password strings. Again, leaving this here in case someone else runs into connection problem, perhaps this will help them.
Onto further code development. I am trying your code that I intend to use in greenhouse to monitor temperature, possibly other values. I had this working on other ESP32, but it stopped working after a while (sometimes within same day, sometimes within a week). Not a big deal, just needed to restart, but still not perfect. Possibly loss of connection (LAN or MQTT) somewhere and I didn’t have reconnect code in place. Your code has it, so it give me hope it will be more stable.
Thanks for letting me know! I’m glad it worked. Yes, I forgot to mention to add the b‘your_user’, I’ve also updated my previous comment to help anyone having this problem too.
The b is used to convert a String to bytes literal. The uMQTT Simple library uses bytes on topics and authentication keys (like user and pass) because that data is sent over network sockets as bytes.
Hello,
I come back to you after many hours to search a solution… (i’m biguiner in MQTT and microphyton)…
I’m following your “MicroPython_Programming_with_ESP32_and_ESP8266_V2” Courses -> MODULE 5 – MQTT Protocol -> 5.3 – MQTT – Establishing a Two-way Communication…
When i arrive to Page 271, after the configuration of ESP#1 (with umqttsimple.py + boot.py + main.py) The following message appear when i launch the main.py :
>> %Run -c $EDITOR_CONTENT
Traceback (most recent call last):
File “<stdin>”, line 25, in <module>
File “<stdin>”, line 10, in connect_and_subscribe
NameError: name ‘MQTTClient’ isn’t defined
And finaly, when i push the “enable” buton of ESP#1, the following message appear :
rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0030,len:4728
load:0x40078000,len:14888
load:0x40080400,len:3368
entry 0x400805cc
Connection successful
(‘192.168.1.97’, ‘255.255.255.0’, ‘192.168.1.254’, ‘192.168.1.254’)
Failed to connect to MQTT broker. Reconnecting…
Please could you help me to resolve it ?