The IOTA Data Marketplace: a technical introduction
The full article was originally published by Alexey Sobolev on Medium. Read the full article here.
Part 3: Publishing Sensor Data
Let me re-introduce the screenshot from the previous article, so we can start from there.
Click the “Download Publish Script” link at the bottom of the sensor card. This will initiate the download of a preconfigured .zip archive.
Once you download and extract the content of the archive, please open an IDE of your choice to start working with the script. I suggest you use a simple yet powerful IDE like Atom or VS Code. You may also use a simple Text Editor if you prefer.
The archive (aka project) structure is fairly simple, and contains a number of .js and .json files, along with the project documentation (README.md).
Please always read the README.md before you start using the script.
- Install Node.js (if not done already).
- Check Node.js version. Run “node -v” in your console or terminal.
We support Node.js version 8 and above. Please update if your Node.js version is older.
- Install a package manager (if not done already). I suggest Yarn or NPM.
- Carefully read the README.md file.
- Install packages by running yarn or npm install, depending on your package manager.
The script is pre-configured to publish data for the selected sensor. You’ll find the sensor ID and it’s secret key in the config.json file.
If you decide to use the same script for multiple sensors, please note that the secret key should be changed as well. Otherwise you won’t be able to decrypt your published data.
By default the script runs in debug mode, which means that no data is published. All captured data is printed out in the console, so you can verify and adjust it. Once the payload looks good, you can disable debug mode by setting the debug variable to false in the config.json file (see screenshot below), and let data be published.
Please note that the Proof-of-Work operation is conducted for every data package, which might take up to 60 seconds depending on your hardware. Please take this into account and do not interrupt the script while running this operation.
Processing Sensor Data
At this point we’re not targeting any real sensor. In this tutorial, we will read data from a static file and by requesting an API endpoint, that should provide data.
A library that reads real sensor data will require a way more technical know-how and also will be too specific to a selected sensor.
The index.js file comes with 2 examples:
- Example 1 demonstrates how to read sensor data from a static file or database. For this example we provided a data.json file with sample data.
- Example 2 demonstrates how to run the script in an infinite loop to query data at specified time intervals. It reads data from a remote server and contains a condition to break the loop (i.e. stop execution) once a defined status code is received. For this example we utilize a server which serves sample data. To modify the server URL, please update the serverUrl field from the config.json file in same folder.
Let’s see how to use each of the provided examples:
1Example 1 reads data from a JSON file. We assume that this file contains an array of data sets, where each data set has data relevant for the payload.
Alternatively you can implement functionality that reads data from a database of your choice.
Once you secured the source of data, process it in a loop and send payload to the publish() function, that does all the necessary job for publishing your data in Tangle.
In the provided example we included a debug functionality, so you can safely verify your payload without publishing it to Tangle.
If you know what you’re doing and absolutely sure that your payload is correct, your publish script could shrink to only 3 lines of code:
2Example 2 requests data sets from a remote server in an infinite loop. For this example we utilize the Star Wars API which serves sample data. To modify the server URL and point it to your server, please update the serverUrl field from the config.json file in same folder.
Since some calls to this API might result in an empty response, and total dataset currently contains only 73 entries, we additionally introduced logic that randomly requests one data set from the server. In other words, the function getRandomInt is only relevant for the example shown and not necessary for capturing your sensor data.
A simplified version of this script could look like the code below:
We covered a lot today, so please take your time to review the article, play with the script, and ask questions.
Part 3: Publishing Sensor Data