Category Archives: Uncategorized

Reading multiple DS18B20 sensors

Some people have been asking for some Python code that can read multiple temperature sensors.
The following code will work for up to 10 sensors (maximum allowed under the kernel driver)



#!/usr/bin/env python

from os import path, listdir, system
from time import sleep


# Borrowing some elements from https://github.com/timofurrer/w1thermsensor/blob/master/w1thermsensor/core.py
THERM_SENSOR_DS18S20 = 0x10
THERM_SENSOR_DS1822 = 0x22
THERM_SENSOR_DS18B20 = 0x28
RESOLVE_TYPE_STR = {"10": THERM_SENSOR_DS18S20, "22": THERM_SENSOR_DS1822, "28": THERM_SENSOR_DS18B20}
BASE_DIRECTORY = "/sys/bus/w1/devices"
SLAVE_FILE = "w1_slave"

# function to find how many sensors are connected, works for 3 types of sensor
def get_available_sensors():
   types = [THERM_SENSOR_DS18S20, THERM_SENSOR_DS1822, THERM_SENSOR_DS18B20]
   is_sensor = lambda s: any(s.startswith(hex(x)[2:]) for x in types)
   return [(RESOLVE_TYPE_STR[s[:2]], s[3:]) for s in listdir(BASE_DIRECTORY) if is_sensor(s)]

# function to return the initial 2 letters of the device ID ("28" for DS18B20 sensors)
def slave_prefix(mytype):
   return "%s-" % hex(mytype)[2:]

# function that grabs the raw temperature data from a sensor, requires full path+filename
def read_temp_raw(device_file):
   f_1 = open(device_file, 'r')
   raw_reading = f_1.readlines()
   f_1.close()
   return raw_reading

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

for sensor in get_available_sensors():
   sp = slave_prefix(sensor[0])
   sensorpath = path.join(BASE_DIRECTORY, sp + sensor[1], SLAVE_FILE)
   print("Sensor %s has a temperature %.3f" % (sensor[1], read_temp(sensorpath)))

Solving Cacti problems on the logger – full s/w install details & SD image

Some people have been having problems getting the Cacti graphs on the data logger to update correctly – they are missing graph lines and numerical outputs. I remember having similar issues when I was first messing around setting up the loggers. So to try to fix this, I went through a complete software install from scratch starting with a completely fresh Raspbian install on a bare Pi to which I connected the various items of hardware.

It is a tedious process going through a full install AND documenting it in detail at the same time!

Anyway I did it, and I also encountered the issue of non-updating Cacti graphs. After a bit of fiddling around, I managed to get them working. Not sure exactly what step of the debugging was responsible, but the various steps were all recorded. So if you follow these instructions:

https://www.dropbox.com/s/5gzl8mfj1chaxq9/R-Pi%20software%20install.docx?dl=0

you should be able to get to the point I have here – a working Pi reading and graphing the temperature and humidity data. I also include details of installing the LCD driver script and the code for the pressure sensor, but these are optional.

If you still struggle to get it to work and can withstand a 1.2 GB download, I have placed a ZIPped copy of the SD card image on OneDrive:

https://onedrive.live.com/redir?resid=2E3466E51A503CA7!3241&authkey=!APvujrDX8j05ggc&ithint=file%2czip

Thanks for your patience!

Andrew

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

Main site moves into the blog

I decided that the main Jartweb site (actually on the same servers) was looking a bit old and tatty, plus it did not behave properly on browsers on smartphones and tablets, so I decided to copy across the old content into the blog, creating new pages for each item.

When I get a moment, I’ll replace the original pages with redirects to the new blog pages and hopefully work out how to do the photo galleries properly as they don’t work at all in mobile browsers as they use a whole range of outdated tech.

Fingers crossed!

Warm May Bank Holiday weekend again

Is the weather this year repeating that of last year?

In preparing this post on the latest riverside walk, I noticed another post from almost the same date last year. The similarity being the nice sunny warm weather in May. This time last year the weather was nice and sunny in early May then cooled off and became rainy for early June. Will it do the same again this year?  The BBQ planners need to know…

Raspberry Pi temperature logger

Raspberry Pi (boxed) temperature logger

OK, so a while ago I built a little temperature measuring system using an Arduino Uno, an Arduino ethernet Shield, a real time clock module and some digital 1-wire temperature sensors. I hooked up a serial interfaced LCD display to display information such as IP address, time, date and the temperature readings and sat it running and logging data to a micro SD card.

Where I left the post, I was planning on implementing more functions to the system, basically to serve web pages containing graphics (temperature charts) as well as store data and to implement an email alerting system.

