In the boot.py file I removed: topic_sub = b’output’ and led = machine.Pin(2, machine.Pin.OUT. value=0)
In the main.py file I removed: client.subscribe(topic_sub).
The defenition -def connect_and_subscribe(0: I don,t know how to configure because there is no topic_sub and I get the error no sub_cb is defined.
Hello, here’s what you need to remove. In the boot.py: https://github.com/RuiSantosdotme/ESP-MicroPython/blob/master/code/MQTT/Node_RED_Client/boot.py
Line 22 and line 42:
topic_sub = b'output'
led = machine.Pin(2, machine.Pin.OUT, value=0)
In the main.py: https://github.com/RuiSantosdotme/ESP-MicroPython/blob/master/code/MQTT/Node_RED_Client/main.py
You need to remove lines 20-25:
def sub_cb(topic, msg): print((topic, msg)) if msg == b'on': led.value(1) elif msg == b'off': led.value(0)
Modify line 28 to:
global client_id, mqtt_server
And line 30:
You also need to remove line 32 and 33:
client.subscribe(topic_sub) print('Connected to %s MQTT broker, subscribed to %s topic' % (mqtt_server, topic_sub))
You forgot that line 30, 32, 33 and that’s why you got that error message. Let me know if that solves your problem.
The above correction do not solve the problem and also I don’t understand.
Start your code here def connect_and_subscribe(): global client_id, mqtt_server, topic_sub client = MQTTClient(client_id, mqtt_server) client.set_callback(sub_cb) client.connect() client.subscribe(topic_sub) print('Connected to %s MQTT broker, subscribed to %s topic' % (mqtt_server, topic_sub)) return client
In the above “def connnect_and_subscribe():” The client.subscribe(topic_sub) is removed. In the following line is connected to the broker and subscribed to mqtt_server, Topic_sub).
But there is no Topic_sub I hope you can give me more info.
You’re right! Sorry about that. I’ve updated my previous answer… Can you double-check what I’ve said?
In summary, your connect_and_subscribe() function should look like this:
def connect_and_subscribe(): global client_id, mqtt_server client = MQTTClient(client_id, mqtt_server) client.connect() return client
I hope that works for you!