1
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 5 years have passed since last update.

ESP8266 で mydns.jpの自動通知マシン

Last updated at Posted at 2020-06-25

mydns.jpを使っているんですが、IPの更新通知が面倒です。
そこで、いつものESPrDeveloper(ESP8266)を使って、自動通知マシンを作りました。

勢いでテキトーにコードをコピペしたので、不要なコードも混じってるかもです・・。

WiFi Manager

別で投稿しましたが、WiFiManagerを入れたので、最初にWiFi設定ができます。

NTP

1日に1回だけ通知したかったので、NTPで24H毎にチェックするようにしました。
下記を参考にしました。ありがとうございます。
https://intellectualcuriosity.hatenablog.com/entry/2017/03/12/015947

mDNS,HTTPサーバ

WEB画面をつけて、手動でも通知できるようにしました。
また、ここで、WiFiManagerで設定した設定をリセットできます。

課題

せっかくWiFiManagerを入れてるのに、mydnsの情報が埋め込みなのがいただけないです。
これは、後日、WEBから設定できるように頑張ります・・。

以下、ソースです。

# include <ESP8266HTTPClient.h>
# include <NTPClient.h>
# include <Time.h>
# include <TimeLib.h>
# include <FS.h>
# include <DNSServer.h>
# include <ESP8266WiFi.h>
# include <ESP8266WebServer.h>
# include <WiFiManager.h>
# include <ESP8266mDNS.h>
# include <WiFiClient.h>

# define SERIAL_SPEED 115200


// mydns info.
const char* id="mydnsXXXXXX"; //マスターID
const char* pw="XXXXXXXXXXX"; //パスワード

// mDNS
static const char* mdns_name = "mydnsnotifer";

//  NTP用
WiFiUDP ntpUDP;
const char *NTP_SERVER = "ntp.nict.jp";
const int TIME_OFFSET = 9 * 60 * 60;  // UTC+9h (JST)
NTPClient timeClient(ntpUDP, NTP_SERVER, TIME_OFFSET);
const unsigned long NTP_INTERVAL_TIME = 24 * 60 * 60 * 1000;  // 24h毎に更新
unsigned long ntp_interval;

// Web page (Port 8080)
ESP8266WebServer server(8080);
static const char* cpResponse200 = "<HTML>"
 "<BODY style='background-color:#ffffde;font-family:sans-serif;font-size:40px;'>"
 "mydns notifier<br/><br/>"
 "<a href=/cmd?CMD=notice>Notice MyIP to mydns.</a><br/>"
 "<a href=/cmd?CMD=reset>Reset WiFi Setting.</a><br/>"
 "</BODY></HTML>\r\n";

 
void setup() {
  Serial.begin(SERIAL_SPEED);
  Serial.println("");
  
  // WiFi
  //WiFiManager
  WiFiManager wifiManager;
  wifiManager.setBreakAfterConfig(true);
  if (!wifiManager.autoConnect("Auto Connect AP", "password")) {
    Serial.println("failed to connect, we should reset as see if it connects");
    delay(3000);
    ESP.reset();
    delay(5000);
  }

  Serial.println("");
  Serial.println("WiFi Connected.");
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  Serial.println("");
  Serial.println("");

  // NTP
  timeClient.begin();
  timeClient.update();
  setTime(timeClient.getEpochTime());
  Serial.println("NTP started.");
  ntp_interval = millis();
  Serial.println(timeClient.getFormattedTime());
  
  // Set up mDNS responder:
  if (!MDNS.begin(mdns_name)) {
    Serial.println("Error setting up MDNS responder!");
    while (1) {
      delay(1000);
    }
  }
  Serial.println("mDNS responder started");
  Serial.println("");

  // WebServer
  server.on("/cmd", WebCommand);
  server.begin();
  Serial.println("Web Server started");

  // Add service to MDNS-SD
  MDNS.addService("http", "tcp", 8080);

}

void WebCommand() {
  String cmd = server.arg("CMD");
  if (cmd == "notice")  { notice(); }
  else   if (cmd == "reset")  {
    WiFiManager wifiManager;
    delay(1000);
    Serial.println("");
    Serial.println("WiFi Reset !");
    Serial.println("");
    wifiManager.resetSettings();
    delay(3000);
    ESP.reset();
  }
  Serial.println("  cmd=" + cmd);
  server.send(200, "text/html", cpResponse200);
}

void loop() {
  MDNS.update();
  server.handleClient();  
  // 定期的に時刻を合わせて、IPを通知。
  if (millis() - ntp_interval > NTP_INTERVAL_TIME) {
    timeClient.update();
    Serial.println(timeClient.getFormattedTime());
    notice();
    ntp_interval = millis();
  }
}

void notice() {
  // 通知
  WiFiClient client;
  HTTPClient http;
  http.begin(client, "http://www.mydns.jp/login.html");
  http.setAuthorization(id, pw);
  Serial.print("http GET= ");
  Serial.println(http.GET());
}

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