Published 3 December 2018

Credits LED logo for blockchain platform monitoring

The idea of blockchain monitoring

The idea of blockchain monitoring

The company received the original LED "Credits" logo (Figure 1) as a gift, after which we had the idea of its "revival" and use to monitor the operability of the blockchain platform. The solution should clearly demonstrate the working capacity of the Credits platform API for external systems.

Figure 1 Credits.com LED logo

Solution description

Solution description

The number of the last blockchain block, which increases after each successful round, is selected as a basic metric that allows you to judge the working capacity of the blockchain. Thus, we get the ability to turn on or turn off the LED logo, depending on the appearance of new blocks in the blockchain.

One of the options for obtaining the last number of blockchain block is the use of monitor.credits.com web service. Using the monitor.credits.com cloud service allows you to significantly simplify the monitoring task, because it already contains the necessary infrastructure in the form of separate Credits nodes, and a separate deployed RPC service on the basis of Apache Thrift, written in C# language to obtain the required metric over the HTTP protocol.

As mentioned above, the Credits platform API is implemented using the Apache Thrift framework (thrift.apache.org). Thrift is the language of the interface description and allows you to define RPC services for different programming languages.

Thrift includes several levels to work with the client and the server part. The top level is the generated Thrift description code, from which the services generate client and server code.

Description of the API in the form of a Thrift file for Credits platform can be found on GitHub.

The C# client on the web server is formed based on this file, resulting the web server giving client the number of the last block by the link: http://monitor.credits.com/main/getlastblock.

An example of a monitor.credits.com web service that uses the Credits.com API is shown in Figure 2.Figure 2 monitor.credits.com service

To reduce the load of network requests, we will check the last block number every 30 seconds. The LED logo should be on if the last block number increases as a result of each request.

In case the number of the last block was not obtained, or it did not increase, the logo should go out.

The logo should automatically turn on if a new block is received and its number is greater than the last received block.

To solve the problem, the solution on Arduino platform as the most popular one was chosen due to ease of use and low entry threshold. With basic C/C++ programming skills, you can learn the basics of working with Arduino in a matter of hours.

During the lifetime of Arduino, the Internet has accumulated a significant amount of resources to work with this platform, which allows you to successfully use this experience in solving a wide range of tasks.

Development of sketches (programs) for Arduino is conducted using the "Arduino IDE" programming environment. Debugging of devices is possible through USB port from a computer running OS Linux, Windows, Mac OS.

Troyka-modules of Amperka.ru company were used when constructing the gadget.

"Troyka-modules are devices that contain all the necessary elements and harnessing to quickly connect electronic components to the control board." Any Troyka-module can be connected to the control board with the help of "plug to jack" wires, which allows you to quickly assemble the necessary circuits without using a soldering iron.

Arduino for monitoring

Arduino for monitoring

The choice of Arduino UNO to implement the solution is primarily due to its popularity as an inexpensive and convenient tool for prototyping programmable electronic devices. The board is available in Amperka.ru online store, as of November 2018 it costs $ 26

Figure 3 Arduino UNO R3 board

Arduino Uno is a development platform based on the ATmega328P microcontroller. Digital inputs/outputs, PWM outputs, analog inputs, 16 MHz quartz crystal, USB and power supply connectors, hardware reset button are available.

The Arduino Uno can be powered via a USB connector (+5V) or via an external power supply (VIN) connection. In the second case, the stabilized voltage should not exceed 12V.

In our case Arduino Uno is powered by an external power supply with a voltage of 12V, and the power of the LED tape of the Credits logo is taken from the VIN pin.

Additionally, Arduino Uno has a 5V, 3.3V, and the GND ground pins.

When you connect Arduino Uno to a computer, it is determined as a COM port. This port is used to load the sketch into the device and read debug messages. An example of Arduino IDE screen with a COM port connected to the Arduino Uno is shown in Figure 4.

When power is initially supplied to the device (when the sketch is loaded), the setup() function is executed once, in which the device is initialized, after which the main loop() cycle is started.

Figure 4 Selecting a port in Arduino IDE

Connecting the WiFi module

Connecting the WiFi module

Use a WiFi wireless connection using the Troyka WiFi module to communicate with the HTTP server (Figure 5). The Troyka WiFi module is built on the ESP-12 module with the ESP8226EX chip. This module can work in "AT-commands" mode (this mode is enabled by default) and in controller mode. The cost of the module as of November 2018 is 890 rubles.

