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.
Join the DZone community and get the full member experience.
Join For Freethe 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.
materials
esp8266 wi-fi module:
![]() |
esp8266 esp-01 serial wifi wireless transceiver module |
|
$2.05
![]() |
|
$2.85
![]() |
![]() |
esp8266 esp-03 serial wifi wireless transceiver module |
|
$2.45
![]() |
![]() |
esp8266 esp-12 serial wifi wireless transceiver module |
|
$2.33
![]() |
|
$2.83
![]() |
![]() |
esp8266 esp-05 serial wifi wireless transceiver module |
|
$1.88
![]() |
|
$3.89
![]() |
![]() |
esp8266 esp-07 serial wifi wireless transceiver module |
|
$2.18
![]() |
|
$1.87
![]() |
![]() |
esp8266 lua nodemcu wifi network development board |
|
$7.43
![]() |
|
$3.49
![]() |
ds18b20 temperature sensor:
![]() |
ds18b20 dallas temperature sensor |
|
$0.99
![]() |
|
$0.89
![]() |
![]() |
ds18b20 dallas waterproof temperature sensor |
|
$1.56
![]() |
|
$4.05
![]() |
![]() |
ds18b20 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 ohm |
|
$3.55
![]() |
|
$2.99
![]() |
3.3v power supply (battery or regulator):
![]() |
5pcs 3.3v regulator module 800ma |
|
$1.2
![]() |
|
$1.61
![]() |
![]() |
2xaa 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.
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.
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.
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:
Published at DZone with permission of Igor Jarc, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments