6.7 KiB
ESP32 Deployment Guide for Button-Triggered Display Application
This guide provides step-by-step instructions for setting up and deploying the button-triggered display application to an ESP32 microcontroller.
Required Hardware Components
- ESP32 Development Board - Any ESP32-based board (like NodeMCU-32S, WEMOS LOLIN32, etc.)
- DHT22 Sensor - Temperature and humidity sensor
- SSD1306 OLED Display - 128x64 pixel I2C OLED display
- Push Button - Momentary push button
- Breadboard and Jumper Wires - For connecting components
- Micro USB Cable - For connecting ESP32 to your computer
Wiring Instructions
Connect the components to your ESP32 as follows:
DHT22 Sensor
- VCC pin → 3.3V on ESP32
- GND pin → GND on ESP32
- DATA pin → GPIO 4 on ESP32
SSD1306 OLED Display (I2C)
- VCC pin → 3.3V on ESP32
- GND pin → GND on ESP32
- SCL pin → GPIO 22 on ESP32
- SDA pin → GPIO 21 on ESP32
Push Button
- Connect one terminal to GPIO 0 on ESP32
- Connect the other terminal to GND on ESP32
- (The internal pull-up resistor will be enabled in the code)
Setting Up MicroPython on ESP32
-
Install Python and required tools:
pip install esptool -
Download MicroPython firmware:
- Visit the MicroPython downloads page
- Download the latest stable firmware (.bin file)
-
Erase the ESP32 flash:
esptool.py --port /dev/ttyUSB0 erase_flashNote: Replace
/dev/ttyUSB0with your ESP32's port (on Windows, it might beCOM3or similar) -
Flash MicroPython firmware:
esptool.py --chip esp32 --port /dev/ttyUSB0 --baud 460800 write_flash -z 0x1000 esp32-20230426-v1.20.0.binNote: Replace the firmware filename with the one you downloaded
Installing Required Libraries on ESP32
You'll need to install the following libraries on your ESP32:
ssd1306.pyfor the OLED displaydht.pyfor the DHT22 sensor
You can use rshell to upload these libraries:
-
Install rshell:
pip install rshell -
Download the required libraries:
- SSD1306 library: micropython-ssd1306
- DHT library: This is usually included in MicroPython firmware for ESP32
-
Upload the SSD1306 library (if not already included in firmware):
rshell -p /dev/ttyUSB0 cp ssd1306.py /pyboard/
Deploying the Code to ESP32
-
Clone the repository (if you haven't already):
git clone https://github.com/yourusername/esp-sensors.git cd esp-sensors -
Create a deployment directory:
mkdir deploy -
Copy the necessary files:
cp -r src/esp_sensors deploy/ cp src/main.py deploy/main.pyNote: The main.py file is already configured with the correct imports for deployment.
-
Upload the files to ESP32:
# Connect to the ESP32 rshell -p /dev/ttyUSB0 # Inside rshell, create directory and copy files mkdir /pyboard/esp_sensors cp deploy/esp_sensors/__init__.py /pyboard/esp_sensors/ cp deploy/esp_sensors/sensor.py /pyboard/esp_sensors/ cp deploy/esp_sensors/temperature.py /pyboard/esp_sensors/ cp deploy/esp_sensors/humidity.py /pyboard/esp_sensors/ cp deploy/esp_sensors/dht22.py /pyboard/esp_sensors/ cp deploy/esp_sensors/oled_display.py /pyboard/esp_sensors/ # Upload the main script (will run automatically on boot) cp deploy/main.py /pyboard/ # Exit rshell exitAlternatively, you can use a single command to copy all files:
rshell -p /dev/ttyUSB0 "mkdir -p /pyboard/esp_sensors; cp -r deploy/esp_sensors/* /pyboard/esp_sensors/; cp deploy/main.py /pyboard/"
Running the Application
-
Reset your ESP32 by pressing the reset button or disconnecting and reconnecting power.
-
Monitor the output (optional):
rshell -p /dev/ttyUSB0 replThis will open the REPL (Read-Eval-Print Loop) interface where you can see the output. (Press Ctrl+X to exit the REPL)
Or use a serial monitor:
screen /dev/ttyUSB0 115200(Press Ctrl+A, then K to exit screen)
-
Interact with the device:
- The OLED display should show "Ready - Press Button"
- Press the button connected to GPIO 0
- The ESP32 will wake up, read temperature and humidity from the DHT22 sensor, and display the values on the OLED screen
- After 5 seconds, the display will clear and show "Ready - Press Button" again
- The ESP32 will enter light sleep mode to save power until the button is pressed again
Troubleshooting Common Issues
Display Not Working
- Check I2C connections (SCL and SDA pins)
- Verify the I2C address (default is 0x3C, but some displays use 0x3D)
- Try running an I2C scanner script to detect the display
DHT22 Sensor Not Reading
- Check the data pin connection
- Ensure the sensor is powered correctly (3.3V)
- Try adding a 10kΩ pull-up resistor between the data pin and VCC
- DHT22 sensors can be sensitive to timing; try reducing the reading frequency
ESP32 Not Entering Sleep Mode
- Ensure the button is connected to GPIO 0
- Check that the button is properly grounded
- Try using a different GPIO pin and update the code accordingly
Upload Errors
- Make sure the ESP32 is in the correct mode for uploading
- Try pressing the BOOT button while connecting power
- Check USB cable and port connections
- Verify you have the correct port in your commands
Advanced Configuration
Changing Pin Assignments
If you need to use different GPIO pins, modify the pin numbers in the main.py file:
# Initialize a DHT22 sensor
dht_sensor = DHT22Sensor(
name="Living Room",
pin=4, # Change this to your DHT22 data pin
interval=5,
unit="C"
)
# Initialize an OLED display
display = OLEDDisplay(
name="Status Display",
scl_pin=22, # Change this to your SCL pin
sda_pin=21, # Change this to your SDA pin
width=128,
height=64,
interval=1
)
# Set up button on GPIO pin 0
button_pin = 0 # Change this to your button pin
Power Optimization
For battery-powered applications, you can increase the sleep duration by modifying the code. Look for the time.sleep(5) line and adjust as needed.