Did I turn the Stove off??

August 25, 2017

Building a Stove Top sensor with nio

We’ve all been there (some maybe much more than others). You’ve left home for a long weekend of camping, relaxing, taking in nature and enjoying the wildlife and this creeping voice in the back of your mind starts pestering you asking:

“Did I turn the stove off??”

You’re sure you did, yet you don’t specifically remember turning it off, but you’re pretty sure you would have, right? As you keep driving the voice keeps occupying more of your consciousness asking the “What ifs?”, “What if I come back and my apartment is a smoldering ruin?”. Only being half an hour away from home, you decide it better to turn around and just make certain you turned it off, rather than having a building anxiety spoiling the relaxing weekend.

30 minutes later:

Of course the stove was turned off. That’s what I thought, I just couldn’t remember explicitly doing it. Now we are behind schedule and most likely won’t arrive to the camp site until it’s already dark.

But hey! No lingering anxiety!

So how do we solve this problem easily and cost effectively?

Requirements:

  • Not needing to do any electrical work or cut any wires
  • Cheap Solution – < $40
  • Easy to setup / maintain

Where to start:

Step 1 is to find a sensor that meets our requirements. I put together a list of sensors I thought would fit the bill:

The Infrared Thermometer looks to be a winner in the cost department, but will it do what we need? Since they’re so cheap I figured why not buy one and give it a shot.

In order to get the Thermometer reading temperature data we need:

  • A Raspberry Pi
  • A breadboard
  • Wires

This is the Infrared Thermometer’s datasheet. This is needed in order to know how to connect it to the pi. Page 5 has a pin definition in order to see which 2 or the 4 connections are for power and which 2 are for reading data. There is a tiny bump on the sensor which tells us how to align it to read this diagram. Here you see the bump faces to the right. VSS is the ground connection and VDDis the input from the power supply. The PWM / SDA pin is the important one for reading temperature data. Reading from the RaspPi pin diagram we can see where to connect wires.

  • VDD to the 3v3 power input
  • VSS to a GND
  • PWM / SDA to the SDA1 (pin 03)
  • SCL / Vz to the SCL1 (pin 05)

After setting this up the next step is to try and get a temperature reading in the console. From the datasheet we see that the address we need to read from is 0x07 (Tobj1) – the Temperature of the Object

In bash we need to run

i2cdetect -y 0

to get the Address of the i2c Bus. In my case it was 0x5a. We can then read the Raw temperature data in python with:

import smbus

bus = smbus.SMBus(1)
word = bus.read_word_data(0x5a, 0x07)

The number we are receiving is word data, and we need to convert it to fahrenheit, this is how I did that:

kelvin = word * 0.02
celcius = kelvin - 273
fahrenheit = (9 / 5 * celcius) + 32

Awesome! Running this program gives us a fahrenheit temperature reading and it is room temperature! The next thing I did was to make a hot cup of tea and hold it over the temperature sensor and take another reading. It should be much greater than room temperature if everything is setup correctly!

Our next step is to design a system using nio that will take a reading every 5 seconds and publish that data to a socket server.

In order to do this we need to create a simple Infrared Thermometer block. The first step is to clone down the nio Block Template, I named mine irthermometer_block. The next step is to use the commands we wrote above in order to get the temperature and pass that signal on.

Once that block is installed we can simply import it into nio and process a signal every 5 seconds and publish that to a socket server.

The nio service


The next piece is to install the actual temperature sensor onto the stove! We need to have it connected to the RaspPi still so the best solution I found was to extend the 4 wires and have just the sensor hang over the stove while the electronics sit above the kitchen cabinets, far away from the electronic damaging steam that comes from the stove.

My very professional install


The last step is to setup our socket server and output data to a url! For this project I setup a subdomain stove.aaronranard.com. I then cloned the socket.io chat example repo to setup my socket server.

For this implementation we need to store previous temperature readings so we can know if it is going up, down, or staying relatively even.

The biggest use case for checking if you turned your stove off is about 5 – 10 minutes after you’ve left home. In this situation the stove might still be hot, BUT it should be cooling down.

So we need to show more than the exact temperature reading because if I were to view it right after I left and it said 130 I may think I left it on, whereas in reality it is just cooling down.

Code:

      const halfLength = valuesToStore / 2;
      const oldData = previous.slice(0, halfLength);
      const newData = previous.slice(halfLength);

      const oldAvg = oldData.reduce((total, data) => { return total + parseFloat(data.fahrenheit) }, 0) / halfLength;
      const newAvg = newData.reduce((total, data) => { return total + parseFloat(data.fahrenheit) }, 0) / halfLength;

A history of recent temperatures is stored and a diff on the delta of the more recent ones (newAvg) vs the older ones (oldAvg) will tell us if it is warming up or cooling down.

On the front end we need 3 things.

  • A switch to tell us if the stove is on or off
  • The current temperature reading
  • A color display if the stove is cool, warming, hot or cooling