0
1

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.

Ubuntu の Load Average を Arudino の RGBLED で表示

Posted at

Ubuntu の Load Average を Arudino の RGBLED で表示します。

こんな感じでサーバーの現在の Load Average 分を表示できます。
EuvY_BZUcAQMC_7.jpeg
まずは、サーバーのロードアベレージをパーセントで返す PHP プログラム

ソースコードのリファクタリングはそのうち、、、

loadAvaragePercent.php
<?php

function get_server_memory_usage(){

    $free = Shell_exec('free');
    $free = (string)trim($free);
    $free_arr = explode("\n", $free);
    $mem = explode(" ", $free_arr[1]);
    $mem = array_filter($mem);
    $mem = array_merge($mem);
    $memory_usage = $mem[2]/$mem[1]*100;

    return $memory_usage;
}

function get_server_cpu_usage(){

    $load = sys_getloadavg();
    return $load[0];

}

exec("grep \"processor\" /proc/cpuinfo | wc -l" , $cpu_num );
//$cpu_num[0]

$load_average = trim( get_server_cpu_usage() );

echo intval( $load_average / $cpu_num[0] * 100 );

//get_server_memory_usage();

返り値はパーセント整数値で 20 とかその辺が返ってきます。これを Arduino で読んでその分だけ赤色を点滅させることになります。

topRgbLed.ino
// 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";

#include <Adafruit_NeoPixel.h>
 
#define PIN            2
 
// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS      16
 
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
 
int delayval = 250; // delay for half a second
 
void setup() {

  Serial.begin(9600);

  // LAN start
  delay( 1000 );
  Ethernet.begin(mac, ip , myDns );
  delay( 1000 );

  // 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();

  }

  pixels.begin(); // This initializes the NeoPixel library.

}
 
void loop() {
  
  Serial.println( "lopp in" );
  
  // character settings
  char c;
  long c_index = 0;
  boolean is_c_blank = true;
  
  //Serial.println( "----------header start --------" );
  while (client.connected()) {
    
    if (client.available()) {
      
      c = client.read();      

      // check 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 );

        int active_num = int( String( result ).toInt() / NUMPIXELS );
        Serial.println( active_num );
        
        // color blank
        for(int i=0;i<active_num;i++){
          pixels.setPixelColor(i, pixels.Color(0,0,0));
          pixels.show();
          delay(delayval);
        }

        // blue
        for(int i=0;i<NUMPIXELS;i++){
          pixels.setPixelColor(i, pixels.Color(0,0,10));
          pixels.show();
          delay(delayval);
        }

        // color red
        for(int i=0;i<active_num;i++){
          pixels.setPixelColor(i, pixels.Color(10,0,0));
          pixels.show();
          delay(delayval);
        }

        delay( ( NUMPIXELS + active_num ) * 1000 );

        break;

      }

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

    }else{

      break;

    }

  }

}

HDDやメモリの状態が見れても面白いかもしれません。あとせっかくRGBLEDですのでアニメーション機能があると良さげです。
####参考リンク

0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?