Over a million developers have joined DZone.

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

Access the survey results 'State of Industrial Internet Application Development' to learn about latest challenges, trends and opportunities with Industrial IoT, brought to you in partnership with GE Digital.

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.

Introduction


Here's our historical temperature chart.

Historical temperature chart


Materials

ESP8266 Wi-Fi module:

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

$2.05

$2.85
ESP8266 ESP-03 Serial WIFI Wireless Transceiver ModuleESP8266 ESP-03 Serial WIFI Wireless Transceiver Module

$2.45
ESP8266 ESP-12 Serial WIFI Wireless Transceiver ModuleESP8266 ESP-12 Serial WIFI Wireless Transceiver Module

$2.33

$2.83
ESP8266 ESP-05 Serial WIFI Wireless Transceiver ModuleESP8266 ESP-05 Serial WIFI Wireless Transceiver Module

$1.88

$3.89
ESP8266 ESP-07 Serial WIFI Wireless Transceiver ModuleESP8266 ESP-07 Serial WIFI Wireless Transceiver Module

$2.18

$1.87
ESP8266 Lua Nodemcu WIFI Network Development BoardESP8266 Lua Nodemcu WIFI Network Development Board

$7.43

$3.49


DS18B20 temperature sensor:

DS18B20 DALLAS Temperature SensorDS18B20 DALLAS Temperature Sensor

$0.99

$0.89
DS18B20 DALLAS Waterproof  Temperature Sensor DS18B20 DALLAS Waterproof Temperature Sensor

$1.56

$4.05
DS18B20 Temperature Sensor moduleDS18B20 Temperature Sensor module

$1.89

$2.20
 

4.7K resistor:

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

$3.55

$2.99


3.3V power supply (battery or regulator):

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

$1.2

$1.61
2xAA 3V Battery Holder Box Case Wire2xAA 3V Battery Holder Box Case Wire

$1.09

$0.94


Hardware

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


Program

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 

#ifdef DEBUG_PROG
  #define DEBUG_PRINTLN(x)  Serial.println(x)
  #define DEBUG_PRINT(x)    Serial.print(x)
#else
  #define DEBUG_PRINTLN(x) 
  #define DEBUG_PRINT(x)
#endif


EIoTCloudRestApi eiotcloud;

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



#define REPORT_INTERVAL 1

#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 = {
  CONFIG_VERSION,
  // token
  "1234567890123456789012345678901234567890",
  // The default module 0 - invalid module
  0,
  //0 // not valid
};

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

void setup() {
    Serial.begin(115200);
    DEBUG_PRINTLN("Start...");

    EEPROM.begin(512);
    loadConfig();

    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: ");
      DEBUG_PRINTLN(token);
      eiotcloud.SetToken(token);

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

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

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

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

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

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

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

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

      // save configuration
      saveConfig();
    }

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

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


void loop() {
  float temp;

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



  if (tempOld != temp)
  {
    // send temperature
    bool valueRet = eiotcloud.SetParameterValue(parameterId, String(temp));
    DEBUG_PRINT("SetParameterValue: ");
    DEBUG_PRINTLN(valueRet);
    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.
  if (EEPROM.read(CONFIG_START + 0) == CONFIG_VERSION[0] &&
      EEPROM.read(CONFIG_START + 1) == CONFIG_VERSION[1] &&
      EEPROM.read(CONFIG_START + 2) == CONFIG_VERSION[2])
    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));

  EEPROM.commit();
}


 

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


The IoT Zone is brought to you in partnership with GE Digital.  Discover how IoT developers are using Predix to disrupt traditional industrial development models.

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

Published at DZone with permission of Igor Jarc, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}