Storing Sensor Data in MicroPython for ESP32 and ESP8266

In this project, we will learn how to use Storing Sensor Data in MicroPython for ESP32 and ESP8266 to store sensor data from a DHT11 sensor. You will read the temperature and humidity values from the sensor and store them in variables using basic data types such as integers and floats. This beginner-friendly project demonstrates how to handle data types in MicroPython, use sensor readings, and display the data. Additionally, this project lays the foundation for more advanced IoT projects.

Data Types and Variable Table for Storing Sensor Data in MicroPython:

Data Type Variable Name Description Example Value
Integer temperature Stores the temperature reading from the sensor 25°C
Float humidity_float Stores the humidity reading as a float 60.5%

In this project, temperature will be stored as an integer, while humidity will be converted to and stored as a float to allow for greater precision.

Syntax Table for Storing Sensor Data in MicroPython for ESP32 and ESP8266:

Function Syntax Example
Initialize DHT11 sensor = dht.DHT11(machine.Pin(pin)) sensor = dht.DHT11(machine.Pin(4))
Trigger measurement sensor.measure() sensor.measure()
Read temperature sensor.temperature() temperature = sensor.temperature()
Read humidity sensor.humidity() humidity = sensor.humidity()
Convert to float float(value) humidity_float = float(humidity)

Required Components:

  • ESP32 or ESP8266 microcontroller
  • DHT11 sensor (Temperature and Humidity Sensor)
  • Jumper wires
  • Breadboard

Circuit Diagram:

          DHT11          ESP32/ESP8266

           ——-         ————

           VCC  ———-> 3.3V

           GND  ———-> GND

           DATA ———-> GPIO4 (Pin 4)

 

Circuit Connection Table:

DHT11 Pin Connection ESP32/ESP8266 Pin Explanation
VCC Connect to 3.3V 3.3V on ESP32/ESP8266 Provides power to the DHT11 sensor.
GND Connect to GND GND on ESP32/ESP8266 Connects the sensor to ground (GND).
DATA Connect to GPIO4 GPIO4 on ESP32/ESP8266 Sends temperature and humidity data to the ESP32/ESP8266 via GPIO4.

Warnings:

  • Ensure that the DHT11 sensor is connected to 3.3V and not 5V, as the ESP32/ESP8266 operates at 3.3V. Connecting the sensor to a higher voltage may damage the microcontroller.
  • Double-check the wiring before powering the circuit to avoid short circuits or incorrect sensor readings.

Circuit Analysis:

The DHT11 sensor provides temperature and humidity readings through its DATA pin, which is connected to GPIO4 of the ESP32/ESP8266. The sensor requires VCC (3.3V) and GND to operate. The ESP32/ESP8266 reads the sensor data through GPIO4 and stores the values in variables, which can then be displayed or further processed.

Installing MicroPython and Libraries:

  1. Install MicroPython on ESP32/ESP8266:
    • To run MicroPython, you first need to install the firmware on your ESP32 or ESP8266. You can use esptool or Thonny to install MicroPython:

esptool.py –chip esp32 erase_flash

esptool.py –chip esp32 write_flash -z 0x1000 esp32-20210902-v1.17.bin

  1. Install Required Libraries:
    • Use the built-in dht and machine modules. Import them into your MicroPython code:

import dht

import machine

Writing the MicroPython Code for Storing Sensor Data:

Here’s the code that reads the temperature and humidity data from the DHT11 sensor and stores it in variables:

import dht

import machine

import time

 

# Initialize the DHT11 sensor on GPIO4

sensor = dht.DHT11(machine.Pin(4))

 

while True:

    try:

        # Trigger measurement

        sensor.measure()

        

        # Store temperature as an integer

        temperature = sensor.temperature()

        

        # Store humidity, converting it to a float

        humidity = sensor.humidity()

        humidity_float = float(humidity)

        

        # Print the sensor data

        print(“Temperature:”, temperature, “°C”)

        print(“Humidity:”, humidity_float, “%”)

        

        # Delay for 2 seconds before next reading

        time.sleep(2)

    

    except OSError as e:

        print(“Failed to read from DHT sensor:”, e)

 

Running the Code and Checking the Output:

  1. Upload the code to your ESP32/ESP8266 using Thonny or another MicroPython IDE.

Open the serial monitor to see the sensor readings. The temperature and humidity values will be printed every 2 seconds:
Temperature: 25 °C

Humidity: 60.0 %

Expanding the Project:

  • Add a Display: You can add an OLED or LCD screen to display the temperature and humidity readings.
  • Data Logging: Store the sensor data in a text file on the ESP32/ESP8266 filesystem for later analysis.
  • Wi-Fi Integration: Expand the project to send temperature and humidity data to an online server via HTTP for IoT applications.

Common Problems and Solutions:

  1. Problem: The sensor readings are not appearing on the serial monitor.
    • Solution: Check the wiring, especially the connection between the DATA pin and the GPIO pin. Ensure you are using the correct GPIO pin in the code.
  2. Problem: OSError occurs when reading the sensor.
    • Solution: Ensure that the sensor is connected correctly and receiving power. You might also need to ensure the sensor is functioning properly.
  3. Problem: Incorrect or fluctuating sensor readings.
    • Solution: Add a delay between readings to give the sensor enough time to stabilize. DHT11 needs at least 2 seconds between readings.

FAQ:

Q: Can I use a different GPIO pin for the DHT11 sensor?
A: Yes, you can change the pin by connecting the DATA pin of the sensor to another available GPIO pin. Be sure to update the pin number in the code.

Q: How often can I take sensor readings?
A: The DHT11 sensor has a delay of 1-2 seconds between measurements to ensure accurate readings.

Q: How can I convert the temperature from Celsius to Fahrenheit?
A: You can convert the Celsius reading to Fahrenheit using this formula:

fahrenheit = (temperature * 9/5) + 32

Conclusion:

In this project, you successfully learned how to store sensor data from a DHT11 sensor using MicroPython on ESP32 and ESP8266. By reading temperature and humidity data, you explored the usage of integers and floats in MicroPython. This foundational project can be further expanded with displays, data logging, or by sending the data to the cloud for IoT applications. Understanding how to handle sensor data is a critical step in building more advanced projects with MicroPython and ESP32/ESP8266.