Wait a moment...

Using NEM Catapult to control multiple IoT devices

Hank Bot
nem32.13xem (7)
2019-09-04 15:36:13
Using NEM Catapult to control multiple IoT devices


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)


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 your private key: 41************************************************************FF
Introduce NEM 2 Node URL. (Example: http://localhost: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!

Studying NEM blockchain technology to be applied in SWARM Robotics