Rakesh George is a Windows and Windows Phone application developer at IdentityMine and has helped develop a number of popular Windows applications. He is passionate about technology, and recently has been experimenting with IoT (Internet of Things) and Windows using Raspberry Pi. Rakesh is also an avid photography enthusiast.
Editor's Note: Sadly, soon after creating this article Rakesh died in a tragic accident.
March marks the beginning of summer here in India. Days when people prefer not to go out too much and get burnt by the scorching Sun. Things were no different at home. Discussions got heated up on how to bring down temperature inside the house. That's when I thought to myself, what exactly is temperature around me? Thoughts went wild and I finally decided to make something myself, all I have is my Windows Phone and a Raspberry Pi.
My plan was to make a temperature monitor using Pi, which would be on the entire day. The data would be periodically sent to the cloud; a client application for Windows phone and tablet would retrieve the data from the cloud and plot the results. Let's have a look at the overall plan.
Raspberry Pi has made a revolution in the field of Internet of Things. A credit card sized computer with decent specs, Pi runs on a light weight version of any Linux OS (and Windows 10 too from RPi 2 onwards) and has a set of 26-40 pins or more known as GPIO pins with which we can connect to various devices and wide range of sensors.
The cloud is the new buzz word today. Everything, from data storage to computing moves to cloud. Azure Cloud service provides a wide range of services in which we make use of its Table Storage service.
From the local electronics store, I got the rest of the components, an LM35, ADC 0804 and a couple of resistors and capacitors. LM35 is an analog sensor which gives an output voltage corresponding to the atmospheric temperature where it's kept. Unlike the Arduino boards, Raspberry Pi models (I use a model B+), doesn't have any inputs for analog voltages, so we have to convert the sensor's output to binary data before sending it to Pi. ADC 0804 (Analog to Digital Converter) does that job well. Having all the components, the hardware part details looks like this:
I haven't yet found a better tool to draw the schematics. A clearer, hand drawn version can be found along with the source code.
In a brief, the below given algorithm is used to read data from LM35 using the ADC, the pins that are mentioned are those of the ADC.
- Make pin 1 (CS') = 0. This step is of importance only when we have multiple ADCs in a circuit. Since we are only working with one, I've connected this pin to ground.
- Send a high to low pulse to pin 3 (WR'). This marks the beginning of one read cycle. WR stands for Write.
- Keep checking the value of pin 5 (INTR'). When its value becomes 0, the conversion is done. INTR or interrupts are analogous to events in C#.
- Send a high to low pulse in pin 2 (RD') and then the binary 8 bit data is available in the pins 11-18, one cycle of conversion is complete. RD stands for Read.
You can check more on this part here. As you can figure out, pin 6 is the analog input pin, pins 2, 3 and 5 are the control pins and pins 11-18 are the data pins for the ADC. You might have noticed those colons next to WR', RD' and INTR'. These inputs are active low pins. Wikipedia has a very good explanation about them.
Raspberry Pi does the job of implementing the above said algorithm, and then send the data being read to an azure table storage. I've used python for coding in Raspberry Pi since it's easier to learn and is the recommended language for Pis. I've included some pictures of how the hardware looks. Those jumper wires are such a mess!!!
I've used a combination of Python Tools for Visual studio and Sublime Text for Python development. Python Tools for VS is an awesome plugin that converts Visual Studio into a powerful IDE with all IntelliSense and many other features which aren't available in conventional Python IDEs like IDLE or Eclipse. You can even debug Pi straight from VS but with a bit of tweaking (I couldn't use this feature due to some other issues). Sublime text on the other hand is an advanced notepad app with syntax highlighting and many other features.
Azure Table Storage
I've used Azure as our cloud platform that sits in between the Pi, and the apps. Azure is easy to use and what's really cool is that they've provided an easy to use python module. Since I'm using Python to code in Raspberry Pi, this module came in handy.
Azure service comes with Table Storage, a relatively easy method to store large chunks of data, especially if you need something that's not as complicated as SQL storage. In fact it doesn't even involve the use of SQL or its complicated table structure. Each chunk of data or what Azure calls Entity is characterized by two properties, namely PartitionKey and RowKey, and that's it J . More on these can be found online.
Our Python code has three modules:
Sensor.py as the name implies handles reading the data from the sensor (via ADC) and AzureModule handles the cloud. It's the logger module that handles both these modules for the entire run time. The Pi sends data to the cloud every 15 seconds. Raspberry Pi runs on a light weight Linux version named Raspbian, which comes with two versions of Python preinstalled, Python 2.x and Python3.x
The latest version of Azure Python module runs only in Python 3.4 but since I had issues with configuring Python 3.4, we use an older Azure Python module (v 0.9.0) in Python 2.x. I've detailed more on how to install this particular version, along with the Python source code. You can find the link to the same at the end of the article.
The applications are designed in such a way that they give an overview of the recent temperature changes in the past 5 minutes up to 24 hours in the form a graph. The time interval between the readings can be varied from 1 minute to 1 hour, such that if I need a granular level analysis of the changes, I can opt for 1 minute interval or if I just need a synopsis for the changes in a longer period, it's better to select the 1 hour interval. Along with this, the applications also display the latest temperature read by Pi.
The universal application template for Windows 8.1 in VS proved to be the best solution for the applications. One template and two applciations. Shared code content can be maximized to a great extent. Both the applciations have just a single page. XAML UI is the only part which differs for the applications. Even the code behind files for both the pages have been moved to the shared project.
For the graphs, I've used Telerik controls which are beautiful and fits the modern UI. I've used a 30 day trial pack (Telerik UI for Windows Universal) so those of you who intend to try out may want to install it.
Visual Studio Online proved to be another great tool for project management and source control. It provides the same familiar TFS experience as in the company, hence managing the source code was nothing new.
The final UI of the applications looks like this, along with data read from Pi for a time period of 12 hours.
Note: I did find a difference in the measured temperature sometimes, up to +2 degrees. I used Google weather and an analog thermometer as a reference. I haven't yet explored into what the reason would be. This hasn't been tested in a controlled environment at the time of writing this blog.