from utime import sleep
from hx711 import HX711
from time import sleep
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
# WiFi configuration
SSID = 'Wokwi-GUEST'
PASSWORD = ''
# MQTT configuration
MQTT_BROKER = "broker.mqtt-dashboard.com"
MQTT_CLIENT_ID = "clientId-ABoJZ7lRmk"
MQTT_TOPIC_SUBSCRIBE = "sensor_data"
def connect_to_wifi(ssid, password):
print("Connecting to WiFi", end="")
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.connect(ssid, password)
while not wifi.isconnected():
print(".", end="")
sleep(0.1)
print(" Connected!")
print(wifi.ifconfig())
connect_to_wifi(SSID, PASSWORD)
# MQTT message callback
def mqtt_callback(topic, msg):
print(f'Received message: {msg} on topic: {topic}')
# You can add code here to handle the message
# MQTT setup
mqtt_client = MQTTClient(MQTT_CLIENT_ID, MQTT_BROKER)
mqtt_client.set_callback(mqtt_callback)
mqtt_client.connect()
mqtt_client.subscribe(MQTT_TOPIC_SUBSCRIBE)
print(f'Subscribed to {MQTT_TOPIC_SUBSCRIBE}')
# 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 # Pin on ESP32
NUM_PIXELS = 16 # Number of NeoPixels
np = neopixel.NeoPixel(Pin(NEOPIXEL_PIN), NUM_PIXELS)
# DHT22 configuration
dht_pin = Pin(4, Pin.IN, Pin.PULL_UP) # Define the pin where the DHT22 is connected
sensor = dht.DHT22(dht_pin)
# Servo motor configuration
servo1 = PWM(Pin(12), freq=50) # Define the pin and frequency for the first servo
servo2 = PWM(Pin(15), freq=50) # Define the pin and frequency for the second servo
# Function to set servo angle
def set_servo_angle(servo, angle):
duty = int((angle / 180) * 1023) # Map angle to duty cycle (0-180 degrees)
servo.duty(duty)
# LDR configuration
ldr = ADC(Pin(27)) # Define the pin for LDR
ldr.atten(ADC.ATTN_11DB) # Set the attenuation for full range (0-3.3V)
# Buzzer configuration
buzzer = Pin(25, Pin.OUT) # Define the pin for the buzzer
# Relay configuration
relay = Pin(26, Pin.OUT) # Define the pin for the relay
# Example usage
def main():
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) # Clear the display
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: # Check if LDR value is below threshold
for i in range(NUM_PIXELS):
np[i] = (255, 255, 255) # Set all pixels to white
else:
for i in range(NUM_PIXELS):
np[i] = (0, 0, 0) # Turn off all pixels
np.write()
# Control servo motors
set_servo_angle(servo1, 90) # Set servo1 to 90 degrees
set_servo_angle(servo2, 90) # Set servo2 to 90 degrees
sleep(1)
set_servo_angle(servo1, 0) # Set servo1 to 0 degrees
set_servo_angle(servo2, 0) # Set servo2 to 0 degrees
sleep(1)
# Control buzzer
buzzer.value(1) # Turn on buzzer
sleep(0.5)
buzzer.value(0) # Turn off buzzer
sleep(0.5)
# Control relay
relay.value(1) # Turn on relay
sleep(1)
relay.value(0) # Turn off relay
sleep(1)
# Check for new messages
mqtt_client.check_msg()
if __name__ == "__main__":
main()