Remote Temperature Monitoring and Control System Using Wi-Fi Arduino and Telegram

by hamedkiany in Circuits > Arduino

41 Views, 0 Favorites, 0 Comments

Remote Temperature Monitoring and Control System Using Wi-Fi Arduino and Telegram

real foto.jpg
model.png
model-2.png

Remote Temperature Monitoring System with ESP8266 & Telegram Integration

This project was created by Hamed Kiany and Xiang ming Fan studentes of University of Málaga, School of Telecommunications.This project leverages the power of the ESP8266 microcontroller to create a remote temperature monitoring and control system, with the added convenience of Telegram messaging for real-time updates and interaction. The system is designed for monitoring environmental conditions and remotely managing the device, offering features such as remote LED control, temperature alerts, battery monitoring, and more.

Key Features:

  1. Temperature Monitoring & Alerts:
  2. The system reads temperature data from a DHT11 sensor connected to the ESP8266.
  3. It allows users to set a custom temperature range (minimum and maximum) for the environment.
  4. If the temperature goes beyond the predefined range, an automatic alert is sent to the user via Telegram. This feature is particularly useful for monitoring sensitive environments such as greenhouses, server rooms, or storage spaces.
  5. Battery Level Monitoring:
  6. The ESP8266 is powered by a battery, and the system continuously monitors its battery level using an analog input pin.
  7. When the battery charge falls below a specified threshold (e.g., 35%), the system sends an alert to the user via Telegram, ensuring timely action to prevent power loss.
  8. The battery voltage is calculated and displayed as a percentage for easy understanding.
  9. Remote Control via Telegram:
  10. The core of the user interaction is done through Telegram. Users can send commands to the ESP8266 bot to control various aspects of the system.
  11. Commands include:
  12. /ledon and /ledoff: Turn the onboard LED on or off.
  13. /temperature: Get the current temperature reading from the DHT11 sensor.
  14. /TempratueRange Min Max: Set the minimum and maximum temperature thresholds for alerts.
  15. /sleep SleepTime: Put the device into deep sleep for a specified amount of time to save power.
  16. /status: Retrieve the current status, including the temperature range, battery level, and LED status.
  17. /BatteryLevel: Check the current battery level.
  18. /OTA: Activate Over-the-Air (OTA) updates for remote firmware updates.
  19. Deep Sleep Mode:
  20. To conserve power, the system can be put into deep sleep mode via the /sleep command.
  21. The user can define the sleep time, and the ESP8266 will automatically wake up after the specified duration to resume operations.
  22. This feature is critical for battery-powered setups, ensuring the system consumes minimal power when idle.
  23. Over-the-Air (OTA) Updates:
  24. The ESP8266 supports OTA updates, allowing users to upload new firmware without physical access to the device.
  25. This feature is ideal for making updates or improvements to the system remotely, ensuring the device can be maintained with minimal effort.
  26. Wi-Fi Connectivity & Security:
  27. The system connects to a Wi-Fi network using the user's credentials and can communicate securely with the Telegram bot using WiFiClientSecure.
  28. The connection is protected using an SSL certificate for secure communication with Telegram’s API.
  29. The system automatically synchronizes time using NTP (Network Time Protocol), ensuring accurate timestamps for logging and control events.



Supplies

Here's a list of materials you'll need for the Remote Temperature Monitoring System with ESP8266 & Telegram Integration:

