forked from s60sc/ESP32-CAM_MJPEG2SD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetupAssist.cpp
130 lines (123 loc) · 5.05 KB
/
setupAssist.cpp
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
// Assist setup for new app installations
// original provided by gemi254
#include "myConfig.h"
#include <HTTPClient.h>
// DigiCert valid till 22/10/2028
static const char* git_rootCACertificate = R"~(
-----BEGIN CERTIFICATE-----
MIIEsTCCA5mgAwIBAgIQBOHnpNxc8vNtwCtCuF0VnzANBgkqhkiG9w0BAQsFADBs
MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3
d3cuZGlnaWNlcnQuY29tMSswKQYDVQQDEyJEaWdpQ2VydCBIaWdoIEFzc3VyYW5j
ZSBFViBSb290IENBMB4XDTEzMTAyMjEyMDAwMFoXDTI4MTAyMjEyMDAwMFowcDEL
MAkGA1UEBhMCVVMxFTATBgNVBAoTDERpZ2lDZXJ0IEluYzEZMBcGA1UECxMQd3d3
LmRpZ2ljZXJ0LmNvbTEvMC0GA1UEAxMmRGlnaUNlcnQgU0hBMiBIaWdoIEFzc3Vy
YW5jZSBTZXJ2ZXIgQ0EwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQC2
4C/CJAbIbQRf1+8KZAayfSImZRauQkCbztyfn3YHPsMwVYcZuU+UDlqUH1VWtMIC
Kq/QmO4LQNfE0DtyyBSe75CxEamu0si4QzrZCwvV1ZX1QK/IHe1NnF9X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static fs::FS fp = STORAGE;
static void wgetFile(const char* filePath, bool restart = false) {
if (!fp.exists(filePath)) {
if (WiFi.status() != WL_CONNECTED) return;
char downloadURL[150];
sprintf(downloadURL, "%s%s", GITHUB_URL, filePath);
for (int i = 0; i < 2; i++) {
// try secure then insecure
File f = fp.open(filePath, FILE_WRITE);
if (f) {
HTTPClient https;
WiFiClientSecure wclient;
if (!i) wclient.setCACert(git_rootCACertificate);
else wclient.setInsecure(); // not SSL
https.begin(wclient, downloadURL);
LOG_INF("Downloading %s from %s", filePath, downloadURL);
int httpCode = https.GET();
int fileSize = 0;
if (httpCode == HTTP_CODE_OK) {
fileSize = https.writeToStream(&f);
if (fileSize <= 0) {
httpCode = 0;
LOG_ERR("Download failed: writeToStream");
} else LOG_INF("Downloaded %s, size %d bytes", filePath, fileSize);
} else LOG_ERR("Download failed, error: %s", https.errorToString(httpCode).c_str());
https.end();
f.close();
if (httpCode == HTTP_CODE_OK) break;
else fp.remove(filePath);
} else LOG_ERR("Open failed: %s", filePath);
}
if (restart) doRestart();
}
}
bool checkDataFiles() {
// Download missing data files
if (!fp.exists(DATA_DIR)) fp.mkdir(DATA_DIR);
wgetFile(CONFIG_FILE_PATH, true);
wgetFile(INDEX_PAGE_PATH);
wgetFile(DATA_DIR "/OTA.htm");
wgetFile(DATA_DIR "/LOG.htm");
wgetFile(DATA_DIR "/jquery.min.js");
return true;
}
const char* defaultPage_html = R"~(
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>ESP32-CAM_MJPEG2SD setup</title>
</head>
<script>
function Config(){
if (!window.confirm('This will reboot the device to activate new settings.')) return false;
fetch('/control?ST_SSID=' + encodeURI(document.getElementById('ST_SSID').value))
.then(r => { console.log(r); return fetch('/control?ST_Pass=' + encodeURI(document.getElementById('ST_Pass').value)) })
.then(r => { console.log(r); return fetch('/control?save=1') })
.then(r => { console.log(r); return fetch('/control?reset=1') })
.then(r => { console.log(r); });
return false;
}
</script>
<body style="font-size:18px">
<br>
<center>
<table border="0">
<tr><th colspan="3">ESP32-CAM_MJPEG2SD Wifi setup..</th></tr>
<tr><td colspan="3"></td></tr>
<tr>
<td>SSID</td>
<td> </td>
<td><input id="ST_SSID" name="ST_SSID" length=32 placeholder="Router SSID" class="input"></td>
</tr>
<tr>
<td>Password</td>
<td> </td>
<td><input id="ST_Pass" name="ST_Pass" length=64 placeholder="Router password" class="input"></td>
</tr>
<tr><td colspan="3"></td></tr>
<tr><td colspan="3" align="center">
<button type="button" onClick="return Config()">Connect</button> <button type="button" onclick="window.location.reload;">Cancel</button>
</td></tr>
</table>
</center>
</body>
</html>
)~";