But I ran into a basic problem that is the limited memory on the Uno. From yesteryear I remembered doing some Z80 assembler programming and writing some BASIC programs on a Sinclair Spectrum with only 32 kB of memory. I think that probably led me into a false sense of freedom on the Uno; with my skills limited to programming in wiring, I just could not cram in all the functions I wanted on this small device.

Truth be told, the Uno was really a stopgap whilst I was waiting for my Raspberry Pi to be delivered.

Well, now it’s here, or rather they are here, as I ended up buying two. Well, actually three, but the last one is owned by work …

I decided that the best way forwards with the higher resolution temperature logger/server project was to do it all on a Raspberry Pi. I bought one to start playing with and then realised that some of the configurations I had set up on the Pi had to be changed to trial alternative software hardware solutions and rather than keep changing the same single Pi, I bought a second one to experiment with. This also let me take a Pi away for Xmas leaving the other one running 24/7 to continue logging, as a sort of unattended test.

Having arrived at the optimum software/hardware configuration, I got work to order the various parts from their main electronics supplier (RS) and then built a copy if the optimised system for them, modifying the SD card contents to match the IT systems at work (and getting permission from the IT department to connect the device). So now three Raspberry Pis are running different versions of my system, two at home and one at work. All three log temperatures. One logs my router and itself (!) as well. That was my first Pi and the one that I crammed with most trials.

All three Pis are running Raspbian, generally the latest version (2013-02-09) but Pi1 is lagging slightly behind on the updates. For some reason, I had issues getting it to accept the main updates via sudo apt-get upgrade – it tended to red-light (the Pi’s version of BSOD) after restarting. I might try another upgrade soon after taking an SD card backup.

Pi1 is running MRTG and CACTI, for data capture. It interfaces to a pair of DS18B20s via a breadboard and to the same Sparkfun serial interfaced LCD (PPLCD-09395) that I used on the Arduino.

Pi2 is running a modified version of the code from NETTEMP for data capture, and display. A single DS18B20 is interfaced using a 1-wire to USB adaptor from Sheepwalk (model: DS9490R). At the moment, NETTEMP requires a device like this since it relies on Digitemp for its 1-wire interfacing. It also runs an experiment using Dygraphs to produce a zoomable temperature graph.

Pi3 is running CACTI for data capture, a pair of DS18B20 temperature sensors (RS: 5402805), a 16 x 2 LCD display (RS: 5326436) and sits inside a slightly too large plastic box (RS: ) which our nice engineering team machined to fit the ethernet and USB sockets of the Pi and a cutout for the LCD screen. A couple of extra holes for the sensor cables and a hole for the USB power lead complete the hardware. Inside the case, the LCD is connected to the GPIO using a 26 way ribbon cable and push fit IDC connector. Several of the ribbon wires are separated (torn away…) to connect to the DS18B20s in a rather amateurish way. So I don’t think I’ll be showing pictures of that bit of the build !

More about the choices behind the various hardware/software options will follow in a later posting. Essentially, I was trying to find a system that had all the functionality I needed but that would also be robust, maintainable, scalable, and usable by people other than me.

PS if anyone is looking for a nice engineering/kickstarter project for the Raspberry Pi, I would love to see the following case:

Big enough for a Pi and a display (e.g. 16 x 2 LCD, 16 x 4 LCD, small display screen).
Nice material, perhaps aluminium.
Ability to mount to something (e.g. magnetic back plate, plus wall mounting screw holes).
Integral PSU or moulding for USB power lead to the socket on the Pi with cable relief gland.
Light pipes for the LEDs.
Some form of breakout connector for the GPIO pins.


There are several nice cases out there, but few of them are much bigger than the Pi itself, meaning there is no room to mount other items in the case, such as an LCD display and the necessary cabling. I really would like a case that fits all of the above hardware inside in a neat and tidy fashion, and lets the case mount somewhere. For home use, I would like a really nice looking case, perhaps something like the Proto Armour Raspberry Pi Enclosure Kit which has almost everything I want, except the extra room for the display, same with this case from PiHolder.com (both these are USA based but ship to Europe, but I’m wary of the import duties and tax on such items).

 

Anyway, time for a few pictures from the logs.

 

Monthly temperature plot from modified Nettemp code - click to enlarge

 

2 DS18B20 temperatures tracking each other (from Cacti)

 

snr Traffic Graph

MRTG's ADSL modem SNR ratio - oh dear, what happened at 7 pm?

 

 

Raspberry Pi - load average (from Cacti)

 

Finally a rather large graph generated by Dygraph, based on a simple text file data store, updated every minute:

 

Dygraph temperature plot - click to enlarge