Writing Scripts

Please Note

TIP

Scripts are written in JavaScriptCore, the common part of each JavaScript dialect. Therefore, in order to use Scripts you need to know how to program in JavaScript.

Applications of the Scripts

Scripts can be used to

  • manipulate things and properties of the IoTHub,
  • connect different things and properties and create program logic inside the IoTHub,
  • read and write data from a database,
  • request and receive HTTP requests to access APIs of external systems,
  • read and write files,
  • send E-mails,
  • send push notifications to notify users on smartphones or tablets.
  • ...

Prerequisites for using Scripts

In order to use Scripts in the IoTHub, you have to keep in mind, that you need to have a license and installation file of the IoTHub, which also contains the Script Module. A decent understanding of JavaScript or similar scripting language is recommended.

Overview of the Scripts Functions in the Web UI

In this paragraph the basic functions of the Scripting Module are explained.

Open the Scripts Tab

To access the scripts, you need to log in to your IoTHub and then click on the Scripting tab in the navbar.

Navigation Bar
Navigation Bar of the IoTHub

Scripting Overview and Editing

The Scripting Overview gives you a list of all Scripts with their names. The list can be sorted by clicking at the column names like the "Name". Three cards give you further information how many scripts are running and how many are stopped.

Script Overview
The Scripting Overview gives you information about how many scripts are running. Select a script so start editing.

Once you selected one of the scripts, you can see the script code in the editor field. On top of the editor, multiple buttons are displayed for interacting witht he script.

Add Script
The menu bar gives you access to various control over a script, for example start and stopping it.

More information about what each action does can be found in the table below.

IconDescription
Downloads the latest version from the IoTHub
Saves/Uploads the version from the IDE to the IoTHub
Starts the script uploaded to the IoTHub
Stops the script running at the IoTHub
Refreshes the output windows
Edits the metadata of the script
Deletes the script from the IoTHub

A click on the context menu also gives you multiple options for deleting, searching and editing the script.

Script Details
The Script View shows the code of the current script as well as an button bar with multiple control actions.

Add a Script

Add a new script by opening the scripts menu and clicking on the Add new Script Item.

or clicking the Button directly. Then a dialog will open up for entering the unique name, human-readable title and a description of the script. Confirm your input by clicking the CONFIRM button.

Add Script
Clicking Adding A Script opens a dialog

Edit a Scripts Details

Edit a selected script's meta information by clicking Edit. A dialog will open up for entering the unique name, human-readable title and a description of the script. Confirm your input by clicking the CONFIRM button.

Edit Script
Edit a scripts meta information including name, title and description.

Script Editor

The script editor in the IoTHub contains e.g. the following functions:

  • Autocompletion
  • Syntax Highlighting
  • Right click support

Once you have a working script, you can save it to the IoTHub by clicking the Save button, then start the script by clicking the Start button. The Output Tab will show the output of the script.

If you are not sure where to start, you can start with the example scripts from the tutorial section.

Upload a Script

When the development of the script is completed or the intermediate state should be stored, upload the script by clicking on the button.

Uploading a Script is a necessary step in order to push the Script from the WebUI in the browser to the IoTHub. This is because the WebUI and the IoTHub can be located on different computers.

If your Script was uploaded successfully, the pop-up 'Script successfully updated' will appear on the bottom.

Control a Script

In order to control the Script you find different functions in the toolbar on the top right. These controlling functions will be explained in the following paragraph.

Script Actions
Start, stop, refresh and delete a script with the script actions on the top right of the Editor.

These can also be found in the table above.

Start a Script

In order to get a Script running you have to click on the button. If the Script started successfully, the black icon in the middle will switch to .

The view will then switch to the Output View.

Stop a Script

In order to stop a running Script you have to click on the button. If the Script stopped successfully, the black icon in the middle will switch to .

Download a Script

In order to see the current version of a Script, which is stored on the actual IoTHub, in your WebUI, you have to click the button. If you have downloaded the Script successfully, your WebUI gets updated automatically, and you see the downloaded Script in your WebUI.

