ESP32, MQTT, and LED temperature trigger

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Ivanhosa13
Posts: 14
Joined: Sat Feb 15, 2020 10:53 pm

ESP32, MQTT, and LED temperature trigger

Post by Ivanhosa13 » Fri Apr 10, 2020 10:11 pm

Hello everyone,

i am trying to use a WS2812B LED strip to work with a code that i have for the esp32. the code is a MQTT subscription code that subscribe to a temperature topic that i run on another ESP32. in this code if the temperature get higher than a certain degree, it will print a danger message. but what i need help with is to connect the led strip to esp32 and make the led turn on whenever the temperature get higher than a certain degree. also is it possible to run the LED strip using just the esp32?

here is the code:

Code: Select all

import time
from umqtt.robust import MQTTClient

SERVER = '172.20.10.11'  # MQTT Server Address (Change to the IP address of your Pi)
CLIENT_ID = 'ESP32_2'
TOPIC = b'temp_humidity'

def sub_cb(topic, msg):
    print((topic, msg))
    if float(msg.decode().split(",")[0])>30:
        print("danger")

c = MQTTClient(CLIENT_ID, SERVER)

c.DEBUG = True
c.set_callback(sub_cb)
        
if not c.connect(clean_session=False):
    print("New session being set up")
    c.subscribe(TOPIC)

while 1:
    c.wait_msg()


c.disconnect()


VicLuna
Posts: 11
Joined: Fri Sep 13, 2019 8:36 pm

Re: ESP32, MQTT, and LED temperature trigger

Post by VicLuna » Sat Apr 11, 2020 9:03 am

Yes of course you can switch ON your LED strip.

I guess that you LED strip is 220 AC power, so you need to add a relay for it.
This relay you should connect to a pin.out that it's subscribe to a topic.

this topic could be something like /led and you send a message 'on' or 'off' something like this:

def sub_cb(topic, msg):
global led

print((topic, msg)) this is just for checking.
if msg == b'on':
led.value(0)
elif msg == b'off':
led.value(1)

where led is the pin of your relay.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: ESP32, MQTT, and LED temperature trigger

Post by jimmo » Tue Apr 14, 2020 5:49 am

Ivanhosa13 wrote:
Fri Apr 10, 2020 10:11 pm
also is it possible to run the LED strip using just the esp32?
Yes. See http://docs.micropython.org/en/latest/e ... xel-driver

Note that you will need to power the strip from 5V, not 3.3V. I also recommend using a level shifter to convert the ESP32 pin (3V) to the WS2812 input (5V).
VicLuna wrote:
Sat Apr 11, 2020 9:03 am
I guess that you LED strip is 220 AC power, so you need to add a relay for it.
The OP mentioned that it's a WS2812 (i.e. NeoPixels) so likely this is not the case.

Ivanhosa13
Posts: 14
Joined: Sat Feb 15, 2020 10:53 pm

Re: ESP32, MQTT, and LED temperature trigger

Post by Ivanhosa13 » Fri Apr 17, 2020 7:51 pm

jimmo wrote:
Tue Apr 14, 2020 5:49 am
Ivanhosa13 wrote:
Fri Apr 10, 2020 10:11 pm
also is it possible to run the LED strip using just the esp32?
Yes. See http://docs.micropython.org/en/latest/e ... xel-driver

Note that you will need to power the strip from 5V, not 3.3V. I also recommend using a level shifter to convert the ESP32 pin (3V) to the WS2812 input (5V).


Thank you for your response, I have a question, for the wiring, the LED strip has three wires. The ground, 5v input, and Di . Where do I connect the Di in my esp32 (DEVKIT V1) regarding to the example you Provided it. I really appreciate your help.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: ESP32, MQTT, and LED temperature trigger

Post by jimmo » Fri Apr 17, 2020 11:38 pm

Ivanhosa13 wrote:
Fri Apr 17, 2020 7:51 pm
Where do I connect the Di in my esp32 (DEVKIT V1) regarding to the example you Provided it.
In that example it's connected to GPIO 0.

Code: Select all

pin = Pin(0, Pin.OUT)
You should probably use a level shifter, but I've heard people say it works without.

Ivanhosa13
Posts: 14
Joined: Sat Feb 15, 2020 10:53 pm

Re: ESP32, MQTT, and LED temperature trigger

Post by Ivanhosa13 » Fri Apr 17, 2020 11:45 pm

jimmo wrote:
Fri Apr 17, 2020 11:38 pm
Ivanhosa13 wrote:
Fri Apr 17, 2020 7:51 pm
Where do I connect the Di in my esp32 (DEVKIT V1) regarding to the example you Provided it.
In that example it's connected to GPIO 0.

Code: Select all

pin = Pin(0, Pin.OUT)
You should probably use a level shifter, but I've heard people say it works without.
The GPIO 0 is the same as the ground pin in my esp32 I think. Do I connect the ground also to it.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: ESP32, MQTT, and LED temperature trigger

Post by jimmo » Fri Apr 17, 2020 11:46 pm

No it's not the same.

You can use pretty much any pin though, just change the code.

Ivanhosa13
Posts: 14
Joined: Sat Feb 15, 2020 10:53 pm

Re: ESP32, MQTT, and LED temperature trigger

Post by Ivanhosa13 » Fri Apr 17, 2020 11:47 pm

Okay thank you

Post Reply