Problem with connection between ESP8266-01 and MQTT-Broker

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
homeracer
Posts: 1
Joined: Thu Feb 22, 2018 3:36 pm

Problem with connection between ESP8266-01 and MQTT-Broker

Post by homeracer » Thu Feb 22, 2018 5:35 pm

Iam very new in programming Python und Micropython and i am new in this forum.
I want to run the example code "example_sub_led.py" from the umqtt.simple library on github.

I use a ESP8266-01 with Micropython v1.9.3 and a mosquitto broker on a raspberry pi 3.
I tested the broker with an app on my smartphone, so the server works good.
The Broker works only with authentification.
I added code for the authentification (PORT, USER and PASSWORD) to the example code:

# Default MQTT server to connect to
SERVER = "192.168.178.68"
PORT = 1883
USER = b"username"
PASSWORD = b"Waschtrockner"
CLIENT_ID = ubinascii.hexlify(machine.unique_id())
TOPIC = b"led"
........

and

def main(server=SERVER, port=PORT, user=USER, password=PASSWORD):
c = MQTTClient(CLIENT_ID, server, port, user, password)
# Subscribed messages will be delivered to this callback
c.set_callback(sub_cb)
.........

In the "simple.py" file which i renamed to "umqtt.simple.py" i didn´t anything, is this right?

I added code to the boot.py file so that the ESP8266 connects automaticaly with my WLAN, thats woks. After booting the ESP i get the Ip-adress in the serial REPL of the ESP.

I loaded up the simple.py file but i renamed it to umqttt.simple.py. And i loaded up the file "example_sub_led.py" but renamed in main.py

So now i have three files on the ESP8266 the boot.py, umqtt.simple.py and main.py

When i have done something wrong, please tell me.

After reset the ESP-Board i published "on" and "off" to the Topic "led" (from my ipad to the Broker).
I hoped that the Led goes on and off but nothing happend.
After flashing the firmware to the ESP it had no main.py file, maybe this is an important information.

Then i tried to connect manualy with the mqtt over the REPL:

c=MQTTClient("client", "192.168.178.68", "username", "Waschtrockner")

At the fist time i send this i get this message back:
Traceback (most recent call last):
File "<stdin>", line 1
SyntaxError:invalid syntax

Then i tried it again:
c=MQTTClient("client", "192.168.178.68", "username", "Waschtrockner")
c.connect()

Now i get the error-message after "c.connect()":

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "umqtt/simple.py", line 57, in connnect
TypeError: can´t convert str to int

Could it be that my main.py file doesn´t run? If yes, how can i fix it?
But when the main.py runs the connetion problem is there.

I hope someone can help me. At this point i have no idea how i fix this.
(i hope my english ist good enough to understand)

best regrads Konstantin

cefn
Posts: 230
Joined: Tue Aug 09, 2016 10:58 am

Re: Problem with connection between ESP8266-01 and MQTT-Broker

Post by cefn » Sun Feb 25, 2018 9:32 am

There may be other issues here, but for the error you report, please look more closely at MQTTClient#__init__() in https://github.com/micropython/micropyt ... /simple.py as you are apparently populating the port argument with your username. You can see that the port argument is accessed on line 57 and us the only thing which might need to be an int on that line.

Personally I would not put a main.py on the board until I can prove interactively that the steps work by copying them line-by-line into the REPL and seeing the results.

Post Reply