bio photo

Email

Getting the Arduino to send and Receive SMS’s

####Research

Arduino GSM Shield: PROS: The library is open source and the help section is self explanatory. The code (in C) looks familiar and easier to understand (as it’s deductive).

Cons: Required getting another SIM up running (ando) and since I wished my Arduino to do more than send SMS another module would have clogged it up. I could have ordered multiple arduinos but that seemed like an expensive proposition not to mention the difficulty in getting multiple arduinos to talk to eachother.

ENC28J60 and Thingspeak

This app makes the process pretty simple. There’s a ready made app where you can customize the message that needs to be sent via the arduino. The interaction between the arduino and app is initiated through bluetooth. I’m not inclined to use it because I wont be doing enough work on my own.

Arduino Yun Sends SMS on demand

Required an Arduino Yun (basically an UNO with a wifi module so the Arduino connects to the internet).

Twilio

Instead, I try implementing Jonathan Gottfried’s tutorial on building a Lockitron with Twilio, Arduino and Node.js. It makes use of Twilio libraries in Node to communicate with an Arduino over SMS making it move a servo motor which unlocks the door. My thought is, perfect, I don’t need to buy a different Arduino or add modules to it, the barrier to entry should be low on this one and I get to get my hands dirty with Node.js.

First attempt:

I setup the tutorial to replicate what’s being done to understand the code so that I can modify it later.

//ARDUINO CODE

Servo myservo;
int servoPin = 12;
int lock = 0;
int unlock = 180;
 
void setup() {
  // initialize serial:
  Serial.begin(9600);
  myservo.attach(servoPin);
  myservo.write(lock);
}

void loop() {
   // Recieve data from Node and write it to a String
   while (Serial.available()) {
    char inChar = (char)Serial.read();
    if(inChar == 'V'){ // end character for locking
     if (myservo.read() >= 90) {
       Serial.println("L");
       myservo.write(lock);
       delay(3000);
     }
     else {
       Serial.println("U");
       myservo.write(unlock);
       delay(3000);
     }
    }
  }  
}

// NODELOCK.JS CODE

var twilio = require('twilio'),
  SerialPort = require("serialport").SerialPort,
  express = require('express'),
  //bodyParser = require('body-parser');

var app = express();

function sendMessage(res, message) {
  var resp = new twilio.TwimlResponse();
  resp.message(message);
  res.type('text/xml');
  res.send(resp.toString());
}

var serialPort = new SerialPort("/dev/tty.usbmodem411", {
  baudrate: 9600
});

app.use(express.bodyParser());

    app.post('/sms', twilio.webhook('your auth token', {
      host: 'foo.herokuapp.com',
      protocol: 'https'
    }), function(req, res) {
      if (req.body.From == "+12128675309" && req.body.Body == "ABC123") {
        console.log("verified number!");

        serialPort.once('data', function(data) {
          if (data.toString().indexOf('U') > -1) { //check if the Arduino returned a U for unlocking
            sendMessage(res, 'Unlocking!');
          } else if (data.toString().indexOf('L') > -1) {
            sendMessage(res, 'Locking!');
          } else {
            sendMessage(res, 'ERROR');
          }
          console.log('data received: ' + data);
        });

        serialPort.write("V", function(err, results) {
          if (err) {
            console.log('err ' + err);
          }
          console.log('results ' + results);
        });

      } else {
        console.log("Wrong number!");
        sendMessage(res, "Invalid number!");
      }

    });

    serialPort.open(function() {
      app.listen(3000);
      console.log('Listening on port 3000');
    });

Error

I get an error unfortunately,

“Most middleware (like bodyParser) is no longer bundled with Express and must be installed separately. Please see https://github.com/senchalabs/connect#middleware.”

So I checked the Github pages: https://gist.github.com/jonmarkgo/9061701 https://github.com/senchalabs/connect#middleware

And added the missing bundles using npm install commands. It still didn’t work, then made sure I had the right paths, made adjustments, still no luck. Then I looked at the body-parser library, added a new variable “bodyParser” and modified the callback for the express() function (declared as var app;),

var bodyParser = require('body-parser');

app.use(express.bodyParser());

And it still doesn’t work. I know I’m a little out of my depth here, but I tried making some sense and left it to try the next day. Unfortunately, the next day, it didn’t work again.

####Conclusion - doing Node.js labs

So this is the next step, I’m going to do the Node.js labs now to see if there is any conceptual understanding I can get out of this and maybe use Twilio and Express() on my own to write a program for my door knob.