add configuration system for ESP Sensors project

This commit is contained in:
OMGeeky
2025-05-07 21:27:32 +02:00
parent e12ba1aaee
commit f544a64ecf
14 changed files with 813 additions and 158 deletions

View File

@@ -16,6 +16,7 @@ sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')
from src.esp_sensors.oled_display import OLEDDisplay
from src.esp_sensors.dht22 import DHT22Sensor
from src.esp_sensors.config import load_config, get_sensor_config, get_display_config, get_button_config
# Import hardware-specific modules if available (for ESP32/ESP8266)
try:
@@ -44,28 +45,29 @@ def main():
"""
Main function to demonstrate button-triggered sensor display.
"""
# Initialize a DHT22 sensor
# Load configuration
config = load_config()
# Initialize a DHT22 sensor using configuration
dht_sensor = DHT22Sensor(
name="Living Room",
pin=4, # GPIO pin for DHT22 data
interval=5, # Read every 5 seconds
unit="C" # Celsius
config=config # Pass the loaded config
)
print(f"Initialized DHT22 sensor: {dht_sensor.name}, pin: {dht_sensor.pin}")
# Initialize an OLED display
# Initialize an OLED display using configuration
display = OLEDDisplay(
name="Status Display",
scl_pin=22, # GPIO pin for I2C clock
sda_pin=21, # GPIO pin for I2C data
width=128, # Display width in pixels
height=64, # Display height in pixels
interval=1 # Update every second
config=config # Pass the loaded config
)
print(f"Initialized OLED display: {display.name}, size: {display.width}x{display.height}")
# Set up button using configuration
button_config = get_button_config("main_button", config)
button_pin = button_config.get("pin", 0)
print(f"Using button on pin: {button_pin}")
# Set up button on GPIO pin 0 (common button pin on many ESP boards)
button_pin = 0
if not SIMULATION:
button = Pin(button_pin, Pin.IN, Pin.PULL_UP)
pull_up = button_config.get("pull_up", True)
button = Pin(button_pin, Pin.IN, Pin.PULL_UP if pull_up else None)
# Display initialization message
display.clear()

View File

