//Sketch for ESP8266 Dynamic DNS Update - Ver. 1.0 //Celso Ferrarini Junior cferrarini@gmail.com #include #include #include // the timer object SimpleTimer timer; char serverName[] = "checkip.dyndns.org"; char externalIP[17]; const char* ssid = "ssid"; //your wifi ssid const char* password = "password"; //your wifi pwd const char* host = "www.yourhost.com"; //your host without /IP const int httpPort = 80; int delayvalue = 3600000; // 1 hour update delay void setup() { //Timer to renew IP timer.setInterval(delayvalue, repeatMe); // Connect no Wifi Serial.begin(9600); Serial.println("\r\n") ; WiFi.mode(WIFI_STA); // Station Mode (Wifi-Client) WiFi.begin(ssid, password); // Do not change value while (WiFi.status() != WL_CONNECTED) // Wait for Connection { delay(500); Serial.print("."); // if (Serial.available()) InputEEPR(); // Start When Serial Starts receiving } // Next WLAN Username and Password Serial.println(""); Serial.print("Connected To: "); Serial.println(ssid); Serial.print("IP address: "); Serial.println(WiFi.localIP()); WiFiClient client; TextFinder finder( client); char c = ' '; // Connect to Get Wan IP and parse ip nunmber. if (client.connect(serverName, 80)) { Serial.println("Connected - Acquiring WAN IP:"); //make HTTP request client.println("GET /"); client.println(); delay(1000); }//end if else { // you didn't get a connection to the server: Serial.println("connection failed"); }//end else while (client.available()) { char c = client.read(); // Serial.print(c); if (client.available()) { if(finder.find("IP Address: ")) { for (char k = 0; k < 17; k++) { c = client.read(); if(c != '<') { Serial.print(c); externalIP[k] = c; } //endif else break; }//end for }//end if }//end if client.flush(); client.stop(); //Writing to host Serial.print('\n'); Serial.print("connecting to "); Serial.println(host); // Use WiFiClient class to create TCP connections if (!client.connect(host, httpPort)) { Serial.println("connection failed"); return; } // We now create a URL for the request String url = "/ip/"; url += "input.php?ip="; url += externalIP; Serial.print("Requesting IP Update: "); Serial.println(url); // This will send the request to the server client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n"); delay(10); // Read all the lines of the reply from server and print them to Serial while(client.available()){ String line = client.readStringUntil('\r'); Serial.print(line); } client.flush(); client.stop(); // WiFi.disconnect(); Serial.println("Ok"); } } void loop() { timer.run(); uint8_t i; } // Renew IP Every hour void repeatMe() { // Connect no Wifi WiFi.disconnect(); Serial.begin(9600); Serial.println("\r\n") ; WiFi.mode(WIFI_STA); // Station Mode (Wifi-Client) WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) // Wait for Connection { delay(500); Serial.print("."); // if (Serial.available()) InputEEPR(); // Start When Serial Starts receiving } // Next WLAN Username and Password Serial.println(""); Serial.print("Connected To: "); Serial.println(ssid); Serial.print("IP address: "); Serial.println(WiFi.localIP()); WiFiClient client; TextFinder finder( client); char c = ' '; // Connect to Get Wan IP and parse ip nunmber. if (client.connect(serverName, 80)) { Serial.println("Connected - Acquiring WAN IP:"); //make HTTP request client.println("GET /"); client.println(); delay(1000); }//end if else { // you didn't get a connection to the server: Serial.println("connection failed"); }//end else while (client.available()) { char c = client.read(); // Serial.print(c); if (client.available()) { if(finder.find("IP Address: ")) { for (char k = 0; k < 17; k++) { c = client.read(); if(c != '<') { Serial.print(c); externalIP[k] = c; } //endif else break; }//end for }//end if }//end if client.flush(); client.stop(); //Writing to Host Serial.print('\n'); Serial.print("connecting to "); Serial.println(host); // Use WiFiClient class to create TCP connections if (!client.connect(host, httpPort)) { Serial.println("connection failed"); return; } // We now create a URL for the request String url = "/ip/"; url += "input.php?ip="; url += externalIP; Serial.print("Requesting IP Update: "); Serial.println(url); // This will send the request to the server client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n"); delay(10); // Read all the lines of the reply from server and print them to Serial while(client.available()){ String line = client.readStringUntil('\r'); Serial.print(line); } client.flush(); client.stop(); // WiFi.disconnect(); Serial.println("Ok"); } }