by Eric Crowley, Data Engineer
As technology improves, more and more devices are being built ‘smart’. Refrigerators are receiving weekly software updates, ovens are sending text message alerts, and even garbage cans require a Wi-Fi connection. Thanks to the internet of things (IoT), the newest generation of devices are not just efficient but also intelligent. As these smart devices start to become more common in the home, the dumb devices of yesterday will be considered obsolete. This will lead to many households debating the merit of replacing their dumb devices. After all, if an appliance does not ask you for your Wi-Fi password, is it even worth keeping?
The answer is yes! Any device or appliance can be made intelligent, all you need to do is give it a brain. In the world of IoT one of the best ‘brains’ out there is the small low-cost computer known as the Raspberry Pi.
The Raspberry Pi (seen above in Figure 1) has a few key attributes that make it one of our favorite devices for making things smart. The first attribute is price. The Raspberry Pi is cheap. How cheap? Well for 35 dollars you get a 1.2GHz 64-bit quad-core ARM CPU with 1GB of Ram, built in Wi-Fi and Bluetooth connectivity, four USB ports, full size HDMI port, 40 GPIO pins, 3.5mm audio jack, and a micro SD card slot. That’s a lot of computing power for less than 50 dollars! The second thing that makes the Raspberry Pi stand out is its size; it’s tiny. At 4.8 x 3 x 1.3 inches, the device is small enough that it can be attached to just about anything. This small footprint allows the Pi to be used in a plethora of IoT applications since it can be attached to other appliances/devices without taking up a large footprint. The last, but definitely not least, key attribute of the Pi is its 40 general purpose input output (GPIO) pins. These pins, which are highlighted in Figure 2, allow for the Raspberry Pi to interact with the outside world through various sensors and components. If there is something in the environment that you would like to measure such as temperature, humidity, light, sound, vibration, etc., then there is probably a sensor out there that can measure it. Cost, size, and the ability to interact with the world are the key attributes that make the Raspberry Pi one of the best tools out there for adding intelligence to our dumb things.
Almost anything can be made into a smart device using this incredible tool. Instead of taking my word for it, lets look at a practical example. One dumb thing that we have in the Virtulytix office is a space heater. This space heater, as seen in Figure 3, has two knobs on top of it. One of the knobs controls fan speed and the other controls the maximum heater temperature.
The heater will keep heating until the maximum temperature is reached. Now you might have noticed from the picture that there is no actual temperature on the heater, but instead there is a bar that gradually increases in width. Wouldn’t it be nice if the heater was smart enough to know what we wanted the room temperature to be and then adjusted its output accordingly? Thanks to the Raspberry Pi, a few sensors, and a smart plug (all shown in Figure 4) it can be!
The first thing our smart heater will need to know is the temperature of the room that it is in. The best way to acquire this information is using a sensor. In this case, the sensor used is a temperature and humidity sensor as seen in Figure 5.
To attach the temperature sensor to the Raspberry Pi we will use the GPIO pins that we mentioned earlier. The sensor can be connected directly to the GPIO pins, but to make things easier we will be using a breakout board and a bread board. A breakout board is what connects our GPIO pins to our bread board. A bread board is a base that allows us to create circuits without needing to solder anything. From a functionality standpoint, there is no difference between the data produced by a sensor connected directly to the GPIO pins and one connected to a bread board, but the bread board does add a nice base for the electronic components and makes things easier to keep organized.
Now that our temperature sensor is attached (as seen in Figure 6), our Pi has the hardware necessary to know what the room temperature is. However, just knowing the current temperature is not enough. The Pi will need the ability to turn off the heater when the room becomes too hot or turn on the heater when the room becomes too cold. In order to accomplish this we will use a radio controlled outlet and a 433MHz Radio Frequency (RF) transmitter. The wireless outlet comes with a remote. Using a 433MHz RF receiver we can figure out what signal the remote sends to the outlet and then have the RF transmitter connected to our Pi replicate the signal.
With both our temperature sensor and our RF transmitter attached (see Figure 7), our Pi now has all the hardware it needs to control the temperature in the room. For this project I decided to add two additional pieces of hardware to our Raspberry Pi. The first is an LED. We will use the LED as an indicator that the Pi is recording the current temperature. The second piece of hardware is a small LCD screen. The LCD screen allows for our Pi to display information relevant to the current temperature in the room.
The Pi now has all the hardware (see Figure 8) it needs to control our dumb heater. Hardware cannot function on its own though, so we will need to write some software that will tell the Pi how to interact with the various sensors we have attached. Since the Raspberry Pi is running a full Linux OS we have many programming languages available to use. For this project I decided to use Python. Python is an excellent programing language to use with the Raspberry Pi due to both the extensive amount of support libraries available and its large community of active users. This helps to speed up development time since we do not have to reinvent the wheel when writing programs for the Raspberry Pi. Another software component that we will use is a relational database. Since the Pi has limited computational power, I decided to use SQLite. SQLite’s minimal footprint and Python libraries make it an ideal database for use on the Raspberry Pi when writing Python scripts.
Our smart heater will use two Python scripts. The purpose of the first script is to provide a web page where users can input their data. See Figure 9 to get an idea of what the simple web page looks like. This script will use a web framework called Flask. Flask is simple and lightweight microframework, which makes it perfect for powering our temperature input web page. The temperature input page is written in HTML and can be seen below.
Now that our users have a way to input their temperature preference we can add our second Python script. Our second script will be designed to run once a minute and it will use the RPi.GPIO library. This library allows us to easily interact with the sensors connected to our GPIO pins. The logic for the script is fairly simple; the script will first query the database for the most recent recorded temperature. The temperature sensor will then get the current room temperature. If the current temperature is not the same as the last recorded temperature, the current temperature will be inserted into the DB. Next, the current temperature will be compared to the user provided temperature setting. If the current temperature is less than the preferred temperature, then the Pi will send the ‘ON’ signal to our wirelessly controlled outlet using the RF transmitter. If the current temperature is greater then the preferred temperature, the Pi will send the ‘OFF’ signal to our outlet. One thing to note, if the heater is already on and the outlet receives an ‘ON’ signal, nothing happens. The same thing occurs with the off signal. The last thing our script will do is output the current temperature to the small LCD screen attached to the Pi.
After creating both the scripts we are now able to deploy our smart heater! This smart heater can be used anywhere where an outlet and Wi-Fi connection are available. See Figure 10 to view the complete smart heater setup.
While it sounds complex, the overall process of converting a heater into a smart heater is relatively simple. This same logic can be used to turn any device into a smart device using the Raspberry Pi. In our current environment more and more of the things we use are becoming smart. Thanks to the Raspberry Pi, we can make any device an intelligent device. All it takes is some sensors and a few lines of code!