Tuesday 13 January 2015

Raspberry Pi - 1 Wire OWFS i2c bus

In this basic How-To I work through the software configuration steps I use so that the Raspberry Pi can read values from a 1-wire DS18B20 temp sensors. The basic Raspberry Pi is fully operational with the latest fully updated Raspbian image. It is possible to interface directly to the GPIO pins, which I tested on a breadboard before deciding to go with the dedicated add on module (the RPI3v1) from Sheepwalk Electronics, which provides me with a convenient connection options given I have Cat5E wiring throughout my house. Sad I know, but useful none the less. :) The first thing we need to do is to enable the required kernel nodules. To achieve this across reboots insert the lines below in the /etc/modules file:
i2c-bcm2708
i2c-dev
If ic2detect isn't already installed it is available through the standard repository. Whilst we are installing the i2c-tools we will install the owfs and ow-shell packages as well:
sudo apt-get install i2c-tools owfs ow-shell
The command below will probe the i2c bus. Change the value of -y accordingly (0 = RPi-V1 1 = RPi-V2):
pi@rasp-owfs ~ $ sudo i2cdetect -y 0
 0 1 2 3 4 5 6 7 8 9 a b c d e f
 00: -- -- -- -- -- -- -- -- -- -- -- -- --
 10: -- -- -- -- -- -- -- -- -- -- -- -- 1c -- -- --
 20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
 30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
 40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
 50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
 60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
 70: -- -- -- -- -- -- -- --
 pi@rasp-owfs ~ $
Edit the /etc/owfs.conf file as appropriate below:
server: FAKE = DS18S20,DS2405
 #server: FAKE = DS18S20,DS2405
 # Chose appropriate line below based on rev1 or rev2 RPi
 # server: device=/dev/i2c-0
 # server: device=/dev/i2c-1
Assuming everything is operational and configured correctly, restart the service and check to see the connected 1-wire sensors are being read.
pi@rasp-owfs ~ $ sudo service owserver restart
[ ok ] Restarting 1-Wire TCP Server: owserver.
pi@rasp-owfs ~ $
pi@rasp-owfs ~ $
pi@rasp-owfs ~ $ owdir
 /28.BAB11E050000
 /28.69EF1E050000
 /bus.7
 /bus.6
 /bus.5
 /bus.4
 /bus.3
 /bus.2
 /bus.1
 /bus.0
 /uncached
 /settings
 /system
 /statistics
 /structure
 /simultaneous
 /alarm
pi@rasp-owfs ~ $
pi@rasp-owfs ~ $
pi@rasp-owfs ~ $ owread /28.BAB11E050000/temperature ;echo
23.9375
pi@rasp-owfs ~ $ owread /28.69EF1E050000/temperature; echo
23.9375
pi@rasp-owfs ~ $
We have now got 2 sensors plugged in and being read, they even agree and report the same temperature, which given they are next to each other is hardly surprising. Wouldn't it be good though to change the long hexidecimal strings so we can refer to the sensors using a location name. Fortunately with a bit more editing that's exactly what we can do. Work out which one you wish to name first by simply removing the other one. When we down run the owdir command the only one listed will be that of the one to rename.
pi@rasp-owfs ~ $ owdir
 /28.CE1613050000
 /bus.7
 /bus.6
 /bus.5
 /bus.4
 /bus.3
 /bus.2
 /bus.1
 /bus.0
 /uncached
 /settings
 /system
 /statistics
 /structure
 /simultaneous
 /alarm 
pi@rasp-owfs ~ $
The observant may have noticed that this is a different sensor to those addressed before. This is a waterproof one that is mounted outside in a wind sheltered spot. It should be noted though that using this method to create alias breaks the built in web server front end to owserver. If you use that interface such as browsing to http://yourIPaddr:2121, then this isn't for you. I am sure it can be solved, I've just never needed to so haven't looked into it.So to proceed, simply create a owfs.alias file in /etc with the format as below:-
pi@rasp-owfs ~ $ sudo vi /etc/owfs.alias
 28.69EF1E050000 = datacab1
 28.D9301F050000 = lounge1
 28.CC871E050000 = loft1
 28.A8A21E050000 = kitchen1
 28.BAB11E050000 = bedroom2
 28.EB4C1E050000 = bedroom1
 28.1ADE1F050000 = office1
 28.22A61E050000 = garage2
 28.CE1613050000 = outside
Now the alias file has been created, we need to instruct the owserver to use those names. Simply edit the /etc/init.d/owserver file find the section below (not far from the top) :-
d_start() {
 [ -d $PIDDIR ] || {
 mkdir -m 0775 -p $PIDDIR
 chown root:root $PIDDIR >/dev/null 2>&1
 }
 start-stop-daemon --start --quiet --oknodo --exec $DAEMON -- -c $CONFFILE \
 --pid-file $PIDFILE
 # ensure the daemon has been started
 sleep 1
 pidofproc -p $PIDFILE $DAEMON >/dev/null
 }
Under the start-stop-daemon section we need to change it to as shown below for it to use the alias file we created earlier.
start-stop-daemon --start --quiet --oknodo --exec $DAEMON -- -c $CONFFILE \
 -a /etc/owfs.alias --pid-file $PIDFILE
Once we have updated, either reboot the RPi or, issue the following command:-
sudo /etc/init.d/owserver restart
Personally I would recommend a reboot, but either should work as shown below.
pi@rasp-owfs ~ $ owdir
 /outside
 /bedroom2
 /datacab1
 /bus.7
 /bus.6
 /bus.5
 /bus.4
 /bus.3
 /bus.2
 /bus.1
 /bus.0
 /uncached
 /settings
 /system
 /statistics
 /structure
 /simultaneous
 /alarm
 pi@rasp-owfs ~ $
 pi@rasp-owfs ~ $ owread /outside/fasttemp;echo
 3
 pi@rasp-owfs ~ $ owread /outside/temperature;echo
 3.1875
 pi@rasp-owfs ~ $
That's all folks. Feel free to leave a comment.

No comments:

Post a Comment

All comments made are subject to moderation.