-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDDNS_esp01w.ino
133 lines (112 loc) · 3.34 KB
/
DDNS_esp01w.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <ArduinoOTA.h>
#include "ESP8266WiFi.h"
#include "ESP8266HTTPClient.h"
#include <EasyDDNS.h>
#define delayTime 50 // Delay between Frames
#define wifiTimeout 60000
#define bright 128 // led pwm brightness 0=On 255=Off(Esp8266 >= 3.0) 1023=Off(esp8266 < 3)
const char* ssid = "SSID";
const char* password = "PASSWD";
const char* ddnsdomain = "ddnsdomain";
const char* ddnsuser = "ddnsuser";
const char* ddnspass = "ddnspass" ;
//const char* ddnstoken = "ddnstoken" ;
/*
List of supported DDNS providers:
- "duckdns"
- "noip"
- "dyndns"
- "dynu"
- "enom"
- "all-inkl"
- "selfhost.de"
- "dyndns.it"
- "strato"
- "freemyip"
- "afraid.org"
*/
const char* ddnsprovider = "dynu" ;
const int dnsInterval = 30000; // DDNS check interval
const char* OTAname = "OTANAME";
const char* OTApasswd = "OTAPASSWD";
static const char ntpServerName[] = "th.pool.ntp.org";
const int timeZone = 7; // Central European Time
unsigned long lastMillis; // will store last time LED was updated
int LEDinterval = 1000; // interval at which to blink (milliseconds)
const int intervalSlow = 1000; // Wifi connected blink rate
const int intervalFast = 200; // Wifi disconnected blink rate
const int ledPin = LED_BUILTIN;// the number of the LED pin
int ledState; // ledState used to set the LED
String myIP;
void Blink(){
if(ledState){
//digitalWrite(ledPin, LOW);
analogWrite(ledPin, bright);
}else{
digitalWrite(ledPin, HIGH);
}
ledState=!ledState;
}
int wifiConnected() {
return (WiFi.status() == WL_CONNECTED);
}
void setup() {
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
ledState=HIGH;
delay(100);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.print("Connecting to wifi ");
unsigned long Millis = millis();
while (!wifiConnected()) { // Wait for the Wi-Fi to connect
if (millis() - Millis >= wifiTimeout) {
Serial.println(" Timeout!");
break;
}
delay(intervalFast);
Blink();
}
if(wifiConnected()) {
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
ArduinoOTA.setHostname(OTAname);
ArduinoOTA.setPassword(OTApasswd);
ArduinoOTA.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH) {
type = "sketch";
} else { // U_FS
type = "filesystem";
}
});
ArduinoOTA.begin();
EasyDDNS.service(ddnsprovider);
EasyDDNS.client(ddnsdomain, ddnsuser, ddnspass); // Enter your DDNS Domain & user passwd
// EasyDDNS.client(ddnsdomain, ddnstoken); // Enter your DDNS Domain & token
// Get Notified when your IP changes
EasyDDNS.onUpdate([&](const char* oldIP, const char* newIP){
Serial.print("EasyDDNS - IP Change Detected: ");
Serial.println(newIP);
});
lastMillis = millis();
Serial.println();
Serial.println("Running.");
}
void loop(){
if (wifiConnected()) {
LEDinterval=intervalSlow;
ArduinoOTA.handle();
EasyDDNS.update(dnsInterval);
} else {
LEDinterval=intervalFast;
}
unsigned long currentMillis = millis();
if (currentMillis - lastMillis >= LEDinterval) {
lastMillis = currentMillis;
Blink();
}
delay(delayTime);
}