To transfer the module into the controller mode it is necessary to "upgrade" it by the corresponding firmware.

The TX and RX module contacts connect to Arduino Uno to the pins 8 and 9, respectively. Since Wi-Fi Troyka can use both 3.3V and 5V power, we connect it to the 5V pin of Arduino Uno board, and G contact – to GND pin.

Figure 5 WiFi Troyka module

Working with the module in the "AT command" mode is carried out from the control board over the UART protocol. In addition to standard pins (signal, power, ground), the board displays a signal pin P, which is used to put the module in sleep mode or low power consumption.

Functional diagram of the module is shown in Figure 6

Figure 6 Functional diagram of WiFi Troyka module

Connecting the LED logo

Connecting the LED logo

The LED logo is connected to Arduino Uno via a power key made in the Troyka module format. Cost of the power key as of November 2018 - $ 2.86.

PWM signal can be used to adjust the voltage to load in a wide range, and thus control DC load.

The module is designed for switching the load of DC voltage up to 30 and current up to 12A. The load is connected to the pads by its pins under the screw L. The negative contact of the LED logo is connected to the L-contact and positive – to the L+ contact.

As in our case power of the LED indicator is taken from the VIN Arduino Uno pin (from 7 to 12V when connecting the board through an external power supply), the pins of the pad P+ and P- remain unused, and it is necessary to install a jumper to join the power V=P+

Contact S is connected to the pin 10 of Arduino Uno, contact V – to pin VIN, contact G – to GND.

Figure 7 Troyka power key

Functional diagram of MOSFET Troyka module power key is shown in Figure 8

Figure 8 Functional diagram of MOSFET Troyka module

The device diagram is shown in the figure. Power must be supplied from an external power unit providing a stabilized voltage from 7V to 12V. To do this, the Arduino Uno Board provides a 2.1 mm connector for external power supply, voltage is supplied by the central contact.

Figure 9 Component connection diagram

Figure 9 shows the connection diagram for all components.

Sketch development

Sketch development

WiFi setting

The WiFi module supports 2 modes of operation – AT commands mode, and programmable mode of microcontroller. The sketch uses the AT command mode because it is the one that is in the module straight "out of the box".

The interaction of Arduino and WiFi module is carried out via Serial port, i.e. it is necessary to connect RX and TX contacts of Arduino Uno board and Troyka WiFi. But the hardware Serial port is already used for Arduino board firmware, so the interaction between the boards will be implemented through the software SoftwareSerial port. The listing shows the definition of the software Serial port on outputs 8 and 9.

// library to work with software Serial

#include <SoftwareSerial.h>

// сreate an object to work with software Serial

// and transfer it TX and RX pins

SoftwareSerial mySerial(8, 9);

#define WIFI_SERIAL    mySerial // Override serial-port, to which the Wi-Fi module is connected

Interaction with the WiFi Troyka module in the AT command mode is to sequentially record the Software Serial commands and read the response from it.

In the course of testing, it became clear that writing and reading the strings to the Software Serial buffer was unstable, so the functions of byte read/write to/from the Software Serial port were written.

void writeString2Serial(String stringData) { // Used to serially push out a String with Serial.write()

 for (int i = 0; i < stringData.length(); i++)

 {

   Serial.write(stringData[i]);   // Push each char 1 by 1 on each loop pass

 }

}// end writeString

void writeString2WIFI_SERIAL(String stringData) { // Used to serially push out a String with Serial.write()

 for (int i = 0; i < stringData.length(); i++)

 {

   WIFI_SERIAL.write(stringData[i]);   // Push each char 1 by 1 on each loop pass

 }

}// end writeString

Work with AT commands is written by the sendATcommand() function, which sends a command to the Software serial port and waits for response for a set period of time. The "ATcommand" command, the expected response "expected_answer" and the response time-out in milliseconds "timeout" are passed as arguments to the function.
Additionally, the response of the WiFi module is analysed for the occurrence of the string «HTTP/1.1 200 OK». Detection of this string allows to determine that the sent data is the response of the HTTP server to the GET request, after which the number of the last block is allocated from the received data.

