I’ve been following the MicroPython MQTT example in the MicroPython course and I am not sure how to enter my MQTT username and password. I have found it works !! if I edit the library file but this does not feel correct. I would have expected password and username to be set in the boot.py
section of boot.py
ssid = 'REPLACE_WITH_YOUR_SSID' password = 'REPLACE_WITH_YOUR_PASSWORD' mqtt_server = 'REPLACE_WITH_YOUR_MQTT_BROKER_IP' #EXAMPLE IP ADDRESS #mqtt_server = '192.168.1.144' client_id = ubinascii.hexlify(machine.unique_id()) section of mqtt library umqttsimple.py class MQTTClient: def __init__(self, client_id, server, port=0, user=None, password=None, keepalive=0, ssl=False, ssl_params={}): if port == 0: port = 8883 if ssl else 1883 self.client_id = client_id self.sock = None self.server = server self.port = port self.ssl = ssl self.ssl_params = ssl_params self.pid = 0 self.cb = None self.user = user self.pswd = password self.keepalive = keepalive self.lw_topic = None self.lw_msg = None self.lw_qos = 0 self.lw_retain = False
Please can you help ?
Peter
Hi Peter.
If your MQTT broker requires username and password, you should use the following line to pass your broker username and password as arguments when initializing the client.
client = MQTTClient(client_id, mqtt_server, user=your_username, password=your_password)
Tell me if this works.
Regards,
Sara
Thank you, that works works, you can’t imagine how long it has taken me to solve the problem.
I entered user = ‘hassio’ and password =’xxxxxx’ is that correct ? This then means I don’t need to touch the library file.
I’m so pleased, i’ve got Micropython to read a switch, set an led and send the status to my MQTT server !!!
It’s so much quicker and more visual than using arduino. It’s better than using Tasmota as I can fully control the code and add oled screens etc.
A further question I need to ask is :
My program runs fine with boot.py, main.py and MQTT loaded onto the ESP8266 – I press reset to run.
It fails if I run it from the IDE, is this what you would expect?
I do appreciate being able to ask questions
Best wishes
Peter
Hi Peter.
I’m glad your problem was solved and that you are enjoying MicroPython.
What do you mean by it fails if you run from the IDE? How do you run from the IDE?
What IDE are you using?
Regards.
Sara
Ok,
If I have three files, boot.py main.py and the mqtt.py on the nodemcu and I press reset on the board with the board connected to Thonny Ide the board boots, loads the connecting reports and any print statements that I have programmed. It also sends the mqtt commands which I can read with a mqtt debugging program.
If instead of reset, i use the run button within Thonny to run main.py the execution fails.
I am unclear if Thonny can execute boot.py.
What’s the version of Thonny IDE that you’re using?
They recently updated to a new version that is slightly different than previous versions.
When the execution fails, do you get any error messages on the shell? Can you copy the messages?
Regards,
Sara
I’m using Thonny 3.2.7. on my laptop.
Should I be able to execute boot.py from the ide ?
Peter
Hi Peter.
Yes, you should be able to execute from the IDE.
However, notice that when you execute from the IDE, you’re only running the code and not actually uploading it to the board.
What’s the error that you get?
Regards,
Sara
Sara, I’ve done lots of experimenting and firstly, I am very impressed with the standard of your tutorials, thanks. I have tried a few of your tutorials and they are well documented and work.
I’ve also got your ebook on my Ipad (you simply email the pdf to your kindle account) and it’s easy to read.
I more or less understand how to run code off the pc or on the microprocessor and Thonny does work well.
I am still unsure if I run boot.py from the ide, is this the same as pressing reset and I am not sure why you would put some code in boot.py rather than leave boot.py empty and put all the code in main.py
i would appreciate your comments about how boot.py should be used.
Peter
Hi.
Running boot.py from the IDE, it’s not the same as pressing the RST button.
When you press the RST button, the ESP32 runs the stored program. If it doesn’t have any program stored, it will do nothing.
However, if you run boot.py from the IDE, you’re using the ESP32 to run your program. The program isn’t actually stored on the ESP32.
Usually, you can see the files stored on the ESP32 on the left side of the IDE. If there isn’t a file saved on the ESP32 device, when you press the RST button, nothing will happen.
With MicroPython, there’s the boot.py file that is created by default when you flash microPython, and then there’s the main.py that you should create to contain the main code.
The MicroPython documentation says that boot.py is used to store configurations, include libraries and initialize variables (things that will only run once). And then, store the main program on main.py.
However, if you put everything on main.py, it will work well too. If that’s more practical for you, you can just use main.py.
I hope this is clear.
Thanks for following our work.
Regards,
Sara