diff --git a/mainCode/data/css/bootstrap-theme.min.css.gz b/mainCode/data/css/bootstrap-theme.min.css.gz deleted file mode 100644 index eecc2ea..0000000 Binary files a/mainCode/data/css/bootstrap-theme.min.css.gz and /dev/null differ diff --git a/mainCode/data/css/bootstrap.min.css.gz b/mainCode/data/css/bootstrap.min.css.gz deleted file mode 100644 index aafab37..0000000 Binary files a/mainCode/data/css/bootstrap.min.css.gz and /dev/null differ diff --git a/mainCode/data/data.json b/mainCode/data/data.json new file mode 100644 index 0000000..87bed8a --- /dev/null +++ b/mainCode/data/data.json @@ -0,0 +1 @@ +{"test":"hh"} \ No newline at end of file diff --git a/mainCode/data/index.html b/mainCode/data/index.html index 9318ce6..b00a2e8 100644 --- a/mainCode/data/index.html +++ b/mainCode/data/index.html @@ -3,76 +3,16 @@ - - ESP8266 - - - - -
- -
-

欢迎使用WiFiduino

-
-

点击下面按钮

-

可控制蓝色LED灯

-
- -
-
- -
-
- - - - - +

Welcome to wIot!

+
+

Control url:

+

/ctl?p=/*Pin Code*/&a=/*Action: high or low*/

+

e.g. /ctl?p=2&a=low Means to open the led on the board.

+
+

Data Get:

+

/data

+

Click Here to see!

