I recently got my hands on Raspberry Pi 3 and it’s quite a wonderful little device. In meantime I was also trying to learn NodeJS. So, why not make a NodeJS project on Raspberry Pi? What else could be better way of learning than making projects.
I made a simple NodeJS app that would periodically tweet sensor reading from raspberry pi. It could be any sensor reading, just needs few modification here and there to make it work for any sensor reading. I used
vcgencmd to get [readings] (http://www.elinux.org/RPI_vcgencmd_usage) from Pi’s internal sensors. To make our application possible we just need to parse output from
vcgencmd and then tweet it to internet. But while doing that , NodeJS’s way of using callbacks and synchronizing code confused me too much but anyway after reading some articles I was able to figure it out.
I divided the project into 3 modules
twitter.js - This is responsible for storing all API keyss and tokens.
bot.js - This is reponsible for actually sending tweets periodically after fetching the readings from sensor.js
sensor.js - This is responsible for providing the sensor readings to other modules , this module interacts with the shell to get output.
Headover to Twitter’s application manager. Enter your Application Name, Description and your website address. You can leave the callback URL empty. Submit the form by clicking the Create your Twitter Application. After creating your Twitter Application, you have to give the access to your Twitter Account to use this Application. To do this, click the Create my Access Token.
Get the Consumer Key, Consumer Secret, Acesss token, Access Token Secret.Now you can store it in twitter.js like
remmber to name twitterAPI‘s properties correctly because twitter module which would be used to make connection to twitter will depend on it.
Getting sensor data is fairly simple , just execute the shell command and parse the output generated by shell
we are returning values obtained by executing shellcommand using callback.
This is the fun part , first of all I needed the twitter npm module , you can install it easily with node package manager by
after we import twitter module using require(‘twitter’) , we’ll also need to initialize twitter client , it can be done by
remember twitterAPI object , which stored app’s configuration ?
I also used bit of API’s from async module , so you might wanna install that too
Tweeting part is simple , first we’ll need to fetch sensor reading from sensor.js module after we have reading we’re just gonna tweet it. To synchronize it , I used async.series() . You can read about it here. After data retrial is successful we’ll tweet it using twitterClient.post() , You can read more about twitter module here.
Lastly we’re gonna put that that in a function and call it every x milliseconds using setInterval()
Full source code for this is here
You can use various other sensors to record values and tweet it. Maybe you can use DH22
and hook it up using breadboard and Raspberry Pi’s GPIO pins to record temperature and humidity. Unfortunately , my DH22 is yet to get shipped hence I had to use internal sensor for this articlt