-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwifimode.ino
More file actions
158 lines (140 loc) · 3.59 KB
/
wifimode.ino
File metadata and controls
158 lines (140 loc) · 3.59 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
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
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <SoftwareSerial.h>
#include "html_tool.h"
#include <FS.h>
#ifndef APSSID
#define APSSID "xsjesp"
#define APPSK "thereisnospoon"
#endif
const char *ssid = APSSID;
const char *password = APPSK;
bool first = true;
ESP8266WebServer server(80);
SoftwareSerial espSerial(4, 5); //RX,TX
HtmlTool htool;
String getContentType(String filename)
{
if (server.hasArg("download"))
return "application/octet-stream";
else if (filename.endsWith(".htm"))
return "text/html";
else if (filename.endsWith(".html"))
return "text/html";
else if (filename.endsWith(".css"))
return "text/css";
else if (filename.endsWith(".js"))
return "application/javascript";
else if (filename.endsWith(".png"))
return "image/png";
else if (filename.endsWith(".gif"))
return "image/gif";
else if (filename.endsWith(".jpg"))
return "image/jpeg";
else if (filename.endsWith(".ico"))
return "image/x-icon";
else if (filename.endsWith(".xml"))
return "text/xml";
else if (filename.endsWith(".pdf"))
return "application/x-pdf";
else if (filename.endsWith(".zip"))
return "application/x-zip";
else if (filename.endsWith(".gz"))
return "application/x-gzip";
return "text/plain";
}
bool handleFileRead(String path)
{
if (server.hasArg("message"))
{
String recv = server.arg("message");
Serial.println(recv);
espSerial.print(recv);
server.sendHeader("Location", "/");
server.send(301);
return true;
}
Serial.println("handleFileRead: " + path);
if (path.endsWith("/"))
path += "index.html";
String contentType = getContentType(path);
String pathWithGz = path + ".gz"; // 路徑結尾加上".gz"
// 確認請求路徑或者.gz結尾的資源存在
if (SPIFFS.exists(path) || SPIFFS.exists(pathWithGz))
{
if (SPIFFS.exists(pathWithGz))
{
path += ".gz";
}
File file = SPIFFS.open(path, "r");
server.streamFile(file, contentType);
file.close();
return true;
}
return false;
}
void getMessage()
{
}
//no need authentication
void handleNotFound()
{
if(first == true)
{
File file = SPIFFS.open("/init.html", "r");
server.streamFile(file, "text/html");
file.close();
first = false;
}
if (!handleFileRead(server.uri()))
{
server.send(404, "text/plain", "FileNotFound");
}
}
void handleGetData()
{
String LastMSG;
while(espSerial.available()>0)
{
LastMSG += char(espSerial.read());
}
while(espSerial.read()>0);
Serial.print("收到消息:");
String JSONmsg = "{\"firstName\":\""+LastMSG+"\"}";
Serial.println(JSONmsg);
server.send(200,"text/plain",JSONmsg);
server.sendHeader("Location", "/");
server.send(301);
}
void setup(void)
{
Serial.begin(115200);
espSerial.begin(115200);
espSerial.println("device start!!");
SPIFFS.begin();
WiFi.mode(WIFI_AP);
WiFi.softAP(ssid, password);
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
server.begin();
Serial.println("Web server started");
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
server.on("/get/",handleGetData);
server.onNotFound(handleNotFound);
const char *headerkeys[] = {"User-Agent", "Cookie"};
size_t headerkeyssize = sizeof(headerkeys) / sizeof(char *);
//ask server to track these headers
server.collectHeaders(headerkeys, headerkeyssize);
server.begin();
Serial.println("HTTP server started");
}
void loop(void)
{
server.handleClient();
}