This project demonstrates how to use an ESP32 microcontroller with a DHT22 temperature and humidity sensor to publish sensor data to an MQTT broker. The data can then be used for various applications such as home automation, environmental monitoring, etc.
- ESP32 microcontroller
- DHT22 temperature and humidity sensor
- LED (optional, used for indication)
- WiFi connection for ESP32
- Arduino IDE
- Libraries:
- DHT Sensor Library - for interfacing with the DHT22 sensor
- WiFi - for connecting to the WiFi network
- PubSubClient - for MQTT communication
- Clone or download this repository.
- Open the project in the Arduino IDE.
- Install the required libraries through the Arduino Library Manager if not already installed.
- Update the following parameters in the code:
- WiFi SSID and password
- MQTT broker details (server, port, username, password, topic)
- Connect the DHT22 sensor and LED to the ESP32 as per the pin configurations in the code.
- Compile and upload the code to your ESP32 board.
mqtt:
sensor:
- name: "TemperaturaEsterna"
unique_id: "TemperaturaEsterna"
state_topic: "home/external/sensor"
unit_of_measurement: "°C"
value_template: "{{ value_json.temperature }}"
- name: "UmiditaEsterna"
unique_id: "UmiditaEsterna"
state_topic: "home/external/sensor"
unit_of_measurement: "%"
value_template: "{{ value_json.humidity }}"
- Once uploaded, the ESP32 will connect to the specified WiFi network.
- It will then start publishing temperature and humidity data to the specified MQTT topic.
- You can monitor the data using an MQTT client or integrate it with other applications like Home Assistant.
- The code includes a workaround for negative temperature values which may vary from standard DHT22 sensor behavior. Further investigation or alternative libraries may be required for precise handling.
Contributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request.
This project is licensed under the MIT License - see the LICENSE file for details.
