ESP8266 Projects

Monitor the temperature with Telegram and an ESP8266

cover temperature warning

In this project you monitor the temperature with a sensor and your ESP8266. As soon as a certain temperature defined by you is exceeded, your microcontroller sends a message to your Telegram bot.

This project is the second part of a series: In terms of content it is set up on our Silent Alarm with Telegram. Have a look there to learn more about the basic functions of the sketch. Also in this project, your ESP8266 is waiting for a certain event to occur. However, it is not triggered by an interrupt here, but requests the data of a temperature sensor itself.

Beginner

1 – 2 hours

ca. $12

For this project you need (quantities see description):

The right temperature sensor

In this project we use the sensor BMP180. But you can use any other temperature sensor – e.g. a simple TMP36, a DHT22 or a GY-906. In this case make sure to adjust your sketch accordingly.

In these tutorials you will learn how to connect and use a TMP36 and a GY-906.

The set up of the project

You only need your ESP8266, the temperature sensor (in our case a BMP180), a breadboard and cables. Please follow this scheme when setting up the system:

Set up Monitor temperature

The sensor BMP180 is connected via I²C. On the ESP8266, you must therefore use the two pins D1 and D2. Connect the sensor as follows:

BMP180ESP8266
VIN3v3
GNDGND
SDAD2
SCLD1

The sketch

Copy the following sketch into your Arduino IDE, complete your data and load it onto your ESP8266

View Sketch as .txt

/*
   Monitor the temperature with Telegram - polluxlabs.net
*/

//Libraries
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>

#include <Wire.h>
#include <Adafruit_BMP085.h>

//WiFi Credentials
const char* ssid = "NETZWERKNAME";
const char* password = "PASSWORT";

//Initialize Telegram-Bot
#define botToken "YOUR TOKEN"  //from the Botfather

//Your UserID
#define userID "YOUR USERID"

WiFiClientSecure client;
UniversalTelegramBot bot(botToken, client);

Adafruit_BMP085 bmp;

//Variable for the Temperature
float temp;

//Connecting to WiFi
void connectToWiFi() {
  Serial.print("Connecting to: ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(300);
  }
  Serial.println("");
  Serial.println("Connected!");
}

void setup() {
  Serial.begin(115200);
  client.setInsecure();

  if (!bmp.begin()) {
    Serial.println("Sensor not found - check connections.");
    while (1) {}
  }
}

void loop() {
  temp = bmp.readTemperature();
  Serial.print("Temperature = ");
  Serial.print(temp);
  Serial.println(" °C");

  if (temp > 27.00) { //Temperature threshold
    connectToWiFi();
    bot.sendMessage(userID, "Temperature is too high!  " + String(temp) + " °C", "");
    Serial.println("Temperature is too high!");
    WiFi.disconnect();
    delay(300000);
  }
  delay(500);
}

What is new in this sketch?

Much of the code we have already used in Silent Alarm. But some parts are new and we will take a closer look at them now.

First you need two more libraries. Wire.h is responsible for the communication via I²C. This library is pre-installed by default. To use the sensor BMP180 as easy as possible, there is also a suitable library: Arduino_BMP085.h. Don’t let the name confuse you. It says BMP085 (this is the predecessor of the BMP180), but it works fine with the BMP180 as well.

So at the beginning of your sketch you additionally include these two libraries:

#include <Wire.h>
#include <Adafruit_BMP085.h>

Also at the beginning of the sketch, you create the object bmp for the sensor and two variables: one for the measured temperature (temp) and one for the threshold value (threshold), which your ESP8266 will send you a message if it is exceeded. In our example, we set this threshold to 27.00 °C. Since the BMP180 outputs the temperature as a decimal number, you need the file type float for these variables.

Adafruit_BMP085 bmp;
float temp;
float threshold = 27.00;

In the setup function, you check whether the temperature sensor has been found by your ESP8266 and can be used. If not, you will be informed in the serial monitor and the sketch will go into an endless loop with while(1) – so it freezes.

  if (!bmp.begin()) {
    Serial.println("Sensor not found - check connections.");
    while (1) {}
  }

So if you see the above text on your serial monitor, disconnect your ESP8266 from the power supply and check your wiring. In most cases, this should be the problem.

The Loop

In the Loop you request the temperature in half-second intervals and check with a threshold value whether it has exceeded this.

temp = bmp.readTemperature();

If this is the case, your ESP8266 will connect to the internet and send a message to your smartphone.

if (temp > threshold) {
connectToWiFi();
bot.sendMessage(userID, "Temperature is too high! " + String(temp) + " °C", "");

The function bot.sendMessage() consists of three parts. The one in the middle is the message itself, but here you “assemble” it from two strings and the variable temp. To do this, simply connect the individual substrings with a plus sign – but be careful: In the middle is the variable temp as float. To send the value in this variable, you have to convert it into a string with the function string(temp).

On your smartphone the text “Temperature too high! x °C” – where the x stands for the measured temperature.

Finally, your ESP8266 disconnects from your Wi-Fi and shuts down for 5 minutes (300,000 milliseconds). Only then does it start requesting the temperature from the BMP180 every 500 milliseconds.

WiFi.disconnect();
delay(300000);
}
delay(500);
}

If the temperature is still above the threshold, you will receive another message.

If not already done, load the sketch onto your ESP8266 and try it out right away.

What’s next?

In the next part of the series, you don’t wait for your ESP8266 to contact you, but request the temperature yourself from your smartphone.

Instead of a temperature sensor you can also use other sensors: This way you can be warned if the air is too bad or the light is too bright. There are also sensors that can detect flames – if you use one, we hope you never get a message!

Letzte Aktualisierung am 2021-01-09 / Affiliate Links / Bilder von der Amazon Product Advertising API

You may also like

Comments are closed.