Written June 6, 2017

Getting Started with Webtask.io and IoT

I was in a very interesting workshop this past weekend on Webtask.io presented by Auth0’s Glenn Block. Webtask.io brings to the table a thoughtfully designed in-browser editor, a powerful CLI and impressive startup times that live up to their messaging of “run(ning) code in 30 seconds”. While the tool chain appears to favor reactive implementations akin to AWS Lambda or Azure Functions, they do offer the capability to run jobs on a cron like schedule.
What better way to explore this feature set than to implement a trivial IoT device simulator … so let’s get started.

Get started by installing the Webtask CLI available here. Open a Powershell terminal, create a new folder in your source directory and cd into it: mkdir webtaks; cd webtasks Kick off npm init and set up a basic package/project scaffold.

  1. package name: webtask
  2. version: 1.0.0
  3. description: My First Webtask
  4. entry point: main.js Add the Azure IoT Device SDK for Node: npm install azure-iot-device-amqp --save Open VS Code in the project folder and create a new file called main.js

In main.js, add a shell function for the webtask that includes a callback to indicate function completion as follows:

Remove the body of the above function and add the Azure IoT Device library, a connection string and create a device client. You can follow these instructions to learn how to create an IoT Hub, add a test device and get a device connection string.

We’ll now define an array to hold logging data, create a simple logging method and alias the function completion callback passing it our log data.

The last elements we need are a callback for the client connect function and a call for the client to open it’s connection to IoT Hub.

Open a Powershell command prompt and create a scheduled Webtask: ```wt cron schedule "*/1 * * * *" .\main.js ``` This will create a new Webtask that will send our device data to Azure IoT Hub every minute. The newly created Webtask can then be monitored via the CLI by simply issuing `wt logs`. Take note however that this is a feed of all Webtasks running under your account. If everything is configured correctly you should see output similar to the following: To clean up the Webtask, simply issue `wt cron rm webtask` The full demo source code is available [here](https://gist.github.com/WilliamBerryiii/b6001c23247afdbfc31e1a6f28407f09.js?file=main.js). Happy Coding!