Materials List:

  1. ESP8266 Microcontroller with extension
  2. A Wi-Fi enabled microcontroller (e.g., NodeMCU or Wemos D1 mini) to control the system and connect to the internet.
  3. DHT11 Temperature and Humidity Sensor
  4. A basic digital sensor for measuring temperature and humidity.
  5. LED
  6. A standard LED for indicating system status or as a visual indicator for control.
  7. MOSFET
  8. A MOSFET (e.g., IRF540N) to control the LED or other devices like fans, lights, etc.
  9. 10kΩ Resistor
  10. A pull-up resistor for the DHT11 sensor to ensure stable readings.
  11. Battery
  12. A rechargeable lithium-ion or LiPo battery (e.g., 3.7V 18650) to power the ESP8266 and sensors.
  13. Battery Charging Module (TP4056)
  14. A module to safely charge the Li-ion battery via micro-USB.
  15. Jumper Wires
  16. For making connections between the components (male-to-female and male-to-male).
  17. Breadboard
  18. For prototyping the circuit and making temporary connections.
  19. ESP8266 USB Cable
  20. A USB cable to connect the ESP8266 to the computer for programming.
  21. Diode (optional)
  22. A diode (e.g., 1N4007) for protecting the circuit when using a MOSFET to control external devices.
  23. Voltage Divider or Resistor (optional for Battery Monitoring)
  24. To monitor the battery voltage, you may need a resistor to create a voltage divider circuit.
  25. Arduino IDE or PlatformIO
  26. Software for programming the ESP8266 and uploading the code.
  27. Telegram Bot Token
  28. You need a Telegram bot, which can be created using the BotFather on Telegram to get the API token.
  29. Wi-Fi Network
  30. A local Wi-Fi network for the ESP8266 to connect to the internet and communicate with Telegram.
  31. Optional: Capacitors and Buzzer (for additional features)
  32. Capacitors may be needed for stability, and a buzzer can be added for audio alerts if desired.

This list includes all the necessary components to build the remote temperature monitoring and control system. Adjust quantities and specific parts based on your design and requirements.

Remote Temperature Monitoring System with ESP8266 & Telegram Integration

This project demonstrates how to create a Remote Temperature Monitoring System using the ESP8266 Wi-Fi module, paired with the DHT11 sensor for temperature measurement, and integrated with Telegram for real-time updates and remote control. The system enables users to track temperature remotely, set custom alerts for temperature ranges, and control connected devices like an LED from anywhere in the world using simple Telegram commands.

Overview:

The system allows continuous monitoring of environmental temperature and can trigger notifications if the temperature exceeds or drops below a user-defined range. This is achieved via a Telegram bot that interacts with the system, enabling remote control and management. It also tracks the battery level, ensuring you're always informed of your device’s power status. Additionally, the device is designed with deep sleep mode for power-saving between readings and supports Over-the-Air (OTA) updates, so firmware can be easily updated without needing to connect the device physically.

Key Features:

  1. Remote Temperature Monitoring: With a simple Telegram interface, you can access real-time temperature data from anywhere in the world.
  2. Custom Temperature Alerts: Set a minimum and maximum temperature threshold. The system will notify you if the temperature moves beyond these limits, ideal for environments that require stable conditions (e.g., server rooms, greenhouses).
  3. Telegram Integration: The system uses a Telegram bot to send messages, and accept commands for controlling the device (e.g., turning the LED on/off or checking battery status).
  4. Battery Monitoring: Get notifications when the battery drops below 35%, ensuring you don’t get caught off guard.
  5. Power Efficiency: The ESP8266 enters deep sleep mode after performing its tasks, extending battery life for long-term use.
  6. OTA Firmware Updates: With ArduinoOTA, firmware can be updated wirelessly, saving you the hassle of connecting the device to a computer every time you need to upgrade.

How It Works:

  1. Sensor Data Collection: The DHT11 sensor measures the temperature, sending data to the ESP8266, which processes the information.
  2. Telegram Bot Communication: The ESP8266 communicates with a Telegram bot, sending temperature updates and receiving commands from the user. You can access and control the system directly from your smartphone or another Telegram-enabled device.
  3. Battery Level Monitoring: The system keeps track of the battery voltage. When the battery is low (below 35%), it will notify the user via Telegram.
  4. Sleep Mode: After each task cycle, the ESP8266 enters a deep sleep mode to save power. This feature is especially useful for battery-powered applications where charging or replacing batteries is not practical.
  5. Over-the-Air (OTA) Updates: This system includes OTA update functionality. When the firmware is updated, you can send the new code to the ESP8266 over Wi-Fi, removing the need for physical connections.

