Sensor status twitter bot with Nodejs and Raspberry Pi

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.

Setting up Twitter API keys and tokens

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

1
2
3
4
5
6
7
8
var twitterAPI = {
access_token_key:"your access token here",
access_token_secret:"your access token here",
consumer_key:"your consumer key here",
consumer_secret:"your consumer secret here"
};
exports.twitterAPI = twitterAPI; //make sure to export variable

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

Getting sensor data is fairly simple , just execute the shell command and parse the output generated by shell

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
var exec = require('child_process').exec;
const coreTempCmd = 'vcgencmd measure_temp'; //command to get core temperature
const coreVoltCmd = 'vcgencmd measure_volts core'; //command to get core voltage
function getcoreT(callback){
//get core temperature from the shell
exec(coreTempCmd , function(err , stdout , stderror){
callback(null, stdout); //don't forget the callback
});
}
function getcoreV(callback){
//get core voltage from the shell
exec(coreVoltCmd , function(err , stdout , stderror ){
callback(null , stdout); //don't forget the callback
});
}
exports.getcoreV = getcoreV; //make sure to export the functions
exports.getcoreT = getcoreT;

we are returning values obtained by executing shellcommand using callback.

Tweeting the data periodically

This is the fun part , first of all I needed the twitter npm module , you can install it easily with node package manager by

1
$ npm install twitter

after we import twitter module using require(‘twitter’) , we’ll also need to initialize twitter client , it can be done by

1
2
var twitter = require('twitter');
var twitterConfig = require('./twitter.js'); // this modules has out credentials

remember twitterAPI object , which stored app’s configuration ?

1
var twitterClient = new twitter(twitterConfig.twitterAPI);

I also used bit of API’s from async module , so you might wanna install that too

1
$ npm install async

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

What’s next?

You can use various other sensors to record values and tweet it. Maybe you can use DH22
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