For some time now I’d been wanting to look into the one
wire interface, mainly for the temperature sensors. There seems to be a lot of
experimentation and projects around using the Raspberry Pi, the Arduino and
plenty of free software to enable home automation and to take readings, but I
really wanted to play about with the code, in a Windows Environment, as part of a larger development
project and also use my SQL Server skills.
I’d also wanted to use this with my lab HP Microserver.
So let’s start! At the minimum for the hardware we’re going to need a host
interface and a sensor with the connecting cables.
Hardware
as it gave me a few more options later on with my set-up.You
can, of course, purchase the DS18S20 1-Wire Parasite-Power Digital Thermometer http://www.maximintegrated.com/datasheet/index.mvp/id/2815 and wire it up yourself but for
simplicity I wanted a plug and play solution at the beginning.
Software
Next we
need the 64 bit version of Java. The Java website will automatically direct you
to the version of the browser you are
running, usually 32 bit so we need to download the required version (assuming we're 64 bit, we are right?), 64 bit, from
Java Downloads for All Operating Systems http://java.com/en/download/manual.jsp
Next we need the Microsoft Visual J#® 2.0 Redistributable
Package – Second Edition (x64) which can be found at http://www.microsoft.com/en-gb/download/details.aspx?id=15468
Following the instructions and install the drivers before plugging in your hardware.
Development
Once we’ve got all this installed we can plug in our USB
Host Adapter and the cable and sensors. Next looking at the 1-wire SDK we can
examine the code in the GetTemps project.
In the excellent example we can see we declare the adapter
using the onewire reference and then
we get all the devices and the containers. Then if we have any containers,
we’ll check the container to see if it is of the Temperature type and then if
so we’ll issue a read command and get the state.
We can that wrap all this code
up nicely in a function returning a structure datatype and insert the returned
information into our SQL Server Table. We would setup a timer in the
application to run the polling every minute or so, but never more than every 15
seconds, not many environments change temperature that quickly.
In our
table we would use a 64-bit integer to store the 1-wire unique ID, decimal(5,4)
to store the temperature and a datetime2(7) to store the date time. Using small
fixed data types like these is not only good practice its essential if we are
using a limited version such as SQL Server express.
Now that we’re got the sensor storing data into our database
we may want to think of other, related, data sources that may be useful, such as
electricity usage or the weather.
My Takeaway: Get playing with technology!
Labels: 1-wire, home automation, sql, sqlserver