{"id":26458676,"url":"https://github.com/unrealbg/esp32-mqtt-envmonitor","last_synced_at":"2025-03-19T00:26:33.524Z","repository":{"id":63150659,"uuid":"563410003","full_name":"unrealbg/ESP32-MQTT-EnvMonitor","owner":"unrealbg","description":"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.","archived":false,"fork":false,"pushed_at":"2025-03-15T16:16:23.000Z","size":405,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-18T04:46:20.027Z","etag":null,"topics":["aht","aht10","dht","esp32","mqtt","nanoframework","rest-api","shtc3","webserver"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/unrealbg.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-11-08T14:56:18.000Z","updated_at":"2025-03-15T16:16:27.000Z","dependencies_parsed_at":"2024-02-23T20:27:00.358Z","dependency_job_id":"fd775478-b087-41a8-aec0-e3657f54f5cd","html_url":"https://github.com/unrealbg/ESP32-MQTT-EnvMonitor","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/unrealbg%2FESP32-MQTT-EnvMonitor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/unrealbg%2FESP32-MQTT-EnvMonitor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/unrealbg%2FESP32-MQTT-EnvMonitor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/unrealbg%2FESP32-MQTT-EnvMonitor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/unrealbg","download_url":"https://codeload.github.com/unrealbg/ESP32-MQTT-EnvMonitor/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244328096,"owners_count":20435533,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aht","aht10","dht","esp32","mqtt","nanoframework","rest-api","shtc3","webserver"],"created_at":"2025-03-19T00:26:32.909Z","updated_at":"2025-03-19T00:26:33.518Z","avatar_url":"https://github.com/unrealbg.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ESP32-MQTT Environmental Monitor\n\n## Table of Contents\n- [Introduction](#introduction)\n- [Requirements](#requirements)\n- [Setup](#setup)\n- [Usage](#usage)\n- [Troubleshooting](#troubleshooting)\n- [WebServer with API Endpoints](#webserver-with-api-endpoints)\n- [Index Page](#index-page)\n- [Relay Control](#relay-control)\n- [Additional Functionalities](#additional-functionalities)\n- [Project Images](#project-images)\n- [Contributing](#contributing)\n- [Acknowledgements](#acknowledgements)\n- [Changelog](#changelog)\n- [License](#license)\n\n## Introduction\n\nThis 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.\n\nNote: 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.\n\nVisit our demo portal at: [iot.unrealbg.com](https://iot.unrealbg.com)\n\nI 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.\n\n## Requirements\n\n- ESP32 development board\n- DHT21, AHT10, or SHTC3 temperature and humidity sensor\n- MQTT broker (e.g., Mosquitto)\n\n### nanoFramework Firmware Version\n\n- The required nanoFramework firmware version depends on the NuGet packages used in this project.\n- To ensure compatibility, use the firmware version that matches the latest tested state of this repository.\n- The last tested firmware version for this project was **1.12.4.14**.\n- You can check your current firmware version using the **Device Explorer** in the nanoFramework extension for Visual Studio or by running:\n\n  ```sh\n  nanoff --platform esp32 --target \u003cYOUR_ESP32_TARGET\u003e --serialport \u003cYOUR_COM_PORT\u003e --list\n\n- If needed, update your firmware to the version used in this project:\n  \n  ```sh\n  nanoff --platform esp32 --target \u003cYOUR_ESP32_TARGET\u003e --serialport \u003cYOUR_COM_PORT\u003e --masserase --update --fwversion 1.12.4.14\n\n- Important: Replace \u003cYOUR_ESP32_TARGET\u003e with the correct target for your device (e.g., ESP32_WROOM_32, ESP32_S3, ESP32_C3). You can find the available targets by running:\n\n  ```sh\n  nanoff --platform esp32 --listtargets\n\n- Also, replace \u003cYOUR_COM_PORT\u003e with the actual serial port where your ESP32 device is connected (e.g., COM31 on Windows or /dev/ttyUSB0 on Linux/macOS).\n- If you decide to use a **newer firmware version**, you **must also update the corresponding NuGet packages** in your project to ensure compatibility. Check for updates in Visual Studio's **NuGet Package Manager** and make sure all dependencies align with the firmware version you are using.\n- For detailed firmware update instructions, visit the [nanoFramework documentation](https://docs.nanoframework.net/content/getting-started-guides/getting-started-managed.html).\n\n\n## Tested Hardware\n\nThis project has been tested and confirmed to work with the following hardware:\n\n### ✅ Supported ESP32 Boards\n- ESP32-S3  \n- ESP32 Wroom32 DevKit\n- ESP32-WROOM-32 DevKit (38 Pins)\n- ESP-WROOM-32 ESP-32S\n- Wemos/Lolin D32 ESP32\n\n### ❌ Not Working With:\n- ESP32-C3 (known compatibility issues)\n\n### ✅ Supported Sensors:\n- AHT10  \n- AHT20 + BMP280  \n- SHTC3  \n- DHT21  \n\nIf you test the project with other hardware, feel free to contribute feedback!\n\n## Setup\n\n1. **NanoFramework Installation:**  \n   Install the C# nanoFramework on your ESP32 device. Detailed instructions are available [here](https://docs.nanoframework.net/content/getting-started-guides/getting-started-managed.html).\n\n2. **Sensor Connection:**  \n   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.\n\n3. **MQTT Broker Setup:**  \n   Set up an MQTT broker (like Mosquitto) on your network. Note down the hostname and port number.\n\n4. **Code Configuration:**  \n   In the code, configure the necessary settings in the `DeviceSettings`, `MqttSettings`, and `WiFiSettings` classes:\n   - ***DeviceSettings***: Set values for `DeviceName`, `Location`, and `SensorType`.\n   - ***MqttSettings***: Provide the `broker address`, `username`, and `password` for your MQTT broker.\n   - ***Wi-Fi Settings***: Enter the `ssid` and `password` for your Wi-Fi network.\n     \n   These settings are defined in dedicated classes and should be modified according to your environment and setup requirements.\n\n5. **Code Deployment:**  \n   Compile and upload the code to your ESP32 device.\n\n## Usage\n\nOnce 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.\n\n## Troubleshooting\n\n- **Bootloop Prevention:**   \n   If the device repeatedly enters a bootloop, ensure that the correct GPIO pins are configured for your sensor(s) and relay. Verify the pin assignments in the code (e.g., in `BaseSensorService`, `AhtSensorService`, and `RelayService`) match your hardware setup. Incorrect pin assignments can cause improper initialization, leading to boot issues.\n\n- **MQTT Connection Issues:**  \n  Ensure the MQTT broker hostname and port number are correctly set in the code.\n\n- **Sensor Data Accuracy:**  \n  If temperature and humidity readings are inaccurate, verify the sensor connections and calibrations.\n\n## WebServer with API Endpoints\n\nThis 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.\n\n**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.\n\n### WebServer Features\n\n- **API Endpoints**: API endpoints for interacting with the DHT21, AHT10, and SHTC3 sensors and device control.\n- **Real-Time Data Access**: Fetch real-time temperature and humidity data.\n- **Device Control**: Endpoints for controlling the ESP32 device functionalities.\n\n### API Endpoints\n\n1. **Temperature Data**:  \n   - Endpoint: `/api/temperature`  \n   - Method: `GET`  \n   - Description: Returns the current temperature reading from the selected sensor in JSON format.\n\n2. **Humidity Data**:  \n   - Endpoint: `/api/humidity`  \n   - Method: `GET`  \n   - Description: Returns the current humidity reading in JSON format.\n\n3. **Complete Sensor Data**:  \n   - Endpoint: `/api/data`  \n   - Method: `GET`  \n   - Description: Returns both temperature and humidity readings along with the sensor type in a structured JSON format.\n\n## Index Page\n\nAn 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.\n\n### Index Page Content\nThe index page provides a clean and responsive UI for real-time monitoring:\n- Displays temperature and humidity readings.\n- Shows the current date, time, and sensor type.\n\n## New Feature: Relay Control with Toggle Button\n\nThe 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.\n\n- **\"Turn On\"**: Green button appears when the relay is off.\n- **\"Turn Off\"**: Red button appears when the relay is on.\n\n### Relay Control API\n\nYou can control the relay using the following API endpoint:\n\n1. **Toggle Relay**  \n   - Endpoint: `/api/toggle-relay`  \n   - Method: `POST`  \n   - Description: Toggles the relay state between ON and OFF. The response returns the updated relay state (`isRelayOn`).\n\n2. **Check Relay Status**  \n   - Endpoint: `/api/relay-status`  \n   - Method: `GET`  \n   - Description: Returns the current state of the relay (ON or OFF) in JSON format.\n\n### Updated Index Page\n\nThe web interface has been updated with a new **Relay Control** button. This button will dynamically update based on the state of the relay:\n\n- **Turn On**: Green button displayed when the relay is off.\n- **Turn Off**: Red button displayed when the relay is on.\n\n### Example HTML:\n```html\n\u003cbutton id=\"relayButton\" class=\"btn-on\" onclick=\"toggleRelay()\"\u003eLoading...\u003c/button\u003e\nasync function toggleRelay() {\n    const response = await fetch('/api/toggle-relay', { method: 'POST' });\n    const data = await response.json();\n    updateRelayButton(data.isRelayOn);\n}\n\nfunction updateRelayButton(isRelayOn) {\n    const relayButton = document.getElementById('relayButton');\n    if (isRelayOn) {\n        relayButton.textContent = 'Turn Off';\n        relayButton.className = 'btn-off';\n    } else {\n        relayButton.textContent = 'Turn On';\n        relayButton.className = 'btn-on';\n    }\n}\n```\n\n## Additional Functionalities\n\n### Relay Control\nYou 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.\n\n### SHTC3 Sensor Support\nIn 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.\n\n## Project Images\n\n### Without Breadboard Power Supply\n![Without Breadboard Power Supply](https://user-images.githubusercontent.com/3398536/201364419-9ba27b3e-6638-490f-90f5-0e380fbc2900.png)\n\n### With Breadboard Power Supply\n![With Breadboard Power Supply](https://user-images.githubusercontent.com/3398536/201362770-067d8fe3-254e-48e2-8cec-10766898c3e6.png)\n\n### Additional Project Image\n\u003cimg src=\"https://user-images.githubusercontent.com/3398536/200621001-ac09d95d-9f0f-4ef7-bf87-8b352f5f1a17.jpg\" width=\"200\" /\u003e \u003cimg src=\"https://github.com/unrealbg/NF.Esp32.Mqtt.Dht21/assets/3398536/4cde056b-9c80-467d-a05f-481e5dae26ea\" width=\"200\" /\u003e \u003cimg src=\"https://github.com/unrealbg/NF.Esp32.Mqtt.Dht21/assets/3398536/1e2c80ee-9d03-45d0-8034-ce2004eb4cf1\" width=\"200\" /\u003e\n\n## Contributing\n\nContributions are welcome! Please follow these steps to contribute:\n\n1. Fork the repository.\n2. Create a new branch (`git checkout -b feature-branch`).\n3. Make your changes.\n4. Commit your changes (`git commit -m 'Add new feature'`).\n5. Push to the branch (`git push origin feature-branch`).\n6. Open a Pull Request.\n\n## Acknowledgements\n\n- [nanoFramework](https://www.nanoframework.net/)\n- [Mosquitto MQTT](https://mosquitto.org/)\n- Special thanks to everyone who contributed to this project.\n\n## Changelog\n\n### [v1.1.0] - 2025-03-02\n- **Sensor Services:**  \n  - Introduced `BaseSensorService` to centralize sensor reading, error handling, and resource management.\n  - Updated AHT10, AHT20+BMP280, DHT21, and SHTC3 sensor services to inherit from `BaseSensorService`.\n- **MQTT Client:**  \n  - Improved connection stability with dynamic ClientId generation, locking to prevent duplicate connection attempts, and exponential backoff with jitter.\n- **Network \u0026 Internet:**  \n  - Refactored `ConnectionService` with proper locking and enhanced IP validation.\n  - Improved `InternetConnectionService` with resource cleanup and stop-signal checks.\n- **Web Server \u0026 Relay:**  \n  - Enhanced `WebServerService` and `RelayService` with thread-safe operations and proper resource disposal.\n\n### [v1.0.2] - 2025-01-15\n- Added support for the AHT20+BMP280 sensor.\n\n### [v1.0.1] - 2024-12-13\n- Added support for the SHTC3 sensor.\n- Improved MQTT connection stability.\n\n### [v1.0.0] - 2024-11-01\n- Initial release with support for DHT21 and AHT10 sensors.\n\n## License\n\nThis project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Funrealbg%2Fesp32-mqtt-envmonitor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Funrealbg%2Fesp32-mqtt-envmonitor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Funrealbg%2Fesp32-mqtt-envmonitor/lists"}