MQTT for MicroPython under ESP32

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
guyd
Posts: 81
Joined: Fri Jul 20, 2018 6:08 am

MQTT for MicroPython under ESP32

Post by guyd » Sun Jul 22, 2018 7:36 am

Hi,
I'm using a python code to execute GPIO commands on demand, using MQTT - which works excellent on RPI.
Now I'm trying to implement same concept on a ESP32, but MQTT syntax is a bit different on umqtt. I'm looking for some examples for such code that is subscribed to a topic ( with loop_forever ) to deploy command.

Any help will be appreciated.

guy

goochy
Posts: 12
Joined: Tue Apr 17, 2018 9:58 am

Re: MQTT for MicroPython under ESP32

Post by goochy » Sun Jul 22, 2018 7:54 am

G'day Guy,

I found using the uPy IDE (https://dfrobot.gitbooks.io/upycraft/) , this example (search for MQTT) works.

https://dfrobot.gitbooks.io/upycraft/ne ... icate.html


cheers, Steve

guyd
Posts: 81
Joined: Fri Jul 20, 2018 6:08 am

Re: MQTT for MicroPython under ESP32

Post by guyd » Sun Jul 22, 2018 8:20 am

goochy wrote:
Sun Jul 22, 2018 7:54 am
G'day Guy,

I found using the uPy IDE (https://dfrobot.gitbooks.io/upycraft/) , this example (search for MQTT) works.

https://dfrobot.gitbooks.io/upycraft/ne ... icate.html


cheers, Steve
Hi,
Great! my guess is "sub_cb" @uMQTT replaces "on_message" @ MQTT.

Can you explain why code at the end :" finally; c.disconnect()" ?

goochy
Posts: 12
Joined: Tue Apr 17, 2018 9:58 am

Re: MQTT for MicroPython under ESP32

Post by goochy » Sun Jul 22, 2018 8:51 am

G'day Guy,

I am new to micropython (and python for that matter, and MQTT for that matter as well), I would say that since MQTT uses threads to LISTEN to traffic, when you use threads, you nominate a function to execute code when there is activity, so I would say yes (unless someone can educate us better).

You could call sub_cb donald_duck if you wanted to.

The " finally: c.disconnect()" is the function to close the socket.

I placed a statement 'while True:' within the try: block (at a place that is logical) calling check_msg() instead of wait_msg() as my ESP32 was responding to external events etc and I didn't want the ESP to be held up waiting for MQTT traffic.

Cheers,

guyd
Posts: 81
Joined: Fri Jul 20, 2018 6:08 am

Re: MQTT for MicroPython under ESP32

Post by guyd » Sun Jul 22, 2018 9:04 am

Hi,
donald_duck will do too :)
when you close a socket, how would you keep listening for future publishes ?

goochy
Posts: 12
Joined: Tue Apr 17, 2018 9:58 am

Re: MQTT for MicroPython under ESP32

Post by goochy » Sun Jul 22, 2018 9:16 am

G'day Guyd (apologies, didn't see the 'd' part).

Re "when you close a socket, how would you keep listening for future publishes ?", you cant as the 'finally:' is the NICE way of exiting the program.
So, TRY and run code 'blah blah' EXCEPT of there is an error that I have trapped or if I haven't then fall over gracefully, FINALLY: wrap it all up and finish off stuff like 'close sockets', 'close files', 'display a nice message' and say "ta ta's".

I hope this makes sense?

cheers,

guyd
Posts: 81
Joined: Fri Jul 20, 2018 6:08 am

Re: MQTT for MicroPython under ESP32

Post by guyd » Sun Jul 22, 2018 9:24 am

Hi,
you don't have to be formal, Guy will do, or: d, guyd as james says.

So- the "finally" part is for crash exemptions ? I just want to be sure that ESP will keep waiting for my calls.

Guy,

goochy
Posts: 12
Joined: Tue Apr 17, 2018 9:58 am

Re: MQTT for MicroPython under ESP32

Post by goochy » Sun Jul 22, 2018 10:02 am

G'day Guyd,

No, the Finally: goes at the end of the Try: and is used to do clean up operations.

Try:
Run some code.

Except:
if there is an error then it will get trapped and hopefully handled here.

Finally:
This will run no matter what happens.

Cheers, Steve

guyd
Posts: 81
Joined: Fri Jul 20, 2018 6:08 am

Re: MQTT for MicroPython under ESP32

Post by guyd » Sun Jul 22, 2018 10:10 am

Thank you,
the purpose of Finally - I know from good old python,
BUT
after closing the socket, will it keep listening forever ?

Cheers
Guy

goochy
Posts: 12
Joined: Tue Apr 17, 2018 9:58 am

Re: MQTT for MicroPython under ESP32

Post by goochy » Sun Jul 22, 2018 11:08 am

G'day Guyd,

Technically, closing a socket connection closes that method of transferring BITS over a network protocol. It would be considered a BAD way of doing it unless there was a specific reason to do so like saving battery power and just have some kind of RF triggers to instruct the socket to be opened again.

Unless the author of MQTT has written it so, there well could be a function still LISTENING for requests that never happen.

You would have to look at SIMPLE.PY stepping through the code to find out, or contact the author.


Cheers,

Post Reply