In the boot.py file i added:
led1 = machine.Pin(0, machine.Pin.OUT, value=0)
Last line
In the main.py file I added at -def sub_cb(topic, msg):
if msg == b'on': led.value(1) elif msg == b'off': led.value(0) elif msg == b'1on': led1.value(1) elif msg == b'1off': led1.value(0)
On the node Red Dash board I made a new Led1 switch and a output node and deployed it.
The new switch on webpage 192.168.2.192:1880/ui iI can switch but nothing happens.
Hello, I think you have two things missing. I need to know your exact MQTT topic name for the second LED… I’ll assume it’s called output1.
In the boot.py, you need to subscribe to that MQTT topic that Node-RED is publishing message:
topic_sub1 = b'output1'
In main.py, you need to subscribe to that topic. Add the following to your def connect_and_subscribe() function:
(...)
client.subscribe(topic_sub) client.subscribe(topic_sub1)
(...)
If you subscribe to the second topic, it should work for you! Let me know your results (double-check that you’ve entered the right topic name and values 1on/1off in Node-RED).
Good question! I’m trying to add a second (or more) temperature sensors (DS18B20) to the node-red chart. Has anyone done this? Seems like it would be similar to adding an LED but a little more involved.
Hi, can you please create a new question since it’s a different topic? This thread is to subscribe to an MQTT topic, but feel free to post your question here: https://rntlab.com/ask-question/
Thanks for your understanding!
No problem! Just trying keep every thread about one topic to make it easier to follow 🙂
(Note: this is the new thread “Python esp32 course chapter Node-Red, How does one add an additional temp sensor (DS18B20)?“)
hello
It still does not work . In the node_red I changed the “on” and “off” string to “1on” “1off”.
This is my boot file and main file
Start your code here # Complete project details at https://RandomNerdTutorials.com import time import onewire import ds18x20 from umqttsimple import MQTTClient import ubinascii import machine import micropython import network import esp esp.osdebug(None) import gc gc.collect() ssid = 'H368N1BC062' password = '222222222' mqtt_server = '192.168.2.192' #EXAMPLE IP ADDRESS #mqtt_server = '192.168.1.144' client_id = ubinascii.hexlify(machine.unique_id()) topic_sub = b'output' topic_sub1 = b'output1' topic_pub = b'temp' last_sensor_reading = 0 readings_interval = 5 station = network.WLAN(network.STA_IF) station.active(True) station.connect(ssid, password) while station.isconnected() == False: pass print('Connection successful') print(station.ifconfig()) ds_pin = machine.Pin(14) ds_sensor = ds18x20.DS18X20(onewire.OneWire(ds_pin)) led = machine.Pin(2, machine.Pin.OUT, value=0) led1 = machine.Pin(0, machine.Pin.OUT, value=0) main.py file
Start your code here # Complete project details at https://RandomNerdTutorials.com def read_ds_sensor(): roms = ds_sensor.scan() print('Found DS devices: ', roms) print('Temperatures: ') ds_sensor.convert_temp() time.sleep(1) for rom in roms: temp = ds_sensor.read_temp(rom) if isinstance(temp, float): # uncomment for Fahrenheit #temp = temp * (9/5) + 32.0 msg = (b'{0:3.1f}'.format(temp)) print(temp, end=' ') print('Valid temperature') return msg return b'0.0' def sub_cb(topic, msg): print((topic, msg)) if msg == b'on': led.value(1) elif msg == b'off': led.value(0) elif msg == b'on': led1.value(1) elif msg == b'off': led1.value(0) 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) client.subscribe(topic_sub1) 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() while True: try: client.check_msg() if (time.time() - last_sensor_reading) > readings_interval: msg = read_ds_sensor() client.publish(topic_pub, msg) last_sensor_reading = time.time() except onewire.OneWireError: print('Failed to read/publish sensor readings.') time.sleep(1) except OSError as e: restart_and_reconnect() if I change on off to 1on and 1off in below no result elif msg == b'1on': led1.value(1) elif msg == b'1off': led1.value(0) no result
Hows the Node-RED MQTT in node configured? Do you have a Node subscribe to output1?
On the Node-RED MQtt I have had configured output1 as topic
When I change the following in the main.py. I can switch the 2 leds on ……../ui
Start your code here
def sub_cb(topic, msg): print((topic, msg)) if msg == b'on': led.value(1) elif msg == b'off': led.value(0) elif msg == b'1on': led1.value(1) elif msg == b'1off': led1.value(0)