forked from SmingHub/Sming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.cpp
106 lines (88 loc) · 3.02 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
#include <user_config.h>
#include <SmingCore.h>
// If you want, you can define WiFi settings globally in Eclipse Environment Variables
#ifndef WIFI_SSID
#define WIFI_SSID "PleaseEnterSSID" // Put you SSID and Password here
#define WIFI_PWD "PleaseEnterPass"
#endif
#define LED_PIN 0 // GPIO number
HttpServer server;
int counter = 0;
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["ledstate"] = (*portOutputRegister(digitalPinToPort(LED_PIN)) & digitalPinToBitMask(LED_PIN)) ? "checked" : "";
vars["IP"] = WifiStation.getIP().toString();
vars["MAC"] = WifiStation.getMAC();
response.sendTemplate(tmpl); // this template object will be deleted automatically
}
void onHello(HttpRequest& request, HttpResponse& response)
{
response.setContentType(MIME_HTML);
// 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.println("\r\n=== WEB SERVER STARTED ===");
Serial.println(WifiStation.getIP());
Serial.println("==============================\r\n");
}
Timer downloadTimer;
HttpClient downloadClient;
int dowfid = 0;
void downloadContentFiles()
{
downloadClient.downloadFile("http://simple.anakod.ru/templates/index.html");
downloadClient.downloadFile("http://simple.anakod.ru/templates/bootstrap.css.gz");
downloadClient.downloadFile("http://simple.anakod.ru/templates/jquery.js.gz",
(RequestCompletedDelegate)([](HttpConnection& connection, bool success) -> int {
if(success) {
startWebServer();
}
}));
}
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();
}
}
void init()
{
spiffs_mount(); // Mount file system, in order to work with files
pinMode(LED_PIN, OUTPUT);
Serial.begin(SERIAL_BAUD_RATE); // 115200 by default
Serial.systemDebugOutput(true); // Enable debug output to serial
WifiStation.enable(true);
WifiStation.config(WIFI_SSID, WIFI_PWD);
WifiAccessPoint.enable(false);
// Run our method when station was connected to AP
WifiEvents.onStationGotIP(gotIP);
//Change CPU freq. to 160MHZ
System.setCpuFrequency(eCF_160MHz);
Serial.print("New CPU frequency is:");
Serial.println((int)System.getCpuFrequency());
}