\ No newline at end of file diff --git a/mainCode/data/js/bootstrap.js.gz b/mainCode/data/js/bootstrap.js.gz deleted file mode 100644 index b2977bc..0000000 Binary files a/mainCode/data/js/bootstrap.js.gz and /dev/null differ diff --git a/mainCode/data/js/bootstrap.min.js.gz b/mainCode/data/js/bootstrap.min.js.gz deleted file mode 100644 index 169ee94..0000000 Binary files a/mainCode/data/js/bootstrap.min.js.gz and /dev/null differ diff --git a/mainCode/mainCode.ino b/mainCode/mainCode.ino index f4a34bc..1e343b8 100644 --- a/mainCode/mainCode.ino +++ b/mainCode/mainCode.ino @@ -3,11 +3,8 @@ #include ESP8266WebServer server ( 80 ); -String ssid = "TOTOLINK_3be124"; //你的wifi热点名称 -String password = "13371035727"; //你的wifi热点密码 - -//String ssid = "236-iot"; //你的wifi热点名称 -//String password = "236236236"; //你的wifi热点密码 +String ssid = "yimian-iot"; //你的wifi热点名称 +String password = "1234567890."; //你的wifi热点密码 String getContentType(String filename){ if(server.hasArg("download")) return "application/octet-stream"; @@ -25,8 +22,33 @@ String getContentType(String filename){ else if(filename.endsWith(".gz")) return "application/x-gzip"; return "text/plain"; } -/* NotFound处理 - */ + +/* wifi 连接 */ +void wifiCnct() { + int connectCount = 0; + WiFi.begin ( ssid.c_str(), password.c_str() ); + while ( WiFi.status() != WL_CONNECTED ) { + digitalWrite(2, HIGH); + delay ( 1000 ); + Serial.print ( "." ); + if(connectCount > 30) { + Serial.println( "Connect fail!" ); + break; + } + connectCount += 1; + } + if(WiFi.status() == WL_CONNECTED) { + Serial.println ( "" ); + Serial.print ( "Connected to " ); + Serial.println ( ssid ); + Serial.print ( "IP address: " ); + Serial.println ( WiFi.localIP() ); + connectCount = 0; + digitalWrite(2, LOW); + } +} + +/* NotFound处理 */ void handleNotFound() { String path = server.uri(); Serial.print("load url:"); @@ -54,6 +76,8 @@ void handleNotFound() { } server.send ( 404, "text/plain", message ); } + +/* index提示页面 */ void handleMain() { Serial.print("handleMain"); File file = SPIFFS.open("/index.html", "r"); @@ -61,45 +85,58 @@ void handleMain() { file.close(); return; } + +/* data提取url */ +void handleData() { + Serial.print("handleData"); + + char cmd[135]; + sprintf(cmd,"{\"D0\":\"%d\",\"D1\":\"%d\",\"D2\":\"%d\",\"D3\":\"%d\",\"D4\":\"%d\",\"D5\":\"%d\",\"D6\":\"%d\",\"D7\":\"%d\",\"D8\":\"%d\",\"D9\":\"%d\",\"D10\":\"%d\",\"D11\":\"%d\",\"D12\":\"%d\",\"D13\":\"%d\",\"A0\":\"%f\"}",digitalRead(D0),digitalRead(D1),digitalRead(D2),digitalRead(D3),digitalRead(D4),digitalRead(D5),digitalRead(D6),digitalRead(D7),digitalRead(D8),digitalRead(D9),digitalRead(D10),digitalRead(D11),digitalRead(D12),digitalRead(D13),analogRead(A0)); + + File file = SPIFFS.open("/data.json","w+"); + if (!file) { + Serial.println("There was an error opening the file for writing"); + return; + } + if (file.print(cmd)) { + Serial.println("File was written"); + } else { + Serial.println("File write failed"); + } + file.close(); + file = SPIFFS.open("/data.json","r"); + size_t sent = server.streamFile(file, "application/json"); + file.close(); + return; +} + +/* execute url */ void handlePin() { - if(server.hasArg("a")) { - String action = server.arg("a"); - if(action == "on") { //a=on - digitalWrite(2, LOW); //点亮8266上的蓝色led - server.send ( 200, "text/html", "Pin 2 has turn on"); return; //返回数据 - } else if(action == "off") { // a=off - digitalWrite(2, HIGH); //熄灭板载led - server.send ( 200, "text/html", "Pin 2 has turn off"); return; + if(server.hasArg("p")) { + String pin = server.arg("p"); + if(server.hasArg("a")) { + String action = server.arg("a"); + if(action == "high") { //a=on + digitalWrite(atoi(pin.c_str()), HIGH); //抬高电平 + server.send ( 200, "application/json", "{\"code\":\"2\",\"state\":\"HIGH\"}"); return; //返回数据 + } else if(action == "low") { // a=off + digitalWrite(atoi(pin.c_str()), LOW); //拉低电平 + server.send ( 200, "application/json", "{\"code\":\"1\",\"state\":\"LOW\"}"); return; + } + server.send ( 200, "application/json", "{\"code\":\"-1\",\"state\":\"UNKNOW ACTION!\"}"); return; } - server.send ( 200, "text/html", "unknown action"); return; + server.send ( 200, "application/json", "{\"code\":\"-2\",\"state\":\"ACTION NO FOUND!\"}"); } - server.send ( 200, "text/html", "action no found"); } + void setup() { pinMode(2, OUTPUT); Serial.begin ( 115200 ); SPIFFS.begin(); - int connectCount = 0; - WiFi.begin ( ssid.c_str(), password.c_str() ); - while ( WiFi.status() != WL_CONNECTED ) { - delay ( 1000 ); - Serial.print ( "." ); - if(connectCount > 30) { - Serial.println( "Connect fail!" ); - break; - } - connectCount += 1; - } - if(WiFi.status() == WL_CONNECTED) { - Serial.println ( "" ); - Serial.print ( "Connected to " ); - Serial.println ( ssid ); - Serial.print ( "IP address: " ); - Serial.println ( WiFi.localIP() ); - connectCount = 0; - } + wifiCnct(); server.on ("/", handleMain); - server.on ("/pin", HTTP_GET, handlePin); + server.on ("/ctl", handlePin); + server.on ("/data", HTTP_GET, handleData); server.onNotFound ( handleNotFound ); server.begin(); Serial.println ( "HTTP server started" ); @@ -107,4 +144,5 @@ void setup() { void loop() { server.handleClient(); + //if( WiFi.status() != WL_CONNECTED) wifiCnct(); }