CDC Pi Weather Station Has A New Sensor The 1Wire DS18B20
Time to tackle the 1Wire sensor
After the furore of last weeks success we’re back down to earth with a bit of a bang. We’re now trying to code the Raspberry Pi so it will read, and more importantly broadcast the readings from the 1Wire sensors to Plot.ly, the MET office or an alternative means of hosting the data online. The 1Wire sensor is already waterproof and designed to run in series with other 1Wire sensors which would greatly help with the expansion plans for the project. Although the Bosch BMP180 and the associated code worked, the sensor isn’t designed to be outdoors and nor does it measure water temperature. So I’ve turned to the 1Wire system.
We’re introducing the 1Wire to i2c board into the mix. Hopefully allowing us to connect multiple 1Wire sensors using the RJ12 socket. Previous coding was fairly rudimentary as a lot of the compiling had been done for us. This new endeavour requires our own interpretation of the code and its application. So I’ve tidied the desk – when ever I really need to concentrate I always start by cleaning the studio.
This is a screen grab of the code compiling the 1Wire File System. We actually managed to get this running by connecting to the Pi remotely using SSH. Something we’ll need to do once the Pi is installed at the Creekside Discovery Centre. Apart from the fact that visiting the site to check or make changes to the code will be inconvenient, there isn’t going to be a monitor attached to the Pi so remoter access is the only sensible way to control the device.
A simple bit of Python code meant we could read the sensor data. This code was reading from the Pi’s ROM so although we’re getting real time readings, the data isn’t being stored yet, let alone uploaded. Still, seeing the new sensors working was a huge relief.
#!/usr/bin/env python # owtemp.py. This script displays the measured temperature in C in the terminal window. # The update interval is based on the variable on line 22: time.sleep() import time import os # The file name and path uses the unique id generated by the onboard ROM on the sensor. file_name=os.path.join("/","mnt","1wire","28.AF6A9E050000","temperature") # The code isn't actually 'opening' and then 'closing' a file as the syntax suggests # The 'file' is actually a path to the Raspberry Pi's ROM while 1: file_object=open(file_name,'r') line=file_object.read() print(line+'C') time.sleep(5) file_object.close()
The result is a continuous data stream of temperature readings from the 1Wire DS18B20 with an accuracy of ±0.5°C Accuracy from -10°C to +85°C. Having spent a significant amount of time in Terminal (and loving it) I’ve become accustom to using commands such as…
- pwd – Present Working Directory – really important to know where you are when executing code.
- cd CDC – Changing the directory to the location of the code
- ls – Checking that the files you need are actually present in the directory you’re in.
The slight increase in the temperature is simply a result of me holding the sensor in the palm of my hand for a moment.