< Back

Tweet and Text Message an Arduino air quality reading

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.

Talking arduino plant with sensors

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.

// Arduino code to read sensor and send through the USB

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

void loop(){
    Serial.println(analogRead(0));
    delay(1000);
}

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:

// Initialising the Twitter API

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

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

// Tweeting from Node.js

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

Texting from JavaScript

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

// Initialising the Twilio API

var client = require('twilio')('xy', 'yz');

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

// Texting from Node.js

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: ');
});

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.

Introduction

Get started with JavaScript Electronics and learn the basiscs in just a few hours!

Learn working with the Arduino UNO, building simple circuits, controlling an LED light and measuring light and temperature with JavaScript and Node.js!

Introduction to JavaScript Electronics

Get the print edition from Amazon for only
$4.95 - £4.95:

Complete book

Learn all about programming electronics JavaScript and Node.js, all from the very basics.

Build a smart talking plant, a motion sensing alarms, learn data logging, send SMS notifactions and many more!

Introduction to JavaScript Electronics

Get the print edition from Amazon for only $12.95 - £12.95:

Mate Marschalko

Mate Marschalko

Web Developer, Creative Technologist and Maker. Builds Internet connected devices for the Internet of Things.

Get started with JavaScript Electronics for FREE