int8_t sendATcommand(String ATcommand, String expected_answer, unsigned int timeout){

   uint8_t answer=0;       // Returns if the expected_answer was not obtained

   String response = "";   // Response to AT command

   unsigned long previous; // Time of sending AT command

   delay(100); // Delay to be sure no passed commands interfere

   while( WIFI_SERIAL.available() > 0) WIFI_SERIAL.read();// Wait for clean input buffer

   writeString2WIFI_SERIAL(ATcommand);// Send the AT command

   previous = millis();

   // this loop waits for the answer

   do{

       // if there are data in the UART input buffer, reads it and checks for the asnwer

       if(WIFI_SERIAL.available() != 0){    

           char c = WIFI_SERIAL.read();

           response += c;

           // check if the desired answer is in the response of the module

          /* if (strstr(response, expected_answer) != NULL){

                answer = 1;

            }*/

           if (response.lastIndexOf(expected_answer) != -1) {

             answer = 1;

           }

       }

   // Waits for the answer with time out

   } while((answer == 0) && ((millis() - previous) < timeout));    

   // Search for block number in the server response

   if (response.indexOf("HTTP/1.1 200 OK") != -1) {

     int p1 = response.indexOf("close\r\n")+8; //The first string after the "Connection: Close" command

     int p2 = response.indexOf("\r\n",p1);     //Pointer to the next new string

     p1 = p2;                                  //The first pointer to the beginning of the string and block number

     p2 = response.indexOf("\r\n",p1+1);       //The second pointer to the beginning of the string and block number

     String temp = response.substring(p1,p2);  //Copy the string with the block number

     numLastBlock = temp.toInt();              //Set global variable

     Serial.print(temp);

   }

   Serial.println(response);

   return answer;

}

To get the block number from the HTTP server, the string array containing the required AT command set is used:

// Array of AT commands to receive data from HTTP server

#define AT_CMD_ARRAYSIZE 13

String at_commands[AT_CMD_ARRAYSIZE] = {

 "AT+CIPSTART=\"TCP\",\"monitor.credits.com\",80\r\n", //Setting up TCP connection monitor.credits.com:80

 "AT+CIPSEND=33\r\n",                                  //Sending request 33 bytes

 "GET /main/getlastblock HTTP/1.1\r\n",                //GET request 33 bytes

 "AT+CIPSEND=25\r\n",                                  //

 "User-Agent: arduino/0.1\r\n",

 "AT+CIPSEND=27\r\n",

 "Host: monitor.credits.com\r\n",

 "AT+CIPSEND=13\r\n",

 "Accept: */*\r\n",

 "AT+CIPSEND=19\r\n",

 "Connection: Close\r\n",

 "AT+CIPSEND=2\r\n",

 "\r\n"

};

Calling AT commands is implemented in the getNumBlock() function. In the last AT command, instead of the OK response, we expect a "Close" response

void getNumBlock(){

 //Sequentially run AT commands from the array

 for (int i = 0; i < AT_CMD_ARRAYSIZE-1; i++) {

   sendATcommand(at_commands[i],"OK",5000);

 }

 //The last command to wait for all data  

 //from the server and close the connection

 sendATcommand(at_commands[AT_CMD_ARRAYSIZE-1],"CLOSED",5000); //This command save value in numLastBlock

}

Load management and implementation of logic to turn on the Credits logo

Load management and implementation of logic to turn on the Credits logo

LED illumination is controlled via PWM (PWM – Pulse-width modulation) port

#define LED_PIN 10              // PWM port, to which load is connected via the MOSFET transistor

Modulation allows you to smoothly turn the led indicator on and off. The MOSFET Troyka module contains an additional LED indicator that lights up when the transistor is opened. This allows you to customize the sketch without attaching the load initially. The power is controlled by the command

analogWrite(LED_PIN, brightness);

where brightness can take a value from 0 (disabled) to 255 (full load).

Turning on/off the LED logo is implemented in the main sketch cycle. Every 30 seconds, the getNumBlock() function is executed, and the incBlock  increment is determined from the last time it was run. If incBlock is greater than zero, the brightness value is set to 125 (50% PWM signal power), otherwise the brightness value is set to 0 – the logo is turned off.

 if(millis() > time_now + 30*second){ //run every 30 second

   time_now = millis();

   numPrevLastBlock = numLastBlock;

   getNumBlock(); //Get the current block number to the numLastBlock variable

   incBlock = numLastBlock - numPrevLastBlock;

   if (incBlock > 0){

     brightness = 125; // 50% brightness

     Serial.println(incBlock);

     Serial.println(brightness);

   }

   else{

     brightness = 0;

     Serial.println(incBlock);

     Serial.println(brightness);   

   }

   analogWrite(LED_PIN, brightness);

 }

23 Useful article

Contents