MQTT for MicroPython under ESP32
MQTT for MicroPython under ESP32
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
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
Re: MQTT for MicroPython under ESP32
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
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
Re: MQTT for MicroPython under ESP32
Hi,goochy wrote: ↑Sun Jul 22, 2018 7:54 amG'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
Great! my guess is "sub_cb" @uMQTT replaces "on_message" @ MQTT.
Can you explain why code at the end :" finally; c.disconnect()" ?
Re: MQTT for MicroPython under ESP32
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,
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,
Re: MQTT for MicroPython under ESP32
Hi,
donald_duck will do too
when you close a socket, how would you keep listening for future publishes ?
donald_duck will do too
when you close a socket, how would you keep listening for future publishes ?
Re: MQTT for MicroPython under ESP32
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,
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,
Re: MQTT for MicroPython under ESP32
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,
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,
Re: MQTT for MicroPython under ESP32
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
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
Re: MQTT for MicroPython under ESP32
Thank you,
the purpose of Finally - I know from good old python,
BUT
after closing the socket, will it keep listening forever ?
Cheers
Guy
the purpose of Finally - I know from good old python,
BUT
after closing the socket, will it keep listening forever ?
Cheers
Guy
Re: MQTT for MicroPython under ESP32
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,
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,