have an esp8266.
i'm trying to create a DDNS updater.
it has to wait 5 min then do a GET request to a URL.
Here's my code:
/* Code For DNS Updater.*/
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#define LOOP_TIME 300000
const char* ssid = "SomeSSID";
const char* password = "SomePSWD";
const String address="http://dynamicdns.park-your-domain.com";
const String path="update";
const String pathHost="host";
const String pathPassword="password";
const String pathDomain="domain";
const String dnsName="my-example-host.com";
const String ddnsPassword="my-example-password";
const String updateHosts[]={
"@",
"subdomain1",
"subdomain2",
};
void setup() {
Serial.begin(115200);
connectWiFi();
}
long nextUpdate=0;
void loop() {
if(nextUpdate<millis()){
if (WiFi.status() == WL_CONNECTED) {
updateDNS();
}else{
connectWiFi();
}
nextUpdate=millis()+LOOP_TIME;
}
}
void updateDNS(){
Serial.println("Updating Hosts:");
for(int host=0;host<(sizeof(updateHosts)/sizeof(String));host++){
Serial.print(updateHosts[host]+": ");
String assembledURL=address+"/"+path+"?"+pathHost+"="+updateHosts[host]+"&"+pathDomain+"="+dnsName+"&"+pathPassword+"="+ddnsPassword;
// Serial.println("Host: "+updateHosts[host]+" URL: "+assembledURL);
HTTPClient http;
http.begin(assembledURL);
http.GET();
http.end();
Serial.println("Done");
}
}
void connectWiFi(){
WiFi.begin(ssid, password);
Serial.print("Connecting To ");
Serial.println(ssid);
Serial.print("Password: ");
Serial.println(password);
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.print("Connected To ");
Serial.println(ssid);
}
and it stops after a couple of hours - which isnt good for a DDNS updater.
EDIT: maybe it has something to do with my timer. will try an unsigned long instead.
if(nextUpdate<millis()){
beif(nextUpdate>millis()){
, as in the timeout is past the current time?