The Weather Outside Is Frightful (Or Is It?)

Blistery cold weather is starting to sink in, which ought to ignite an instinctual desire to get your house in order and monitor it so the water pipes don’t freeze and burst. So, we’ll take a timely look at a project setting up some temperature probes in various areas, reading them, and reporting in a custom dashboard. A true home automation expert would take things further by setting up relays to turn on heat tape or even maybe some actuators to control water flow. Maybe next year, but for now, we just want to be able to monitor important areas of our home (in this case a cabin in Montana) and understand temperature patterns over time for better planning. As with most projects, there is more than one solution, especially if it depends on what you have on hand. At the time, the list of things in hand were as follows:

  • 4x Temperature sensors (Thermocouples)
  • 1x RS-232 Thermocouple Module, specifically the DGH D5331
  • 1x TS-7500 Single Board Computer with TS-752 and Enclosure
  • 1x USB Thumb Drive for storage
  • Various wiring, including Ethernet
  • 1x DB9 Male to RJ45 Female Adapter
  • 1x Web server (desktop, virtual machine, or cloud based)

If you’re interested in doing something like this, I’d look into using any temperature sensor that uses the DS18B20 IC (or something like it) and use the TS-1700 temperature sensor module with modbus and TS-7680 single board computer. You’d save some money and double the number of temperature sensors you could use.

Overall, this is what we’re looking to build:

Four temperature sensors connected to a thermocouple module that is read using a single board computer acting as an SFTP file server. A web server in a different location is able to access the file server to store temperature data in it’s own database and present a nice looking dashboard to an end user. This project guide is going to briefly skim over the hardware setup, highlight a python script that reads temperature data over RS-232, and spend some time on setting up an HTML dashboard using Google Charts. Hopefully it’ll inspire your next project.

Setting up the Hardware

One of the first considerations was where the temperature sensors should be located in order to provide the most valuable data. In our case, which is in a remote cabin, we wanted to monitor both the inside and outside temperature as well as under the floor where the pipes are located and the coldest bathroom.

Once the temperature probes were installed in those key locations, wires were run into a closet where the RS-232 thermocouple module and TS-7500 is housed. The wiring here was pretty straightforward, especially when you consult the manuals, so there’s not a lot of value in going into detail here. We used some RJ45 wire and jacks to make things as clean as possible and hooked our thermocouple module to the TS-7500’s RS-232 port using the DB9 to RJ45 adapter.

Another consideration you might want to keep in mind is power and Internet connection reliability. This project was setup in a remote area which is more or less off the grid and very prone to power and Internet outages. So, we hooked everything up to a battery backup. Not only that, but given the nature of Linux and power outages, we also setup the TS-7500 to boot from a read-only filesystem (after setting up our software). That’s why we’re using the USB thumbdrive to store our periodically collected temperature data to. We could technically just poll for temperatures when we want them, and then store them directly into our website database, but then we might miss collecting that historical data. A final consideration is to make sure to setup port forwarding on your router for SSH (port 22) and then use a service like dyndns.org to make it easy to access this server from the outside world.

Setting up the Software

The next step is to come up with a script to read the temperature data coming from the RS-232 port. Python lends itself very nicely to this sort of thing, so that was the chosen solution. The script opens the RS-232 port (using xuartctl), sends a read command (RD) to the thermocouple module, reads the result, converts it, and then saves it off to a CSV formatted file on the USB thumbdrive. Here’s what that script ended up looking like:

get-temps.py

Okay, great. With the script written and working, you can easily schedule it to run every hour as a cronjob. Before you configure the system to boot from a read-only filesystem (if you chose to), make sure you have SSH setup correctly and that you’re using a static IP address. This will be necessary for us to collect the data from our web dashboard server. Otherwise, that takes care of this server (we call it the temperature server). We can confidently leave and lock the closet door if needs be. The TS-7500 is rated to operate between 0 °C (32 °F) and 70 °C (158 °F), but has been tested to withstand temperatures down to -40 °C (-40 °F). This particular project has been installed and operating since 2011 and has successfully withstood the harsh Montana winters, with lowest recorded outside temperature reaching -40 °F (the highest temperature recorded is 95 ºF, for those who are curious).

