Using webscript.io to check your device status

When building an IoT system, no matter if it sends sensor information or it receives some triggers, it is always good to have a mechanism that checks the device status and alerts you in case of a power or a network failure. There are several ways to do that, and it always depends on the type of communication mechanism you use for data exchange. For example, if you use MQTT, the broker can tell when a link with a client goes down. Or, if you simply post sensor values to a service like Cosm, there are ways to check whether values are updated or not.

Most of the methods require from you to have access to the server and add some code/script that will alert you when some condition occurs (link breaks, values not updated, etc.). What if you don’t have access to the server? Another approach is to add some code at your device that regularly posts a request on some service to declare availability. But then you need to implement the service and take care of hosting as well.

I recently came across Webscript and I think it is pretty useful for (among other things) using it as a way to check your device status. Webscript allows you to write web scripts in Lua. Scripts get their own url through a user-defined subdomain on webscript.io (like http://arduino.webscript.io) and can be also scheduled for periodical execution through  a cron-like manager. What I have found amazing about Webscript is that it is free and it has a very simple interface for writing and running your own script in minutes. It comes with very good documentation and many examples covering anything from simple ‘Hello world’ examples to SMS texting and payment transactions!

Webscript online Lua editor, comes with syntax highlighting!

Let’s see how you can make a script that will check your device’s status.

The concept is that your device (let’s say your Arduino) will post every x minutes to Webscript to let your script know that it is ‘alive’. At the same time the script itself will check every y minutes (y>x) through cron if the script has been updated. If not it will use twilio to send you a text message alerting you about!

Create a new script through here (preferably sign up first and login, although you can create scripts without any registration) and give it a name-subdomain so that it can be accessed remotely. Then add the following code:


local twilio = require('webscriptio/lib/twilio.lua')
local ACCOUNTSID = '<YOUR TWILIO ACCOUNT SID>'
local AUTHTOKEN = '<YOUR TWILIO AUTH TOKEN>'
local THRESHOLD = 10

live = request.query.live
if live == "1" then
storage.time = tonumber(os.time())
end
if (tonumber(os.time()) - tonumber(storage.time)) > THRESHOLD then
dead = true
else
dead = false
end

if dead==true then
local response = twilio.sms(ACCOUNTSID, AUTHTOKEN,'+1234556789', '+1987654321','Oh no something is wrong with the Arduino!')
end

return {dead=dead, time=tostring(storage.time) }

The code initially imports the twilio library (for texting messages) and then reads the value of the variable ‘live’ from the URL request. If the script URL is requested by the Arduino, it will have the live=1 value, so the script saves the timestamp of the request. If the script URL is requested by cron, it just calculates the difference between current time and the saved time (last request from Arduino). If the difference is greater than a threshold (600 seconds in this example) it uses the twilio library to text an alerting message to a phone number. For debugging purposes (so you can check on your browser) it returns the status of the device and the saved timestamp. When done, remember to click the ‘Save’ button. That’s it!

To test the script, simply open in your browser the link of your script, adding the live=1 parameter:

http://arduino.webscript.io/?live=1

You should get something like:

{"dead": false, "time": "1351790077"}

For testing you might want to use a smaller threshold value in your code, like 10 seconds. Now wait a bit more than the defined threshold and open the script url:

http://arduino.webscript.io

Now you should get something like:

{"dead": true, "time": "1351790320"}

and if you have properly set your twilio credentials in the script, a text message shall soon arrive on your phone!

To use the webscript with your Arduino, simply add the following method in your code and call it regularly from the loop() function:


void keepAlive() {

if (client.connect("arduino.webscript.io", 80)) {
client.println("GET /?live=1 HTTP/1.1");
client.println("Host: arduino.webscript.io");
client.println("Connection: close");
client.println();
}

}

Finally, go back to Webscript, on the Cron tab, and set your webscript to be invoked every e.g., 10 minutes:

Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>