Integrate Blockchain Transactions with anything by use of Amberdata API, Node-RED and a Blocklet node from Blockchainfirst

Blockchainfirst
11 min readMar 31, 2020

--

Blockchainfirst’s aim since the beginning in 2016 is to deliver functional, tangible real-life blockchain-enabled products and solutions for everyone. A huge part of our development efforts goes into the Blockchain & IoT Space. IoT, as well as Blockchain, are rapidly evolving markets.

Let’s get started

In this article, I want to describe how you can control IoT Devices by listening to Blockchain transactions, either as a simple payment or triggered by an event of a smart contract.

What is Amberdata?

Amberdata is a Blockchain Startup that provides real-time Blockchain data. They offer tools, APIs and SDKs for traders, investors, wallets, & developers. You can find more about Amberdata on their website at https://amberdata.io/. The nice thing about Amberdata is that they offer real-time Blockchain data across multiple Blockchains at a single API which makes their product pretty unique.

Prerequisites

In order to use this Demo, you should have a basic understanding of Node-RED. Node-RED was created by IBM and developed in nodejs, Node-RED is developed by Nick O’Leary and Dave Conway-Jones. Node-RED is a very powerful tool and it’s used not only by hobbyists but in the industry as well.

On Node-Red’s website, they describe it as a tool for wiring together hardware devices, APIs and online services in new and interesting ways. Because of the visual nature of node-red, it also makes IoT coding much more streamlined. You don’t need to be a fantastic coder in order to use node-red successfully. Check out the video to see something that node-red allows you to do quickly and easily. At the end of this article, we’re going to describe what we’ve done with node-red and the blockchain & crypto API from Amberdata.

One awesome thing about node-red is that there are already nodes that other people have made that you can add to your node-red installation. In our case, the amberdata-blocklet node, developed by us. This node is able to listen for blockchain transactions. Check out this video and get an overlook of how easy it is to control IoT Devices by utilizing our Amberdata-Blocklet node.

Before you get started, you may watch this video series about how to use Node-RED here.

Although you could install Node-RED on your local computer or in the Cloud and use our node, you should have a Raspberry Pi Computer in order to control physical devices such as a relay. A Raspberry Pi is a small but powerful Single Board Computer (SBC) and it’s pretty cheap (15–65 USD). You can buy a Raspberry Pi on Amazon. We recommend buying a Kit (Raspberry, Power Supply, SD Card, etc.) so that you have everything to get it up and running. If you want to control a physical relay as in this article described, you can buy a cheap one on Amazon or Gearbest as well.

There are tons of articles and videos online, how to set up a Raspberry Pi and installing Node-RED. Watch the following video about how to start with a Raspberry Pi.

How to set up a Raspberry PI

Because we want to listen for payment transactions, we also need a Wallet and some tokens. One of the most famous Ethereum Wallet is Metamask. Metamask is a Browser extension as well as a mobile App. Go to their website and install Metamaks in your Chrome Browser. Here is a Link about Metamask and how to install the extension. After you installed Metamask you should connect your Wallet to the Rinkeby Network. After you installed Metmask, let’s get some test tokens. Copy your Wallet address and got to https://faucet.rinkeby.io/. In order to get some test Ether, you have to follow the instructions. If everything went ok, you should see some Ether in your Wallet account. Now we still need a second account, we can send some token to. Create another account on Metamaks and try to send a small amount from one account to the other.

How to install and use our amberdata-blocklet node on Node-RED

If you made it this far, you should have a Raspberry Pi and Node-RED set up and running. As you might know already a node in Node-RED terms is a functional component you can connect with other nodes in order to create a so-called flow. Flows can be very simple as well as any complex. Basically, you can create flows to control an entire factory. But in this demo, we keep it simple at first. If you understand the basic idea of how to you use our blockchain node combined with other nodes, you can create real-world uses cases such as a blockchain-enabled Charging station, -Vending machine, Car Park, Smart-meter and a lot more. Literally speaking, the sky and your imagination is the limit. In further articles, we’re going to show how we’ve implemented all the aforementioned uses cases.

