Visualization in Arduino of the System Information Bar

Demonstration

  • Arduino WeMos D1 R2
  • Hardware Simulation

Introduction

The Internet of things is a concept which refers to a digital interconnection of daily objects with internet.

For the year of 2020, there is a forecast there will be 31 bill贸n divices and 4 billion people.


One of the divices which has revolucionized the concept of Do it yourself (known as DIY) is Arduino.

Arduino is an electric platform of open code based in hardware and software easy to use. The plates Arduino can read entries (light in a sensor, a finger in a bottom or a Twitter message) and turn it into an exit: activate a motor, turn on an LED, and publish something online. You can tell your card that it must send a set of instructions to the microcontroller on the card.

Objective

This article will show how to automate the deployment of information that is stored in a text file, in which its updated by the program System Information Bar.

Software

Hardware

Code

  • Repository in Github
    • Fritzing
    • Arduino IDE
      • Configuration
      • Display_on_Arduino_of_System_Information_Bar_-_WemosD1R2.ino
      // Display on Arduino of System Information Bar - WemosD1R2.c
      // v0.0.1 - 2018-10-07 - nelbren.com
      
      #include <ESP8266WiFi.h>
      #include "LiquidCrystal_I2C.h"
      
      const char* host = "104.251.217.217";
      const int   port = 80;
      const char* uri = "/si.txt";
      String httpPacket = "GET " + String(uri) + 
                          " HTTP/1.1\r\n" + 
                          "Host: " + String(host) + "\r\n" + 
                          "Connection: close\r\n\r\n";
      
      int n = 0;
      
      const char* ssid = "CHANGE-TO-YOUR-SSID";
      const char* password = "CHANGE-TO-YOUR-PASSWORD";
      const int addr = 0x27, en = 2, rw = 1,rs = 0, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
      LiquidCrystal_I2C lcd(addr, en, rw, rs, d4, d5, d6, d7);
      const int lcd_cols = 20;
      const int lcd_rows = 4;
      
      void wait_time() {
        String msg = "";
        char buffer[10];
        lcd.noBlink();
        sprintf(buffer, "%03d", n);
        msg.concat(buffer);
        lcd.setCursor(lcd_cols - 3, lcd_rows - 1);
        lcd.print(buffer);
        lcd.setCursor(lcd_cols - 1, lcd_rows - 1);
        lcd.blink();
        Serial.print(buffer); 
        Serial.print(",");
      }
      
      void lcd_print(String msg, int r = 0) {
        String msg2;
        int wc_x, wc_y;
        int l = msg.length();
        lcd.setCursor(0, r);
        if (l > lcd_cols) {
          if (r >= lcd_rows - 1) {
            msg2 = msg.substring(0, lcd_cols - 3) + "...";     
            lcd.print(msg2);
            wc_x = msg2.length()-1;
            wc_y = r;
          } else {
            lcd.print(msg.substring(0, lcd_cols));
            wc_x = lcd_cols - 1;
            wc_y = r + 1;
            lcd_print(msg.substring(lcd_cols), r + 1);
          }
        } else {
          lcd.print(msg);
          wc_x = msg.length() - 1;
          wc_y = r;
        }
      }
      
      void setup(void) {
        String msg = "My IP: ";
        Serial.begin(115200); 
        lcd.begin(lcd_cols, lcd_rows);
        lcd.setBacklightPin(3,POSITIVE);
        lcd.setBacklight(LOW);  
        lcd.home();  
        WiFi.begin(ssid, password);
        while (WiFi.status() != WL_CONNECTED) { delay(500); }
        IPAddress myIP = WiFi.localIP();
        msg.concat(myIP.toString());
        lcd.clear();
        lcd_print(msg);
        Serial.println(msg);
      }
      
      void display_error(String msg, int nn) {
        lcd_print(msg);
        lcd.setBacklight(HIGH);
        n = nn;
      }
      
      void read_stream(WiFiClient client) {
        unsigned long timeout = millis();
        while (client.available() == 0) {
          if (millis() - timeout > 5000) {
            display_error("Comm failed (2)!", 10);
            client.stop();
            return;
          }
        }   
        if (client.find("\r\n\r\n")){
          delay(5);
          unsigned int i = 0;
          String message = "";
          while (i<60000) {
            if(client.available()) {
              char c = client.read();
              if (c != '\r' && c != '\n') message += c;
              i=0;
            }
            i++;
          }
          Serial.println("\r\n[" + message + "]");
          lcd.setBacklight(HIGH);
          lcd_print(message, 0);
          delay(500);
          if (message.indexOf("*") == -1) lcd.setBacklight(LOW);
          delay(500);
        } else {
          display_error("Comm failed (3)!", 15);
        }
      }
      
      void loop(void){
        wait_time();
        if ( n <= 0 ) {
          WiFiClient client;
          n = 120;
          if (!client.connect(host, port)) {
            display_error("Comm failed (1)!", 5);
            return;
          }  
          client.print(httpPacket);
          read_stream(client);
        } else {
          delay(1000);
        }
        n--;
      }
      
      • [Display_on_Arduino_of_System_Information_Bar_-WemosD1R2.ino](https://raw.githubusercontent.com/nelbren/display_on_arduino_of_sib/master/arduino_ide/Display_on_Arduino_of_System_Information_Bar-WemosD1R2/Display_on_Arduino_of_System_Information_Bar-_WemosD1R2.ino)

Update of text file

  1. Install the program System Information Bar.

  2. Create the following program (sequence of commands):
    $ cat loop.bash
    #!/bin/bash
    # loop.bash 
    # v0.0.1 - 2018-10-07 - nelbren.com
    si=/usr/local/npres/bin/system/si.bash output=/home/hosting/npr3s/si/si.txt
    while true; do
      $si -n | cut -d"|" -f1 > $output
      sleep 120
    done
    
  3. Start the program:
    $ sudo nohup ./loop.bash &