Preventing Fire with IoT: A Simple Solution

ESP temperature monitoring system to prevent stove fires 🔥

Ariel Liu
8 min readMay 17, 2020

A couple of days ago my grandma forgot to turn off the stove, and it stayed lit through the night. She’s often forgetful, making such accidents a common theme nowadays.

Other than being a huge waste of fuel, it could have also been quite dangerous if a fire did catch on. And no, I do not want my house evenly roasted.

Since recently I’ve been digging deep into IoT (Internet of Things) I thought I’d make a project on this!

The Grand Plan 🚀

The idea is simple, collect data with a temperature sensor, and record it all online in real-time.

I connected a TMP temperature sensor to my ESP32, a microcontroller similar to Arduino but with the capability of connecting to wifi. Then using the Arduino IDE I programmed my ESP to receive the data, connect to my home’s wifi network, and send the data to the ThingSpeak API. From there ThingSpeak does the rest, storing and graphing the data in real-time!

Want to read this story later? Save it in Journal.

Image Courtesy of Author

Set up 🔌

There’s still a lot of preparations to do both in terms of hardware and software

About ESP 🔧

The ESP was created as a chip microcontroller designed for low power consumption at a low cost. I have the ESP32 Dev Module, it was designed with IoT applications like smart wearable in mind, with onboard WiFi and Bluetooth support. It also has onboard sensors like an internal temperature sensor, hall sensor (measures magnitude of a magnetic field), and touch sensors.

It can also be programmed with the Arduino IDE. If you go to File → Preferences in Arduino. It opens up a window, paste this link into the Additional Board Manager URLs box.

Then go to Tools → Board → Board manager, use the search bar to find ESP32, and install the package. Then make sure that under tools the correct board and port are selected and you’re all set!

The temperature sensor 🔥

This is probably the simplest portion. I used a TMP36 temperature sensor that looks like this…

Yes, the ones that look annoying similar to a transistor. One leg connects to power (in this case 5V), one to an analog input pin, and one leg to the ground.

A diagram made on https://www.circuito.io/

The TMP is a good analog sensor, pretty precise, with a decent range of -40 to 150C. Here’s its technical information from the distributor.

Here’s my hardware setup

ThingSpeak 💡

ThingSpeak is a good analytics platform, created for IoT applications. Using their server, API, and channels I could send data from my ESP to ThingSpeak and monitor it in realtime. Once you have a ThingSpeak account you can create a new channel.

Features represent the different data sources/sections, the data in each feature is kept separate and the channel can hold up to 8 features. For this project, we only need one field: temperature.

Under the API Keys tab, we can find the channels Write API Key, take note of this. Along with the channel ID under the channel title, both of which we’ll be using in our code soon!

By using the Write API Key and the Channel ID we can write to the ThinkSpeak channel through the internet from the ESP.

Similar to the ESP, in the Arduino IDE I head to Sketch →Include Library →Manage Libraries and find/install the ThingSpeak library.

Coding it all Together 👩‍💻

Connecting to wifi 🧰

In order to connect to wifi, I use the WiFi library from the ESP package. I also import another file called “tempwrite.h”.

#include "tempwrite.h"
#include <WiFi.h>

Tempwrite is a file from the notepad that I saved as “tempwrite.h” containing the wifi information and channel information. This makes it easier to use in later programs, and maybe safer?

