Using Node-RED with Twilio

For a while now I’ve been using the most excellent Node-RED to provide a way of integrating my various sensors and devices around the house. One thing that was lacking is a general mobile control or status update facility. This could be achieved using browsers on smartphones or custom made Android or iOS apps. These have the problem that they take up resources, are platform specific and may not always work if there is no data signal available. Using SMS to receive notification and perform simple control and status request operations would therefore seem to be the ideal solution. There are a number of providers that provide an API to send SMS but this is all they do. Having a service that enabled both sending and receiving SMS plus the ability, if needed to use voice calls would be ideal. Such a service is provided by Twilio. You can sign up for free and try a limited version of the service. Once you upgrade and start paying for the service you can choose your own number instead of the number allocated originally. You can also have multiple numbers, there is no Twilio banner at the start of your SMS and no Twilio announcement at the start of each call.


Twilio already provide extensive documentation and a number of libraries to use their service. They conveniently provide a helper library for Node.js so this was a perfect starting point for the Twilio node. The node I created is a basic output only node as this is what uses the helper library. The input side can be implmented using existing Node-RED nodes.

The Twilio node is currently not yet pary of the Node-RED package, but this is in progress.

The Node-RED Twilio output node

The Twilio output node is configured with a twiliokey.js file located in the directory (or folder) above node-red. This includes your Account-SID, Auth-Token and the telephone number that you will be sending SMS from.

2013-11-08 10.41.20

To send a SMS to a number, the topic parameter of the msg object must be set to the number, e.g. +4416358000xx and the payload parameter is the actual message that is to be sent.

In a simple test, you would use an inject node to create the msg topic and payload, this would feed directly into the Twilio node to send the SMS. How this is used would be up to your own imagination and requirements.

Receiving SMS

To receive and process SMS from Twilio is a little more involved, however there isnt a node for this and it can be easily implemented using the available Node-RED nodes. Input is handled using a http input node. This URL must be available to the Twilio servers so you may have to set up a dynamic dns service such as to point to your IP address. Fortunately my BT HomeHub supports this service. in addition to this, you’ll also need to setup port forwarding to your internal Node-RED server. This URL mus then be configured in the Twilio Messaging Request URL field. The purpose of this URL is that it returns a TwiML XML file to tell Twilio what to send as a response.

The basic function to create the response message could be:

var messageObj = { "Message": "You sent: " + msg.req.query.Body } ;
msg.payload = messageObj;
return msg;

The JSON msg object must be converted to XML format using the json2xml node and specify a root element of Response. The final node is the http response node that then sends the reply to Twilio. Once Twilio receive the XML they will read the message and send the reply to the originator of the SMS. This could be as simple as a status update or an acknowledgement that the command had been processed.

The received message can be processed to look for key words and commands to perform actions such as returning the status of a switch or sensor or just acknowledging that a light was switched on as requested.

Example usage:
2013-11-08 17.16.56

The usage described here only relates to sending and receiving SMS, however it is possible with little more effort to use voice calls instead. I’ve done a simple test where the number is called and a voice reads a custom message. Other examples include having your computer call you up and tell you that a window has been opened on your property. The possibilities for communication with mobile devices in this way is only limited by your imagination.

Bookmark this post: bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Comments are closed.