Let’s get it up running.

In order to install the node on your Raspberry PI, you need to login to the console via SSH. You can find the amberdata-blocklet node on Github.

ssh pi@<IP ADDRESS OF YOUR RASPBERRY>enter your password. The default password is "raspberry"

After you logged in, got into the node-red directory:

cd ~/.node-red

Install our amberdata-blocklet node:

npm i https://github.com/blockchainfirst/amberdata-node-red-blocklet.git

Start node-red by entering:

node-red

Once Node-RED is running you can access the editor in your browser by typing the IP address of your Raspberry Pi and the port number which is 1880 by default.

http://ip-address-of-your-pi:1880

If everything went ok, you should see a new node category “Blockchainfirst” on the left side (You may scroll a bit in order to find the category). Under this category, you’ll find the amberdata-blocklet node. Just drag this node to the canvas. If you double click on it, a menu appears on the right side. At first, let’s give the node a name. What about “Payment received”.

Now you need an API Key from Amberdata. After you signed in, you can check your API Key here: https://amberdata.io/user/api-keys. Enter your API-Key in the respective field. In the next field, you can select the network (Blockchain) you want to connect to. It’s totally up to you what Blockchain you choose. For this demo, you should choose “Ethereum Rinkeby Network”. In the address field, you have to enter your wallet address you want to listen to (it should be the second account in Metamak). Now you can save the configuration by clicking “ready” at the upper right corner. If the configuration was correct, your node should show “Connected” beneath the component. That’s it, now your Blocklet listens to incoming and outgoing payments on this particular address.

after configuration, you should see “Connected”

But what can I do now you may ask. I can tell you a lot but step by step. At first, let's check whether the node really receives transactions as expected. For this purpose, we got a debug node. You’ll find the node under the “common” category in the upper left side. Drag this node to your canvas close to your amberdata-blocklet. Every node in Node-RED can have multiple inputs and outputs. An output, for example, passes all its data (payload) to the input of the next node. You just have to connect them by clicking on the little square of the output and dragging the mouse to the input of the next node. You should see our amberdata-blocklet and the debug node are connected.

Our node is connected to the debug node

Info: Please keep in mind. In order to make a flow working, you always have to click the Button “deploy” at the upper right corner. Every time you change something on your flow, you have to click “deploy” again.

From now on all the incoming transaction data are passed to the debug node. To watch for debug information you have to click the bug icon at the upper right corner. Now it’s time to send some money around. For testing purposes, we use the Ethererum Rinkeby Testnet. Click on the Metamaks icon in your browser, select the account you want to send some Ether to. Click on the address. The address is copied to your clipboard. Now double click on the amberdata-blocklet node and paste the address of your account into the address field (don’t forget to click “deploy”). Open Metamaks again and change to the account where you got some Ether from the faucet. Click send, paste the address in the respective field, enter a small amount and click next and hit confirm to send the transaction.

After a few seconds, you should see some data appearing in the debug area like on the screenshot below.

You should see the transaction data in the debug area

Congrats now come the fun part.

As I mentioned you can create flows any complex by using different nodes, wiring them together and do some configuration. In this demo, we want to control a Relay. Let's say we want to open a barrier gate if someone sends a certain amount to your wallet address. The schematic of this looks like in the picture further down. If you don’t have a Relay, don’t worry you can create some interesting flows even without a physical device. For example, you can send an email to your email account if someone sends you some Ether. We’ll cover this in our video as well.

For controlling relays, Node-RED provides us with GPIO (General Purpose Input/Output) nodes. For our demo, we going to use an output node. You’ll find this node on the left side under the category “Raspberry PI”. Drag the lower node onto the canvas right below your debug node and double click on the node. You should see a menu like on the screenshot below. Click on the GPIO04–7 option. This is the output we connect the relay to.

Click “ready”. Now we still need to wiring up the connection between our amberdata-blocklet and the GPIO output node. As we did before with the debug node, click on the right output of the amberdata-blocklet and drag your mouse to the input of the GPIO node and click “deploy”. Your canvas should look like this.

