Arduino でサーバーから情報を取得して結果を LCD の LiquidCrystal から出力する

Arduino でサーバーから情報を取得して結果を LCD の LiquidCrystal から出力します。
![EuWWz1KU4AMqb3x.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/115935/9050135c-cf84-169f-245f-4aff540629b8.jpeg)

使用機材

|項目|機材名|
|---|---|
|使用しているArduino |arduino nano|
|書き込みブートローダ |atmega328old|
|LAN シールド|[ENC28J60](https://www.diymore.cc/products/enc28j60-ethernet-network-module-shield-v2-0-for-arduino)|
|LCDディスプレイ|16 x 2 の LiquidCrystal ( I2C 付き )|

GPIO端子の接続設定

|センサー端子|Arduino端子|
|---|---|---|
|SDA|A4|
|SDL|A5|
|VCC|5V|
|GND|GND|

ソースコードはこちら

たまにサーバーへの接続が失敗しますので、その時は再起動を行ってみてください。

````c:I2CLiquidCrystalDisplay.ino
//=================================================================================
// I2C Scanner scan i2c address (ex 0x27
//=================================================================================

// Written by Nick Gammon
// Date: 20th April 2011

/*
#include <Wire.h>

void setup() {

  Serial.begin (9600);
  Serial.println ("I2C scanner. Scanning ...");
  byte count = 0;
  
  Wire.begin();
  
  for (byte i = 8; i < 120; i++){

    Wire.beginTransmission (i);

    if (Wire.endTransmission () == 0){
      
      Serial.print ("Found address: ");
      Serial.print (i, DEC);
      Serial.print (" (0x");
      Serial.print (i, HEX);
      Serial.println (")");

      count++;
      
      delay (1);  // maybe unneeded?

    }

  }

  Serial.println ("Done.");
  Serial.print ("Found ");
  Serial.print (count, DEC);
  Serial.println (" device(s).");

}

void loop() {}  

*/

//=================================================================================
// New-LiquidCrystal Please download and unziped files into Arduino/Libraries
//
// fmalpartida/New-LiquidCrystal: Clone of the new liquid crystal library from: 
// https://bitbucket.org/fmalpartida/new-liquidcrystal https://github.com/fmalpartida/New-LiquidCrystal
//=================================================================================

// LCD setting
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd( 0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address, if it's not working try 0x27.

// LCD customize character
// https://maxpromer.github.io/LCD-Character-Creator/
byte musictone[8] = { // ♪
    B01100,
    B01010,
    B01001,
    B01001,
    B01010,
    B11000,
    B11000,
};
byte checkmark[8] = { // チェックマーク
    B00001,
    B00001,
    B00010,
    B00010,
    B10100,
    B11100,
    B01000,
    B00000
};

// LAN setting
#include <UIPEthernet.h>
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x78, 0xEE  };  
IPAddress ip(192, 168, 0, 117);
IPAddress myDns(192, 168, 0, 1);                        
EthernetClient client;

// setting server's ip address
char server[] = "192.168.0.104";

// init function 
void setup(){

  // for usb serial print
  Serial.begin(9600);

  // LCD init
  lcd.begin(16,2);   // iInit the LCD for 16 chars 2 lines
  lcd.backlight();   // Turn on the backligt (try lcd.noBaklight() to turn it off)
  //lcd.autoscroll();
  
  // LCD add my character
  lcd.createChar(1, musictone);
  lcd.createChar(2, checkmark);

  // LCD start my message
  lcd.setCursor(0,0); //First line
  lcd.print("Get From Server");
  lcd.setCursor(0,1); //Second line
  lcd.print("Now Loading....");

  // LAN start
  delay( 1000 );
  Ethernet.begin(mac, ip , myDns );
  delay( 1000 );
  // LAN show my ip addresses
  /*
  Serial.print("localIP: ");
  Serial.println(Ethernet.localIP());
  Serial.print("subnetMask: ");
  Serial.println(Ethernet.subnetMask());
  Serial.print("gatewayIP: ");
  Serial.println(Ethernet.gatewayIP());
  Serial.print("dnsServerIP: ");
  Serial.println(Ethernet.dnsServerIP());
  */

  // get server's data
  Serial.println("Connecting...");
  if (client.connect(server, 80)) {

    Serial.println("Connected");

    // Make a HTTP request:
    client.println("GET /1.php HTTP/1.1");
    client.println("Host: 192.168.0.104" );
    client.println("Connection: close");
    client.println();

  }
  
  // get server's data failed
  else {
    
    Serial.println("connection failed");
    lcd.setCursor(0,1); //Second line
    lcd.print("Failed Pls Rebot");
    //lcd.autoscroll(); ?
    //lcd.print("Failed please reboot");

  }

  delay( 1000 );

}

// while( 1 )
void loop(){

  // character settings
  char c;
  long c_index = 0;
  boolean is_c_blank = true;
  
  // LCD show my message
  lcd.setCursor(0,0);
  lcd.print("SV:" + String( server ) );

  // read server's data
  //Serial.println( "----------header start --------" );
  while (client.connected()) {
    if (client.available()) {
      
      c = client.read();      

      // check c not blank
      if (c == '\n' && is_c_blank) {

        //Serial.println( "----------header end --------" );

        // get server's body data into result[ ]
        //Serial.println( "----------body start --------" );
        char result[ ] = "                ";
        while (client.connected()) {
          if (client.available()) {
            c = client.read();  

            // add text into result[ ] if not \n
            if( c != '\n' ){
              Serial.print( c );
              result[c_index] = c ;
              c_index ++;
            }

          }
        }
        //Serial.println( "----------body end --------" );
        //Serial.println( result );
        
        // LCD print my result[]
        lcd.setCursor(0,1);
        lcd.write(2); // my custom character
        lcd.print(result);
          
        break;

      }

      // check c data if \n 
      if (c == '\n') {
        is_c_blank = true;
      } 
      else if (c != '\r') {
        is_c_blank = false;
      }

    }else{

      break;

    }

  }

}
````

サーバー側のコード

````php:1.php
<?php

echo " result ";
for ($i=0; $i<6; $i++) {
    $d=rand(1,30)%2;
    echo $d ? chr(rand(65,90)) : chr(rand(48,57));
}
````
LiquidCrystal の autoscroll とか使えば結構遊べるかと思います。
Facebookのイイネの数や日経平均株価を表示しても面白いでしょう。
jsonでの取得はそのうち
###参考リンク
- [How to use an I2C LCD 16x2 with Arduino - Ardumotive Arduino Greek Playground](https://www.ardumotive.com/i2clcden.html)
- [fmalpartida/New-LiquidCrystal: Clone of the new liquid crystal library from:](https://github.com/fmalpartida/New-LiquidCrystal)

- [WebClient only works occasionally · Issue #156 · ntruchsess/arduino_uip](https://github.com/ntruchsess/arduino_uip/issues/156)

- [液晶表示（LiquidCrystalクラス） | Renesas ](https://www.renesas.com/us/ja/products/gadget-renesas/reference/gr-sakura/library-liquid)
- [Webクライアントから送信されたデータをATP3011にしゃべらせる（I2C通信）](http://www.nina.jp/server/arduino/ATP3011/ATP3011_and_webserver/ATP3011_and_webserver.html)
- [LCD Custom Character Generator ](https://maxpromer.github.io/LCD-Character-Creator/)
- [キャラクタディスプレイで独自の文字を表示する | Program Resource ](https://programresource.net/2020/02/17/2838.html)