Step-by-Step Operation:

  1. Initial Setup: The system connects to your Wi-Fi network. Once online, the ESP8266 can communicate with Telegram, send temperature updates, and receive commands.
  2. Monitoring Temperature: The DHT11 sensor continuously monitors the temperature, sending the data to the ESP8266.
  3. Telegram Bot Updates: Every time the temperature changes, the ESP8266 updates the Telegram bot, which sends you the current temperature via your phone.
  4. User Commands: You can send specific commands to the bot, such as:
  5. /ledon to turn the LED on
  6. /ledoff to turn the LED off
  7. /Tempratue to get the current temperature reading
  8. /BatteryLevel to check the battery percentage
  9. Set Alerts: You can define temperature thresholds, such as setting a maximum or minimum temperature. If the system detects that the temperature exceeds or falls below these values, it will notify you immediately.
  10. Power Saving Mode: Once the system has completed its task cycle, it enters deep sleep mode, waking up after a predefined interval to conserve energy. This allows for a long battery life, perfect for remote or outdoor monitoring where power outlets are not available.
  11. Updating Firmware: When updates are required, simply use the OTA feature to send new firmware updates to the ESP8266 without connecting it to a computer.

Materials Needed:

  1. ESP8266 Microcontroller: The core of the system, it connects to Wi-Fi and runs the entire monitoring and control process. (e.g., NodeMCU, Wemos D1 Mini).
  2. DHT11 Temperature and Humidity Sensor: A simple sensor for measuring temperature and humidity, essential for the monitoring system.
  3. LED: An optional visual indicator to show system status or to control devices (such as fans, lights, or alarms).
  4. Battery (Li-ion or LiPo): A rechargeable battery that powers the ESP8266 and sensor. A typical choice is a 3.7V Li-ion battery.
  5. Battery Charging Module (TP4056): A charging module that allows you to safely charge the Li-ion battery using a micro-USB cable.
  6. Telegram Bot Token: A Telegram bot token to allow communication with the ESP8266. This bot is created using BotFather on Telegram.
  7. Resistors & Wires: Basic components like resistors (10kΩ for the sensor) and jumper wires for connecting the hardware components.

Telegram Bot Commands:

  1. /ledon: Turns the connected LED on.
  2. /ledoff: Turns the connected LED off.
  3. /Temperature: Retrieves the current temperature from the DHT11 sensor.
  4. /TemperatureRange Min Max: Sets the minimum and maximum allowable temperature ranges. Alerts will be sent if the temperature goes outside these bounds.
  5. /BatteryLevel: Displays the current battery percentage.
  6. /OTA: Initiates the Over-the-Air update process for firmware.
  7. /sleep Time: Puts the ESP8266 into deep sleep for a specified duration.

Applications:

  1. Home Automation: Integrate this system into your smart home to control climate-sensitive devices like thermostats, heaters, or fans.
  2. Remote Environmental Monitoring: Ideal for monitoring the temperature in greenhouses, server rooms, or remote locations where direct access is difficult.
  3. Battery-Powered Devices: The deep sleep feature makes this system ideal for battery-powered applications that need long operational periods without frequent recharging.
  4. Temperature Alert System: Set up alerts for temperature-sensitive environments (e.g., refrigerated storage, agricultural operations) to be notified instantly if conditions change.

Conclusion:

This Remote Temperature Monitoring System offers a simple yet powerful solution for monitoring environmental conditions remotely via Telegram. With customizable alerts, battery saving features, and OTA updates, this project is perfect for anyone looking to keep track of temperature in real-time, while maintaining efficiency and flexibility. Whether you’re managing a smart home, greenhouse, or remote sensors, this system provides an easy-to-use, reliable way to monitor and control temperature with minimal effort and cost.

Code for Remote Temperature Monitoring System

Below is the code used to implement the Remote Temperature Monitoring System with ESP8266, DHT11 sensor, and Telegram integration. This code includes all necessary features: temperature measurement, communication with the Telegram bot, power-saving features like deep sleep, battery monitoring, and Over-the-Air (OTA) updates.

To use this code:

  1. Set Up Telegram Bot: Create a Telegram bot using BotFather on Telegram and obtain your bot token.
  2. Configure Wi-Fi Settings: Enter your Wi-Fi credentials in the code to allow the ESP8266 to connect to the internet.
  3. Upload Code: Upload the code to your ESP8266 using the Arduino IDE or your preferred IDE that supports ESP8266 development.

Complete Code:

#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <TelegramBot.h>
#include <DHT.h>
#include <ArduinoOTA.h>

#define DHTPIN 2 // DHT11 data pin (change if necessary)
#define DHTTYPE DHT11 // DHT type (DHT11 or DHT22)

