-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimpleconfig.ino
366 lines (319 loc) · 13.1 KB
/
simpleconfig.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
//*************************************************************************
// Simple web server for WIFI NAT router configuration and OTA
// => Establishes a local WIFI and routes to an uplink WIFI
// => NAT code inspired by https://github.com/paclema/esp32_lwip_nat_example
// => Parameters stored in flash - erase by GPIO23 low during boot
// => Adapted to Arduino IDE, uses low level IDF functions
// + secured Tinymqtt broker
// => modified to use HTTPSServer lib by derived classes
// + app which communicates by MQTTS over the local WIFI with other units
//*************************************************************************
#include "TinyMqtt.h" // https://github.com/hsaturn/TinyMqtt
// We will use WIFI
#include <WiFi.h>
#include <WebServer.h>
#include <Update.h>
// Includes for the server
// Include sample certificate data (see HTTPSServer)
#include "cert.h"
#include "private_key.h"
// Note: We include derived classes of HTTPSServer
#include <SSLCert.hpp>
#include "utils.h"
#include "pages.h"
// The HTTPS Server comes in a separate namespace. For easier use, include it here.
using namespace httpsserver;
// Create an SSL certificate object from the files included above
SSLCert cert = SSLCert(
example_crt_DER, example_crt_DER_len,
example_key_DER, example_key_DER_len
);
#define PORT 8883
MqttBroker broker(&cert, PORT); // the TinyMqtt broker
/** Basic Mqtt Broker with TLS
+-----------------------------+
| ESP |
| +--------+ |
| | broker | | 8883 <--- External client/s
| +--------+ |
| ^--internal client |
+-----------------------------+
The ESP will become a secure MqttBroker.
*/
#include "esp_wifi.h"
#include "nvs.h"
#include "nvs_flash.h"
// holding GPIO23 low during boot erases nvs
#include "dhcpserver/dhcpserver.h"
#include "dhcpserver/dhcpserver_options.h"
WebServer server(80);
bool uplinkConnected = false;
#define IP_NAPT 1
#if !IP_NAPT
#error "IP_NAPT must be defined"
#else
#include "lwip/lwip_napt.h"
extern "C" void ip_napt_enable(u32_t addr, int enable);
#endif
void setup_Client(void); // client app setup
void loop_Client(void); // client app loop
uint8_t AP_clients = 0;
uint8_t AP_clients_last = AP_clients;
char sta_ssid[PARAMLEN] = "";
char ent_username[PARAMLEN] = "";
char ent_identity[PARAMLEN] = "";
char sta_password[PARAMLEN] = "";
char static_ip[PARAMLEN] = "";
char subnet_mask[PARAMLEN] = "";
char gateway[PARAMLEN] = "";
char ap_ssid[PARAMLEN] = "";
char ap_password[PARAMLEN] = "";
char ap_ip[PARAMLEN] = "";
char* config_page = NULL;
const char* ota_username = "otauser";
const char* ota_password = "otapass";
// create web page with current parameters
void fillPage(void)
{
const char* config_page_template = CONFIG_PAGE;
char* safe_ap_ssid = html_escape(ap_ssid);
char* safe_ap_passwd = html_escape(ap_password);
char* safe_sta_ssid = html_escape(sta_ssid);
char* safe_sta_password = html_escape(sta_password);
char* safe_ent_username = html_escape(ent_username);
char* safe_ent_identity = html_escape(ent_identity);
int page_len =
strlen(config_page_template) +
strlen(safe_ap_ssid) +
strlen(safe_ap_passwd) +
strlen(safe_sta_ssid) +
strlen(safe_sta_password) +
strlen(safe_ent_username) +
strlen(safe_ent_identity) +
256; // some more for parameters
Serial.printf("len=%d\n", page_len);
if (config_page) free(config_page);
config_page = (char *)malloc(sizeof(char) * page_len);
snprintf(
config_page, page_len, config_page_template,
uplinkConnected ? "00ff00" : "ff0000", safe_ap_ssid, safe_ap_passwd,
safe_sta_ssid, safe_sta_password, safe_ent_username, safe_ent_identity,
static_ip, subnet_mask, gateway);
free(safe_ap_ssid);
free(safe_ap_passwd);
free(safe_sta_ssid);
free(safe_sta_password);
free(safe_ent_username);
free(safe_ent_identity);
}
// check what we got from config page
bool parseUri(String getLine)
{
bool reboot = false;
if (server.args()) // we get parameters
{
if (server.hasArg("ap_ssid")) {
set_config_param_str("ap_ssid", server.arg("ap_ssid").c_str(), ap_ssid);
}
if (server.hasArg("ap_password")) {
set_config_param_str("ap_password", server.arg("ap_password").c_str(), ap_password);
}
if (server.hasArg("sta_ssid")) {
set_config_param_str("sta_ssid", server.arg("sta_ssid").c_str(), sta_ssid);
}
if (server.hasArg("sta_password")) {
set_config_param_str("sta_password", server.arg("sta_password").c_str(), sta_password);
}
if (server.hasArg("ent_username")) {
set_config_param_str("ent_username", server.arg("ent_username").c_str(), ent_username);
}
if (server.hasArg("ent_identity")) {
set_config_param_str("ent_identity", server.arg("ent_identity").c_str(), ent_identity);
}
if (server.hasArg("static_ip")) {
set_config_param_str("static_ip", server.arg("static_ip").c_str(), static_ip);
}
if (server.hasArg("subnet_mask")) {
set_config_param_str("subnet_mask", server.arg("subnet_mask").c_str(), gateway);
}
if (server.hasArg("gateway")) {
set_config_param_str("gateway", server.arg("gate").c_str(), gateway);
}
if (server.hasArg("reset")) {
reboot = true;
}
fillPage();
}
if (reboot)
{
printf("restart\n");
}
return reboot;
}
// check erase, then get parameters from nvs or set defaults
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(23, INPUT_PULLUP);
delay(300);
if (digitalRead(23) == 0) nvs_flash_erase(); // hold low during boot for erase
initialize_nvs();
if (get_config_param_str("sta_ssid", sta_ssid) != ESP_OK) param_set_default("", sta_ssid);
if (get_config_param_str("ent_username", ent_username) != ESP_OK) param_set_default("", ent_username);
if (get_config_param_str("ent_identity", ent_identity) != ESP_OK) param_set_default("", ent_identity);
if (get_config_param_str("sta_password", sta_password) != ESP_OK) param_set_default("", sta_password);
if (get_config_param_str("static_ip", static_ip) != ESP_OK) param_set_default("", static_ip);
if (get_config_param_str("subnet_mask", subnet_mask) != ESP_OK) param_set_default("", subnet_mask);
if (get_config_param_str("gateway", gateway) != ESP_OK) param_set_default("", gateway);
if (get_config_param_str("ap_ssid", ap_ssid) != ESP_OK) param_set_default("NAT_Router", ap_ssid);
if (get_config_param_str("ap_password", ap_password) != ESP_OK) param_set_default("", ap_password);
if (get_config_param_str("ap_ip", ap_ip) != ESP_OK) param_set_default("", ap_ip);
//** First, connect to STA so we can get a proper local DNS server
//**
//***********************************
WiFi.mode(WIFI_MODE_APSTA);
if (sta_ssid[0]) // is the uplink SSID configured?
{
Serial.printf("\nConnect to WIFI: %s ", sta_ssid);
WiFi.begin(sta_ssid, sta_password);
int round = 0;
while (WiFi.status() != WL_CONNECTED) {
Serial.print('.');
delay(500);
if (round++ > 10) break;
}
if (round <= 10)
{
Serial.printf("\nUplink connected to: %s (dns: %s / %s)\n",
WiFi.localIP().toString().c_str(),
WiFi.dnsIP(0).toString().c_str(),
WiFi.dnsIP(1).toString().c_str());
//** Give DNS servers to AP side (Added after softAP setup)
//**
//***********************************
// For esp32:
#ifndef __DHCPS_H__
Serial.println("DHCPS_H not included");
#endif
uplinkConnected = true;
}
}
//** Enable wifi AP:
//**
//***********************************
WiFi.softAP(ap_ssid, ap_password);
Serial.printf("\nStarted AP: %s at %s\n", ap_ssid, WiFi.softAPIP().toString().c_str());
if (uplinkConnected)
{
//** Give DNS servers to AP side:
//**
//***********************************
esp_err_t err;
tcpip_adapter_dns_info_t ip_dns;
err = tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_AP);
if (err != ESP_OK) Serial.printf("\ntcpip_adapter_dhcps_stop: err %s", esp_err_to_name(err)) ;
err = tcpip_adapter_get_dns_info(TCPIP_ADAPTER_IF_STA, ESP_NETIF_DNS_MAIN, &ip_dns);
if (err != ESP_OK)
Serial.printf("\ntcpip_adapter_get_dns_info: error %s", esp_err_to_name(err)) ;
// err = tcpip_adapter_set_dns_info(TCPIP_ADAPTER_IF_STA, ESP_NETIF_DNS_FALLBACK, &ip_dns);
// Serial.printf("\ntcpip_adapter_set_dns_info ESP_NETIF_DNS_FALLBACK: err %s . ip_dns:" IPSTR, esp_err_to_name(err), IP2STR(&ip_dns.ip.u_addr.ip4)) ;
err = tcpip_adapter_set_dns_info(TCPIP_ADAPTER_IF_AP, ESP_NETIF_DNS_MAIN, &ip_dns);
if (err == ESP_OK)
Serial.printf("\ntcpip_adapter_set_dns_info ESP_NETIF_DNS_MAIN: ip_dns:" IPSTR, IP2STR(&ip_dns.ip.u_addr.ip4)) ;
else
Serial.printf("\ntcpip_adapter_set_dns_info: error %s", esp_err_to_name(err)) ;
// ip_dns.ip.u_addr.ip4.addr = ipaddr_addr("8.8.8.8");
// ip_dns.ip.type = IPADDR_TYPE_V4;
// err = tcpip_adapter_set_dns_info(TCPIP_ADAPTER_IF_STA, ESP_NETIF_DNS_BACKUP, &ip_dns);
// Serial.printf("\tcpip_adapter_set_dns_info ESP_NETIF_DNS_BACKUP: err %s . ip_dns:" IPSTR, esp_err_to_name(err), IP2STR(&ip_dns.ip.u_addr.ip4)) ;
dhcps_offer_t opt_val = OFFER_DNS; // supply a dns server via dhcps
tcpip_adapter_dhcps_option(ESP_NETIF_OP_SET, ESP_NETIF_DOMAIN_NAME_SERVER, &opt_val, 1);
err = tcpip_adapter_dhcps_start(TCPIP_ADAPTER_IF_AP);
if (err != ESP_OK)
Serial.printf("\ntcpip_adapter_dhcps_start: err %s\n", esp_err_to_name(err)) ;
//** Enable NAT:
//**
//***********************************
// For esp32:
#if IP_NAPT
ip_napt_enable(WiFi.softAPIP(), 1);
Serial.printf("\nSetup completed - NAPT enabled\n");
#endif
//Serial.printf("Heap before: %d\n", ESP.getFreeHeap());
Serial.println();
}
broker.setlogin("user", "pass");
broker.begin();
Serial.printf("MQTTS broker ready @: %s on port %d\n", WiFi.softAPIP().toString().c_str() , PORT);
fillPage();
server.on("/", HTTP_GET, []() {
if (!server.authenticate(ota_username, ota_password))
return server.requestAuthentication(DIGEST_AUTH, "OTA", "AUTHENTICATION FAILED" );
bool reboot = parseUri(server.uri());
server.sendHeader("Connection", "close");
server.send(200, "text/html", config_page);
if (reboot) {
delay(1000);
ESP.restart();
}
});
/*handling uploading firmware file */
server.on("/update", HTTP_POST, []() {
if (!server.authenticate(ota_username, ota_password))
return server.requestAuthentication(DIGEST_AUTH, "OTA", "AUTHENTICATION FAILED" );
server.sendHeader("Connection", "close");
server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK - will reboot");
delay(1000);
ESP.restart();
}, []() {
HTTPUpload& upload = server.upload();
if (upload.status == UPLOAD_FILE_START) {
Serial.printf("Update: %s\n", upload.filename.c_str());
if (!Update.begin(UPDATE_SIZE_UNKNOWN)) { //start with max available size
Update.printError(Serial);
}
} else if (upload.status == UPLOAD_FILE_WRITE) {
/* flashing firmware to ESP*/
if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
Update.printError(Serial);
}
} else if (upload.status == UPLOAD_FILE_END) {
if (Update.end(true)) { //true to set the size to the current progress
Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
} else {
Update.printError(Serial);
}
}
});
server.begin();
Serial.println("Config web server running\n");
setup_Client(); // set up client app (defined in app.cpp)
}
void loop() {
// put your main code here, to run repeatedly:
loop_Client(); // the app loop
broker.loop(); // the MQTT broker loop
server.handleClient(); // the webserver loop
AP_clients = WiFi.softAPgetStationNum(); // NAT loop
if (AP_clients_last != AP_clients) {
Serial.printf("Stations connected to AP: %d\n", AP_clients);
AP_clients_last = AP_clients;
wifi_sta_list_t wifi_sta_list;
tcpip_adapter_sta_list_t adapter_sta_list;
memset(&wifi_sta_list, 0, sizeof(wifi_sta_list));
memset(&adapter_sta_list, 0, sizeof(adapter_sta_list));
delay(500); // To give time to AP to provide IP to the new station
esp_wifi_ap_get_sta_list(&wifi_sta_list);
tcpip_adapter_get_sta_list(&wifi_sta_list, &adapter_sta_list);
for (int i = 0; i < adapter_sta_list.num; i++) {
tcpip_adapter_sta_info_t station = adapter_sta_list.sta[i];
Serial.printf("\t - Station %d MAC: ", i);
for (int i = 0; i < 6; i++) {
Serial.printf("%02X", station.mac[i]);
if (i < 5)Serial.print(":");
}
Serial.printf(" IP: " IPSTR, IP2STR(&station.ip));
Serial.println();
}
}
}