LoginSignup
0
0

More than 3 years have passed since last update.

M5Atomでpingに失敗したらLEDを光らす

Posted at

はじめに

pingに失敗するとM5AtomのLEDがゲーミングカラーに輝く、みたいな事がしたいなぁと思い作ってみた。

ping.ino
#include <WiFi.h>
#include <WiFiClient.h>
#include <ESPmDNS.h>
#include <ESP32Ping.h>
#include "M5Atom.h"

// wifi
const char* ssid = "ssid";
const char* password = "password";
IPAddress ip(xxx,xxx,xxx,xxx);
IPAddress gateway(xxx,xxx,xxx,xxx);
IPAddress subnet(xxx,xxx,xxx,xxx);
IPAddress DNS(xxx,xxx,xxx,xxx);

IPAddress pingip (xxx,xxx,xxx,xxx); // ping対象

boolean IsPingCheck = false; // ping成功可否

int LedPoint = 0; // LED点灯位置

// ランダムなLEDの値を返す
int RandomLedVal() {
  return random(1, 16777215);
}

// Pingが通る場合、LEDを1つ移動させながらランダムに点灯させる。
void PingSuccess() {
  LedOffAll();

  if(LedPoint > 25) {
    LedPoint = 0;
  }

  M5.dis.drawpix(LedPoint, RandomLedVal()); // LEDをランダムに光らせる
  ++LedPoint;
}

// Pingに失敗した場合、LEDをランダムに光らせる。
void PingFailure(){
  for (uint32_t i = 0; i < 25; i++)
  {
    M5.dis.drawpix(i, RandomLedVal());
  }
  M5.update();  
}

// LEDを全てOFFにする。
void LedOffAll(){
  for (uint32_t i = 0; i < 25; i++)
  {
    M5.dis.drawpix(i, 0x000000);
  }
  M5.update();  
}

void setup(void) {
  Serial.begin(115200);

  //wifi
  WiFi.config(ip, gateway, subnet, DNS);
  delay(10);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }

  // LED初期化
  M5.begin(true, false, true);
  delay(50);
}

void loop(void) {

  IsPingCheck = Ping.ping(pingip); // ping

  if(!IsPingCheck){
    PingFailure();
  }
  else{
    PingSuccess();
  }

  M5.update();
}
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