Over a million developers have joined DZone.

Make Your Own Temperature Sensor and Email Alarm

DZone 's Guide to

Make Your Own Temperature Sensor and Email Alarm

Here's a DIY project to make your own temperature monitor and create email notifications if things are getting too hot.

· IoT Zone ·
Free Resource

The Internet of Things is becoming a more ever-present part of our lives. So, it should come as no surprise that something as crucial as temperature monitoring is a part of that. Follow along with this tutorial to learn how to create your own temperature sensor. More importantly, learn how to set up an email alert system for when things are getting warm.


Here's our historical temperature chart.

Historical temperature chart


ESP8266 Wi-Fi module:

ESP8266 ESP-01 Serial WIFI Wireless Transceiver Module ESP8266 ESP-01 Serial WIFI Wireless Transceiver Module


ESP8266 ESP-03 Serial WIFI Wireless Transceiver Module ESP8266 ESP-03 Serial WIFI Wireless Transceiver Module

ESP8266 ESP-12 Serial WIFI Wireless Transceiver Module ESP8266 ESP-12 Serial WIFI Wireless Transceiver Module


ESP8266 ESP-05 Serial WIFI Wireless Transceiver Module ESP8266 ESP-05 Serial WIFI Wireless Transceiver Module


ESP8266 ESP-07 Serial WIFI Wireless Transceiver Module ESP8266 ESP-07 Serial WIFI Wireless Transceiver Module


ESP8266 Lua Nodemcu WIFI Network Development Board ESP8266 Lua Nodemcu WIFI Network Development Board



DS18B20 temperature sensor:

DS18B20 DALLAS Temperature Sensor DS18B20 DALLAS Temperature Sensor


DS18B20 DALLAS Waterproof  Temperature Sensor DS18B20 DALLAS Waterproof Temperature Sensor


DS18B20 Temperature Sensor module DS18B20 Temperature Sensor module



4.7K resistor:

400X 0.25w 1/4w Metal Film Resistor Pack Kit 1% 20 Value Each 20 Pcs 10 ~ 1M ohm 400X 0.25w 1/4w Metal Film Resistor Pack Kit 1% 20 Value Each 20 Pcs 10 ~ 1M ohm



3.3V power supply (battery or regulator):

5pcs 3.3V regulator module 800mA 5pcs 3.3V regulator module 800mA


2xAA 3V Battery Holder Box Case Wire 2xAA 3V Battery Holder Box Case Wire




The connection diagram is shown below. Instead of an ESP8266-01, you can use different type of ESP8266. You can also use a 3.3V power supply instead (AMS1117-3.3) of 2 AA batteries. 

ESP8266 DS18B20 connections


Our program is written in the Arduino ESP8266 IDE. See our Arduino ESP8266 IDE tutorial to see how to connect your ESP8266 module to a computer. If you are not experienced, it's recommended to use an ESP8266 Node MCU board — just connect it to your computer's USB port.

Download the program from our GitHub. Also download the EIoTCloudRestApiV1.0 library and add it to your library folder in the Arduino IDE. The DallasTemperature lib can be downloaded here.

In the program, you need to change only three lines: set access point name, access point password, and your instance ID. To get the instance ID, you need to register to the EasyIoT Cloud service. The instance ID can be found under Configure->User Info->Instance ID.

Instance ID

Change the lines:

#define AP_USERNAME "xxxx"
#define AP_PASSWORD "xxxx"
#define INSTANCE_ID "xxxx"

ThepProgram reads the configuration from EEPROM. If the module ID is 0, then it knows that it is not configured in the cloud. It adds a new module, names it, and adds the appropriate parameters and parameter settings (look in the program comments for details). The newly added module ID is stored in ESP8266 EEPROM. Next time, when we switch on our temperature sensor, it reads the module ID from EEPROM and no cloud configuration is needed. The temperature sensor is automatically added to EasyIoT Cloud. You can see the new module in EasyIoT Cloud or a native mobile application.

#include <ESP8266WiFi.h>
#include "EIoTCloudRestApiV1.0.h"
#include <EEPROM.h>
#include <OneWire.h>
#include <DallasTemperature.h>

#define DEBUG_PROG 

  #define DEBUG_PRINTLN(x)  Serial.println(x)
  #define DEBUG_PRINT(x)    Serial.print(x)
  #define DEBUG_PRINTLN(x) 
  #define DEBUG_PRINT(x)

EIoTCloudRestApi eiotcloud;

// change those lines
#define AP_USERNAME "xxxx"
#define AP_PASSWORD "xxxx"
#define INSTANCE_ID "xxxx"


#define ONE_WIRE_BUS 2  // DS18B20 pin
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);

#define CONFIG_START 0
#define CONFIG_VERSION "v01"

