- // M5ATOM Activity Checker (c)2022- @logic_star Allrights reserved.
 
- #include <M5Atom.h>
 
- #include <WiFi.h>
 
- #include <time.h>
 
- // Wifi 関連定義
 
- const char* ssid       = "SSID"; //WiFi APのSSID
 
- const char* password   = "PASSWORD";  //WiFi APのPassword
 
- WiFiClient  client;
 
- // 時計関連
 
- #define NTP_SERVER "ntp.nict.jp"
 
- const char* ntpServer = NTP_SERVER;
 
- const long  gmtOffset_sec = 9 * 3600;
 
- const int   daylightOffset_sec = 0;
 
- struct  tm timeinfo;
 
- uint8_t secLastReport = 0;
 
- // メール関連
 
- #include <ESP_Mail_Client.h>
 
- #define SMTP_HOST "aaa.bbb.ccc.jp"  //メールサーバ名
 
- //#define SMTP_PORT esp_mail_smtp_port_25   //SMTP Port 25
 
- //#define SMTP_PORT esp_mail_smtp_port_465  //SMTP port 465
 
- #define SMTP_PORT esp_mail_smtp_port_587  //SMTP port 587
 
- #define AUTHOR_EMAIL "aaa@bbb.ccc.jp"  //SMTP認証ID:メール送信者のメールアドレス
 
- #define AUTHOR_PASSWORD "password" //SMTP認証パスワード
 
- #define AUTHOR_DOMAIN "bbb.ccc.jp"  //送信者のドメイン
 
- #define AUTHOR_NAME "ACTIVITY CHECKER"  //送信者名
 
- #define MESSAGE_SUBJECT "OFFLINE ALART!"  //送信メールタイトル
 
- #define MESSAGE_RECIPIENT "xxx@bbb.ccc.jp" //メール送信先アドレス
 
- #define MESSAGE_RECIPIENT_NAME "Administrator"  //メール送信先名
 
- #define MESSAGE_ID "Message-ID: <aaa@bbb.ccc.jp>"  //メールのメッセージID
 
- SMTPSession smtp;
 
- const char rootCACert[] PROGMEM = "-----BEGIN CERTIFICATE-----\n"
 
-                                   "-----END CERTIFICATE-----\n";
 
- /* Callback function to get the Email sending status */
 
- void smtpCallback(SMTP_Status status)
 
- {
 
-   /* Print the current status */
 
-   Serial.println(status.info());
 
-   /* Print the sending result */
 
-   if (status.success())
 
-   {
 
-     Serial.println("----------------");
 
-     ESP_MAIL_PRINTF("Message sent success: %d\n", status.completedCount());
 
-     ESP_MAIL_PRINTF("Message sent failled: %d\n", status.failedCount());
 
-     Serial.println("----------------\n");
 
-     struct tm dt;
 
-     for (size_t i = 0; i < smtp.sendingResult.size(); i++)
 
-     {
 
-       /* Get the result item */
 
-       SMTP_Result result = smtp.sendingResult.getItem(i);
 
-       time_t ts = (time_t)result.timestamp;
 
-       localtime_r(&ts, &dt);
 
-       ESP_MAIL_PRINTF("Message No: %d\n", i + 1);
 
-       ESP_MAIL_PRINTF("Status: %s\n", result.completed ? "success" : "failed");
 
-       ESP_MAIL_PRINTF("Date/Time: %d/%d/%d %d:%d:%d\n", dt.tm_year + 1900, dt.tm_mon + 1, dt.tm_mday, dt.tm_hour, dt.tm_min, dt.tm_sec);
 
-       ESP_MAIL_PRINTF("Recipient: %s\n", result.recipients.c_str());
 
-       ESP_MAIL_PRINTF("Subject: %s\n", result.subject.c_str());
 
-     }
 
-     Serial.println("----------------\n");
 
-     //You need to clear sending result as the memory usage will grow up.
 
-     smtp.sendingResult.clear();
 
-   }
 
- }
 
