A couple days ago I found my DigiPixel so I started thinking what quick project I could use it for and remembered CheerLights. I gave away my Ethernet shield to a friend and never got around to buy another one so I decided to have it connected to my laptop and get data using Serial through USB. I quickly wrote a sketch to do just this but since I didn't want the DigiPixel to stay on all the time I started to modify it to turn off after a while and then back on for a few seconds. While messing with the code node-red came to mind and I realized 2 things: first, I don't need to modify the code on the Arduino (and having to do it again and again if I decided to change the behavior, like different ON/OFF timeouts), I can leave the sketch simple as it was and implement the behavior I wanted in the node-red flow; second, I can run the flow on my RasPi which uses wifi to connect to my home network and for which I also have a battery so my project becomes "wireless".
A quick search revealed this @Cheerlights to various RGB devices example flow (thanks dceejay!) which is exactly what I needed it. Started my RasPi, deployed the flow, replaced the blink device with a serial node connected to my Arduino and I was done! After I "deployed" the project in my living room and my son started to ask me why there is no light I realized that if the CheerLights color doesn't change for a long time, the DigiPixel doesn't show anything so I tweaked the flow a little bit to wake up every 2 minutes and show the last known color and if there is no last known color just use some color to start things off; the result is here, not as good and generic as the original flow but exactly what I needed.
My setup is a bit more complex than it needs to be because I am using a Raspberry Pi A which has only one USB: since I need USB for both the wifi dongle and to communicate with the Arduino, I used a hub (it doesn't need power, the Pi power supply is good enough to power both the dongle and the Arduino).
node-red is indeed amazing: the amount of changes that can be made in short time and the ability to swap out nodes for different ones very easily (I could decide to replace my Arduino+DigiPixel with some other device and probably be up and running in a matter of minutes or an hour at the most), make it an awesome tool that will very likely be used more and more in the IoT world. I'm repeating myself but I simply love it and can't be thanking enough to its authors!
a place for microcontroller and Raspberry Pi projects, eBooks and books and some geocaching stuff.
Tuesday, December 23, 2014
Monday, December 15, 2014
resin.io
I recently discovered resin.io and I have to admit it sounded great being able to deploy apps to a running Raspberry Pi so I decided to give it a try. And apart from a few small things I'd like to see changed, it is indeed awesome.
Following the Getting Started guide I loaded the OS on an 8 GB Sandisk micro SD, inserted it in my Raspberry Pi B+, booted it up, waited and... nothing happened (well, the power LED on the RasPi came on but that's about it). I reformatted the card and tried again and this time RasPi's green LED started to blink soon after boot and like it says on the website, in about 6-10 minutes, my RasPi showed in the list of devices. I assume the first try didn't work because of something I did, so this is not in any way a bad mark for resin.io - in fact, the process is really smooth and explained in such detail on the website that setting it up is quite easy - the only reason I mentioned it is if someone else runs into the same issue, don't give up, retry and it will work for sure.
Now the device showed up on the dashboard, I went ahead with the next steps: cloned the text2speech app, added the resin git endpoint, commit the code and waited for the project to build. This step worked great but it took a very long time to install and compile all the packages and their dependencies; it would have taken a lot longer to build all the node.js modules directly on the Pi so this is a definite advantage. The problem though is that every time when I update the code and commit and push again, the install process starts all over again and it takes as long as the first time; for a simpler app, with a lot less dependencies this is not an issue but for the example app I tried this was definitely a nuisance. Normally, if I deployed such an app on a Pi the initial time to setup the node modules and their dependencies would have been much longer; but subsequent updates would find the modules in place and would only take seconds.
Another issue I found and it is probably only an issue for me is the terminal on the web interface didn't start; every time I try I get a message saying terminal works only for recently provisioned devices but my devices was added recently so it should have worked. This combined with the fact that I was not able to ssh into my Pi (maybe because the SSH service is not installed/started [update] because that would be a security risk) made me shutdown the Pi by unplugging it which is not very safe from the SD card point of view, it can become easily corrupted. It is true that if this is the case I can always start over by installing the OS file again; also, I can probably create my own Docker container with SSH installed and started by default so this may not be a real problem but since I am not familiar with Docker, it is a problem for me.
Last small issue is not having the ability to deploy more than one app on a running Pi - maybe this is possible but I haven't seen anywhere the docs talking about multiple apps on the same device; same app on multiple devices is possible but the other way around I am not sure how to do. This reason along with the previous ones I mentioned made me decide to wait until resin.io becomes a bit more mature before trying it again.
I hope these small problems I ran into won't stop you from trying resin.io - the idea is great and it works so it is indeed an awesome app worth checking out. Thank you to resin.io author(s) for such a great idea and app!
[Update] Recently I started to look into Docker which seems to be really awesome; a quick search for Docker on Raspberry Pi turned out this article on resin.io blog. I wanted to bring this up because this was a big task for the resin.io team and a huge success and whoever is going to use Docker on Raspberry Pi will owe the team a big thank you. Thank you, guys!
Following the Getting Started guide I loaded the OS on an 8 GB Sandisk micro SD, inserted it in my Raspberry Pi B+, booted it up, waited and... nothing happened (well, the power LED on the RasPi came on but that's about it). I reformatted the card and tried again and this time RasPi's green LED started to blink soon after boot and like it says on the website, in about 6-10 minutes, my RasPi showed in the list of devices. I assume the first try didn't work because of something I did, so this is not in any way a bad mark for resin.io - in fact, the process is really smooth and explained in such detail on the website that setting it up is quite easy - the only reason I mentioned it is if someone else runs into the same issue, don't give up, retry and it will work for sure.
Now the device showed up on the dashboard, I went ahead with the next steps: cloned the text2speech app, added the resin git endpoint, commit the code and waited for the project to build. This step worked great but it took a very long time to install and compile all the packages and their dependencies; it would have taken a lot longer to build all the node.js modules directly on the Pi so this is a definite advantage. The problem though is that every time when I update the code and commit and push again, the install process starts all over again and it takes as long as the first time; for a simpler app, with a lot less dependencies this is not an issue but for the example app I tried this was definitely a nuisance. Normally, if I deployed such an app on a Pi the initial time to setup the node modules and their dependencies would have been much longer; but subsequent updates would find the modules in place and would only take seconds.
Another issue I found and it is probably only an issue for me is the terminal on the web interface didn't start; every time I try I get a message saying terminal works only for recently provisioned devices but my devices was added recently so it should have worked. This combined with the fact that I was not able to ssh into my Pi (maybe because the SSH service is not installed/started [update] because that would be a security risk) made me shutdown the Pi by unplugging it which is not very safe from the SD card point of view, it can become easily corrupted. It is true that if this is the case I can always start over by installing the OS file again; also, I can probably create my own Docker container with SSH installed and started by default so this may not be a real problem but since I am not familiar with Docker, it is a problem for me.
Last small issue is not having the ability to deploy more than one app on a running Pi - maybe this is possible but I haven't seen anywhere the docs talking about multiple apps on the same device; same app on multiple devices is possible but the other way around I am not sure how to do. This reason along with the previous ones I mentioned made me decide to wait until resin.io becomes a bit more mature before trying it again.
I hope these small problems I ran into won't stop you from trying resin.io - the idea is great and it works so it is indeed an awesome app worth checking out. Thank you to resin.io author(s) for such a great idea and app!
[Update] Recently I started to look into Docker which seems to be really awesome; a quick search for Docker on Raspberry Pi turned out this article on resin.io blog. I wanted to bring this up because this was a big task for the resin.io team and a huge success and whoever is going to use Docker on Raspberry Pi will owe the team a big thank you. Thank you, guys!
Friday, November 14, 2014
The almost perfect Raspberry Pi dev setup
I want to learn more coding for Raspberry Pi and Arduino and one problem I have is being able to do so from different places and machines. I use both github and bitbucket and they are awesome but not once it happened that I wrote a bit of code and forgot to commit before leaving that machine. Even if I could be more careful with this, there is the aspect of having an IDE installed everywhere like Eclipse for Java, Arduino IDE, IDLE for Python. The solution for Arduino coding is codebender that I mentioned in a couple of my previous posts. Python I am mostly doing on Raspberry Pi and the solution is Adafruit's WebIDE - it is a great editor and one of the first things I install on every SD card for my Raspberry Pis.
For Java, I started a couple weeks ago to look at online editors like Cloud9 and others. Cloud9 looks great and it is one of the few editors of this kind that handles Java projects. However, recently I found an article (unfortunately, I didn't keep the link) that was reviewing several online editors from a Java coding point of view and the one the author of that article liked the most was Codenvy about which I have to admit I didn't even know until then. There were a lot of pro arguments for it in the article so I decided to give it a try. And I am so happy I did! As I read in the article, creating Java projects is a breeze; I tried both a new project and one cloned from a git repo: both worked like a charm. As a side note, I don't know Maven and Codenvy uses Maven (same as Heroku and other cloud apps) so this was a bit of a hiccup for me but I'm learning.
Codenvy integrates with both github and bitbucket so one can keep online and offline projects in sync this way. Also, there is a very powerful command line utility, Codenvy CLI that can be used to interface with projects, take them offline, or execute critical developer services.
But the thing I absolutely love about Codenvy is the fact that it has an Eclipse plugin that makes Eclipse aware of the Codenvy projects and allows both updating the Eclipse project with the Codenvy changes and the other way around. This is absolutely awesome! Now I can write code from anywhere without having Eclipse installed locally and when I get home, update the Eclipse project with these changes, keep coding and when done pushing the new code back to Codenvy. More, my Eclipse setup at home uses a plugin I found recently, LaunchPi that makes it possible to run and debug Java projects on Raspberry Pi directly from Eclipse. I found another similar plugin but it needed to be configured, with jars to be installed on the Pi and started separately; LaunchPi is so simple, just configure the Pi's IP address, the main class to run and done; and it also allows debugging which is simply great.
And if I want to deploy the code to the Pi, not just test it, I can always use a Maven goal to do this; as an example, look at the pom file used by Robert Savage, the author of the amazing Pi4J - but about this awesome library in another post.
I love this setup and the only reason I said in the title the "almost" perfect setup because there is one thing missing: if I could run and debug the code on the Pi from the Codenvy editor directly, that would be the icing on the cake. Maybe it is possible, I know there are ways to create a Pi emulator using QEMU but I haven't figured out yet how to do it and if it is possible to use it directly from Codenvy; maybe not but I'll try to find a way. Even without that, I am very happy with the current setup. Now, to do some coding!
For Java, I started a couple weeks ago to look at online editors like Cloud9 and others. Cloud9 looks great and it is one of the few editors of this kind that handles Java projects. However, recently I found an article (unfortunately, I didn't keep the link) that was reviewing several online editors from a Java coding point of view and the one the author of that article liked the most was Codenvy about which I have to admit I didn't even know until then. There were a lot of pro arguments for it in the article so I decided to give it a try. And I am so happy I did! As I read in the article, creating Java projects is a breeze; I tried both a new project and one cloned from a git repo: both worked like a charm. As a side note, I don't know Maven and Codenvy uses Maven (same as Heroku and other cloud apps) so this was a bit of a hiccup for me but I'm learning.
Codenvy integrates with both github and bitbucket so one can keep online and offline projects in sync this way. Also, there is a very powerful command line utility, Codenvy CLI that can be used to interface with projects, take them offline, or execute critical developer services.
But the thing I absolutely love about Codenvy is the fact that it has an Eclipse plugin that makes Eclipse aware of the Codenvy projects and allows both updating the Eclipse project with the Codenvy changes and the other way around. This is absolutely awesome! Now I can write code from anywhere without having Eclipse installed locally and when I get home, update the Eclipse project with these changes, keep coding and when done pushing the new code back to Codenvy. More, my Eclipse setup at home uses a plugin I found recently, LaunchPi that makes it possible to run and debug Java projects on Raspberry Pi directly from Eclipse. I found another similar plugin but it needed to be configured, with jars to be installed on the Pi and started separately; LaunchPi is so simple, just configure the Pi's IP address, the main class to run and done; and it also allows debugging which is simply great.
And if I want to deploy the code to the Pi, not just test it, I can always use a Maven goal to do this; as an example, look at the pom file used by Robert Savage, the author of the amazing Pi4J - but about this awesome library in another post.
I love this setup and the only reason I said in the title the "almost" perfect setup because there is one thing missing: if I could run and debug the code on the Pi from the Codenvy editor directly, that would be the icing on the cake. Maybe it is possible, I know there are ways to create a Pi emulator using QEMU but I haven't figured out yet how to do it and if it is possible to use it directly from Codenvy; maybe not but I'll try to find a way. Even without that, I am very happy with the current setup. Now, to do some coding!
Sunday, November 09, 2014
ModMyPi, node-red, gpio issues, the great WiringPi library
Now that I figured out how to use a PIR motion sensor with Arduino I decided to try the same thing on a Raspberry Pi; pretty easy task since a quick search on google returned a lot of links. Using one of these examples, I put together a quick Python script which worked flawlessly. The only thing I didn't like: the continuous polling of the pin; instead, I wanted to use interrupts, same as I did on my Arduino. A few more searches later I found an article about using the PIR sensor via interrupts; this article is just one among many awesome ones on the ModMyPi blog - I am very happy I discovered this website and very grateful to the authors for all the tutorials, articles and help they offer to the community. Thank you!
Since the Python script in the article mentioned above worked without any issues, I decided to try the same thing using node-red. According to the node-red docs related to RPi, there are 2 ways of interacting with RPi: the gpio command using the rpi-gpio nodes and using the wiring-pi module which is the more complex way. So, I decided to give the rpi-gpio node a try. I created a simple flow with an inject and rpi-gpio nodes; to my surprise, as soon as I deployed the flow, I got an error in the debug window: "Error: Command failed: /bin/sh: 1: gpio: not found". This was very strange because I know I tried the gpio command and it works fine; also, the Python scripts had no issues. After a lot of tries and failures and retries, I think I finally figured out what is happening: this is only an issue when node-red is started automatically on boot using the auto-start script (my current setup is based on these steps including the auto-start script that starts node-red when RPi boots up); if I do a simple
Because of the issue with gpio I decided to look into using the wiring-pi module which is a wrapper to the WiringPi library - I've heard about it before but never got to use it until now: just looking at the docs and examples I can say that it looks like a great library. Indeed, writing the code to blink an LED in a function node as described here worked great from the first try. The only issue is that I wanted to write code for my PIR sensor using interrupts and while WiringPi has great ways of working with interrupts, I couldn't find anything like that in the wiring-pi wrapper. So, while WiringPi is great and I plan to learn more about it in the future, for my plans it may not work in node-red via the wiring-pi wrapper which is in fact fine because it gives more of an incentive to look into and maybe learn other libraries or even languages.
Since the Python script in the article mentioned above worked without any issues, I decided to try the same thing using node-red. According to the node-red docs related to RPi, there are 2 ways of interacting with RPi: the gpio command using the rpi-gpio nodes and using the wiring-pi module which is the more complex way. So, I decided to give the rpi-gpio node a try. I created a simple flow with an inject and rpi-gpio nodes; to my surprise, as soon as I deployed the flow, I got an error in the debug window: "Error: Command failed: /bin/sh: 1: gpio: not found". This was very strange because I know I tried the gpio command and it works fine; also, the Python scripts had no issues. After a lot of tries and failures and retries, I think I finally figured out what is happening: this is only an issue when node-red is started automatically on boot using the auto-start script (my current setup is based on these steps including the auto-start script that starts node-red when RPi boots up); if I do a simple
sudo service node-red restart
then gpio works without a glitch. I guess it may have to do with loading node-red before gpio or something like it but since I am not a Linux wiz, I don't know if this is the case and what to do to fix it but maybe someday I will figure it out.Because of the issue with gpio I decided to look into using the wiring-pi module which is a wrapper to the WiringPi library - I've heard about it before but never got to use it until now: just looking at the docs and examples I can say that it looks like a great library. Indeed, writing the code to blink an LED in a function node as described here worked great from the first try. The only issue is that I wanted to write code for my PIR sensor using interrupts and while WiringPi has great ways of working with interrupts, I couldn't find anything like that in the wiring-pi wrapper. So, while WiringPi is great and I plan to learn more about it in the future, for my plans it may not work in node-red via the wiring-pi wrapper which is in fact fine because it gives more of an incentive to look into and maybe learn other libraries or even languages.
Wednesday, October 29, 2014
node.js and Arduino
In my quest of learning more about node.js one of the searches returned results about the johnny-five framework that can be used to send commands from node.js to Arduino, along with Anna Gerber's awesome tutorial pages at node-ardx. Installing Firmata on my Arduino was a breeze using codebender (a while back codebender didn't have that many libraries so I had to use my own Arduino IDE for some of the code; nowadays I have to say that it is awesome, I am using it more and more and loading Firmata in this case took less than a minute). Unfortunately, this is where the easy stuff ended; I am writing this in case someone else will go on the same path and have the same issues and frustrations as I did.
My problem started with the fact that I was using an old Arduino from the great guys at Seeed Studio: nothing wrong with the board, I used is for so many projects and I will keep using it, but in this case it turns out it wasn't a good idea to try to make it work. I connected it to my Linux Mint laptop and tried the first small sample code from node-ardx: nothing - the code would start, detect the Arduino on /dev/ttyUSB0 sit there for a while and then time out. Searched a lot left and write and found a lot of ideas on the github johnny-five issues list but nothing worked (used cu to set the speed, installed an older Firmata 2.2, and many more tries). I knew node-red has an Arduino node (using arduino-firmata) so I tried it as well on my laptop without any success (getting errors like serial port not found, post closed and others). Since I knew node-red can work with Arduino on a Raspberry Pi because I was able to make it work very easily a while back during a hackathon, I connected my Arduino to my B+ RasPi and gave it a try with node-red: it worked from the first try. However, johnny-five still didn't work on the RasPi.
This was getting frustrating so following one of the issues on johnny-five repo mentioning problems with Duemilanove Arduinos I decided to use a different board: I have a SparkFun RedBoard around so installed Firmata on it, connected it to my Linux Mint laptop and guess what: johnny-five worked immediately. I was a bit surprised when I saw johnny-five identified the port as /dev/ttyUSB0 because I thought RedBoard is basically a Uno and according to the list found in this stackoverflow issue it should have been /dev/ttyACM0 (as the first Arduino I tried with node-red on RasPi at the hackathon); the list by the way says:
For now, my lesson is that Duemilanove Arduinos may have problems talking to node.js so I decided to stick with the RedBoard (or one of the Leonardos I have around) and keep the older Seeeduino board with the bluetooth module for communication with my Android phone (which works great by the way, using the Amarino library but that is subject for some future post).
My problem started with the fact that I was using an old Arduino from the great guys at Seeed Studio: nothing wrong with the board, I used is for so many projects and I will keep using it, but in this case it turns out it wasn't a good idea to try to make it work. I connected it to my Linux Mint laptop and tried the first small sample code from node-ardx: nothing - the code would start, detect the Arduino on /dev/ttyUSB0 sit there for a while and then time out. Searched a lot left and write and found a lot of ideas on the github johnny-five issues list but nothing worked (used cu to set the speed, installed an older Firmata 2.2, and many more tries). I knew node-red has an Arduino node (using arduino-firmata) so I tried it as well on my laptop without any success (getting errors like serial port not found, post closed and others). Since I knew node-red can work with Arduino on a Raspberry Pi because I was able to make it work very easily a while back during a hackathon, I connected my Arduino to my B+ RasPi and gave it a try with node-red: it worked from the first try. However, johnny-five still didn't work on the RasPi.
This was getting frustrating so following one of the issues on johnny-five repo mentioning problems with Duemilanove Arduinos I decided to use a different board: I have a SparkFun RedBoard around so installed Firmata on it, connected it to my Linux Mint laptop and guess what: johnny-five worked immediately. I was a bit surprised when I saw johnny-five identified the port as /dev/ttyUSB0 because I thought RedBoard is basically a Uno and according to the list found in this stackoverflow issue it should have been /dev/ttyACM0 (as the first Arduino I tried with node-red on RasPi at the hackathon); the list by the way says:
- Duemilanove - Serial chip: FTDI FT232RL; Serial port: /dev/ttyUSB0
- Uno - Serial chip: Atmel ATmega16U2 (or 8U2 on older boards); Serial port: /dev/ttyACM0
- Leonardo - Serial chip: Atmel ATmega32U4 (built-in); Serial port: /dev/ttyACM0
For now, my lesson is that Duemilanove Arduinos may have problems talking to node.js so I decided to stick with the RedBoard (or one of the Leonardos I have around) and keep the older Seeeduino board with the bluetooth module for communication with my Android phone (which works great by the way, using the Amarino library but that is subject for some future post).
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.
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.
Friday, September 19, 2014
Arduino sensors to Raspberry Pi using 433 MHz radios
Finally getting to wrap up my first end-to-end project using Arduino and Raspberry Pi talking via inexpensive 433 MHz radios - the distance over which they work is pretty small but this instructables explains how to add an antenna which dramatically improved the distance. The main idea was to have multiple Arduinos with TX radios, each equipped with DHT and PIR sensors; they all send the data to the Raspberry Pi which posts it to the web and also writes it to a local database. In addition to the values from sensors, I am also sending the voltage level, hoping that this way I can monitor the discharge of the batteries (I am not sure if this works or not, I didn't use an analog pin for this, I am simply getting the voltage on the MCU, using code from this great instructables).
The code for both the sender (Arduino) and the receiver (Raspberry Pi) is in my github repo. The comments in the code pretty much explain everything (how it works, how to connect the sensors and radios, and so on). Here are just some quick ideas that may be interesting.
Rate limits:
The code for both the sender (Arduino) and the receiver (Raspberry Pi) is in my github repo. The comments in the code pretty much explain everything (how it works, how to connect the sensors and radios, and so on). Here are just some quick ideas that may be interesting.
- I wanted to transmit all the data in one go so I combined 4 numbers into one long, as described in both sender and receiver code.
- The transmission between the radios is pretty flaky, that's why I am repeating the send a lot on the Arduino and there is code on the receiver to ignore identical values coming in a 30s interval. This is OK because transmissions from different stations have different station codes so even if the sensor values are the same, the actual overall value is different. Same for a motion sensor: the ON value is different than the OFF value so even if they happen a second apart, both will be received.
- I started by posting the data to SparkFun's data service, then added Dweet.io and in the end settled on ThingSpeak and I'm also saving all the data to a local SQLite database. See some notes about this later in this post.
- The code in github is ready to use: just change the stationCode for each Arduino station and use the right API keys in the Raspberry Pi code.
Rate limits:
- SparkFun's data service is limited to making 100 log requests every 15 minutes. Given the 5 posts in 15 min, the temperature data stream can accommodate 20 stations without losing data which is more than I need. The motion data though may accommodate only 1 or 2 stations (more if there is not a lot of activity around them) without starting to lose data. I will post all the data I can, the remainder being lost.
- Dweet.io holds on to the last 500 dweets over a 24 hour period so while there won't be data loss, the motion data will probably cover only a few hours at the most, even for just a couple stations.
- ThingSpeak has a rate limit of an update per channel every 15 seconds so more than one station per channel will more than likely run into this limit (even 2 temp stations sending data every 15 minutes can happen to send data less than 15 sec apart); I could wait and retry until the post goes through but this would delay the code and risk missing data sent by the stations. Because of this, I decided to tie the local db data to this service: every time I post to ThingSpeak, I check the response: if > 0, the post was successful, I will log the data to the db with a flag of posted=true (1); if = 0, the post was not successful so I will log the data to the db with a flag of posted=false (0). Later I may create a node-red flow to get all the db data with posted=false and repost it to ThingSpeak; each row records the UTC time of the actual insert so I can send it along with the data, the measurements getting recorded at the actual time they took place not the time I post them.
Thursday, September 04, 2014
SparkFun's Phant.io; libcurl and sqlite on Raspberry Pi
A few months ago SparkFun announced their new data service: phant.io. There are lots of similar services out there, some really awesome, some simple to use others not so much, some with great charting capabilities, others just a data store but I loved Phant from the beginning, mostly because it doesn't try to do a lot but what it does it does great. Also, its simplicity of use is awesome and is open-source. I was writing a JavaScript application at the time that needed a simple backend storage; I could use almost anything for that storage but when I saw the announcement I quickly decided to give it a try and indeed it was a breeze to post and retrieve data.
Fast forward to now: part of a larger project I am writing with a good friend of mine (in a nutshell an Arduino with DHT and PIR sensors gathers data and sends it using a 433 MHz radio to a Raspberry Pi) I wanted to post the data from a Raspberry Pi to some service on the net so again I chose phant.io. There are plenty of great examples on SparkFun's tutorial page (and other resources) about how to use Phant with Arduino, Python, Electric Imp but since the code running on my Raspberry Pi is written in C (because this was the simplest code I found for the 433 MHz receiver) I had to figure out how to use Phant using C. I tried for a couple hours to use sockets (found great code examples) but no matter what I did I was getting stuck in all kinds of issues. While looking around on stackoverflow for a solution I found a mention of libcurl - I don't remember who mentioned it but I owe him/her big thanks. I was a little bit reluctant to try it at first since I knew nothing about it but then another mention showed up so I decided to give it a try.
First thing I tried, adding
and quickly verified that curl was now present in /usr/include/. Then I got the code from the very first example on libcurl page: simple.c but this time I got another error, curl_easy_init not defined or something like it. Another quick search and figured out I had to link the new library so I modified Makefile and added -lcurl. Now everything built and the first try with my test code posted data to data.sparkfun.com. The code as it stands today is here part of the repository that will eventually contain all the project code.
libcurl is really awesome: it took only minutes of getting the first version of the code done, in just a few lines (compared to 4 times as much for the socket code), the code is much more readable and the docs are awesome. Thanks to all the authors and contributors for such a great library!
Not completely related to the subject, in the same project I wanted to use a local database on the Raspberry Pi; a quick search got me to sqlite which is very easy to install and there are a lot of articles about it. One thing that most authors don't mention is that same with libcurl there is a library that needs to be installed so sqlite can be used in code and also that is has to be linked for the code to compile. Not a big deal but here are the steps in one place:
Fast forward to now: part of a larger project I am writing with a good friend of mine (in a nutshell an Arduino with DHT and PIR sensors gathers data and sends it using a 433 MHz radio to a Raspberry Pi) I wanted to post the data from a Raspberry Pi to some service on the net so again I chose phant.io. There are plenty of great examples on SparkFun's tutorial page (and other resources) about how to use Phant with Arduino, Python, Electric Imp but since the code running on my Raspberry Pi is written in C (because this was the simplest code I found for the 433 MHz receiver) I had to figure out how to use Phant using C. I tried for a couple hours to use sockets (found great code examples) but no matter what I did I was getting stuck in all kinds of issues. While looking around on stackoverflow for a solution I found a mention of libcurl - I don't remember who mentioned it but I owe him/her big thanks. I was a little bit reluctant to try it at first since I knew nothing about it but then another mention showed up so I decided to give it a try.
First thing I tried, adding
#include <curl/curl.h>
ended up with an error at compile time: file not found. So I had to install libcurl dev libraries which was really easy:sudo apt-get install libcurl4-openssl-dev
and quickly verified that curl was now present in /usr/include/. Then I got the code from the very first example on libcurl page: simple.c but this time I got another error, curl_easy_init not defined or something like it. Another quick search and figured out I had to link the new library so I modified Makefile and added -lcurl. Now everything built and the first try with my test code posted data to data.sparkfun.com. The code as it stands today is here part of the repository that will eventually contain all the project code.
libcurl is really awesome: it took only minutes of getting the first version of the code done, in just a few lines (compared to 4 times as much for the socket code), the code is much more readable and the docs are awesome. Thanks to all the authors and contributors for such a great library!
Not completely related to the subject, in the same project I wanted to use a local database on the Raspberry Pi; a quick search got me to sqlite which is very easy to install and there are a lot of articles about it. One thing that most authors don't mention is that same with libcurl there is a library that needs to be installed so sqlite can be used in code and also that is has to be linked for the code to compile. Not a big deal but here are the steps in one place:
sudo apt-get install sqlite3 libsqlite3-dev
compiler link argument: -lsqlite3
Friday, June 06, 2014
My first Node-Red node: gcalendar
First thing I found when I was playing with homA was the calendar component that reads an event from a Google calendar - I loved the idea and the implementation. So when I got interested in Node-Red my first thought was: wouldn't it be cool if there was a node like that? I checked the extra nodes repository and couldn't find one so I thought this could be the best opportunity for me to dive deeper into node.js and node-red.
It wasn't all that easy at first since the Node-Red website didn't have any docs at that time about how to write a node - thanks to Nicholas O'Leary a guide is now available, not 100% finished but great enough to help anyone starting on this path. Anyway, looking at existing nodes and experimenting with stuff I was able to do this so here is my first node: gcalendar. While working on this I hit some snags:
It wasn't all that easy at first since the Node-Red website didn't have any docs at that time about how to write a node - thanks to Nicholas O'Leary a guide is now available, not 100% finished but great enough to help anyone starting on this path. Anyway, looking at existing nodes and experimenting with stuff I was able to do this so here is my first node: gcalendar. While working on this I hit some snags:
- To get authorized to a Google calendar, OAuth2 must be used. I could have used Alexander's code from the homA calendar component but since I have no experience with OAuth2 I was not confident I can figure it out. Alexander is great and he always helped me when I asked him something but didn't want to take his time with minor things. On the other hand, a while back I heard about Temboo and never tried to use it so this looked like the perfect time to do so. And I'm happy I did: following the steps on the GetNextEvent page I was able in a few minutes to create the app on Google API, get the credentials and get the code to call this Choreo and get the calendar data. There are 2 drawbacks to this approach, none of them a deal breaker for me:
- there are 3 more properties to set now to configure this node, all related to Temboo - since they don't change in time, this is not a big issue;
- Temboo only allows 1000 calls per month for free - since I don't plan to call this node more than 10 times a day, this limitation doesn't really affect me; anyway, I like their service a lot and if I'm going to be using them more, I won't mind paying for the next tier service.
- After the main JavaScript coding was done, I had to figure out the .html file for the node. This was quite challenging without having any docs and it took me a lot of trials to figure out how and what to do; this page was of great help - all of this is now documented on this page which explains everything in great detail.
- The first version of the node (let's call it v0.1) had all the properties set in the node directly which worked fine but it was a pain to have to re-enter them every time when I created the node during testing; plus, they were saved in the flow JSON which is not a good idea at all. Hardcoding them worked fine for testing but not an option for the final code. So now it was time to figure out the credentials part. First pass was to hardcode them in the settings.js file in Node-Red install dir which worked (this is sort of v1.0 of my node, documented in this file I kept for reference) but not a good idea according to the answers I got to my question. Based on the answers I got from Charalampos and Dave and after a lot more messing with the code, I was able to come up with the current version on my gcalendar node, where credentials are stored in a separate file. There is plenty more I can do to improve this version, but this is the one I use now and I am really happy with it.
Monday, June 02, 2014
Starting with Node-Red
A couple years ago I started to look into connecting devices to the Internet and played with quite a few services, like Pachube then Cosm now Xively and a few others - this all evolved in what is now named the Internet of Things (IoT) and a lot more services like Xively are now available. These services are usually independent of one another and each has a different way of setting up the communication with the device which makes things a bit difficult for beginners; these devices are sometimes hobby oriented like Arduino or Raspberry Pi but in other case they are products that monitor the home and also take actions (like NinjaBlocks, SmartThings, Insteon, TCP Lighting and so many many more). To make working with these devices easier (detection, customization, adding rules, communication with other services) new frameworks and applications are coming up, like TheThingSystem (TTS - node.js based, v1.8 was just released a couple days ago), OpenHab (Java based), Alexander Rust's homA and others.
A few months back, a new application showed up on my radar: Node-Red; built on node.js, Node-Red is another great product started by IBM researchers. Briefly said, Node-Red provides a browser-based flow editor that makes it easy to wire together flows using a wide variety of nodes presented in the palette; flows can be then deployed to the runtime in the same interface. No point in getting into more details about it, you can read more on the Node-Red website and in various articles like this one.
At that time I was digging a lot into the homA app and its components and I loved it; I was just getting started with the rules engine (which is way cool, by the way); but after discovering Node-Red and after seeing Alexander's comment to this google+ post I realized that I can use Node-Red to simplify the interaction between my devices and the outside world, so I started to research it more. There are great resources around it, the ones I use the most are:
A few months back, a new application showed up on my radar: Node-Red; built on node.js, Node-Red is another great product started by IBM researchers. Briefly said, Node-Red provides a browser-based flow editor that makes it easy to wire together flows using a wide variety of nodes presented in the palette; flows can be then deployed to the runtime in the same interface. No point in getting into more details about it, you can read more on the Node-Red website and in various articles like this one.
At that time I was digging a lot into the homA app and its components and I loved it; I was just getting started with the rules engine (which is way cool, by the way); but after discovering Node-Red and after seeing Alexander's comment to this google+ post I realized that I can use Node-Red to simplify the interaction between my devices and the outside world, so I started to research it more. There are great resources around it, the ones I use the most are:
- Node-Red docs;
- flows contributed by people working on Node-Red and 3rd parties;
- Node-Red github repo;
- other nodes contributed by people working on Node-Red and 3rd parties;
- the Node-Red google group - Nicholas O'Leary and Dave C-J are awesome, their extremely fast answers helped me a ton.
- building a Node-Red sample application;
- The ThingBox Project: Raspberry Pi image ready to go containing a MQTT broker (Mosquitto), a javascript HTTP server built on top of Node.Js and Node-Red.
Wednesday, January 22, 2014
Reusing some old microcontrollers Arduino style
The other day I was cleaning up my electronics boxes and I happened on 2 older Atmega32 40 pin controllers I got a long time ago, when first starting my foray into MCUs. I remember those times when I was trying to figure out how to program a controller, when no Arduino was around and everything was done directly in C at pin level, same as I still do now for MSP430; with lots of friends and forum help, I was able to blink an LED even drive an H-Bridge. Cool and also frustrating times! An idea struck me: if people are able to create Arduino like IDEs for all kinds of controllers (see Energia for MSP430 and LaunchPad) maybe it would be possible to program these MCUs the same way: after all, they are Atmel controllers so it shouldn't be that difficult. It all has to start with burning the bootloaded, since this is what makes the Arduino what it is today. It turns out some people figured out how to do this, not very easy but not very complicated either. The post I found most helfpul is here. Thousand thanks to hexskrew for figuring this out!
First thing to do was install Arduino-0022 (I already have 1.0.5 installed but the arduino-extras was written for pre-1.0 versions, didn’t want to go through the pain of updating the code and mess it up for sure) and replace the arduino folder with the content of arduino-extras.zip. Also, in Preferences, I unckecked “Check for updates on startup” because I want to keep this Arduino version as it is - newer versions will apply to my other Arduino-1.0.5 install.
Most important step is wiring the Atmega32 to the Arduino: I have an ISP programmer somewhere but using an Arduino instead seemed to be easier for someone like me. The schematic is shown here and is great!I wired everything as in the image but since I didn't have a 120 ohm resistor, I decided to use a close one instead, 150 ohm. It says clearly in the image that the resistor MUST be 120 ohm but my limited experience said: if 120 ohm can do the job, why not a 150 ohm! Big mistake that cost me quite some time! (Now I know I should listen to my betters and not assume anything, especially since I am just a noob in electronics.) Running the first command in the document:
I got instead of the expected response:
this:
I googled around and found the same exact message in this post which in turn links to this doc in Arduino playground where it is explained with details that the resistor must indeed be 120 ohm! I dug into my box of resistors in the end found a few that connected together yielded the desired 120 ohm. I reran the previous command again and this time I got the expected reply. Awesome!
Note: there is a difference between the original schematic I followed and the last article I mentioned: in the original one, the 120 ohm resistor is between RESET and GND; in this article is between RESET and 5V. I assume they both worked but I decided to go with RESET to 5V.
The commands are listed and detailed in the post I mentioned above, in a nutshell what I did to burn the bootloader was:
Again, big thanks to hexskrew for figuring this out!
First thing to do was install Arduino-0022 (I already have 1.0.5 installed but the arduino-extras was written for pre-1.0 versions, didn’t want to go through the pain of updating the code and mess it up for sure) and replace the arduino folder with the content of arduino-extras.zip. Also, in Preferences, I unckecked “Check for updates on startup” because I want to keep this Arduino version as it is - newer versions will apply to my other Arduino-1.0.5 install.
Most important step is wiring the Atmega32 to the Arduino: I have an ISP programmer somewhere but using an Arduino instead seemed to be easier for someone like me. The schematic is shown here and is great!I wired everything as in the image but since I didn't have a 120 ohm resistor, I decided to use a close one instead, 150 ohm. It says clearly in the image that the resistor MUST be 120 ohm but my limited experience said: if 120 ohm can do the job, why not a 150 ohm! Big mistake that cost me quite some time! (Now I know I should listen to my betters and not assume anything, especially since I am just a noob in electronics.) Running the first command in the document:
avrdude -p m32 -c arduino -P /dev/ttyUSB0 -b 19200
I got instead of the expected response:
avrdude: Device signature = 0x1e9502
this:
avrdude: Device signature = 0x1e9406
avrdude: Expected signature for ATMEGA644P is 1E 96 0A
Double check chip, or use -F to override this check.
I googled around and found the same exact message in this post which in turn links to this doc in Arduino playground where it is explained with details that the resistor must indeed be 120 ohm! I dug into my box of resistors in the end found a few that connected together yielded the desired 120 ohm. I reran the previous command again and this time I got the expected reply. Awesome!
Note: there is a difference between the original schematic I followed and the last article I mentioned: in the original one, the 120 ohm resistor is between RESET and GND; in this article is between RESET and 5V. I assume they both worked but I decided to go with RESET to 5V.
The commands are listed and detailed in the post I mentioned above, in a nutshell what I did to burn the bootloader was:
- Check everything is ok
- Set the chip to be unlocked, and use the internal 8mhz oscillator
- Unlock to bootloader
- Flash the arduino bootloader
- Lock the bootloader.
Again, big thanks to hexskrew for figuring this out!
Tuesday, January 21, 2014
Amazing graphs
This post is only indirectly related to Raspberry Pi, Arduino and cloud data because I found out about this great website, plotly by reading this post on Quora: What is a good first project with a raspberry pi?. And while the website itself and the Arduino code look great, I was amazed by the blog content: each graph is not only telling a story but it is really beautiful as well, like this one.
Check them out: http://blog.plot.ly/
Check them out: http://blog.plot.ly/
Sunday, January 12, 2014
My first smartmaker tiny projects
After more than one year, some of the smartmaker boards made it so I started to play around with them a little bit. First, I have to say that they are pretty cool: it is a lot easier to connect the boards than to put together circuits on breadboard. The docs are still lacking, there are still problems with some boards and I am a bit worried that the connectors may not be quite so resilient in time (for all of these issues and more, visit the smartmaker forums) but overall I like the idea.
First things I tried were pretty simple things (all of these are public projects in my codebender account): changing colors on the RGB led, make 5 leds blinks, read the values of 5 analog buttons and drive 5 leds using 5 analog buttons - the last one was a real hit with my son, he loved pushing one or more buttons to see the corresponding leds turn on.
As a side note, the RGB led board received from smartmaker was not in my original list of boards ordered, as someone mentioned in the forums it seems this was a small gift from smartmaker, so thanks a lot, Dimitri@smartmaker.
The projects I mentioned are nothing special but one thing about them is really cool: think about putting together a circuit with 5 analog buttons and 5 leds; in addition to the buttons and leds, one would need resistors, 10 in this case, wires between the arduino and the breadboard and a lot of patience. It was a breeze to snap a smartcore into an extension board, and the buttons and led boards into another extension board, connected in turn to the first board; seconds really. This is the smartmaker idea that I loved from the start and that I hope will be successful.
A quick photo of the last project I mention is below:
For more info, there are some really nice tutorials posted on the forums by Josephur, specifically here.
First things I tried were pretty simple things (all of these are public projects in my codebender account): changing colors on the RGB led, make 5 leds blinks, read the values of 5 analog buttons and drive 5 leds using 5 analog buttons - the last one was a real hit with my son, he loved pushing one or more buttons to see the corresponding leds turn on.
As a side note, the RGB led board received from smartmaker was not in my original list of boards ordered, as someone mentioned in the forums it seems this was a small gift from smartmaker, so thanks a lot, Dimitri@smartmaker.
The projects I mentioned are nothing special but one thing about them is really cool: think about putting together a circuit with 5 analog buttons and 5 leds; in addition to the buttons and leds, one would need resistors, 10 in this case, wires between the arduino and the breadboard and a lot of patience. It was a breeze to snap a smartcore into an extension board, and the buttons and led boards into another extension board, connected in turn to the first board; seconds really. This is the smartmaker idea that I loved from the start and that I hope will be successful.
A quick photo of the last project I mention is below:
For more info, there are some really nice tutorials posted on the forums by Josephur, specifically here.
Subscribe to:
Posts (Atom)