Repository navigation
Expand file tree
/
Copy pathserver.cpp
More file actions
123 lines (105 loc) · 3.65 KB
/
Copy pathserver.cpp
File metadata and controls
123 lines (105 loc) · 3.65 KB
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
#include <Arduino.h>
#include <WiFi.h>
#include <DNSServer.h>
#include <WebServer.h>
#include <DNSServer.h>
#include "conf.h"
IPAddress apIP(192, 168, 1, 1);
WebServer webServer(80);
EpromTags *config_ptr;
DNSServer dnsServer;
const char index_html_pre_form[] = R"rawliteral(
<!DOCTYPE HTML><html><head>
<title>Dashcam Shutdown Delay</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head><body>
<h2>Dashcam Power Control</h2>
<form action="/">
)rawliteral";
static String form_formatter(String a, String b, String c)
{
String line = a + ":<br><input type=\"text\" name=\"" + b + "\" value=\"" + c + "\"><br>";
return line;
}
const char index_html_post_form[] = R"rawliteral(
<br><br><input type="submit" style="height: 30px; width: 100px" value="Save">
</form>
<form action="/">
<br><br><br><br><br><input type="text" name="erase" value="Type Y E S in this box to erase"><br>
<input type="submit" style="height: 30px; width: 100px" value="Erase">
</form>
</body></html>)rawliteral";
void handleRoot()
{
Serial.println("Web Server Request...");
// parse each posted item
if (webServer.hasArg("ssid"))
sprintf(config_ptr->ssid, "%s", webServer.arg("ssid").c_str());
if (webServer.hasArg("password"))
sprintf(config_ptr->password, "%s", webServer.arg("password").c_str());
if (webServer.hasArg("retries"))
sscanf(webServer.arg("retries").c_str(), "%u", &config_ptr->home_connection_attempts);
if (webServer.hasArg("timeout"))
sscanf(webServer.arg("timeout").c_str(), "%u", &config_ptr->home_connection_timeout);
if (webServer.hasArg("delay"))
sscanf(webServer.arg("delay").c_str(), "%u", &config_ptr->home_shutdown_delay);
// check if erase was pressed
if (webServer.hasArg("erase"))
{
if (strstr(webServer.arg("erase").c_str(), "YES"))
{
Serial.println("Erasing EEPROM...");
conf_init(config_ptr);
conf_put(config_ptr);
conf_print(config_ptr);
}
else
{
Serial.println("Erase button pressed without YES...");
}
}
// lazy way to see if something was posted, should check each item
if (webServer.hasArg("ssid"))
{
Serial.println("Updating EEPROM...");
config_ptr->init = INIT_VALUE;
conf_put(config_ptr);
conf_print(config_ptr);
}
String responseHTML;
responseHTML.concat(index_html_pre_form);
responseHTML.concat(form_formatter("Wi-Fi SSID", "ssid", config_ptr->ssid));
responseHTML.concat(form_formatter("Wi-Fi Password", "password", config_ptr->password));
responseHTML.concat(form_formatter("Wi-Fi Retries", "retries", String(config_ptr->home_connection_attempts)));
responseHTML.concat(form_formatter("Wi-Fi Timeout (Seconds)", "timeout", String(config_ptr->home_connection_timeout)));
responseHTML.concat(form_formatter("Shutdown Delay (Minutes)", "delay", String(config_ptr->home_shutdown_delay)));
responseHTML.concat(index_html_post_form);
webServer.send(200, "text/html", responseHTML);
}
void server_start(EpromTags *config)
{
// save pointer for the async handler
config_ptr = config;
// setup wifi
WiFi.mode(WIFI_AP);
WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
WiFi.softAP("Dashcam Power Control");
// setup dns for captive portal
dnsServer.start(53, "*", apIP);
webServer.onNotFound(handleRoot);
webServer.begin();
uint32_t blink_time = millis();
while (1)
{
dnsServer.processNextRequest();
webServer.handleClient();
// slow non-blocking blink while acting as a server
static bool blink_state = true;
if (millis() - blink_time > 1000)
{
digitalWrite(INDICATOR_LED, blink_state);
blink_state = !blink_state;
blink_time = millis();
}
}
}