- void send_error_mail(String textMsg){
 
-   smtp.callback(smtpCallback);
 
-   ESP_Mail_Session session;
 
-   session.server.host_name = SMTP_HOST;
 
-   session.server.port = SMTP_PORT;
 
-   session.login.email = AUTHOR_EMAIL;
 
-   session.login.password = AUTHOR_PASSWORD;
 
-   session.login.user_domain = F(AUTHOR_DOMAIN);
 
-   session.time.ntp_server = F(NTP_SERVER);
 
-   session.time.gmt_offset = 9;
 
-   session.time.day_light_offset = 0;
 
-   SMTP_Message message;
 
-   message.sender.name = F(AUTHOR_NAME);
 
-   message.sender.email = AUTHOR_EMAIL;
 
-   message.subject = F(MESSAGE_SUBJECT);
 
-   message.addRecipient(F(MESSAGE_RECIPIENT_NAME), F(MESSAGE_RECIPIENT));
 
-   message.text.content = textMsg;
 
-   message.text.charSet = F("us-ascii");
 
-   message.text.transfer_encoding = Content_Transfer_Encoding::enc_7bit;
 
-   message.priority = esp_mail_smtp_priority::esp_mail_smtp_priority_low;
 
-   message.addHeader(F(MESSAGE_ID));
 
-   if (!smtp.connect(&session)) return;
 
-   if (!MailClient.sendMail(&smtp, &message))
 
-     Serial.println("Error sending Email, " + smtp.errorReason());
 
-   ESP_MAIL_PRINTF("Free Heap: %d\n", MailClient.getFreeHeap());
 
- }
 
- #include "ESP32Ping.h"
 
- void ping_check(char *name, IPAddress ip, int x){
 
- char  text_buffer[200];
 
-       M5.dis.drawpix(x, 0xffff00);  //LEDを黄色に
 
-       if(Ping.ping(ip)) {
 
-         M5.dis.drawpix(x, 0x00ff00);  //LEDを緑に
 
-       } else {
 
-         M5.dis.drawpix(x, 0xff0000);  //LEDを赤に
 
-         sprintf(text_buffer, "%s is offline!", name);
 
-         send_error_mail(text_buffer); //アラートメールの送信
 
-       }
 
- }
 
- // NTPによる時刻取得関数
 
- int ntp(){
 
- uint8_t  wifi_retry_cnt;
 
-   WiFi.begin(ssid, password); //WiFi接続開始
 
-   wifi_retry_cnt = 20;  //0.5秒×20=最大10秒で接続タイムアウト
 
-   while (WiFi.status() != WL_CONNECTED){
 
-     delay(500);
 
-     if(--wifi_retry_cnt == 0) {
 
-       WiFi.disconnect(true);  //タイムアウトでWiFiオフ
 
-       WiFi.mode(WIFI_OFF);
 
-       return(false);  //接続失敗でリターン
 
-     }
 
-   }
 
-   configTime(gmtOffset_sec, daylightOffset_sec, ntpServer); //NTPによる時刻取得
 
-   if (!getLocalTime(&timeinfo)) {
 
-     WiFi.disconnect(true);  //時刻取得失敗でWiFiオフ
 
-     WiFi.mode(WIFI_OFF);
 
-     return (false); //時刻取得失敗でリターン
 
-   }
 
-   return (true);  //時刻取得成功でリターン
 
- }
 
- void setup() {  //NTPで時刻取得し、もしエラーだった場合は動作停止
 
-   M5.begin(true, false, true);  
 
-   if(ntp() == false)  while(1);
 
- }
 
- int toggle = 0;
 
- void loop() { //メインプログラム
 
- int target;
 
-   getLocalTime(&timeinfo);
 
-   if((timeinfo.tm_hour == 2)&&(timeinfo.tm_min == 0)&&(timeinfo.tm_sec == 0))  ntp(); 
 
-   if(secLastReport != timeinfo.tm_sec) {
 
-     secLastReport = timeinfo.tm_sec;
 
-     if(toggle){toggle = 0;M5.dis.drawpix(24, 0);} //青色インジケータの表示
 
-     else{toggle = 1;M5.dis.drawpix(24, 0x0000ff);}
 
-     M5.update();
 
-     if((((timeinfo.tm_min % 10) == 0)&&(timeinfo.tm_sec == 0))||M5.Btn.wasPressed()){ //10分毎または、ボタンが押下時に処理
 
-       //監視サーバ定義
 
-       target = 0;
 
-       ping_check("IP Phone", IPAddress (192, 168, 3, 99), target++); //監視先1 監視対象名とIPアドレスを指定
 
-       ping_check("SVR-X", IPAddress (0, 0, 0, 0), target++); //監視先2
 
-       ping_check("SVR-Y", IPAddress (0, 0, 0, 0), target++); //監視先3
 
-       ping_check("Google DNS", IPAddress (8, 8, 8, 8), target++); //監視先4 適宜24まで拡張可能
 
-     }
 
-   }
 
-   delay(100);  //0.1秒ウェイト
 
- }
 
 
		 |