0
Help us understand the problem. What are the problem?

More than 1 year has passed since last update.

posted at

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

はじめに

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();
}
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Sign upLogin
0
Help us understand the problem. What are the problem?