@@ -2,104 +2,112 @@
Example script for using the DHT22 sensor with an ESP32.
This script demonstrates how to initialize and read from a DHT22 sensor
connected to an ESP32 microcontroller.
connected to an ESP32 microcontroller using the configuration system.
Usage:
- Upload this script to your ESP32 running MicroPython
- Connect the DHT22 sensor to the specified GPIO pin
- Ensure config.json is properly set up with DHT22 sensor configuration
- The script will read temperature and humidity at the specified interval
"""
import time
import sys
import os
# Add the src directory to the Python path if needed
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
# Check if running on MicroPython
if sys.implementation.name == 'micropython':
from src.esp_sensors.dht22 import DHT22Sensor
# Configuration
SENSOR_NAME = "living_room"
SENSOR_PIN = 4 # GPIO pin where DHT22 is connected
READ_INTERVAL = 5 # seconds between readings
from src.esp_sensors.config import load_config, get_sensor_config
def main():
print(f"Initializing DHT22 sensor on pin {SENSOR_PIN}")
# Initialize the sensor
sensor = DHT22Sensor(SENSOR_NAME, SENSOR_PIN, READ_INTERVAL)
# Load configuration
config = load_config()
dht_config = get_sensor_config("dht22", config)
print(f"Initializing DHT22 sensor from configuration")
print(f"Sensor name: {dht_config.get('name')}")
print(f"Sensor pin: {dht_config.get('pin')}")
# Initialize the sensor using configuration
sensor = DHT22Sensor(config=config)
print("Starting sensor readings. Press Ctrl+C to stop.")
try:
while True:
# Read temperature
temperature = sensor.read()
# Read humidity
humidity = sensor.read_humidity()
# Get the current timestamp
timestamp = time.time()
# Print readings
print(f"Time: {timestamp}")
print(f"Temperature: {temperature}°C ({sensor.to_fahrenheit()}°F)")
print(f"Humidity: {humidity}%")
print("-" * 30)
# Wait for the next reading
time.sleep(READ_INTERVAL)
time.sleep(sensor.interval)
except KeyboardInterrupt:
print("Sensor readings stopped.")
except Exception as e:
print(f"Error: {e}")
if __name__ == "__main__":
main()
else:
print("This script is designed to run on MicroPython on an ESP32.")
print("Running in simulation mode for demonstration purposes.")
# Import for simulation mode
from src.esp_sensors.dht22 import DHT22Sensor
# Configuration
SENSOR_NAME = "simulation"
SENSOR_PIN = 4
READ_INTERVAL = 2 # shorter interval for demonstration
from src.esp_sensors.config import load_config, get_sensor_config
def main():
print(f"Initializing DHT22 sensor simulation")
# Initialize the sensor
sensor = DHT22Sensor(SENSOR_NAME, SENSOR_PIN, READ_INTERVAL)
# Load configuration
config = load_config()
dht_config = get_sensor_config("dht22", config)
print(f"Initializing DHT22 sensor simulation from configuration")
print(f"Sensor name: {dht_config.get('name')}")
print(f"Sensor pin: {dht_config.get('pin')}")
# Initialize the sensor using configuration
sensor = DHT22Sensor(config=config)
print("Starting simulated sensor readings. Press Ctrl+C to stop.")
try:
for _ in range(5): # Just do 5 readings for the simulation
# Read temperature
temperature = sensor.read()
# Read humidity
humidity = sensor.read_humidity()
# Get the current timestamp
timestamp = time.time()
# Print readings
print(f"Time: {timestamp}")
print(f"Temperature: {temperature}°C ({sensor.to_fahrenheit()}°F)")
print(f"Humidity: {humidity}%")
print("-" * 30)
# Wait for the next reading
time.sleep(READ_INTERVAL)
time.sleep(sensor.interval)
print("Simulation complete.")
except KeyboardInterrupt:
print("Sensor readings stopped.")
except Exception as e:
print(f"Error: {e}")
if __name__ == "__main__":
main()
main()

View File

@@ -1,5 +1,6 @@
"""
Example usage of the OLED display with temperature and humidity sensors.
This example demonstrates how to use the configuration system to initialize sensors and displays.
"""
import time
import sys
@@ -10,29 +11,44 @@ sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')
from src.esp_sensors.oled_display import OLEDDisplay
from src.esp_sensors.dht22 import DHT22Sensor
from src.esp_sensors.config import load_config, get_sensor_config, get_display_config
def main():
"""
Main function to demonstrate OLED display usage with sensors.
"""
# Initialize a DHT22 sensor
dht_sensor = DHT22Sensor(
name="Living Room",
pin=4, # GPIO pin for DHT22 data
interval=5, # Read every 5 seconds
unit="C" # Celsius
)
# Load configuration from file
print("Loading configuration from config.json...")
config = load_config()
# Initialize an OLED display
display = OLEDDisplay(
name="Status Display",
scl_pin=22, # GPIO pin for I2C clock
sda_pin=21, # GPIO pin for I2C data
width=128, # Display width in pixels
height=64, # Display height in pixels
interval=1 # Update every second
# Method 1: Initialize sensors using configuration directly
print("\nMethod 1: Initialize using configuration directly")
# Get configuration for DHT22 sensor
dht_config = get_sensor_config("dht22", config)
print(f"DHT22 config: {dht_config}")
# Initialize a DHT22 sensor with configuration
dht_sensor = DHT22Sensor(
sensor_type="dht22", # This will load config for this sensor type
config=config # Pass the loaded config
)
print(f"Created DHT22 sensor: {dht_sensor.name}, pin: {dht_sensor.pin}, interval: {dht_sensor.interval}s")
# Method 2: Initialize with some parameters from config, others specified directly
print("\nMethod 2: Override some config parameters")
# Initialize an OLED display with some custom parameters
display = OLEDDisplay(
# These parameters will override the config values
name="Custom Display",
interval=1, # Update every second
# Other parameters will be loaded from config
config=config
)
print(f"Created OLED display: {display.name}, size: {display.width}x{display.height}, interval: {display.interval}s")
# Display initialization message
display.clear()