struct StoreStruct {
  // This is for mere detection if they are your settings
  char version[4];
  // The variables of your settings
  char token[41];
  uint moduleId;
  //bool tokenOk; // valid token  
} storage = {
  // token
  // The default module 0 - invalid module
  //0 // not valid

String moduleId = "";
String parameterId = "";
float tempOld = 0;

void setup() {


    eiotcloud.begin(AP_USERNAME, AP_PASSWORD);

    // if first time get new token and register new module
    // here hapend Plug and play logic to add module to Cloud
    if (storage.moduleId == 0)
      // get new token - alternarive is to manually create token and store it in EEPROM
      String token = eiotcloud.TokenNew(INSTANCE_ID);
      DEBUG_PRINT("Token: ");

      // remember token
      token.toCharArray(storage.token, 41);

      // add new module and configure it
      moduleId = eiotcloud.ModuleNew();
      DEBUG_PRINT("ModuleId: ");
      storage.moduleId = moduleId.toInt();

      // set module type
      bool modtyperet = eiotcloud.SetModulType(moduleId, "MT_GENERIC");
      DEBUG_PRINT("SetModulType: ");
      // set module name
      bool modname = eiotcloud.SetModulName(moduleId, "Room temperature");
      DEBUG_PRINT("SetModulName: ");

      // add image settings parameter
      String parameterImgId = eiotcloud.NewModuleParameter(moduleId, "Settings.Icon1");
      DEBUG_PRINT("parameterImgId: ");

      // set module image
      bool valueRet1 = eiotcloud.SetParameterValue(parameterImgId, "temperature.png");
      DEBUG_PRINT("SetParameterValue: ");
      // now add parameter to display temperature
      parameterId = eiotcloud.NewModuleParameter(moduleId, "Sensor.Parameter1");
      DEBUG_PRINT("ParameterId: ");

      //set parameter description
      bool valueRet2 = eiotcloud.SetParameterDescription(parameterId, "Temperature");
      DEBUG_PRINT("SetParameterDescription: ");
      //set unit
      // see http://meyerweb.com/eric/tools/dencoder/ how to encode °C 
      bool valueRet3 = eiotcloud.SetParameterUnit(parameterId, "%C2%B0C");
      DEBUG_PRINT("SetParameterUnit: ");

      //Set parameter LogToDatabase
      bool valueRet4 = eiotcloud.SetParameterLogToDatabase(parameterId, true);
      DEBUG_PRINT("SetLogToDatabase: ");

      bool valueRet5 = eiotcloud.SetParameterAverageInterval(parameterId, "10");
      DEBUG_PRINT("SetAvreageInterval: ");

      // save configuration

    // if something went wrong, wiat here
    if (storage.moduleId == 0)

    // read module ID from storage
    moduleId = String(storage.moduleId);
    // read token ID from storage
    // read Sensor.Parameter1 ID from cloud
    parameterId = eiotcloud.GetModuleParameterByName(moduleId, "Sensor.Parameter1");
    DEBUG_PRINT("parameterId: ");

void loop() {
  float temp;

  do {
    temp = DS18B20.getTempCByIndex(0);
    DEBUG_PRINT("Temperature: ");
  } while (temp == 85.0 || temp == (-127.0));

  if (tempOld != temp)
    // send temperature
    bool valueRet = eiotcloud.SetParameterValue(parameterId, String(temp));
    DEBUG_PRINT("SetParameterValue: ");
    tempOld = temp;
  delay(1000 * 60 * REPORT_INTERVAL);

void loadConfig() {
  // To make sure there are settings, and they are YOURS!
  // If nothing is found it will use the default settings.
    for (unsigned int t=0; t<sizeof(storage); t++)
      *((char*)&storage + t) = EEPROM.read(CONFIG_START + t);

void saveConfig() {
  for (unsigned int t=0; t<sizeof(storage); t++)
    EEPROM.write(CONFIG_START + t, *((char*)&storage + t));



EasyIoT Cloud Configuration 

To use your temperature sensor you do not need to configure anything — the sensor is plug-and-play. If you want to set a temperature email alarm, configure the following automation program. Go to Configure->Automation and press the Add button.

Configure the following:

Enable the program, then select the program type "Condition." In the "If" condition, select your newly added sensor module and Sensor.Parameter1. Then, set the condition to ">= rising edge." Rising edge means that you will be notified only once when temperature raises to a certain value. For the Condition Value, select "Fixed value" and set whatever fixed value you want (in our case fixed value is 27). Then, set the Action "Send email" and set your email address and message. If you want to see the temperature in the message, use {value}. Finally, save the program.

Program configuration

In this example, if the sensor for "9 - room temperature" sendsa  temperature equal or greater than 27C, we will be notified once (on the rising edge) to the email address with a message like this:

Alarm notification

ide ,cloud ,temperature ,alarm ,sensor ,wifi ,arduino ,email

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}