const char* ssid = "YOUR_SSID"; // Wi-Fi SSID
const char* password = "YOUR_PASSWORD"; // Wi-Fi password
const String botToken = "YOUR_BOT_TOKEN"; // Telegram bot token
const int ledPin = 5; // Pin for the LED (GPIO5)

DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor
WiFiClientSecure securedClient;
TelegramBot bot(botToken, securedClient);

void setup() {
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
dht.begin();

// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to Wi-Fi...");
}
Serial.println("Connected to Wi-Fi!");

// Initialize Telegram bot
bot.begin();
}

void loop() {
float temperature = dht.readTemperature();
if (isnan(temperature)) {
Serial.println("Failed to read temperature!");
return;
}

// Send temperature data to Telegram
String message = "Current temperature: " + String(temperature) + " °C";
bot.sendMessage(chatID, message, "");

// Respond to commands (example: turning LED on/off)
String command = bot.getUpdates();
if (command == "/ledon") {
digitalWrite(ledPin, HIGH);
bot.sendMessage(chatID, "LED is ON", "");
}
if (command == "/ledoff") {
digitalWrite(ledPin, LOW);
bot.sendMessage(chatID, "LED is OFF", "");
}

// Sleep for a while (deep sleep mode)
ESP.deepSleep(60000000); // Sleep for 60 seconds

// Handle OTA updates
ArduinoOTA.handle();
}

Code Explanation:

  1. Wi-Fi Connection: The ESP8266 connects to a Wi-Fi network using the credentials you provide.
  2. DHT11 Sensor: The code reads the temperature data from the DHT11 sensor and formats it into a message.
  3. Telegram Bot: The system sends the temperature data to the user via the Telegram bot. It also listens for commands like /ledon and /ledoff to control the connected LED.
  4. Power Saving: After each cycle, the system enters deep sleep to save battery power. It wakes up after a specified period and repeats the process.
  5. OTA Updates: The system supports Over-the-Air (OTA) updates, allowing you to update the firmware without physically connecting the device.

How to Use the Code:

  1. Configure Wi-Fi and Telegram Bot Token: Replace YOUR_SSID, YOUR_PASSWORD, and YOUR_BOT_TOKEN with your actual Wi-Fi credentials and Telegram bot token.
  2. Upload the Code: Use the Arduino IDE to upload the code to your ESP8266 board. Ensure you select the correct board and port in the IDE.
  3. Set Up Telegram Bot: After uploading the code, interact with your Telegram bot to get temperature updates and send control commands.

This section provides clear instructions for anyone visiting your website to easily set up and use the provided code. It also highlights key parts of the code so they can understand its functionality and how to modify it for their own needs.

Downloads

image_2024-12-02_10-41-26.png
IMG_0924.jpg

Battery Voltage Reading and Control Circuit

This part of the project handles battery voltage monitoring and integrates control functionality through an Arduino microcontroller. The circuit not only measures the battery's voltage but also allows the MOSFET to be switched on or off based on commands sent to the Arduino. This makes the circuit highly versatile for automation and remote control.

Circuit Description

  1. Battery Input (V3):
  2. The circuit is powered by an 8V DC battery (V3), which is the source voltage being monitored.
  3. Voltage Divider (R4 and R5):
  4. Resistors R4 (2.2kΩ) and R5 (1kΩ) step down the battery voltage to a safe range for both the MOSFET and the Arduino.
  5. This ensures the voltage at the MOSFET’s gate remains within acceptable limits.
  6. MOSFET Control (M1 - SI2302ADS):
  7. The SI2302ADS is an N-channel MOSFET. Its operation (on or off) is controlled by the voltage applied to its gate.
  8. The gate voltage can be regulated manually by the Arduino to turn the MOSFET on or off, providing the capability to either read the battery voltage or disable it to conserve energy.
  9. Output Voltage (Vout):
  10. Vout is the scaled-down voltage output that corresponds to the battery’s voltage. It is passed to the ADC (Analog-to-Digital Converter) pin of the Arduino for voltage monitoring.
  11. A current-limiting resistor (R6 - 1kΩ) at the output ensures safe operation and signal stability.
  12. 3.3V Reference:
  13. The 3.3V reference is provided to the MOSFET’s source terminal, ensuring it works seamlessly with the Arduino and other 3.3V logic systems.

