Tweet and Text Message an Arduino air quality reading

by Mate Marschalko

This little app tweets and texts notifications to your phone from a Node.js server when the Arduino air quality sensor detects high levels of natural gasses which we normally associate with bad smell.

Arduino air quality sensor
Arduino MQ-2 air quality sensor

I used the cheap MQ-2 natural gas sensor I bought from eBay. It’s sensitive to Methane, Butane, LPG, smoke and many other flammable or combustible gasses. It’s really easy to connect the sensor to the Arduino: first we need to power it up from the board so we connect it to the 5V and GND pins then its analog output pin to the analog input pin on the Arduino (A0).

The sensor reading is then sent to the serial port (USB) for the Node.js server once every second.

void setup(){
    Serial.begin(9600);
}

void loop(){
    Serial.println(analogRead(0));
    delay(1000);
}
Arduino code to read sensor and send through the USB

This message coming through the USB port from the Arduino is something we wouldn’t be able to read from a regular browser. Due to security reasons this is only available for the server which is why we use Node.js along with the serial library. I did something very similar with my Arduino RC Car so have a look for the serial communication logic there.

Tweeting from JavaScript

First we need to load the Twitter node library in our server javascript file then initialise it with the unique keys and tokens:

var T = new Twit({
    consumer_key:         'xyz123'
  , consumer_secret:      'xyz123'
  , access_token:         'xyz123'
  , access_token_secret:  'xyz123'
});
Initialising the Twitter API

Once it’s initialised you can tweet with the T.post() command:

T.post('statuses/update', { status: "The air quality in your room is" + data + "" }, function(err, data, response) {
    console.log(data);
});
Tweeting from Node.js

Texting from JavaScript

For text messages to your phone we use Twilio. Twilio also needs to be initialised with the tokens before using it:

var client = require('twilio')('xy', 'yz');
Initialising the Twilio API

After this you can easily send text messages to any mobile number with the sms.messages.post() method:

client.sms.messages.post({
    to: '+44xxx',
    from:'+44xxx',
    body:"The air quality in the office is" + data + ""
}, function(err, text) {
    console.log('Current status of this text message is: ');
});
Texting from Node.js

This project shows a lot of potential. You can see that as soon as the sensor reading arrives in Node.js the possibilities are infinite. You can start saving these values into a database or a google spreadsheet or simply send it to the browser through web sockets and the socketIO library. The air quality sensor could also be swapped out to any other analog sensor like temperature, light or sound without modifying the code.

Leave a Reply