Raspberry Pi Meets MyM2M Web
Published 03 November 2014 by Trevor O'Grady
While moving office I came across the Raspberry Pi I bought a few years back for my son in the hope that this would start his path to technical guru and I could retire early.
Other than me excitedly firing it up when it arrived, it never got used so I guess I need to find alternative retirement arrangements. Kids have so much awesome technology these days that stuff like this does not really excite but I digress. I blew the dust from the box and while opening it I thought this would make a cool demo of how easy it is to connect a device to our M2M/IoT platform called “MyM2M Web”.
I also found a Dallas “1-Wire” temperature sensor (DS18B20) that I had used on another project. These devices communicate over a one wire multidrop bus with each device having a unique 64 bit address burned into it. Part of the protocol allows you to discover all addresses connected to the bus. They are pretty common and easy to get a hold of.
Right, I thought, it can’t be hard to write a python script on the Raspberry Pi to collect some temperature readings and send them to MyM2M Web using our REST like API interface.
MyM2M Web is a M2M / IOT web platform and described in more detail here. Have a look at the brief demo video. What we are going to do here is record temperature readings and then every so often send those temperature readings to the web site so we can see some temperature graphs and get some alert emails when temperatures stray above or below defined thresholds.
I went hunting for 1-Wire drivers for the Pi. It turns out this is integrated into the Raspberry Pi kernels by default and if not already installed it’s all easy to find online how to install what’s needed.
Next I found a piece of breadboard and wired up a DS18B20 to the Raspberry Pi like so:
The python script I constructed can be obtained via Git here: https://bitbucket.org/trevor_ogrady/rpi_temperature_demo_client.
Feel free to modify and of course please send me a pull request if you make a change/fix that you think should go into my version. You only need the one file to run it i.e. you don’t need the unit testing script.
On the MyM2M Web platform the only configuration I did was to set the alias for data id 1 to “Temperature” and set the units to “℃”. I also decided to set some thresholds so that we get notified (email and SMS) when alarm events occur i.e. temperature going above or below certain values.
Here is the email that arrived from MyM2M Web when I forced a high alarm (by exposing the DS18B20 to a little heat gun treatment). The most recent event is first i.e. the alarm clear event. Note that if these events had occurred over a longer period a separate email for each would have occurred but I have my MyM2M alarm emails set to group events together over short periods - this is configurable in my user settings on MyM2M Web.
Clicking on the high alarm event brings us to alarm events page below (via a login if you are not already logged in).
Above you can see the temperate breech the high alarm threshold of 25℃ (red line). The data point that caused the alarm event is highlighted (red dot). If we now click on the clear alarm event (the most recent event in this case) we get the graph below.
Above you can see the temperature falls below the high threshold but this but does generate a clear alarm event and even though the temperature rises above the high threshold a seconds time there is no alarm event. It only clears the high alarm when the temperature falls below the “high clear” threshold point of 24℃ (amber line). Again the point that caused the alarm event is highlighted with a red dot. This hysteresis in the thresholds is almost always desirable in real world applications as we don’t want too many alarms raise and clear events when the input is sitting around the threshold. In this case the temperature has to drop at least 1℃ below the high threshold and then exceed the high threshold again before generating another high alarm event.
While measuring temperature isn’t mind-blowing, this demo does demonstrate how easy it was to send some useful data to the MyM2M Web platform. The python script was knocked together in an afternoon by a guy who is more at home with embedded C and hardware design than python scripting. I’d welcome comments and feedback.
Note that we also have proprietary “lean” and highly scalable server solutions not based on HTTP for applications that need to work with very small amounts of data e.g. low data mobile tariffs. Contact us to discuss.