This repository has been archived by the owner on Jan 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPWM_LEDServer_ESP32.ino
300 lines (222 loc) · 7.7 KB
/
PWM_LEDServer_ESP32.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/****************************************************************************************************************************
PWM_LEDServer_ESP32.ino
For all Generic boards such as ESP8266, ESP32, WT32_ETH01, SAMD21/SAMD51, nRF52, STM32F/L/H/G/WB/MP1,Teensy, Portenta_H7
with WiFiNINA, ESP8266/ESP32 WiFi, ESP8266/ESP32-AT, W5x00, ENC28J60, Native-Ethernet, Portenta Ethernet/WiFi
DDNS_Generic is a library to automatically add port mappings to router using UPnP SSDP
(Simple Service Discovery Protocol) in order to provide access to the local Web Services from the Internet.
Based on and modified from Ofek Pearl's TinyUPnP Library (https://github.com/ofekp/TinyUPnP)
Built by Khoi Hoang https://github.com/khoih-prog/UPnP_Generic
Licensed under GPL-3.0 license
*****************************************************************************************************************************/
/*
Note: This example uses the DDNS_Generic library (https://github.com/khoih-prog/DDNS_Generic)
You can access this WebServer by either localIP:LISTEN_PORT such as 192.169.2.100:5933/?percentage=20
or DDNS_Host:LISTEN_PORT, such as account.duckdns.org:5933/?percentage=20
*/
#if !defined(ESP32)
#error This code is intended to run on the ESP32 platform! Please check your Tools->Board setting.
#endif
// Debug Level from 0 to 4
#define _DDNS_GENERIC_LOGLEVEL_ 1
#define _UPNP_LOGLEVEL_ 2
// Select DDNS_USING_WIFI for boards using built-in WiFi, such as Nano-33-IoT
#define DDNS_USING_WIFI true
#define DDNS_USING_ETHERNET false
#include <WebServer.h>
#include <UPnP_Generic.h> // https://github.com/khoih-prog/UPnP_Generic
#include <DDNS_Generic.h> // https://github.com/khoih-prog/DDNS_Generic
const char* ssid = "your_ssid";
const char* password = "12345678";
#define LISTEN_PORT 5933
#define LEASE_DURATION 36000 // seconds
#define FRIENDLY_NAME ARDUINO_BOARD "-WIFI" // this name will appear in your router port forwarding section
UPnP* uPnP;
WebServer server(LISTEN_PORT);
// setting PWM properties
const int freq = 5000;
const int ledChannel = 0;
const int resolution = 10; //Resolution 8, 10, 12, 15 bits. Select 10 => 1024 steps
#define LED_REVERSED false
#define LED_ON 100
#define LED_OFF 0
#define LED_PIN 2 // LED_BUILTIN
const int delayval = 10;
void onUpdateCallback(const char* oldIP, const char* newIP)
{
Serial.print(F("DDNSGeneric - IP Change Detected: oldIP = "));
Serial.print(oldIP);
Serial.print(F(", newIP = "));
Serial.println(newIP);
}
// 0 <= percentage <= 100
void setPower(uint32_t percentage)
{
long pwm_val = map(percentage, LED_OFF, LED_ON, 0, 1023);
if (pwm_val > 1023)
{
pwm_val = 1023;
}
ledcWrite(ledChannel, pwm_val);
}
void fadeOn()
{
#if LED_REVERSED
for (int i = 100; i >= 0; i--)
#else
for (int i = 0; i < 100; i++)
#endif
{
setPower(i);
delay(delayval);
}
}
void fadeOff()
{
#if LED_REVERSED
for (int i = 0; i < 100; i++)
#else
for (int i = 100; i >= 0; i--)
#endif
{
setPower(i);
delay(delayval);
}
}
void showLED()
{
for (int i = 0; i < 2; i++)
{
fadeOn();
fadeOff();
}
}
void handleRoot()
{
String message = F("Hello from ");
message += String(ARDUINO_BOARD);
message += F(" running UPnP_Generic & DDNS_Generic\n");
message += F("\nNumber of args received: ");
message += server.args(); // get number of parameters
message += F("\n");
int percentage = 0;
for (int i = 0; i < server.args(); i++)
{
message += "Arg #" + (String)i + " => ";
message += server.argName(i) + ": "; // get the name of the parameter
message += server.arg(i) + "\n"; // get the value of the parameter
if (server.argName(i).equals("percentage"))
{
percentage = server.arg(i).toInt();
}
}
server.send(200, F("text/plain"), message); //Response to the HTTP request
setPower(percentage);
}
void handleNotFound()
{
String message = F("File Not Found\n\n");
message += F("URI: ");
message += server.uri();
message += F("\nMethod: ");
message += (server.method() == HTTP_GET) ? F("GET") : F("POST");
message += F("\nArguments: ");
message += server.args();
message += F("\n");
for (uint8_t i = 0; i < server.args(); i++)
{
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, F("text/plain"), message);
}
void setup()
{
Serial.begin(115200);
while (!Serial && millis() < 5000);
#if ( ARDUINO_ESP32S2_DEV || ARDUINO_FEATHERS2 || ARDUINO_ESP32S2_THING_PLUS || ARDUINO_MICROS2 || \
ARDUINO_METRO_ESP32S2 || ARDUINO_MAGTAG29_ESP32S2 || ARDUINO_FUNHOUSE_ESP32S2 || \
ARDUINO_ADAFRUIT_FEATHER_ESP32S2_NOPSRAM )
#warning Using ESP32_S2
delay(2000);
#endif
Serial.print("\nStart PWM_LEDServer_ESP32 on "); Serial.println(ARDUINO_BOARD);
Serial.println(UPNP_GENERIC_VERSION);
pinMode(LED_PIN,OUTPUT);
// configure LED PWM function
ledcSetup(ledChannel, freq, resolution);
// attach the channel to the LED_PIN
ledcAttachPin(LED_PIN, ledChannel);
showLED();
Serial.print(F("Connecting to "));
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
// Wait for connection
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(F("."));
}
Serial.println();
IPAddress localIP = WiFi.localIP();
Serial.print(F("IP address: "));
Serial.println(localIP);
////////////////
DDNSGeneric.service("duckdns"); // Enter your DDNS Service Name - "duckdns" / "noip"
/*
For DDNS Providers where you get a token:
DDNSGeneric.client("domain", "token");
For DDNS Providers where you get username and password: ( Leave the password field empty "" if not required )
DDNSGeneric.client("domain", "username", "password");
*/
DDNSGeneric.client("account.duckdns.org", "12345678-1234-1234-1234-123456789012");
DDNSGeneric.onUpdate(onUpdateCallback);
////////////////
uPnP = new UPnP(30000); // -1 means blocking, preferably, use a timeout value (ms)
if (uPnP)
{
uPnP->addPortMappingConfig(localIP, LISTEN_PORT, RULE_PROTOCOL_TCP, LEASE_DURATION, FRIENDLY_NAME);
bool portMappingAdded = false;
#define RETRY_TIMES 4
int retries = 0;
while (!portMappingAdded && (retries < RETRY_TIMES))
{
Serial.println("Add Port Forwarding, Try # " + String(++retries));
int result = uPnP->commitPortMappings();
portMappingAdded = ( (result == PORT_MAP_SUCCESS) || (result == ALREADY_MAPPED) );
//Serial.println("commitPortMappings result =" + String(result));
if (!portMappingAdded)
{
// for debugging, you can see this in your router too under forwarding or UPnP
//uPnP->printAllPortMappings();
//Serial.println(F("This was printed because adding the required port mapping failed"));
if (retries < RETRY_TIMES)
delay(10000); // 10 seconds before trying again
}
}
uPnP->printAllPortMappings();
Serial.println(F("\nUPnP done"));
}
showLED();
server.on(F("/"), handleRoot);
server.on(F("/inline"), []()
{
server.send(200, F("text/plain"), F("This works as well"));
});
server.onNotFound(handleNotFound);
server.begin();
Serial.print(F("HTTP WiFiWebServer is @ IP : "));
Serial.print(localIP);
Serial.print(F(", port = "));
Serial.println(LISTEN_PORT);
Serial.print(F("Gateway Address: "));
Serial.println(WiFi.gatewayIP());
Serial.print(F("Network Mask: "));
Serial.println(WiFi.subnetMask());
}
void loop()
{
//delay(100);
DDNSGeneric.update(555000);
uPnP->updatePortMappings(600000); // 10 minutes
server.handleClient();
}