split dht sensor, add oled impl, add button triggered display example

This commit is contained in:
OMGeeky
2025-05-07 20:19:42 +02:00
parent f5af3419d5
commit 46f4e5a95e
9 changed files with 803 additions and 22 deletions

View File

@@ -0,0 +1,89 @@
# Button-Triggered Sensor Display
This document explains how to use the button-triggered sensor display example, which demonstrates an energy-efficient approach to reading and displaying sensor data on ESP32/ESP8266 devices.
## Overview
The button-triggered display example shows how to:
1. Set up a button input on an ESP device
2. Use low-power sleep mode to conserve energy
3. Wake up and read sensor data when the button is pressed
4. Display the data on an OLED screen
This approach is ideal for battery-powered applications where energy conservation is important.
## Hardware Requirements
- ESP32 or ESP8266 development board
- DHT22 temperature and humidity sensor
- SSD1306 OLED display (128x64 pixels recommended)
- Pushbutton
- 10K pull-up resistor (if your button doesn't have an internal pull-up)
- Breadboard and jumper wires
## Wiring
1. **DHT22 Sensor**:
- Connect VCC to 3.3V
- Connect GND to ground
- Connect DATA to GPIO4 (or change the pin in the code)
2. **OLED Display**:
- Connect VCC to 3.3V
- Connect GND to ground
- Connect SCL to GPIO22 (or change the pin in the code)
- Connect SDA to GPIO21 (or change the pin in the code)
3. **Button**:
- Connect one side to GPIO0 (or change the pin in the code)
- Connect the other side to ground
- Connect a 10K pull-up resistor between GPIO0 and 3.3V (if not using internal pull-up)
## Running the Example
1. Flash MicroPython to your ESP device if you haven't already
2. Upload the `button_triggered_display.py` script to your device
3. Run the script
```python
import button_triggered_display
button_triggered_display.main()
```
## How It Works
### Energy Conservation
The example uses ESP32's light sleep mode to conserve energy when not actively reading or displaying data. In light sleep mode:
- The CPU is paused
- RAM is preserved
- Peripherals can be configured to wake the device
- Power consumption is significantly reduced
### Button Wake-Up
The device is configured to wake up from sleep when the button is pressed. This is done using the `wake_on_ext0` function, which allows an external pin to trigger a wake-up event.
### Simulation Mode
The example includes a simulation mode that runs when not on actual ESP hardware. This allows you to test the functionality on a development computer before deploying to the ESP device.
## Customization
You can customize the example by:
1. Changing the GPIO pins for the sensor, display, or button
2. Adjusting the display time before going back to sleep
3. Adding additional sensors
4. Modifying the information displayed on the OLED screen
## Power Consumption
Typical power consumption in different states:
- Active mode (reading sensors and updating display): ~80-120mA
- Light sleep mode: ~0.8-1.5mA
This represents a power saving of approximately 98% during idle periods, significantly extending battery life.

165
docs/oled_display.md Normal file
View File

@@ -0,0 +1,165 @@
# OLED Display Module
This module provides a class for interfacing with SSD1306 OLED displays via I2C on ESP32/ESP8266 microcontrollers.
## Features
- Compatible with SSD1306 OLED displays
- I2C interface support
- Display text at specific coordinates
- Display a list of values (e.g., sensor readings)
- Simulation mode for testing without hardware
- Integration with the ESP Sensors framework
## Hardware Requirements
- ESP32 or ESP8266 microcontroller
- SSD1306 OLED display (common sizes: 128x64, 128x32, 64x48)
- I2C connection (2 pins: SCL and SDA)
## Installation
The OLED display module is part of the ESP Sensors package. No additional installation is required if you have already installed the package.
## Usage
### Basic Initialization
```python
from esp_sensors.oled_display import OLEDDisplay
# Initialize the 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
address=0x3C # I2C address (default: 0x3C)
)
```
### Displaying Text
```python
# Clear the display
display.clear()
# Display text at specific coordinates
display.display_text("Hello, World!", x=0, y=0)
display.display_text("Line 2", x=0, y=10)
display.display_text("ESP32", x=64, y=30)
```
### Displaying Multiple Values
```python
# Display a list of values (e.g., sensor readings)
display.display_values([
"Temperature: 25.5°C",
"Humidity: 45%",
"Pressure: 1013 hPa",
"Time: 12:34:56"
])
```
### Integration with Sensors
```python
from esp_sensors.dht22 import DHT22Sensor
# Initialize a DHT22 sensor
sensor = DHT22Sensor("Living Room", pin=4)
# Read sensor values
temperature = sensor.read_temperature()
humidity = sensor.read_humidity()
# Display sensor values
display.display_values([
f"Temp: {temperature:.1f}°C",
f"Humidity: {humidity:.1f}%"
])
```
## API Reference
### Class: OLEDDisplay
#### Constructor
```python
OLEDDisplay(name, scl_pin, sda_pin, width=128, height=64, address=0x3C, interval=60)
```
Parameters:
- `name` (str): The name of the display
- `scl_pin` (int): The GPIO pin number for the SCL (clock) line
- `sda_pin` (int): The GPIO pin number for the SDA (data) line
- `width` (int): Display width in pixels (default: 128)
- `height` (int): Display height in pixels (default: 64)
- `address` (int): I2C address of the display (default: 0x3C)
- `interval` (int): Refresh interval in seconds (default: 60)
#### Methods
##### clear()
Clears the display.
```python
display.clear()
```
##### display_text(text, x=0, y=0, color=1)
Displays text at the specified position.
Parameters:
- `text` (str): The text to display
- `x` (int): X coordinate (default: 0)
- `y` (int): Y coordinate (default: 0)
- `color` (int): Pixel color (1 for white, 0 for black, default: 1)
```python
display.display_text("Hello", x=10, y=20)
```
##### display_values(values)
Displays a list of values on the OLED screen.
Parameters:
- `values` (list): List of values to display (strings or objects with __str__ method)
```python
display.display_values(["Line 1", "Line 2", "Line 3"])
```
##### get_metadata()
Returns a dictionary containing display metadata.
```python
metadata = display.get_metadata()
print(metadata)
```
## Troubleshooting
### Display Not Working
1. Check the I2C address (common addresses are 0x3C and 0x3D)
2. Verify the SCL and SDA pin connections
3. Ensure the display is powered correctly (usually 3.3V)
4. Try a different I2C bus speed if available
### Text Not Displaying Correctly
1. Check that the coordinates are within the display dimensions
2. Ensure the text doesn't exceed the display width
3. Try using smaller font or breaking text into multiple lines
## Example
See the `examples/oled_display_example.py` file for a complete example of using the OLED display with sensors.