ESP32 and Raspberry pi 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 and Raspberry pi LED temperature trigger.

Post by Ivanhosa13 » Tue Mar 03, 2020 4:08 pm

hello everyone, i am working on a school project and i need help with it. i have MQTT client code that will run on the ESP32.It polls the DHT-22 and publishes the sensor data to a topic called temp humidity. then i have another script that ran on the raspberry pi 3 that will subscribe to the temp humidity topic and present the results to an OLED display. what i am thinking of doing is that i want to use another ESP32 that will connect to a LED strip which will subscribe to topic "temp humidity" and go over the data and when the temperature get higher than a certain number, for example 50c it should turn on the LED strip. "I need help making the code". i am using python for the main script on the raspberry pi 3 and micropython for the script on the ESP32.

Here is the MQTT client code that will run on the ESP32.

Code: Select all

from time import sleep
from umqtt.simple import MQTTClient
from machine import Pin
from dht import DHT22

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

client = MQTTClient(CLIENT_ID, SERVER)
client.connect()   # Connect to MQTT broker

sensor = DHT22(Pin(15, Pin.IN, Pin.PULL_UP))   # DHT-22 on GPIO 15 (input with internal pull-up resistor)

while True:
    try:
        sensor.measure()   # Poll sensor
        t = sensor.temperature()
        h = sensor.humidity()
        if isinstance(t, float) and isinstance(h, float):  # Confirm sensor results are numeric
            msg = (b'{0:3.1f},{1:3.1f}'.format(t, h))
            client.publish(TOPIC, msg)  # Publish sensor data to MQTT topic
            print(msg)
        else:
            print('Invalid sensor readings.')
    except OSError:
        print('Failed to read sensor.')
    sleep(4)

Here is the Raspberry Pi Python MQTT client code. 

import paho.mqtt.client as mqtt
import Adafruit_SSD1306
from PIL import Image, ImageDraw, ImageFont

disp = Adafruit_SSD1306.SSD1306_128_32(rst=0)
disp.begin()
FONT_PATH = '/usr/share/fonts/truetype/piboto/PibotoCondensed-Regular.ttf'
FONT = ImageFont.truetype(FONT_PATH, 22)

def display_data(t, h):
    image = Image.new('1', (disp.width, disp.height))
    draw = ImageDraw.Draw(image)
    # Draw temperature / Humidity values.
    draw.text((0, 8), '{0}°C'.format(t),  font=FONT, fill=255)
    draw.text((71, 8), '{0}%'.format(h), font=FONT, fill=255)
    # Draw bar charts.
    draw.rectangle((0, 0, 50, 8), outline=255, fill=0)
    draw.rectangle((71, 0, 121, 8), outline=255, fill=0)
    draw.rectangle((0, 0, t / 100.0 * 50, 8), outline=255, fill=255)
    draw.rectangle((71, 0, 71 + (h / 100.0 * 50), 8), outline=255, fill=255)
    # Send to OLED display.
    disp.clear()
    disp.image(image)
    disp.display()

	# Callback fires when conected to MQTT broker.
def on_connect(client, userdata, flags, rc):
    print('Connected with result code {0}'.format(rc))
    # Subscribe (or renew if reconnect).
    client.subscribe('temp_humidity')


# Callback fires when a published message is received.
def on_message(client, userdata, msg):
	# Decode temperature and humidity values from binary message paylod.
    t,h = [float(x) for x in msg.payload.decode("utf-8").split(',')]
    print('{0}°C {1}%'.format(t, h))
    display_data(t, h)  # Display data on OLED display.


client = mqtt.Client()
client.on_connect = on_connect  # Specify on_connect callback
client.on_message = on_message  # Specify on_message callback
client.connect('localhost', 1883, 60)  # Connect to MQTT broker (also running on Pi).

# Processes MQTT network traffic, callbacks and reconnections. (Blocking)
client.loop_forever()

kevinkk525
Posts: 969
Joined: Sat Feb 03, 2018 7:02 pm

Re: ESP32 and Raspberry pi LED temperature trigger.

Post by kevinkk525 » Tue Mar 03, 2020 5:12 pm

Your code looks good so far (except that the esp32 code will stop working if your wifi connection experiences a problem but that might be an improvement for later)
Where exactly do you need help with the esp32 code?
Judging from the code you presented, you should be able to write the esp32 code for the LED strip, assuming you find a library for the led-strip. which one is it?
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

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

Re: ESP32 and Raspberry pi LED temperature trigger.

Post by Ivanhosa13 » Wed Mar 04, 2020 5:17 pm

For the internet problem, I already have a solution for it. I have a script that will connect to the internet my it self.I don’t have a library for the led strip. Also I need help making the code if that possible. Thank you

kevinkk525
Posts: 969
Joined: Sat Feb 03, 2018 7:02 pm

Re: ESP32 and Raspberry pi LED temperature trigger.

Post by kevinkk525 » Wed Mar 04, 2020 8:32 pm

I'm still not sure where exactly you need help.. You haven' asked any specific question and without even a name of your led strip, we can't even point you to a library.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

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

Re: ESP32 and Raspberry pi LED temperature trigger.

Post by Ivanhosa13 » Wed Mar 04, 2020 8:59 pm

I am sorry for not being clear in my answer. It will be awesome if you can tell me which led strip will work fine with the project I am working on it. I did my research and all I can find is the neopixel LED strip but I think it will not work fine with the project I am working with. I was thinking of using a normal LED strip which is the one I have.

kevinkk525
Posts: 969
Joined: Sat Feb 03, 2018 7:02 pm

Re: ESP32 and Raspberry pi LED temperature trigger.

Post by kevinkk525 » Wed Mar 04, 2020 10:06 pm

As far as I know, there is no such thing as "normal LED strip". We would need to know what it is and how it can be connected to the ESP32.
However, I am not very familiar with LED strips, so I can't make any recommendations. But lots of people on this forum have used LED strips, they should be able to recommend something.

Why do you think NeoPixel will not work fine with your project?
You could use NeoPixel when temperature >50° and change the color according to the temperature.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

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

Re: ESP32 and Raspberry pi LED temperature trigger.

Post by Ivanhosa13 » Wed Mar 04, 2020 11:38 pm

Thank you for the response, I will order the NeoPixel led and try to work with it and see if it will work.

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

Re: ESP32 and Raspberry pi LED temperature trigger.

Post by Ivanhosa13 » Sun Mar 08, 2020 3:54 am

I am sorry, I know I am asking too much questions, but how to make another eap32 subscribe to the Topic that i have.

kevinkk525
Posts: 969
Joined: Sat Feb 03, 2018 7:02 pm

Re: ESP32 and Raspberry pi LED temperature trigger.

Post by kevinkk525 » Sun Mar 08, 2020 7:33 am

Ivanhosa13 wrote:
Sun Mar 08, 2020 3:54 am
I am sorry, I know I am asking too much questions, but how to make another eap32 subscribe to the Topic that i have.
You're not asking too many questions but some question show that you didn't do your research: https://github.com/micropython/micropyt ... sub_led.py
The same library you already used for publishing the temperature from your esp32 also has an example for subscribing to a topic.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

Post Reply