-
-
Notifications
You must be signed in to change notification settings - Fork 346
/
application.cpp
113 lines (92 loc) · 3.07 KB
/
application.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
#include <SmingCore.h>
// If you want, you can define WiFi settings globally in Eclipse Environment Variables
#ifndef WIFI_SSID
#define WIFI_SSID "PleaseEnterSSID" // Put your SSID and password here
#define WIFI_PWD "PleaseEnterPass"
#endif
#define LED_PIN 0 // GPIO number
namespace
{
HttpServer server;
int counter = 0;
HttpClient downloadClient;
void onIndex(HttpRequest& request, HttpResponse& response)
{
counter++;
bool led = request.getQueryParameter("led") == "on";
digitalWrite(LED_PIN, led);
TemplateFileStream* tmpl = new TemplateFileStream("index.html");
auto& vars = tmpl->variables();
vars["counter"] = String(counter);
vars["IP"] = WifiStation.getIP().toString();
vars["MAC"] = WifiStation.getMacAddress().toString();
response.sendNamedStream(tmpl); // this template object will be deleted automatically
}
void onHello(HttpRequest& request, HttpResponse& response)
{
response.setContentType(MIME_HTML);
// Below is an example how to send multiple cookies
response.setCookie("cookie1", "value1");
response.setCookie("cookie2", "value", true);
// Use direct strings output only for small amount of data (huge memory allocation)
response.sendString("Sming. Let's do smart things.");
}
void onFile(HttpRequest& request, HttpResponse& response)
{
String file = request.uri.getRelativePath();
if(file[0] == '.')
response.code = HTTP_STATUS_FORBIDDEN;
else {
response.setCache(86400, true); // It's important to use cache for better performance.
response.sendFile(file);
}
}
void startWebServer()
{
server.listen(80);
server.paths.set("/", onIndex);
server.paths.set("/hello", onHello);
server.paths.setDefault(onFile);
Serial << endl
<< _F("=== WEB SERVER STARTED ===") << endl
<< WifiStation.getIP() << endl
<< _F("==========================") << endl
<< endl;
}
void downloadContentFiles()
{
downloadClient.downloadFile(F("http://simple.anakod.ru/templates/index.html"));
downloadClient.downloadFile(F("http://simple.anakod.ru/templates/bootstrap.css.gz"));
downloadClient.downloadFile(F("http://simple.anakod.ru/templates/jquery.js.gz"),
[](HttpConnection& connection, bool success) -> int {
if(success) {
startWebServer();
}
return 0;
});
}
void gotIP(IpAddress ip, IpAddress netmask, IpAddress gateway)
{
if(!fileExist("index.html") || !fileExist("bootstrap.css.gz") || !fileExist("jquery.js.gz")) {
// Download server content at first
downloadContentFiles();
} else {
startWebServer();
}
}
} // namespace
void init()
{
Serial.begin(SERIAL_BAUD_RATE); // 115200 by default
Serial.systemDebugOutput(true); // Enable debug output to serial
spiffs_mount(); // Mount file system, in order to work with files
pinMode(LED_PIN, OUTPUT);
WifiStation.enable(true);
WifiStation.config(WIFI_SSID, WIFI_PWD);
WifiAccessPoint.enable(false);
// Run our method when station was connected to AP
WifiEvents.onStationGotIP(gotIP);
// Max. out CPU frequency
System.setCpuFrequency(CpuCycleClockFast::cpuFrequency());
Serial << _F("New CPU frequency is ") << System.getCpuFrequency() << " MHz" << endl;
}