0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

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

Last updated at Posted at 2021-02-16

Arduino でサーバーから情報を取得して結果を LCD の LiquidCrystal から出力します。
EuWWz1KU4AMqb3x.jpeg

使用機材

項目 機材名
使用しているArduino arduino nano
書き込みブートローダ atmega328old
LAN シールド ENC28J60
LCDディスプレイ 16 x 2 の LiquidCrystal ( I2C 付き )

GPIO端子の接続設定

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

ソースコードはこちら

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

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;

    }

  }

}

サーバー側のコード

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での取得はそのうち

参考リンク

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?