-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathSimpleServerEsp32.ino
193 lines (159 loc) · 5.14 KB
/
SimpleServerEsp32.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
/*
Note: This example includes the library EasyDDNS. You'll have to add this package using your Arduino Library Manager.
The purpose of this package is to publish your dynamic IP to a DDNS service that will allocate a human readable
address to your current IP. If you do not need that, you can remove this dependency.
*/
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <ESPmDNS.h>
#include <TinyUPnP.h>
#include <EasyDDNS.h> // see note above
const char* ssid = "<FILL THIS!>";
const char* password = "<FILL THIS!>";
#define LISTEN_PORT <FILL THIS!> // http://<IP>:<LISTEN_PORT>/?name=<your string>
#define LEASE_DURATION 36000 // seconds
#define FRIENDLY_NAME "<FILL THIS!>" // this name will appear in your router's port forwarding section
// #define DDNS_USERNAME "<FILL THIS!>"
// #define DDNS_PASSWORD "<FILL THIS!>"
// #define DDNS_DOMAIN "<FILL THIS!>"
TinyUPnP tinyUPnP(20000); // -1 means blocking, preferably, use a timeout value (ms)
WebServer server(LISTEN_PORT);
int ledChannel = 0;
const int pin = 2; // not all ESP32 versions have an on board LED
const int delayval = 5;
int freq = 5000;
int resolution = 8; // in bits
// 0 <= percentage <= 100
void setPower(int percentage) {
long pwm_val = map(percentage, 0, 100, 0, 255);
if (pwm_val > 255) {
pwm_val = 255;
}
ledcWrite(ledChannel, (int) pwm_val);
}
void handleRoot() {
String message = "Number of args received: ";
message += server.args(); // get number of parameters
message += "\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, "text/plain", message); //Response to the HTTP request
setPower(percentage);
}
void connectWiFi() {
WiFi.disconnect();
delay(1200);
WiFi.mode(WIFI_STA);
//WiFi.setAutoConnect(true);
Serial.println(F("connectWiFi"));
WiFi.begin(ssid, password);
// flash twice to know that we are trying to connect to the WiFi
setPower(50);
delay(200);
setPower(0);
delay(200);
setPower(50);
delay(200);
setPower(0);
// wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(F("."));
}
Serial.println(F(""));
Serial.print(F("Connected to "));
Serial.println(ssid);
Serial.print(F("IP address: "));
Serial.println(WiFi.localIP());
}
void handleNotFound() {
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++) {
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
}
void setup(void) {
Serial.begin(115200);
Serial.println(F("Starting..."));
pinMode(pin, OUTPUT);
ledcSetup(ledChannel, freq, resolution);
ledcAttachPin(pin, ledChannel);
connectWiFi();
portMappingResult portMappingAdded;
tinyUPnP.addPortMappingConfig(WiFi.localIP(), LISTEN_PORT, RULE_PROTOCOL_TCP, LEASE_DURATION, FRIENDLY_NAME);
while (portMappingAdded != SUCCESS && portMappingAdded != ALREADY_MAPPED) {
portMappingAdded = tinyUPnP.commitPortMappings();
Serial.println("");
if (portMappingAdded != SUCCESS && portMappingAdded != ALREADY_MAPPED) {
// for debugging, you can see this in your router too under forwarding or UPnP
tinyUPnP.printAllPortMappings();
Serial.println(F("This was printed because adding the required port mapping failed"));
delay(30000); // 30 seconds before trying again
}
}
Serial.println("UPnP done");
// DDNS
// EasyDDNS.service("dynu");
// EasyDDNS.client(DDNS_DOMAIN, DDNS_USERNAME, DDNS_PASSWORD);
// server
if (!MDNS.begin("esp32")) {
Serial.println("Error while setting up DDNS service!");
while(1) {
delay(1000);
}
}
Serial.println("DDNS service started");
// fade on and then off to know the device is ready
for (int i = 0; i < 100; i++) {
setPower(i);
delay(delayval);
}
for (int i = 100; i >= 0; i--) {
setPower(i);
delay(delayval);
}
setPower(0);
for (int i = 0; i < 100; i++) {
setPower(i);
delay(delayval);
}
for (int i = 100; i >= 0; i--) {
setPower(i);
delay(delayval);
}
setPower(0);
server.on("/", handleRoot);
server.on("/inline", []() {
server.send(200, "text/plain", "this works as well");
});
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
delay(10);
Serial.print(F("Gateway Address: "));
Serial.println(WiFi.gatewayIP().toString());
Serial.print(F("Network Mask: "));
Serial.println(WiFi.subnetMask().toString());
}
void loop(void) {
delay(5);
// EasyDDNS.update(300000); // check for New IP
tinyUPnP.updatePortMappings(600000, &connectWiFi); // 10 minutes
server.handleClient();
}