Now we still need to physically wire up the relay with the Raspberry Pi. It should look like in the schematic below.

Schematic of Raspberry PI and a relay connected to GPIO-04

Let’s trigger the relay by sending money

As we did before, send a small amount of Ether to the address you entered in the config of the amberdata-blocklet. If we did everything correctly, the relay should turn on if the amberdata-blocklet node receives the transaction. Now you could connect the hot contacts of the relay to a door lock, a lamp or whatever comes to mind in order to control it.

ATTENTION: Before you wire the relay to the power grid, be aware that it can be very dangerous. Never ever touch the relay while it’s connected to the power grid !!!

Of course, it does not make any sense if the relay turns on only once after one single payment is received. At least the relay should turn off under some conditions. In a real-world use case like a door lock, for example, we would turn off the relay if someone went through the door or after a certain period of time. As we now know the possibilities of Node-RED already, it should not be a big deal to create such a flow. If we first describe the flow it would look like the following steps.

  1. if the node receives a blockchain transaction
  2. check if the transaction is an input (receiver address)
  3. convert the amount into USD (of course we have to take the rate of the particular token “BTC, ETH, BCH…” into account.
  4. check the received amount whether it is according to a reference amount.
  5. if so, turn on the relay
  6. wait until a photoelectric barrier is triggered (someone went through the door).
  7. or wait for a certain period of time.
  8. turn off the relay.

Actually it’s not that complicated if you dig a bit deeper into all the nodes and their respective functionality. A flow as I just described would look like this. Of course, we would need some additional hardware to make this door lock working. It’s totally up to you how deep you want to dive into the Blockchain & IoT space. I can tell you, for less than 100 USD you can create really cool stuff and awesome products in the Blockchain & IoT space.

Let’s finish our demo

As I mentioned the relay would turn on only once after the first payment transaction is received. At least we still need a function node that turns the relay off after a certain period of time. For this purpose, we got a node called “trigger”. You’ll find the trigger node under the functions category.

trigger node

Drag the node to the canvas. We need to put this node between our amberdata-blocklet and the GPIO node. One way of doing this is to drag the node onto the wire of the amberdata-blocklet and the GPIO node. If the wire turns into a dashed line, you can release the mouse. Now it should look like this (Don’t forget to click “deploy”):

The function of the trigger node is pretty simple. After the input of the node gets triggered, it sends a (turn on) signal to the output (the relay). Then it waits for a certain period of time and after this time it turns the relay off again. The next time a payment transaction is received, it turns on the relay and off.

Mission completed…

That’s basically it about our demo. As I mentioned what's nice about Amberdata is that it serves transaction data across multiple blockchains. If you want to accept Bitcoin, Bitcoin Cash, Stellar, etc. for your door lock, just drag further amberdata-blocklet nodes to the canvas, configure the Network and address as you did before and wire up the nodes to the trigger node.

Now your relay turns on and off no matter someone sends you BTC, ETH, BCH or XLM. Amazing isn't it.

What about security?

Security is pretty important, especially if we talk about Money and IoT. In this demo, we cannot take security into account. This is a separate topic for another article. If you want to secure your Node-RED server, you should read this article:

https://nodered.org/docs/user-guide/runtime/securing-node-red

What's next?

That’s it for this demo. I hope you I could give you an idea, of how powerful this solution can be. With the Blockchain API provided by Amberdata, Node-RED, and our amberdata-blocklet node, you can create a lot of cool stuff even if you’re not a highly skilled coder in both worlds.

We plan to develop more of this kind of nodes. Also, we plan to publish some more sophisticated flows, you can import into your projects.

I’m also curious about what you may build with what you’ve just learned.

If you have created an interesting flow or if you have any further questions, drop me an email at j.schaar@blockchainfirst.org.

In case we get some overwhelming feedback after releasing our amberdata-blocklet node, we plan to give some online courses about how to build real industrial-grade uses cases. We also would provide some ready to use hardware kits.

More about Blockchainfirst

--

--

Blockchainfirst

We are solutions & technology consultants specializing in blockchain and decentralized technologies.