There are plenty of examples out there of temperature logging systems but I wanted to create my own and keep it nice and simple. The other thing I wanted to try out was logging the data to the internet using a third party service. I’ve done it before using my own website based databases but that is a little bit more involved.
So here is my attempt at a basic temperature “Internet of Things” device that hopefully will help others get started or form the basis for a project.
The device consists of :
- Raspberry Pi Model A+
- Basic case
- BMP180 sensor module
- LED with 330ohm resistor
- Small switch
- 4-way female-female jumper cable
- 2 2-way headers
I chose the BMP180 sensor as I already had one available which I used for my BMP180 I2C Digital Barometric Pressure Sensor article. This sensor is small, cheap and handles temperature and pressure. It communicates with the Pi via an I2C interface so requires a minimum of wiring.
To give some visual feedback I added a single green LED. The anode (+ve) was connected to pin 11 (GPIO17) and the cathode (-ve) was connected to pin 9 via a 330ohm resistor. I chose those two pins as they are next to each other and allowed me to use a 2-pin header and plug the LED directly onto the Pi’s GPIO header.
As I wanted to use a model A+ I needed a WiFi dongle to allow the Pi to communicate with the internet. I’ve got a few Edimax EW-7811UN USB dongles so I used one of those. You will need to setup the WiFi so it can successfully connect to your network. You can use my Setting Up WiFi On The Raspberry Pi guide. I also disabled the power saving modes using this guide.
The switch is there to tell the script to exit and/or shutdown the Pi. It was connected to pin 15 (GPIO22) and pin 17 (3V3) using another 2-pin header. By default the script enables an internal pull down resistor on pin 15 so that GPIO22 is LOW. When the switch is pressed it is connected to 3.3V and pulled HIGH. The script monitors the switch and exits if it is pressed. By setting the AUTO_SHUTDOWN flag to 1 the Pi can also shut itself down if required.
Here is how I connected the temperature sensor, LED and switch.
|Signal Name||Header Pin|
|BMP180 SDA||I2C SDA||3|
|BMP180 SCL||I2C SCL||5|
Thingspeak and The Internet of Things
For the third party service I looked at a number of options. I ruled out a number of them because they :
- Failed to make it clear what the cost was (if any)
- Failed to define what the difference was between free trials and paid options
- Failed to explain exactly how you submitted data
- Only retained data for a few days on the free option
I settled on Thingspeak.com simply because within one click of their homepage I knew exactly what I had to do to submit data. Configuring the dashboard was easy and didn’t require reading any additional help.OK setting up the gauges needed a bit more reading but the graphs were fairly straightforward.
My first script was working within five minutes after a bit of cutting and pasting. There are loads of other services out there but for this project Thingspeak suited me perfectly. I’m not opposed to paying for a service but I wanted everyone to be able to do something easy without worrying about the cost or having to use a free, time limited trial. For more elaborate systems with more data then you may want to pay for a more feature rich service.
To use my example script you will need to setup a Thingspeak account, create a new channel and acquire the “Write API Key” from the API settings. See the official documentation for help.
The main script reads temperature and pressure and sends it to Thingspeak using “Field 1” and “Field 2”. It relies on an additional file to make use of the BMP180 sensor. Both scripts are required and should be saved in the same place :
You can download these two files directly to an internet enabled Pi either right-clicking on the links above in a web browser or running these at a command prompt :
wget https://bitbucket.org/MattHawkinsUK/rpispy-misc/raw/master/python/bmp180.py wget https://bitbucket.org/MattHawkinsUK/rpispy-misc/raw/master/python/templogger.py
The script can then be executed on the command line using :
sudo python templogger.py
The script prints text updates to the screen. When launching via SSH over WiFi I wanted the script to continue running after I disconnected so I used this command instead :
sudo python templogger.py > /dev/null &
In this command the status outputs are directed to “null” (i.e. ignored) and the & insures the script runs in the background.
Automatically Run On Boot
In order to avoid having to start the script manually you can configure it to launch when the Pi boots up. You can do this by editing the rc.local file :
sudo nano /etc/init.d/rc.local
then adding the following line at the bottom of the file :
sudo python /home/pi/templogger.py > /home/pi/templogger.log 2>&1
You can save and exit the nano editor using [CTRL-X], [Y] then [ENTER].
On startup the line runs the Python script and directs text output to a log file. This is a useful file to read if you are fault finding. The values in the log are comma separated so it could be loaded into a spreadsheet application if you wanted to analyse the data.
Pressing the switch at anytime will result in the script stopping. If AUTO_SHUTDOWN is set to 1 then the Pi will shutdown. After 20 seconds the power can be disconnected.
The script has some values which you will need to adjust. The most important one is the Thingspeak API Key. You need to enter your own key to update your channel. You can either edit the Python script directly using :
or by providing a set of new values in a configuration file on the boot partition. If a file called “/boot/templogger.cfg” exists and the first line is “Temp Logger” the values that follow are used instead. Here is an example configuration file :
Temp Logger 0x77 1 17 22 10 1 ABCDEFG123456789 https://api.thingspeak.com/update
You can create a configuration file by using :
sudo nano /boot/templogger.cfg
Then create the content as in the example above.
As the file exists in the /boot partition you can edit the configuration file on a PC before powering up the device. This is sometimes useful if you want to change the INTERVAL between readings.
The script doesn’t check for errors in your file. Make sure all the values are specified in the correct order. They are expected in the same order as they are listed in the original Python script.
Once the device is up and running and you’ve configured your Thingspeak Channel you can produce outputs like this :
The graphs update these in realtime as new data arrives. The channel also allows you to download your data in CSV, XML and Json formats.
With an interval of 10 minutes that gives you 144 data points every 24 hours. You can set the graphs to display a set number of data points so you can adjust this to give you a suitable spread of data.
I only used two streams of data in my example (temperature and pressure) but Thingspeak will accept a total of eight.
Here is my public RPiSpy Temp Logger Channel.