I reference the wifi SSID and password in the program and store the wifi credentials in corresponding variables. (Make sure your on the right wifi when you're running your program, and that the credentials are all correct.)

char ssid[] = SECRET_SSID;
char pass[] = SECRET_PASS;

WiFiClient client;
WiFi.mode(WIFI_STA);

Technically after this, all you need is the following line of code to connect to wifi…

WiFi.begin(ssid, password);

But this makes debugging difficult and has little feedback. Instead, we can open a serial connection. Serial.begin(115200);

Using a while loop we call the status method on the Wifi object and print out a loading sign of sorts every few seconds. So that we now the program is running correctly but taking some time to connect to wifi.

while(WiFi.status() != WL_CONNECTED){
WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network
Serial.print(".");
delay(5000);
}
Serial.println("\nConnected.");

Temperature Sensing 🤒

Now let’s work on the key component — the actual data! First of I set the proper input pin for the sensor and some variables we’ll be using later.

const int tempPin = 35;
int tempRaw;
float volts;
float temp;

Getting the sensor value is simple, using the analogRead function.analogRead(Pin_number)

// Get Sensor value
tempRaw = analogRead(tempPin);

But it’s not very useful in its raw form. First, we need to normalize the value using the maximum temperature reading range. Then calculate the degrees in celsius from voltage according to the sensor datasheet.

// Get Sensor value
tempRaw = analogRead(tempPin);
volts = tempRaw/1023.0; //Normalize
temp = (volts - 0.5)*100; // Convert from volts

Now we end up with the final temperature which we can print to the serial monitor and send it to ThingSpeak.

// Out print the temperature for debugging
Serial.print("The Temperature is ");
Serial.print(temp);
Serial.println(" C ");

Sending data to Thingspeak 📁

Similar to the Wifi.h I also use another file from the ThingSpeak library called “ThingSpeak.h”. I also store the API credentials in various variables.

#include "ThingSpeak.h"
unsigned long ChannelID = SECRET_CH_ID;
const char * APIKey = SECRET_WRITE_APIKEY;

Next, I initiate ThingSpeak. ThingSpeak.begin(client);

Once, I have the temperature data sending it to the ThingSpeak API is really simple. And… we’re done! 🎉

// Write the data to ThingSpeak
ThingSpeak.writeField(ChannelID, 1, temp, APIKey);
//Channel ID, which field, value, Write API Key

Final Code 💻

#include "ThingSpeak.h"
#include "tempwrite.h"
#include <WiFi.h>
char ssid[] = SECRET_SSID;
char pass[] = SECRET_PASS;
WiFiClient client;unsigned long ChannelID = SECRET_CH_ID;
const char * APIKey = SECRET_WRITE_APIKEY;
const int tempPin = 35;
int tempRaw;
float volts;
float temp;
void setup() {// Initialize Serial at the baud rate for ESP
Serial.begin(115200);
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client);
}void loop() {

// Connecting to WiFi
if(WiFi.status() != WL_CONNECTED){
Serial.print("Attempting to connect to ");
Serial.println(SECRET_SSID);
while(WiFi.status() != WL_CONNECTED){
WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network
Serial.print(" . ");
delay(5000);
}
Serial.println("\nConnected.");
}
// Get Sensor value
tempRaw = analogRead(tempPin);
volts = tempRaw/1023.0; // Normalize
temp = (volts - 0.5)*100; // Convert from volts
// Out print the temperature for debugging
Serial.print("The Temperature is ");
Serial.print(temp);
Serial.println(" C ");
// Write the data to ThingSpeak
ThingSpeak.writeField(ChannelID, 1, temp, APIKey);
//Channel ID, which field, value, Write API Key
delay(2000); //Wait 2 secs before getting more data}

Future improvements 🌱

Since I’m using this at home, I’m currently adding a few more features to this project. Such as making at an app to go along with the setup, maybe a simple app using MIT App Inventor, or coding it in Xaramin. Then having notifications and alarms when the stove is left on. I was also thinking of applying my AI knowledge and teaching a neural network when a stove is usually on and for how long, and when the stove shouldn’t be on. (Although I could probably just hard code it…hmm… 🤔) So this is still a work in progress, and I will update!

Amusing Setbacks/Dumb Mistakes 😂

Throughout this project, I had a lot of um…oversights, to say the least. Here’s a nice list so you can avoid my mistakes.

  • Making sure that the serial monitor is on the right baud
The correct baud
Not the correct baud 😱
  • Open the serial monitor first then run the program. (Not really a mistake but a tip sorta because the port will be busy)
  • Make sure your credentials are correct. (Spent an hour trying to connect, only to realize I spelled the SSID one letter off fishpong != fishpond 😅)
  • Make sure you installed the ThingSpeak Library, you can also use some of their example code to test. Example code for ESP32 here.
  • Make sure under the “tools” tab that the board and port are all selected correctly.
  • If you haven’t used ESP32 before, this message means the code was uploaded correctly.
  • Test all the components: sensor, wifi, and Thingspeak separately. You can try pinging the wifi.
  • Ensure that the wiring is correct with each leg of the sensor connected correctly and the wires all working. Make sure the flat side faces the right way.
  • This is heading more and more into common sense territory, but remember to have your computer on the same wifi network.

If you want to read more articles in the future, give my account a follow!

In the meantime, feel free to contact me at ariel.yc.liu@gmail.com or connect with me on LinkedIn.

You can also read my monthly newsletter here!

Or visit my personal website to see my full portfolio here!

Till next time! 👋

📝 Save this story in Journal.

👩‍💻 Wake up every Sunday morning to the week’s most noteworthy stories in Tech waiting in your inbox. Read the Noteworthy in Tech newsletter.

--

--

Ariel Liu

A machine learning enthusiast who’s always learning~