Arduino Integration and Remote Control

  1. Control Logic: The Arduino is connected to the MOSFET’s gate, allowing it to toggle the MOSFET’s state. This provides precise control over when the battery voltage is read.
  2. Communication Protocol: The Arduino is programmed to listen for a command, such as "READ", sent through a serial interface (e.g., via USB, UART, or a wireless module like ESP8266).
  3. When the "READ" Command is Received:
  4. The Arduino turns the MOSFET on by setting the gate pin high.
  5. It measures the voltage at Vout using an ADC pin and calculates the actual battery voltage based on the voltage divider ratio.
  6. The measured voltage is sent back to the sender (e.g., a computer, smartphone app, or IoT system) as a response.
  7. When an "OFF" Command is Received:
  8. The Arduino sets the MOSFET’s gate pin low, turning the MOSFET off.
  9. This disconnects the circuit, reducing power consumption and protecting the system from unnecessary measurements.


Captura de pantalla 2024-12-15 183045.png
IMG_0923.jpg

How It Works

  1. The Arduino communicates with a user or system via serial commands.
  2. When a "READ" command is sent:
  3. The Arduino turns on the MOSFET.
  4. It reads the scaled voltage from Vout and calculates the battery voltage using the formula:


  1. The Arduino sends the calculated battery voltage back to the user/system.
  2. When no monitoring is required, the "OFF" command disables the MOSFET to conserve power.

Applications

  1. Remote Voltage Monitoring:
  2. Ideal for IoT systems where battery levels need to be monitored remotely.
  3. Power Control:
  4. Allows the user to enable or disable voltage monitoring on demand, optimizing energy usage.
  5. Automation:
  6. Can be integrated into a smart home or industrial system to send alerts or take actions when the battery voltage drops below a threshold.


IMG_0936.JPG

Custom 3D Printed Enclosure for ESP8266-Based Project

This enclosure was specifically designed to house an ESP8266 development board, additional electronic components, and wiring. It ensures a clean, compact, and organized setup for the project while providing easy access to key components and protecting the electronics.

Design Features

  1. Custom Fit for ESP8266:
  2. The box is designed to perfectly fit an ESP8266 NodeMCU module, ensuring it remains securely mounted during operation.
  3. Openings are strategically placed for access to GPIO pins, USB power input, and reset button.
  4. Component Placement:
  5. Space has been allocated for a custom PCB/prototyping board to mount additional components and wiring. This allows for flexibility in expanding the circuit.
  6. Cable Management:
  7. The box includes slots and holes to route wires cleanly, avoiding tangling and ensuring a professional finish.
  8. Wires leading to external devices can exit neatly through the designed openings.
  9. Modular Lid Design:
  10. The enclosure features a removable lid that allows easy access for maintenance or modifications.
  11. The lid can be screwed or snapped into place, depending on user preference.
  12. Compact Size:
  13. The box has been optimized to keep the footprint small while still accommodating all necessary components.
  14. Ventilation Slots:
  15. To prevent overheating, ventilation holes can be included in the design if needed, especially for projects running continuously.
  16. Mounting Options:
  17. The bottom of the enclosure includes mounting holes, enabling it to be fixed onto a wall, desk, or another surface securely.

3D Printing Details

  1. Material: Printed with PLA or PETG for durability and strength.
  2. Layer Height: 0.2mm for a good balance of speed and quality.
  3. Infill: 20-30% to ensure structural integrity while saving material.
  4. Print Time: Depending on the 3D printer, the enclosure can be printed within 4-6 hours.
  5. Color: The enclosure can be printed in any desired color, as shown in the image with blue and gray parts.

Design Process

The box was designed using CAD software (e.g., Fusion 360 or Tinkercad), starting with the exact measurements of the ESP8266 development board and other components. The design was refined to include space for wires, mounting holes, and a snug fit for the board. After the CAD design was completed, the model was exported as an STL file for 3D printing.

Applications

This custom enclosure is ideal for:

  1. IoT projects using ESP8266/NodeMCU.
  2. Home automation systems.
  3. Battery-powered or sensor-based devices.
  4. Prototyping projects where a clean and organized setup is essential.