Blockchain is a great technology, which can change our daily life and can help to improve a lot of other technologies. Since the release of Satoshi’s Paper we heard a lot of times, that bitcoin can replace money, but the truth is different. There are a lot of problems with Bitcoin and Ethereum transaction fees and payments time. There was a paper in 2016 called Bitcoin-NG: A Scalable Blockchain Protocol, which shows a way how to solve a lot of blockchain problems and make it much faster and reliable. Waves is one of the few blockchains where NG protocol is implemented already. Waves-NG gives a lot of opportunities and one of them is fast payments, which leads to another advantage — cheap payments.

Fast and cheap payments can be used in a lot of ways, but in Waves we love candies. Sergey Tolmachev, one of Waves developers, decided to make proof-of-concept for the candy machine, which accepts Waves cryptocurrency. This proof-of-concept can be used in any vending machine with small improvements and corrections.

Main parts

We found the Manual Rotation Torsion Candy Machine like below:

Manual Rotation Torsion Candy Machine

It costs about $3 and easy to find. It also has space for coins under the glass sphere. We can place there stepper motor to make it rotation automatic instead of manual.

28BYJ-48 Stepper Motor

One of the cheapest, but best options is 28BYJ-48 Stepper Motor. The 28BYJ-48 is a small 5 volt geared unipolar stepping motor.

We will use ESP8266WiFi 12e Witty to listen to the blockchain transactions and decide when we have to hand out a candy. The ESP-12 module is one of the most complete of the ESP family as it allows you to use the biggest amount of pins of all of them. It is possible to program this module to work stand alone with the Arduino IDE or with LUA as NodeMCU. This small chip has a lot of advantages, but the most important for us is WiFi.

ESP8266WiFi 12e Witty

As a power supply we decided to choose YwRobot Breadboard Power Supply MB-V2, it’s also cheap and simple, however, at the same time, it can be powered by a USB.

YwRobot Breadboard Power Supply MB-V2

The schema looks like below:

The other important thing is putting Stepper Motor into the Candy Machine, there is no easy way to it — only sawing and gluing, the result of the work is below:

Jump into the code

Before we can assemble all parts together we have to write some code. Let’s use Arduino IDE for it. Therу are few important things to realize:

We have to import ESP8266WiFi, for example, we can use this library We will parse JSON using a small chip, so it may be slow — our code should be efficient.

Lets start with inclides and global setting variables:

#include <ESP8266WiFi.h>

// Determines the type used to store integer values in JsonVariant.

// 0 - Long

// 1 - Long Long

#define ARDUINOJSON_USE_LONG_LONG 1



#include <ArduinoJson.h>



#include<AccelStepper.h>

We have to create AccelStepper object, passing 4 arguments to the constructor as for the unipolar stepper motor, controlled by 4 transistors.

AccelStepper myStepper(8, 13, 14, 12, 16);



// Steps per revolution for our motor

const int stepsPerRevolution = 4076;

Also we need to provide WiFi credentials to our Arduino chip:

// Wi-Fi settings

const char *ssid = "";

const char *password = "";

And some constants about used Waves Node, JSON fields and address, which we’d like to monitor. If you have your Waves Full Node you can use its URI, not the public one.

const char *balanceJsonField = "balance";



// Waves public node URI

const char *host = "nodes.wavesnodes.com";

const int port = 80;

const String address = "3P7CHn3nndASs6UqgUf9atBEgue7C4cANdY";



// Price of one candy

const unsigned long long price = 100000000;

Before the main logiс, we need to setup few options of stepper and WiFi, e.g. data rate in bits per second, desired constant speed in steps per second, connect to WiFi.

void setup() {

// Sets the data rate in bits per second (baud) for serial data transmission.

// For communicating with the computer we can use 115200

Serial.begin(115200);

Serial.println();



// The desired constant speed in steps per second. Positive is clockwise.

// Speeds of more than 1000 steps per second are unreliable.

myStepper.setMaxSpeed(700.0);

myStepper.setAcceleration(700.0);

myStepper.setSpeed(700);

myStepper.moveTo(myStepper.currentPosition() - stepsPerRevolution / 2);



if (ssid == ""){

Serial.println("WiFi ssid is empty.");

}



connectToWiFi();

}

Our logic will check address every N seconds and try to find transactions corresponding to a predefined address. If it finds transaction and the amount covers the price of candies stepper rotates and candies hang out.

We will use Waves Node REST API for repeated HTTP requests. The response in plain JSON, which looks like:

{

"address": "3PAQT8mofh7EYhvgtXfAgWkdW1b6ptNgXMJ",

"confirmations": 0,

"balance": 0

}

The loop function looks huge, but the majority of code debug information.

void loop() {



// Check WiFi connection and reconnect if needed

if (WiFi.status() != WL_CONNECTED){

WiFi.disconnect();

connectToWiFi();

}



if (candiesToGive > 0) {

if (myStepper.distanceToGo() == 0) {

candiesToGive--;

Serial.printf("[Candy was given, %d left]

", candiesToGive);

myStepper.moveTo(myStepper.currentPosition() - stepsPerRevolution / 2);

}

myStepper.run();

} else {

// Client will make HTTP requests and get JSON response

WiFiClient client;



Serial.printf("

[Connecting to %s ... ", host);

if (client.connect(host, port)) {

Serial.println("connected]");



Serial.println("[Sending a request]");

client.print(String("GET /addresses/balance/" + address) + " HTTP/1.1\r

" +

"Host: " + host + "\r

" +

"Connection: close\r

" +

"\r

"

);



Serial.println("[Response:]");

while (client.connected()) {

if (client.available()) {

// Parse JSON

JsonObject &root = jsonBuffer.parse(client);

if (root.containsKey(balanceJsonField)) {

root.printTo(Serial);

const unsigned long long newBalance = root[balanceJsonField];

Serial.print("

Updated balance: ");

printLLNumber(newBalance, 10);

if (balance != -1) {

int candies = (newBalance - balance) / price;

candiesToGive += candies;

}

balance = newBalance;

}

}

}

client.stop();

Serial.println("

[Disconnected]");

} else {

Serial.println("connection failed!]");

client.stop();

}

// Wait for the 5 sec. before next check

delay(5000);

}

}

There are 2 functions, which we didn’t define yet, but they’re called — connectToWiFi and printLLNumber. The first one tries to connect to the WiFi every 500 ms if needed, the second one prints long long number (its not supported by default). I’ll not show here the code of printLLNumber function, because its out of our business logic.

void connectToWiFi(){

Serial.printf("Connecting to %s ", ssid);

WiFi.begin(ssid, password);



while (WiFi.status() != WL_CONNECTED) {

delay(500);

Serial.print(".");

}

Serial.println(" connected");

}

The whole code can be founded on Github:

All together

We have working code and understanding of the schema, let’s put it all together in one great piece of love between Waves Blockchain and IoT.

The only important thing to add — QR code with an address. Users should send their waves to that address. The final view is:

That’s all! It works, accepts cryptocurrencies and uses internet of things to monitor the blockchain and process candies.

Here is the demo how it works:

Conclusion

The blockchain can make a huge shift of in our lives and even small examples like this show it. With the blockchain as a payment method you can buy anything in any place in the world, without cards and cash, without conversions of currencies and so on.

I think it’s very important in our time to be able to buy candies everywhere.