1-wire interface for temperature sensors

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

We'll need the host interface and I sourced the DS9490R USB Host Adapter from Sheepwalk Electronics at http://www.sheepwalkelectronics.co.uk/product_info.php?cPath=22&products_id=29



DS9490R
The DS9490R USB to 1-Wire Adapter

We’ll also need a cable connecting the RJ11 to RJ45 http://www.sheepwalkelectronics.co.uk/product_info.php?cPath=26&products_id=35

and a temperature sensor. For the sensor I picked the SWE2 Sensor Connection Module http://www.sheepwalkelectronics.co.uk/product_info.php?cPath=23&products_id=54

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

If you don’t already have Microsoft Visual Studio installed then we’ll get ourselves the free Express Version of Microsoft Visual Studio (Express 2013 for Windows Desktop) from http://www.microsoft.com/en-gb/download/details.aspx?id=40787

Also you can obtain the free SQL Server Express, if required, from http://www.microsoft.com/web/platform/database.aspx

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

Now we’ll go on and get the 1-Wire Drivers from http://www.maximintegrated.com/products/ibutton/software/tmex/

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: , , ,