Basic Temperature Logging To The Internet With Raspberry Pi

38

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.

Hardware

Basic Temperature LoggerThe 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

BMP180 Pressure Temperature ModuleSensor

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.

LED (Optional)

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.

Edimax 7811un WiFi Dongle

WiFi Dongle

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.

Switch

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.

Component Setup

Here is how I connected the temperature sensor, LED and switch.

Temperature Logger Hardware Circuit
Here are the connection details :

Signal Name Header Pin
Switch GPIO22 15
Switch 3V3 17
LED (+) GPIO17 11
LED (-) Gnd 9
BMP180 VCC 3V3 1
BMP180 Gnd Gnd 6
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.

Basic Temperature LoggerMy 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.

Python Script

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.

Configuration

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 :

nano templogger.py

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.

 

Final Results

Once the device is up and running and you’ve configured your Thingspeak Channel you can produce outputs like this :

Temperature and Pressure graphs

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.

 

Share.

38 Comments

  1. Christophe De Pauw on

    Nice project, almost what i’m looking for!
    I’d like to monitor the temperature in a freezer. I don’t think it’s a good idea to place a pi in the freezer….
    Does anyone know a temperature sensor, that could sent the data to the pi, so the pi can sent in to thingspeak?

    kind regards,
    Christophe

  2. I am getting the following error:

    File “templogger.py”, line 127, in
    main()
    File “templogger.py”, line 101, in main
    GPIO.setup(LEDGPIO , GPIO.OUT)
    UnboundLocalError: local variable ‘LEDGPIO’ referenced before assignment

    • LEDGPIO is defined at the top of the script. If it is not assigned the assignment line must be missing. Did you download the script direct to your Pi or did you cut and paste it? If you cut and pasted it the script is probably not as it should be.

  3. Great article, saved me a lot of time. Converted to work with a DHT11 temperature and humidity sensor. Didn’t know about Thingspeak so thanks again.

  4. Hi Matt
    On a brand new A+ and very recently downloaded NOOBS, the program stalls at the first line, i.e. it doesn’t find the System Management Bus module. I downloaded the listing directly from you. I’ve been having some other issues with configuring the A+ but… I’ve tried changing the smbus to SMBus but no change!

    It’s a great project idea as I spend the winter three months away from home and need a log of the temperatures to guess how much heat to give the home!!

    Thanks for any suggestion.

    Geoffrey

  5. Hi Matt,

    The Logger is working fine – up to a point! See https://thingspeak.com/channels/62333

    It keeps stopping without any pattern of numbers of samples or time running. Anything from 2 hours to 36 hours. You can see some of the gaps before I can restart it manually.
    I thought of the Power Saving issue with regard to the dongle and so ran iwconfig:

    pi@raspberrypi ~ $ iwconfig
    wlan0 IEEE 802.11bgn ESSID:”BTHub4-CG6F”
    Mode:Managed Frequency:2.412 GHz Access Point: 18:83:BF:45:46:99
    Bit Rate=65 Mb/s Tx-Power=20 dBm
    Retry short limit:7 RTS thr:off Fragment thr:off
    Power Management:off
    Link Quality=61/70 Signal level=-49 dBm
    Rx invalid nwid:0 Rx invalid crypt:0 Rx invalid frag:0
    Tx excessive retries:303 Invalid misc:660 Missed beacon:0

    lo no wireless extensions.

    …and so I guess Power Saving is not the issue.
    Any other thoughts?
    The A+ is running with a powered hub and in the same room as the router. (for the moment. Eventually I need it to run in the loft!). The dongle is an RT5370.

    Eventually I’m hoping that the logger will provide me with temperatures in the loft so that when, during Jan. thro’ March, I’m away abroad, I can adjust the temperatures downstairs to cope with a rural winter. If I can’t get it to work consistently I will then be asking how can I re-boot it over the net from an Android device!!

    Geoffrey

  6. Hi Matt.

    Great post. Thanks for making it all so accessible. Just wondering if you know how I could extract the data to tweet it please. I’ve trawled the internet and keep trying to hack code from anywhere and everywhere but I just can’t get the relevant string to update Twitter. I’m using Tweepy as it seems pretty straightforward.

    Thanks for any pointers you (or anyone else) can offer.

    Jonathan

  7. Hello Matt.
    At the beginning I want to congratulate a great project.
    Im get started with Rasberry and I dont know much about it.
    I have a question and requests.
    Is there a possibility to add to this project two additional sensors DS18B20 and DHT11 and transmission measurements on ThinkSpeak like BMP180?
    If so, could in some simple way to explain how to do it?
    I do not know much about PY and Raspberry.
    I would be very grateful for your help.
    Sorry for my english, I hope that I wrote understandable.
    Thank you again.
    Regards Damian

  8. How i can add a DHT11 in this sript for humidity and a second temperature?
    I can read the sensor with the Adafriut-DHT-library but my python is not good enough for sendData 🙁

  9. Hi Matt

    Nice post. Looking into IOT. Since IOT is here, i’d like to ask some questions.

    What about power consumption? Since iot also means low power. How would you tackle this ‘problem’?

    Best Regards,

    Samuel

    • Ultimately if you are using a Pi you are stuck with the power consumption of the model you are using. If you really need to reduce it down to the bare minimum you would better off using another device. Power consumption is why I prefer the A+ to the B+/Pi 2 or Pi 3. For temperature logging you could use an Arduino based device and there are lots of examples out there where people have done just that.

    • It should work with the PiZero but you will need to get it connected to the internet. So it will need a WiFi dongle connected to the USB port.

  10. I am so new to this and this article is so great! I am just too new to figure out how and where to change the temp from Celsius to Fahrenheit (and the mBar in whatever the US uses!). Anyone with the solution?

    Thanks!!

Leave A Reply