# Affordable Weather Station for Farming Communities
# Program 10: Reduce Inequality
# Designed for ESP32 with MicroPython
print("Program 10: Reduce Inequality")
print("Affordable Weather Station for Farming Communities")
# Import necessary libraries
from machine import Pin, I2C, ADC, PWM
import dht
import ssd1306
import time
# -------------------------------------
# Pin Assignments
# -------------------------------------
# DHT22 Sensor (Temperature and Humidity)
DHT_PIN = 15 # Adjust pin according to your setup
# Light Intensity Sensor (LDR)
LIGHT_SENSOR_PIN = 34 # GPIO 34 for LDR sensor
# OLED Display Pins (SSD1306)
SCL_PIN = 22 # Clock pin for I2C
SDA_PIN = 21 # Data pin for I2C
# Buzzer Pin (PWM)
BUZZER_PIN = 25 # GPIO 25 for Buzzer
# LED Pin
LED_PIN = 26 # GPIO 26 for LED
# -------------------------------------
# Sensor and Component Initialization
# -------------------------------------
# Initialize DHT22 Sensor (Temperature and Humidity)
dht_sensor = dht.DHT22(Pin(DHT_PIN))
# Initialize Light Intensity Sensor (LDR)
light_sensor = ADC(Pin(LIGHT_SENSOR_PIN))
light_sensor.atten(ADC.ATTN_11DB) # Set attenuation for 3.3V input range
# Initialize OLED Display (SSD1306)
i2c = I2C(0, scl=Pin(SCL_PIN), sda=Pin(SDA_PIN))
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
# Initialize Buzzer (PWM)
buzzer = PWM(Pin(BUZZER_PIN)) # Set the buzzer as a PWM output
buzzer.freq(1000) # Set initial frequency to 1000 Hz
buzzer.duty(0) # Turn off buzzer initially
# Initialize LED
led = Pin(LED_PIN, Pin.OUT)
led.off() # Ensure LED is initially off
# -------------------------------------
# Helper Functions
# -------------------------------------
# Function to read light intensity
def read_light_intensity():
return light_sensor.read() # Returns a value between 0 and 4095
# Function to display data on OLED
def display_data(temp, hum, light):
oled.fill(0) # Clear the display
oled.text("Weather Station", 0, 0)
oled.text(f"Temp: {temp} C", 0, 16)
oled.text(f"Humidity: {hum} %", 0, 32)
oled.text(f"Light: {light}", 0, 48)
oled.show()
# Function to blink LED and sound buzzer
def activate_warning():
print("Warning: Activating LED and buzzer blinking...")
for _ in range(5): # Blink and sound warning for 5 cycles
buzzer.duty(512) # 50% duty cycle (sound on)
led.on() # LED on
time.sleep(0.5) # Wait 0.5 seconds
buzzer.duty(0) # Turn off buzzer
led.off() # LED off
time.sleep(0.5) # Wait 0.5 seconds
# Function to deactivate warning (buzzer and LED)
def deactivate_warning():
print("Warning deactivated: Turning off LED and buzzer...")
buzzer.duty(0) # Turn off the buzzer
led.off() # Turn off the LED
# -------------------------------------
# Main Program Loop
# -------------------------------------
def main():
while True:
try:
# Read temperature and humidity from DHT22 sensor
dht_sensor.measure()
temperature = dht_sensor.temperature()
humidity = dht_sensor.humidity()
# Read light intensity from LDR sensor
light_intensity = read_light_intensity()
# Display data on OLED
display_data(temperature, humidity, light_intensity)
# Debugging messages
print(f"Temperature: {temperature} C, Humidity: {humidity} %, Light Intensity: {light_intensity}")
# Check conditions for warning
if temperature < 20 or light_intensity < 1000: # Thresholds for warning
activate_warning() # Blink LED and sound buzzer
else:
deactivate_warning() # Turn off warning signals
except Exception as e:
print("Error:", e)
# Wait for 2 seconds before the next reading
time.sleep(2)
# -------------------------------------
# Run the Program
# -------------------------------------
if __name__ == "__main__":
main()