I’m having trouble getting my head around multiple pins on esp8266 with node red and micropython I can’t get broker to recognize more than one subject.
Hi.
What project are you following? and what errors are you receiving?
Can you provide more information?
Module 5 on micropython for esp8266 and esp32 what iwant to do is equivalent to adding a second led. (I’m going to use it to control two relays)
Hi.
Can you share your code? Are you publishing on multiple topics, or publishing different messages on the same topic?
You can use pastebin to share your code.
Regards,
Sara
Start your code here boot.py import time from umqttsimple import MQTTClient import ubinascii import machine import micropython import network import esp esp.osdebug(None) import gc gc.collect() ssid = '**********' password = '********' mqtt_server = '192.168.1.241' #EXAMPLE IP ADDRESS #mqtt_server = '192.168.1.144' client_id = ubinascii.hexlify(machine.unique_id()) topic_sub = b'relay' station = network.WLAN(network.STA_IF) station.active(True) station.connect(ssid, password) while station.isconnected() == False: pass print('Connection successful') print(station.ifconfig()) gPin = machine.Pin(4, machine.Pin.OUT, value=1) cPin = machine.Pin(5, machine.Pin.OUT, value=1) main.py # Complete project details at https://RandomNerdTutorials.com def sub_cb(topic, msg): print((topic, msg)) if msg == b'gon': gPin.value(0) elif msg == b'goff': gPin.value(1) elif msg == b'con': cPin.value(0) elif msg == 'bcoff': cPin.value(1) 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 def restart_and_reconnect(): print('Failed to connect to MQTT broker. Reconnecting...') time.sleep(10) machine.reset() try: client = connect_and_subscribe() except OSError as e: restart_and_reconnect()
Hi.
Can you add the following snippet to the end of your main.py file and see if it works?
while True: try: client.check_msg() except OSError as e: restart_and_reconnect()
Basically, this snippet checks whether there’s a pending message to receive and delivers that message to the sub_cb() function.
Regards,
Sara
That’s the answer thanks very much. By the way I have enjoyed the course clear and very helpful.
I think it would help if you could produce a tutorial(s) on more elaborate mqtt examples etc.
Hi.
I’m glad it is working now.
Yes, you’re right. Examples like publishing on multiple topics, publishing different messages and so on, have more than two devices communicating, etc, and other subjects.
Thanks for the suggestion. We’ll may come up with new projects on the next ebook update, but at the moment it’s not planned yet.
Regards,
Sara