Skip to content

This project uses the .NET nanoFramework, a specialized version of .NET designed for microcontrollers. It integrates DHT21, AHT10, or SHTC3 sensors (selectable at startup) for temperature and humidity monitoring. The project features an MQTT client for real-time data communication, a built-in WebServer with RESTful APIs for remote data access.

License

Notifications You must be signed in to change notification settings

unrealbg/ESP32-MQTT-EnvMonitor

Repository files navigation

ESP32-MQTT Environmental Monitor

This project integrates an ESP32 device with temperature and humidity sensors (DHT21, AHT10, SHTC3) using C# and the nanoFramework for development. It features MQTT client capabilities for real-time remote data communication and a WebServer component that provides RESTful API endpoints for sensor data access and device control via HTTP requests. The system is ideal for IoT applications requiring flexible communication and interaction interfaces.

Note: Only one sensor is active at a time. The sensor (DHT21, AHT10, or SHTC3) must be selected during the initial setup, and they do not work simultaneously.

Visit our portal at: portal.unrealbg.com
Username: demo@unrealbg.com
Password: Demo123@

I acknowledge that the code may not be perfect and there are certainly areas that can be improved. I am a self-taught programmer, and coding is my hobby. I welcome any constructive criticism and feedback.

Requirements

  • ESP32 development board
  • DHT21, AHT10, or SHTC3 temperature and humidity sensor
  • MQTT broker (e.g., Mosquitto)

Setup

  1. NanoFramework Installation:
    Install the C# nanoFramework on your ESP32 device. Detailed instructions are available here.

  2. Sensor Connection:
    Connect the DHT21, AHT10, or SHTC3 sensor to your ESP32 device as per the sensor's documentation. Select the sensor in the code during setup.

  3. MQTT Broker Setup:
    Set up an MQTT broker (like Mosquitto) on your network. Note down the hostname and port number.

  4. Code Configuration:
    In the code, configure the necessary settings in the DeviceSettings, MqttSettings, and WiFiSettings classes:

    • DeviceSettings: Set values for DeviceName, Location, and SensorType.
    • MqttSettings: Provide the broker address, username, and password for your MQTT broker.
    • Wi-Fi Settings: Enter the ssid and password for your Wi-Fi network.

    These settings are defined in dedicated classes and should be modified according to your environment and setup requirements.

  5. Code Deployment:
    Compile and upload the code to your ESP32 device.

Usage

Once the setup is complete, the ESP32 device will start publishing temperature and humidity data from the selected sensor (DHT21, AHT10, or SHTC3) to an MQTT topic. Subscribe to this topic to receive real-time updates. The system also supports publishing custom messages to the MQTT topic.

Troubleshooting

  • MQTT Connection Issues:
    Ensure the MQTT broker hostname and port number are correctly set in the code.

  • Sensor Data Accuracy:
    If temperature and humidity readings are inaccurate, verify the sensor connections and calibrations.

WebServer with API Endpoints

This project includes a web server that serves API endpoints for structured HTTP request handling. It allows for remote sensor data retrieval and device control via a RESTful interface.

Note: For optimal web server performance, it is recommended to use ESP devices with more memory, such as ESP32-S3. The project has been tested and works without issues on ESP32-S3, thanks to its increased storage and processing resources.

WebServer Features

  • API Endpoints: API endpoints for interacting with the DHT21, AHT10, and SHTC3 sensors and device control.
  • Real-Time Data Access: Fetch real-time temperature and humidity data.
  • Device Control: Endpoints for controlling the ESP32 device functionalities.

API Endpoints

  1. Temperature Data:

    • Endpoint: /api/temperature
    • Method: GET
    • Description: Returns the current temperature reading from the selected sensor in JSON format.
  2. Humidity Data:

    • Endpoint: /api/humidity
    • Method: GET
    • Description: Returns the current humidity reading in JSON format.
  3. Complete Sensor Data:

    • Endpoint: /api/data
    • Method: GET
    • Description: Returns both temperature and humidity readings along with the sensor type in a structured JSON format.

Index Page

An index page has been added for a user-friendly display of sensor data through a simple web interface. It dynamically updates the temperature, humidity, date, time, and sensor type using JavaScript.

Index Page Content

The index page provides a clean and responsive UI for real-time monitoring:

  • Displays temperature and humidity readings.
  • Shows the current date, time, and sensor type.

New Feature: Relay Control with Toggle Button

The project now includes a relay control feature accessible via the web interface. A button has been added to the index page to allow users to toggle the relay state between ON and OFF. The button’s label and color dynamically update based on the current relay state.

  • "Turn On": Green button appears when the relay is off.
  • "Turn Off": Red button appears when the relay is on.

Relay Control API

You can control the relay using the following API endpoint:

  1. Toggle Relay

    • Endpoint: /api/toggle-relay
    • Method: POST
    • Description: Toggles the relay state between ON and OFF. The response returns the updated relay state (isRelayOn).
  2. Check Relay Status

    • Endpoint: /api/relay-status
    • Method: GET
    • Description: Returns the current state of the relay (ON or OFF) in JSON format.

Updated Index Page

The web interface has been updated with a new Relay Control button. This button will dynamically update based on the state of the relay:

  • Turn On: Green button displayed when the relay is off.
  • Turn Off: Red button displayed when the relay is on.

Example HTML:

<button id="relayButton" class="btn-on" onclick="toggleRelay()">Loading...</button>
async function toggleRelay() {
    const response = await fetch('/api/toggle-relay', { method: 'POST' });
    const data = await response.json();
    updateRelayButton(data.isRelayOn);
}

function updateRelayButton(isRelayOn) {
    const relayButton = document.getElementById('relayButton');
    if (isRelayOn) {
        relayButton.textContent = 'Turn Off';
        relayButton.className = 'btn-off';
    } else {
        relayButton.textContent = 'Turn On';
        relayButton.className = 'btn-on';
    }
}

Additional Functionalities

Relay Control

You can control a relay module to manage connected devices based on sensor data or MQTT commands. This enables automated environmental control or remote power management.

SHTC3 Sensor Support

In addition to the DHT21 and AHT10 sensors, the project now supports the SHTC3 sensor, offering flexibility in sensor choice based on precision and calibration needs.

Project Images

Without Breadboard Power Supply

Without Breadboard Power Supply

With Breadboard Power Supply

With Breadboard Power Supply

Additional Project Image

About

This project uses the .NET nanoFramework, a specialized version of .NET designed for microcontrollers. It integrates DHT21, AHT10, or SHTC3 sensors (selectable at startup) for temperature and humidity monitoring. The project features an MQTT client for real-time data communication, a built-in WebServer with RESTful APIs for remote data access.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages