from time import sleep
from hx711 import HX711
import network
import machine
import ujson
from umqtt.simple import MQTTClient
import neopixel
from machine import Pin, I2C, PWM, ADC
from ssd1306 import SSD1306_I2C
import dht
# MQTT configuration
MQTT_BROKER = 'your_mqtt_broker_address'
MQTT_TOPIC = 'your/subscribe/topic'
MQTT_CLIENT_ID = 'ESP32_client'
# OLED configuration
WIDTH = 128
HEIGHT = 64
i2c = I2C(0, scl=Pin(22), sda=Pin(21), freq=200000)
oled = SSD1306_I2C(WIDTH, HEIGHT, i2c)
# NeoPixel configuration
NEOPIXEL_PIN = 14
NUM_PIXELS = 16
np = neopixel.NeoPixel(Pin(NEOPIXEL_PIN), NUM_PIXELS)
# DHT22 configuration
dht_pin = Pin(4, Pin.IN, Pin.PULL_UP)
sensor = dht.DHT22(dht_pin)
# Servo motor configuration
servo1 = PWM(Pin(12), freq=50)
servo2 = PWM(Pin(15), freq=50)
# Function to set servo angle
def set_servo_angle(servo, angle):
duty = int((angle / 180) * 1023)
servo.duty(duty)
# LDR configuration
ldr = ADC(Pin(27))
ldr.atten(ADC.ATTN_11DB)
# Buzzer configuration
buzzer = Pin(25, Pin.OUT)
# Relay configuration
relay = Pin(26, Pin.OUT)
# Wi-Fi configuration
def connect_to_wifi():
ssid = 'your_wifi_ssid'
password = 'your_wifi_password'
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
while not wlan.isconnected():
sleep(1)
print('Connected to Wi-Fi')
# MQTT message callback
def mqtt_callback(topic, msg):
print(f'Received message: {msg} on topic: {topic}')
# Handle the received message here
# Example: if the message is "TURN_ON_BUZZER", turn on the buzzer
if msg == b'TURN_ON_BUZZER':
buzzer.value(1)
elif msg == b'TURN_OFF_BUZZER':
buzzer.value(0)
elif msg == b'TURN_ON_RELAY':
relay.value(1)
elif msg == b'TURN_OFF_RELAY':
relay.value(0)
# Connect to MQTT broker and subscribe to topic
def connect_to_mqtt():
client = MQTTClient(MQTT_CLIENT_ID, MQTT_BROKER)
client.set_callback(mqtt_callback)
client.connect()
client.subscribe(MQTT_TOPIC)
print('Connected to MQTT broker and subscribed to topic')
return client
# Example usage
def main():
connect_to_wifi()
client = connect_to_mqtt()
while True:
# Read temperature and humidity from DHT22
sensor.measure()
temp = sensor.temperature()
humidity = sensor.humidity()
print(f'Temperature: {temp}°C, Humidity: {humidity}%')
# Read LDR value
ldr_value = ldr.read()
print(f'LDR value: {ldr_value}')
# Display temperature, humidity, and LDR value on OLED
oled.fill(0)
oled.text(f'Temp: {temp}C', 0, 0)
oled.text(f'Hum: {humidity}%', 0, 10)
oled.text(f'LDR: {ldr_value}', 0, 20)
oled.show()
# Control NeoPixel based on LDR value
if ldr_value > 1750:
for i in range(NUM_PIXELS):
np[i] = (255, 255, 255)
else:
for i in range(NUM_PIXELS):
np[i] = (0, 0, 0)
np.write()
# Control servo motors
set_servo_angle(servo1, 90)
set_servo_angle(servo2, 90)
sleep(1)
set_servo_angle(servo1, 0)
set_servo_angle(servo2, 0)
sleep(1)
# Check for new MQTT messages
client.check_msg()
# Example of sending a message to the broker (optional)
# client.publish('your/publish/topic', 'Hello from ESP32')
sleep(1)
if __name__ == '__main__':
main()