Friday, September 26, 2014

Project follow-up: Raspberry Pi with 433 MHz radios, mqtt and node-red

Now that my Arduino sensors to Raspberry Pi using 433 MHz radios project is done and I learned a lot about posting to the web and using a db, it was time to learn something new; mqtt is a very hot topic in the IoT world so this was my next target. Using an MQTT broker like mosquitto is not only cool but allows for decoupling of the different parts; for example, instead of writing a monolithic piece of code that does everything (read sensors, post to the web, save to a database, like in my previous project) and which needs a lot of work in order to add some new functionality, one could write a piece of code that for example, only reads sensors and publishes to an MQTT topic; then another piece of code can subscribe to that topic, get the sensor values and post them to the web; and yet another piece can subscribe to the same topic, get the values and save them to a database. And so on, the possibilities are endless and different parts of the system are independent and can be plugged in and out very easily. And since it is very easy to install mosquitto on a Raspberry Pi, I decided to go ahead.

To integrate mqtt in my C code I used mosquitto client library (libmosquitto) with some docs here: not a lot of help for a beginner but I found plenty of articles and example code. I ran into a lot of issues because the library that I installed was older and none of the example code worked; Roger (mosquitto's author) was kind enough to point me to a page explaining how to install the latest library so I installed libmosquitto-dev from this repo and it solved my problems. I also needed to add -lmosquitto to the Makefile.

After this code was done (if you are interested you can find it here), I had to decide what is the next piece so I decided on node-red: a chance for me to dig deeper into this cool technology and add to my experience from my previous projects. As Simen Sommerfeldt so eloquently makes the case in this great article, node-red is perfect as the "glue" between different pieces of code. To start with I decided to create a flow that reads sensor motion data from the MQTT broker and plays a sound via a python script. I already had a script but Simen's was much better so I used his - big thanks goes to him for sharing his work. The node-red flow was done in minutes; I then added 2 exec nodes to start the C code reading sensor data and the python script. As a side note, at this time I am running this exec nodes manually instead of starting them automatically when the flow starts, until I can figure out how to prevent multiple instance from running at the same time. This project is by no means as cool as Simen's moving skull but I will use as my Halloween project to play some random scary sounds when trick-or-treaters come to the door.

While I was working on this project I got an email from the great folks at dweet.io and freeboard about some great features they added recently. When I saw their email I decided to add another branch to my flow to get the sensors data and post it to dweet.io. Node-red is so awesome that I had all this done in a matter of minutes: I found a dweetio node-red node, installed it, added the nodes to post to dweet, and I also had a quick freechart done. The node-red flow is also in my github repo.
By the way, if you haven't yet, you really need to try dweet.io and freeboard: really awesome services with a free tier - big thanks to the guys at Bug Labs that are behind these services.

5 comments:

Simen Sommerfeldt said...

Great blogpost. And thanks for your generous remarks about my code.

I checked out dweet.io after reading your blogpost. How do you think it compares to the public mqtt brokers? And have you considered firebase? I think that it could be great for live updates of IoT data in web pages without having to code a backend.

Keep up the good work,

Simen

merlin13 said...

Thank you so much for your comment, I really appreciate your kind words.

I only know of one public mqtt broker, at mosquitto.org; I need to dig to find more. As for firebase, I only found out about it a couple weeks ago at a hackathon, it seems awesome and I definitely plan to dig into it soon.

rodrigo said...

Hi guy !!
Very good your blog !!
I am learning raspberry PI and you site is happing alot !!

Sorry for my poor english.

How you learn about 433 mhz ????

i am big trouble here in understand how work exchange security code between those devices !!!

thanks.

merlin13 said...

Hi rodrigo, thanks a lot for your words, they mean a lot.
I didn't learn too much by myself about 433 MHz radio because on the Arduino I used the RCSwitch.h library available in several places which makes things very easy. An on Raspberry Pi I started with an example found in NinjaBlocks github account.

For more details, please see my previous post here and also the code in my github account.

Abdul bari said...

The company is often referred to using the acronym MDS MDS radio from remotesiteproducts.com. ... provide frequency diversity which affords good operation regardless of location for stationary radios.