The next part of the project puzzle is getting a web server setup that will collect the data from the temperature server, store it, and display it in a nice dashboard.

Setting up the Dashboard

For this step, you’ll need to setup a web server. For our sake, with unreliable power and connections in a remote area (not to mention dial-up like Internet speeds), we chose to setup a server at a different, more reliable location. Technically, you could run a webserver right from the TS-7500 and have an all-in-one, low power, simpler solution. You’ll need to decide which path to take, but for this project, we used a separate server. As long as the server has the ability to write to a datastore/database and run a scheduled job, you can use anything you’d like, beit a physical dedicated box, a virtual machine, shared host, or cloud solution.

The general steps are to regularly (scheduled cronjob) download data from the temperature server using a script and SSH, store that data into a MySQL database, use a PHP script to read that data, parse and massage it into JSON format for Google Charts API to understand, and finally display it on the webpage.

Admittedly, there is a lot to this step. As a reminder to keep the incentive going, the reason we’re doing all of this is to be able to better prepare for the quickly upcoming cold months given historical data. For us, it answers the question of “when do we need to start worrying about winterizing”? For you, it might be something different, in which case you can adjust as necessary. Here’s a look at the dashboard we’re striving for:

Database

The first thing we need is a database to store the data. MySQL was chosen since it was a familiar tool. The database ‘cabinstats’ and table ‘cabintemps’ was created to hold temperature data.

Scheduled Downloading Script

The next thing we need is a script to regularly fetch the temperatures from the temperature server. Again, Python made fairly quick work of this task. It’s setup to run every hour or so. Here’s what our script ended up looking like the following.

get-cabin-temps.py

Dashboard Code

Let’s take a look at the code behind the dashboard. It’s a pretty typical HTML/Javascript/PHP setup, where the javascript calls the PHP functions to get the massaged data for use with Google’s Chart API, and then draws the charts in the appropriate DIVs in HTML. There is a ton of room for improvement here, but it works. Here are the main code snippets responsible for displaying the data, broken into HTML, Javascript, and PHP sections.

HTML

This is all there is to this page. Just a few elements, with the <div>  elements being the most important. These each have their own id that the javascript uses to know which div content to update.

Javascript

Diving into the next layer of our website code, we see the javascript responsible for loading the Google Charts API and using it. There are a couple of inline PHP calls that will insert the JSON data directly into the script, making things fairly clean.

PHP

Lastly, here’s the PHP code that reads from the database and massages the data into something Google Charts can understand. Notice I’m not providing all the code for a few reasons: 1.) the code was written back before I knew better than to use the deprecated mysql_query() PHP calls instead of PDO, 2.) it’d be overwhelming, 3.) it’s mostly just helper functions anyways. Just pay attention that I’m passing in a $db  object that was instantiated earlier in the PHP script using $db = new DB(); . Other than that, there’s not much else to this code.

Hopefully, this helped you to have a basic understanding of the different code parts that make up the web dashboard. Again, we have a script to download the raw data and store it into a database and we have a webpage that will read from the database and display it in a human-friendly way. This was the final piece in completing our project.

Conclusion

This project was an interesting and rewarding learning experience. We were able to come up with a robust solution that makes planning and monitoring a home easy and meaningful. The ability to view current conditions of your home (or cabin, in this case) from anywhere in the world is incredibly convenient and helpful for planning so your pipes don’t burst. It’s also handy in situations where high temperatures are a concern, like pets or livestock in kennels or barns. Naturally, there’s more than one way to do this, some easier than others. For example, if I were to do it again, I’d use more AJAX calls for real-time temperature polling instead of scheduled. I’d encourage you to come up with your own solution that meets your requirements and suits your particular situation using what you’ve learned from this project and then build upon it. This has potential in both personal and industrial business applications, such as home automation, so feel inspired and start building!

If you have any questions or comments about the project, or if you have ideas to improve it, please be sure to use the comment form below.

Author: Derek Hildreth

eBusiness Manager for Technologic Systems