SMS Send and Receive

The ATSMS module uses standard AT commands to allow you to send and receive SMS text messages via a GSM modem module like the SIM900.

Note: If you want to connect to the internet, check out this page for a list of modules for various GSM/LTE modems.

Wiring

See the SIM900 page for wiring instructions - the only connections you need to the Espruino device are: VCCMCU/VIO, GND, RX and TX.

Software

The software's pretty easy. All you need to remember is to call all the initialisation code from onInit if you're wanting to initialise the modem at power on - and be aware that the modem may take a few seconds to boot.

// Connect to serial device
Serial1.setup(115200, { rx: B7, tx : B6 });
var ATSMS = require("ATSMS");
var sms = new ATSMS(Serial1);
//Use sms.at.debug(); here if you want debug messages

sms.init(function(err) {
  if (err) throw err;
  console.log("Initialised!");

  sms.list("ALL", function(err,list) {
    if (err) throw err;
    if (list.length)
      console.log(list);
    else
      console.log("No Messages");
  });

  // and to send a message:
  //sms.send('+441234567890','Hello world!', callback)
});

sms.on('message', function(msgIndex) {
  console.log("Got new message, index ", msgIndex);
});

On many devices, Espruino's console will automatically move to Serial1 when USB or Bluetooth is disconnected, and so will stop the GSM modem from working. To fix this, work out where you want the console (eg, where you're programming Puck.js from) - for instance USB, and add USB.setConsole(1).

Reacting to received messages

Often you might want to do something in response to a received message. In this case you could do something like this:

sms.on('message', function(msgIndex) {
  console.log("Got message #", msgIndex);
  sms.get(msgIndex, function(err, msg) {
    if (err) throw err;
    print("Read message", msg);
    var txt = msg.text.toLowerCase();
    if (txt=="on") LED1.set();
    if (txt=="off") LED1.reset();
    // delete all messages to free SIM card memory
    sms.delete("ALL");
  });
});

When notified that we have a message, we read it, and turn LED1 on or off depending on whether the text was on or off. We then delete all text messages from memory - as it is possible to fill up the SIM card's memory quite quickly!

Note: Anybody could send your device a text message! It would make sense to check the phone number in msg.oaddr before you do anything important.

Reference

/* Initialise the modem
Calls `callback(null)` on success, or callback(err_msg) on failure
*/
ATSMS.prototype.init = function (callback) { ... }

/* Send a text message, for example `sms.send('+441234567890','Hello world!', callback)`. 
Calls `callback(null)` on success, or callback(err_msg) on failure
*/
ATSMS.prototype.send = function (number, text, callback) { ... }

/* List SMSs. Index is either "ALL","REC READ" or "REC UNREAD".

Returns a callback(null, list) on success,
where list is an array of:

  {
    "index": 12,
    "isRead": true/false,
    "oaddr": "phone_number",
    "oname": "",
    "date": "17/07/24 16:15:34+04",
    "text": "text of message"
   }

Or callback(error_message) on failure
*/
ATSMS.prototype.list = function (index, callback) { ... }

/* Read one SMSs. Index is the index number of the message from 
`sms.list` or the `message` event.

Returns a callback(null, message) on success,
where message is:

  {
    "index": 12,
    "isRead": true/false,
    "oaddr": "phone_number",
    "oname": "",
    "date": "17/07/24 16:15:34+04",
    "text": "text of message"
   }

Or callback(error_message) on failure
*/
ATSMS.prototype.get = function (index, callback) { ... }

/* Delete a text message. Use the index from `sms.list`, or the string "ALL"
to delete all text messages. 
Calls `callback(null)` on success, or callback(err_msg) on failure
*/
ATSMS.prototype.delete = function (index, callback) { ... }

/* This initialises the modem on the specified serial port. 

Once initialised, new messages will fire a `message`
event. You must then call `.list` to get unread messages.
*/
function (serial) { ... }

Using

This page is auto-generated from GitHub. If you see any mistakes or have suggestions, please let us know.