Raspberry Pi temperature (and humidity and pressure) logger – full instructions!

A while ago I reported on progress towards a Raspberry Pi based environmental logger.

Well, it has proved to be quite popular at work (several laboratories now use them to log temperature and humidity values for around £100 each device) so I wrote up pretty much all the instructions on how to build one (including the necessary software installation and case details).

Here are the instructions (Word file, PDF file)

Here is a diagram of the stripboard layout (and here is the Fritzing file for it). Note this is shown with the copper tracks uppermost whereas in reality they are underneath the board).

Enjoy!

 

 

 

 

 

 

 

 

 

Work on these loggers was mostly in my own time, but additional inspiration and funding came from the EMRP project ‘LUMINAR‘. The EMRP is jointly funded by the EMRP participating countries within EURAMET and the European Union.
emrp_eu_small_logo

luminar_logo_small

26 thoughts on “Raspberry Pi temperature (and humidity and pressure) logger – full instructions!

  1. hoa tien

    Thanks your guide! Could you give me python program for display only temperature, date, time on LCD 16×2.

    Reply
    1. wpadmin Post author

      You are welcome. I plan to improve the instructions in the coming months (they are a bit of a mess at the moment) and perhaps make the SD card image available online. Currently I am working on a PCB to replace the Veroboard, to improve reliability and reduce manufacturing time – there are 7 of these in use at work and I have a request for 10 more to be made!

      Reply
  2. Peter Oerlemans

    Also thanks for your guide. Very accurate and good to follow!

    But, I have a problem with the statement on page 24 of your guide:
    When I write:
    sudo /usr/share/cacti/site/scripts/new_temperatures.py
    I do not get the answer:
    T1:23.187 T2:23.187
    but in stead of that the codes of my DS18B20 temperature sensors. So in my case the output is:
    28-0314557369ff:23.187 28-0314558320ff:23.187

    In the Data Input Methods of Cacti I adapted the Field[output] name by resp. 28-0314557369ff and 28-0314558320ff but left the friendly name as it was (resp. T1 and T2) but that was not the solution.

    One last remark: in the Paths-tab of Cacti-settings in the so-called Spine PollerFile Path there is an error: FILE NOT FOUND.

    Well this is a rather long story but I hope you can bring me somewhat further. Thanks for your attention.

    Reply
    1. wpadmin Post author

      Peter,

      Thanks for your comment. I am a little busy with work right now but will try to have a look at the problem. Maybe something changed in the way that cacti gets installed.

      More when i get a spare moment. Can you respond with the contents of your new_temperatures.py file?

      Andrew

      Reply
  3. Anonymous

    Hallo Andrew,

    Here below the content of my new_temperatures.py file
    —————————————————————————-

    #!/usr/bin/python

    # This file is: /usr/share/cacti/site/scripts/new_temperatures.py

    import os, glob, time, sys, datetime

    #set up the location of the two DS18B20 sensors in the system
    device_folder = glob.glob(‘/sys/bus/w1/devices/28*’)
    device_file = [device_folder[0] + ‘/w1_slave’, device_folder[1] + ‘/w1_slave’]

    def read_temp_raw(): #a function that grabs the raw temperature data from the sensors
    f_1 = open(device_file[0], ‘r’)
    lines_1 = f_1.readlines()
    f_1.close()
    f_2 = open(device_file[1], ‘r’)
    lines_2 = f_2.readlines()
    f_2.close()
    return lines_1 + lines_2

    def read_temp(): #a function that checks that the connection was good and strips out the temperature
    lines = read_temp_raw()
    while lines[0].strip()[-3:] != ‘YES’ or lines[2].strip()[-3:] != ‘YES’:
    time.sleep(0.2)
    lines = read_temp_raw()
    equals_pos = lines[1].find(‘t=’), lines[3].find(‘t=’)
    temp = float(lines[1][equals_pos[0]+2:])/1000, float(lines[3][equals_pos[1]+2:])/1000
    return temp

    temp = read_temp() #get the temp
    print(device_folder[0][20:]+’:’+str(temp[0])+’ ‘+device_folder[1][20:]+’:’+str(temp[1]))
    ————————————————————————————-

    Peter

    Reply
  4. Tom

    Andrew,

    I am having the same issue as Peter, and confirmed that my new_temperatures.py file matches the one from the tutorial.

    Thanks for your work and documentation!

    Reply
    1. wpadmin Post author

      OK, a quick response to Peter’s question about the Spine Poller –
      “One last remark: in the Paths-tab of Cacti-settings in the so-called Spine PollerFile Path there is an error: FILE NOT FOUND.”

      That is quite normal and should not be a problem – Spine is an add-on to Cacti used to help with handling large numbers of devices, but we do not need it and do not use it. I also have the same error message. So we can ignore it.

      Regarding the failure of the new_temperatures.py script to format the output correctly, I have found that the instructions contain an old version of the script which I forgot to update – sorry!

      In the last line of the script it says:
      print(device_folder[0][20:]+’:’+str(temp[0])+’ ‘+device_folder[1][20:]+’:’+str(temp[1]))

      Delete that and replace it with the following:
      print(‘T1:’+str(temp[0])+’ T2:’+str(temp[1]))

      Then the script should output the values correctly. Cacti is a bit fussy in how it expects data to be presented. It must be T1:value then a space then T2:value, i.e. T1:20.123 T2:12.345.

      After making the above change you will need to go back into Cacti and re-create the Data Input Method.
      Good luck!

      Andrew

      (I will update the instructions)

      Reply
  5. Peter

    Hallo Andrew,

    I changed the script new_temperatures.py according to your recommendations and indeed now the output is oke.
    But I still cannot make visible the temperature-graph (T1 + T2) in Cacti. I tried a lot of things, repeated reading and controlling your descriptions but without result.
    I didn’t install LCD display software and hardware, also I didn’t install a moisture sensor.
    One remark: on page 26 of the pdf-file you mention as input type: Script/Command, but in the overview picture is shown SNMP as data input method. But that might be irrelevant. I tried both.
    Peter

    Reply
  6. Peter

    Andrew,

    An additional remark: I’m applying WIFI for my network connection. In the file /etc/hosts I find:
    localhost 127.0.0.1 and
    rasberrypi 127.0.1.1 and not 127.0.0.2!!!
    Can that be a reason for malfunctioning?

    Peter

    Reply
  7. Peter

    Hi Andrew,

    You can skip the two E-mail I sent you today.
    I started from start with setting up everthing and now the Temperatures graph is running!!
    However a new problem came up: no data are shown. Every temperature is indicated as nan. Only graph axis are shown.
    Any suggestion?

    Peter

    Reply
    1. wpadmin Post author

      Hi Peter,

      Sorry but i am away for the holidays and busy with family matters.

      Generally with cacti I found that you need to clear out the database files and let it recreate them when fixing some problems. I suggest you try that for now. Also turn on the debugging in cacti to a more detailed level and then look at the little for clues. You should see the poller process get the data and the something else parse it into the correct variables before it gets stored in the databases. A failure at any stage should show up and that gives further clues where to look, but needs a little detective work.

      Good luck and happy xmas.

      Reply
      1. wpadmin Post author

        Just to add, I have updated the latest version of the instructions to detail the changes necessary to have the logger working on the new Raspberry Pi model 2. Essentially you need to update the OS, change some settings to enable some h/w modules (they way they are handled is different on the model 2) and install an updated copy of the python GPIO module (currently only available as a beta). I got this all working on a PCB version of the logger I am working on at work and it all is working fine and backwards compatible with the previous hardware. I think I noted down all the steps, apologies if I forgot something.

        Reply
      2. wpadmin Post author

        Hi all.
        The long-awaited Cacti graph fix is now available! Follow the new blog post (from the front page):

        https://blog.jartweb.net/?p=3941

        Not sure how I fixed it, but it occured for me on an install-from-scratch and I managed to fix it.
        Full details of how to install software from scratch are now available in a new instructional Word document.

        SD card image will follow….
        Thanks!

        Reply
  8. Julio

    Hi:

    Thanks for sharing ;). I’ve followed your tutorial until page 32 and all works perfectly using a Raspberry Pi Model B. I found some considerations like time zone in php (I’m from Mexico), but it works fine :). Soon I’ll follow the next pages and I’ll test your procedure on a Raspberry Pi 2. I would bring the considerations I found. If you like I can send you information by email. Greetings!!

    Reply
    1. wpadmin Post author

      Hi Julio,

      Happy to include your suggestions.

      Have got the software working fine on a Pi 2 now that the python libraries for raspi gpio have been updated.

      Thanks

      Reply
      1. Julio

        Hi:

        After install cacti it is convenient to check time zone in php and if it’s necessary change it according to your country. In my case I’ve modified date.timezone = America/Mexico_City in the files /etc/php5/cli/php.ini and /etc/php5/apache2/php.ini (’cause I had a difference of 6 hours). I hope this be helpful to somebody.

        On the other hand, do you have any information about how to install plugins of cacti on the raspberry pi, such as “thold” or “nectar”? It would be helpful able to send emails when a certain temperature is exceeded and also annexed graphs. I tried to install the plugin “thold”, but I failed. I have little knowledge of linux :(, but I’m very persistent.

        I could not even test the procedure on a rasperry pi 2. I need some dupont cables. I hope to have the necessary material in a week. As I have tested you notice. Greetings!!!

        Reply
  9. Robert

    Hi, I have the same problem as Peter. I see graph but not data. Only N / A …
    The whole installation procedure I repeated two times but the result was the same 🙁
    Please help! Thank you!

    Reply
    1. wpadmin Post author

      I am a bit busy with work at the moment. When I get a chance I will either upload a working SD card image or follow the instructions from scratch to see where the problem lies. Thanks for your patience…

      Reply
      1. wpadmin Post author

        Hi all.
        The long-awaited Cacti graph fix is now available! Follow the new blog post (from the front page):

        https://blog.jartweb.net/?p=3941

        Not sure how I fixed it, but it occured for me on an install-from-scratch and I managed to fix it.
        Full details of how to install software from scratch are now available in a new instructional Word document.

        SD card image will follow….
        Thanks!

        Reply
    2. wpadmin Post author

      When you say that you see the graph but no data, do you mean the graph axes etc are there but no trend/data lines? Or are the graphs working fine but the the latest/max/min numbers below the graphs are missing?

      Cacti is the most difficult bit of the whole thing. I will try to replicate.

      Reply
      1. wpadmin Post author

        Hi all.
        The long-awaited Cacti graph fix is now available! Follow the new blog post (from the front page):

        https://blog.jartweb.net/?p=3941

        Not sure how I fixed it, but it occured for me on an install-from-scratch and I managed to fix it.
        Full details of how to install software from scratch are now available in a new instructional Word document.

        SD card image will follow….
        Thanks!

        Reply
  10. Christof

    Hi
    I followed the great instructions from Andrew up to page 15 where I first ran into errors reading out the temp sensors DS18B20.

    With the help of https://www.raspberrypi.org/forums/viewtopic.php?f=44&t=107606 I edited /usr/local/lib/python2.7/dist-packages/rpiSht1x-1.2-py2.7.egg/sht1x/Sht1x.py by adding at line 105: GPIO.setmode(gpioMode) and modify line 103: def __sendCommand(self, command, gpioMode = GPIO_BOARD)
    Now I get the correct raw data reading and the formatted output of the two DS18B20 by calling new_temperatures.py (page 25)

    By calling the script for the SHT75 sensor new_humidity.py in get the following errors.

    ERROR:sht1x.Sht1x:nack2
    ERROR:sht1x.Sht1x:nack2
    ERROR:sht1x.Sht1x:nack2
    Traceback (most recent call last):
    File “/usr/share/cacti/site/scripts/new_humidity.py”, line 14, in
    SHTdewPoint = sht1x.calculate_dew_point(SHTtemperature, SHThumidity)
    File “/usr/local/lib/python2.7/dist-packages/rpiSht1x-1.2-py2.7.egg/sht1x/Sht1x.py”, line 101, in calculate_dew_point
    return tn * (math.log(humidity / 100.0) + (m * temperature) / (tn + temperature)) / (m – math.log(humidity / 100.0) – m * temperature / (tn + temperature))
    ValueError: math domain error

    I can’t see any error in the calculation of the dew point temperature… any suggestion?

    Reply
    1. wpadmin Post author

      Hi Christof,

      I am slightly confused. The Sht1x code library is used only by the SHT75 humidity sensor and not by the DS18B20 temperature sensors. The temperature sensors use the 1-wire package whereas the SHT75 uses the Sht1x libraries – fixing the Sht1x code (which is a good thing to do!) should not have enabled the DS18B20 sensors to work.

      You can try following the updated installation instructions:
      https://www.dropbox.com/s/5gzl8mfj1chaxq9/R-Pi%20software%20install.docx?dl=0

      or download the software image ready to copy to SD card:
      https://blog.jartweb.net/?p=3941

      I think there are two issues happening in your code example – the ERROR:sht1x.Sht1x:nack2 suggests to me that the library is unable to communicate with the SHT75. The fact that there are 3 error messages probably relates to the 3 calls to code that read things from the sensor – each call is failing and giving an error message. The math domain error is probably then due to a divide by zero or similar error caused by failure to communicate with the device.

      One quick check – is the SHT75 hot to touch? If so, it is connected the wrong way round!
      I suspect, at this point, that the SHT75 is not correctly connected.

      Reply

Leave a Reply to wpadmin Cancel reply

Your email address will not be published. Required fields are marked *