connecting...
Google translation for articles :
6 NEMBER donated to you!!

Using NEM Catapult to control multiple IoT devices

nem31.28xem (6) 682 0 4

Introduction

In this post, we are going to cover using NEM Catapult's aggregate transactions to control multiple IoT devices, as simple as two devices and one JavaScript file (optional as well!).

Image result for aggregate transactions
Lesson 2 in NEM: Aggregate Transaction
Aggregated Transactions merge multiple transactions into one, allowing trustless swaps, and other advanced logic. NEM does this by generating a one-time disposable smart contract. When all involved accounts have cosigned the aggregate transaction, all the inner transactions are executed at the same time. This is extremely crucial if you are issuing a series of actions to many devices at once, one future possible application of this is swarm robotics ( drones, self-driving cars, etc). For the rest of the examples, you can check it out here.

 

Controlling multiple devices 
The concept is very simple, using aggregate transaction to send messages to multiple devices at once, by announcing an aggregate transaction to a node, the devices can read the latest transaction according to the address of an account, and do appropriate action based on the message on the node.

Link to transactions here.

 

For a simple demonstration, I decided to use ESP8266 devices to control an LED by using GET method via REST API, further improvements can be added using relays to control fans or other electrical appliances, etc.

Espresso Lite 2.0 (left) controls the blue LED, NodeMCU(right) controls the red LED.

 

Using Vue.js to build a web app

At first, I decided to build a native mobile app using React-Native, but I couldn't get it to work with NEM catapult libraries, so I decided to build a web app instead.

Using Azure pipelines, I managed to deploy a website where you can send messages to multiple devices using aggregate transaction and display the transaction status. (To deploy your own Vue.js website, follow this tutorial link)

Demo

How does it work?

Try it yourself
Start by creating your own two test-net accounts, you can generate your own private key via online sha3-256 generators. My two devices are IoTDevice1 and IoTDevice2, we will need the address and private key of these two accounts. Keep in mind you need to install nem2-cli@0.13.0 via NodeJs for this to work.

npm install --global nem2-cli@0.13.0
nem2-cli profile create

Introduce network type (MIJIN_TEST, MIJIN, MAIN_NET, TEST_NET): MIJIN_TEST
Introduce your private key: 41************************************************************FF
Introduce NEM 2 Node URL. (Example: http://localhost:3000): http://13.114.200.132:3000
Insert profile name (blank means default and it could overwrite the previous profile): 

 

Download the GitHub code for the IoT Devices here, and go to file Arduino/LED1/LED1.ino, change the wifi settings and add in your public key. Finally, upload the code to your NodeMCU. Repeat this step for Arduino/LED2/LED2.ino with the same public key, and upload the code to your second device.

 

 

Announce a transaction by going to https://decentsensors.azurewebsites.net/, paste in the address of your two accounts. Enter message 1 to turn on the LED, enter message 0 to turn it off.

 

 

Finally, click the send button, the transaction should be announced successfully, copy-paste the link and open in another browser tab.

 

 

The group will be at unconfirmed status at first, but after a few seconds, once the confirmed status is up, you can check if your LEDs are on or not. (For better viewing of the JSON format, download the Google plugin for JSON Formatter)

 

 

Once your status is "confirmed", edit your link by removing the "/status' part, and you can see the details of your transaction.

 

 

Check your payload under the message group, for on status it should be '31' and for off status it should be '30'.

 

 

Github Repositories

React-Native with NEM Catapult

Vue.js with NEM Catapult

IoT Device Code

If you wish to see more of my work, you can donate XEMs (cryptocurrency) to the author via NEMlog, a blogger website powered by NEM blockchain!

Why don't you get crypt currency 'nem' by posting your blog article?

nemlog is blog posting service which has donation feature by crypt currency nem.
nemlog was launched to create environment which can be donated nem among NEMbers via blog articles.
Let's get nem by posting good blogs.

Nem prize event is being held frequently, Please join us on this opportunity!

nemlog registration from here
Register

NEMber who posted this article

Studying NEM blockchain technology to be applied in SWARM Robotics
28376
0

Why don't you read following articles?