As mentioned before the WebUI and the actual IoTHub can be located on different machines. This means the version in the WebUI might be different from the one stored on the IoTHub.

Downloading a Script

Note that downloading a Script will not download it to your computer but will update the script in the WebUI. This will overwrite your unsaved changes.

Delete a Script

Delete a Script

Please note that deleting a script, deletes it for good. You can not undo this action.

If you do not need the script anymore, you can delete it by clicking on the button.

Then you will be asked if you are sure, that you want to delete this script.

If you are sure, then confirm by clicking DELETE. If not, then cancel the deletion by clicking on CANCEL.

Once the Script is deleted successfully, a pop-up will appear at the bottom saying 'Script successfully deleted.'

Output View

The Output View shows the outputs of the Scripts in chronological order. Therefore, the latest output appears on the bottom.

Navigation Bar
The Scripting Output shows the Javascript console output

The outputs of the Script in the Output View are refreshed when switching from the Editor View.

Updating the Output View

Be sure to click on the button, which you can find on the top right as the output will not refresh periodically.

Characteristics of Scripts

The scripts have some special characteristics, that will be described here, in order to understand how the scripts work.

Asynchronous

Scripts are asynchronous. This means

  • that each script runs in its own process,
  • that different scripts can be run parallel,
  • that the different scripts are not synchronized with each other,
  • that therefore one script does not notice whether another script crashes or not,
  • that scripts can only be connected to each other through things and their properties.

Multiple Scripts

In one IoTHub you can create as many scripts as you like. If you create an application with one script or with several scripts is up to you. Creating different scripts makes sense, if different scripts perform different processes, different scripts can be performed independently of each other. If you can split up a process into different scripts, splitting up could be beneficial because you can more easily reuse a script for another IoTHub, if one script crashes, not all scripts crash, you have an easier overview of all the processes performed as scripts; this helps to maintain scripts.

WoT Scripting API

The data model of the IoTHub is based on the WoT Specification. As a base for writing scripts the WoT Scripting API used. A deeper introduction into the usage can be found at

  • Producing a Thing
  • Discovering and Consuming a Thing
  • Reading, Writing and Observing Properties
  • Invoke Actions

Available Node Modules in scripts

The scripts are written in JavaScript and allow to use a bundle of node packages.

How to Use a Node Module

You have to require a Node Module in order to use it.

const axios = require("axios");

List of Node Modules

The following Node Modules are available:

NameVersionLinkUsage
axios0.19.2https://www.npmjs.com/package/axios/v/0.19.0open in new windowHTTP client
bcrypt-pbkdf1.0.2https://www.npmjs.com/package/bcrypt-pbkdfopen in new windowCrypthography
form-data2.3.3https://www.npmjs.com/package/form-data/v/2.3.3open in new windowTransforms files and forms into a HTTP compatible format
is-buffer2.0.3https://www.npmjs.com/package/is-buffer/v/2.0.3open in new windowChecks a variable for being a Buffer object
is-typedarray1.0.0https://www.npmjs.com/package/is-typedarrayopen in new windowChecks a variable for being a typed array
isarray1.0.0https://www.npmjs.com/package/isarrayopen in new windowChecks a variable for being an array
ms2.0.0https://www.npmjs.com/package/ms/v/2.0.0open in new windowConverts various time formats to milliseconds
mssql5.1.4https://www.npmjs.com/package/mssql/v/5.1.0open in new windowMS SQL driver
mysql2.18.1https://www.npmjs.com/package/mysql/v/2.17.1open in new windowMySQL driver
nodemailer6.5.0https://www.npmjs.com/package/nodemailer/v/6.2.1open in new windowSends E-Mail
oauth-sign0.9.0https://www.npmjs.com/package/oauth-signopen in new windowOAuth signing
sqlstring2.3.1https://www.npmjs.com/package/sqlstring/v/2.3.1open in new windowSimple SQL escape and format for MySQL
uri-js4.4.1https://www.npmjs.com/package/uri-jsopen in new windowRFC 3986 compliant, scheme extendable URI parsing/validating/resolving
Last Updated: