第一版 原型系統
連上西電的網
直接發送上網的認證信息
返回認證結果網頁
成功上網
#include <ESP8266WiFi.h> #define Use_Serial Serial struct http_request{ String Referer; char* host; int httpPort = 80; String host_ur ; String postDate ; }; /*WIFI賬號和密碼*/ const char* ssid = "stu-xdwlan"; // Enter SSID here const char* password = ""; //Enter Password here /*網頁認證上網模式 */ String usr_name="1601120383";//賬號 String usr_pwd="mimaHENFuzb";//密碼 加密-1 /*網頁認證上網post*/ String Referer="http://10.255.44.33/srun_portal_pc.php?ac_id=1&"; char* host = "10.255.44.33"; int httpPort = 80; String host_ur = "srun_portal_pc.php"; String postDate = String("")+"action=login&ac_id=1&user_ip=&nas_ip=&user_mac=&url=&username=+"+usr_name+"&password="+usr_pwd; /*-------------------------------------------------------------- * 通過上網認證,請自己修改postDate中的學號和密碼 ---------------------------------------------------------------*/ int hdulogin() { WiFiClient client; if (!client.connect(host, httpPort)) { Use_Serial.println("connection failed"); return 1; } delay(10); if (postDate.length() && postDate != "0") { String data = (String)postDate; int length = data.length(); String postRequest = (String)("POST ") + "/"+host_ur+" HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: Keep Alive\r\n" + "Content-Length: " + length + "\r\n" + "Accept: */*\r\n" + "Origin: http://"+host+"\r\n" + "Upgrade-Insecure-Requests: 1"+"\r\n" + "Content-Type: application/x-www-form-urlencoded;" + "\r\n" + "User-Agent: zyzandESP8266\r\n" + "Accept-Encoding: gzip, deflate"+"\r\n" + "Accept-Language: zh-CN,zh;q=0.9"+"\r\n" + "\r\n" + data + "\r\n"; client.print(postRequest); delay(600); //處理返回信息 String line = client.readStringUntil('\n'); while (client.available() > 0) { line += "\r\n"+client.readStringUntil('\n'); } Use_Serial.println(line); client.stop(); if (line.indexOf("logon success") != -1 || line.indexOf("不需要") != -1) { //認證成功 return 0; } else { return 2; } } client.stop(); return 2; } void setup() { Use_Serial.begin(115200); delay(1000); Use_Serial.println("Connecting to "); Use_Serial.println(ssid); //connect to your local wi-fi network WiFi.begin(ssid, password); //check wi-fi is connected to wi-fi network while (WiFi.status() != WL_CONNECTED) { delay(500); Use_Serial.print("."); } Use_Serial.println(""); Use_Serial.println("WiFi connected"); hdulogin(); } void loop() { }
第二版 直接訪問百度
連上西電WIFI
認證上網后
ping 百度或者其他網頁
返回 請求網頁源碼
bug 百度能ping通,但是沒網頁,換了自己的服務器,訪問hass,有數據
#include <ESP8266WiFi.h> #include <string.h> #include <ESP8266HTTPClient.h> #define Use_Serial Serial #ifdef ESP8266 extern "C" { #include "user_interface.h" //含有system_get_chip_id()的庫 } #endif //儲存SN號 String SN; struct http_request { String Referer; char* host; int httpPort=80; String host_ur ; String usr_name;//賬號 String usr_pwd;//密碼 String postDate; }; /*WIFI賬號和密碼*/ const char* ssid = "stu-xdwlan"; // Enter SSID here const char* password = ""; //Enter Password here /*網頁認證上網post*/ // String Referer="http://10.255.44.33/srun_portal_pc.php?ac_id=1&"; // char* host = "10.255.44.33"; // int httpPort = 80; // String host_ur = "srun_portal_pc.php"; /*網頁認證上網模式 */ // String usr_name;//賬號 // String usr_pwd;//密碼 // String postDate = String("")+"action=login&ac_id=1&user_ip=&nas_ip=&user_mac=&url=&username=+"+usr_name+"&password="+usr_pwd; /*-------------------------------------------------------------- * 通過上網認證,請自己修改postDate中的學號和密碼 ---------------------------------------------------------------*/ int hdulogin(struct http_request ruqest) { WiFiClient client; if (!client.connect(ruqest.host, ruqest.httpPort)) { Use_Serial.println("connection failed"); return 1; } delay(10); if (ruqest.postDate.length() && ruqest.postDate != "0") { String data = (String)ruqest.postDate; int length = data.length(); String postRequest = (String)("POST ") + "/"+ruqest.host_ur+" HTTP/1.1\r\n" + "Host: " +ruqest.host + "\r\n" + "Connection: Keep Alive\r\n" + "Content-Length: " + length + "\r\n" + "Accept: */*\r\n" + "Origin: http://"+ruqest.host+"\r\n" + "Upgrade-Insecure-Requests: 1"+"\r\n" + "Content-Type: application/x-www-form-urlencoded;" + "\r\n" + "User-Agent: zyzandESP8266\r\n" + "Accept-Encoding: gzip, deflate"+"\r\n" + "Accept-Language: zh-CN,zh;q=0.9"+"\r\n" + "\r\n" + data + "\r\n"; client.print(postRequest); delay(600); //處理返回信息 String line = client.readStringUntil('\n'); while (client.available() > 0) { line += "\r\n"+client.readStringUntil('\n'); } Use_Serial.println(line); client.stop(); if (line.indexOf("時間") != -1 || line.indexOf("登陸") != -1) { //認證成功 return 0; Use_Serial.println("time ----------- find "); } else { return 2; } } client.stop(); return 2; } void setup() { Use_Serial.begin(115200); delay(1000); Use_Serial.println("Connecting to "); Use_Serial.println(ssid); //connect to your local wi-fi network WiFi.begin(ssid, password); //check wi-fi is connected to wi-fi network while (WiFi.status() != WL_CONNECTED) { delay(500); Use_Serial.print("."); } Use_Serial.println(""); Use_Serial.println("WiFi connected"); SN = (String )system_get_chip_id(); Serial.println(SN); // 認證上網 http_request ruqest; ruqest.Referer="http://10.255.44.33/srun_portal_pc.php?ac_id=1&"; ruqest.host = "10.255.44.33"; ruqest.httpPort = 80; ruqest.host_ur = "srun_portal_pc.php"; ruqest.usr_name="1601120383";//賬號 ruqest.usr_pwd="mimaHENFuzb";//密碼 密碼已經修改 +1 ruqest.postDate = String("")+"action=login&ac_id=1&user_ip=&nas_ip=&user_mac=&url=&username=+"+ ruqest.usr_name+"&password="+ruqest.usr_pwd; hdulogin(ruqest); } void loop() { if(WiFi.status() == WL_CONNECTED){ HTTPClient http; http.begin("www.baidu.com", 80,"/"); //HTTP int httpCode = http.GET(); if (httpCode) { // 打印返回代碼 Serial.printf("code=%d\n", httpCode); // 數據正常返回 if (httpCode == 200) { String payload = http.getString(); Serial.print(payload); } else{ Serial.print("httpCode... failed\n"); delay(5000); } } else { Serial.print("GET... failed\n"); } delay(5000); } }
第三版 HTTP+MQTT
西電網頁認證上網
直接連接公網mqtt
手機發消息,esp8266向電腦打印出來
arduino 不支持中文漢字打印輸出
上面是 西電網頁認證后返回的網頁源碼
下面是從MQTT服務器接收的數據
至此初步完成ESP8266網頁認證上網模式。
下一步,將固定的post請求信息換成網頁輸入可以配置的
- 網頁輸入wifi名(網頁認證模式的wifi),無密碼
- 網頁輸入自己的學號和密碼
- 自動截取給網頁服務器返回的 http請求和參數格式
- 由此配置ESO8266來網頁認證上網
關於網頁服務器建立,返回頁面,手動輸入配置信息的教程看 ESP8266路由文件教程。
#include <ESP8266WiFi.h> #include <string.h> #include <ESP8266HTTPClient.h> #define Use_Serial Serial #include <ESP8266WiFi.h> #include "Adafruit_MQTT.h" #include "Adafruit_MQTT_Client.h" #define AIO_SERVER "www.dongvdong.top" #define AIO_SERVERPORT 1883 #define AIO_USERNAME "" #define AIO_KEY "" WiFiClient mqttclient; Adafruit_MQTT_Client mqtt(&mqttclient, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_USERNAME, AIO_KEY); Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/test", MQTT_QOS_1); void onoffcallback(char *data, uint16_t len) { Serial.print("Hey we're in a onoff callback, the button value is: "); Serial.println(data); } void MQTT_connect() { int8_t ret; // Stop if already connected. if (mqtt.connected()) { return; } Serial.print("Connecting to MQTT... "); uint8_t retries = 3; while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected Serial.println(mqtt.connectErrorString(ret)); Serial.println("Retrying MQTT connection in 10 seconds..."); mqtt.disconnect(); delay(10000); // wait 10 seconds retries--; if (retries == 0) { // basically die and wait for WDT to reset me while (1); } } Serial.println("MQTT Connected!"); } #ifdef ESP8266 extern "C" { #include "user_interface.h" //含有system_get_chip_id()的庫 } #endif //儲存SN號 String SN; struct http_request { String Referer; char* host; int httpPort=80; String host_ur ; String usr_name;//賬號 String usr_pwd;//密碼 String postDate; }; /*WIFI賬號和密碼*/ const char* ssid = "stu-xdwlan"; // Enter SSID here const char* password = ""; //Enter Password here /*網頁認證上網post*/ // String Referer="http://10.255.44.33/srun_portal_pc.php?ac_id=1&"; // char* host = "10.255.44.33"; // int httpPort = 80; // String host_ur = "srun_portal_pc.php"; /*網頁認證上網模式 */ // String usr_name;//賬號 // String usr_pwd;//密碼 // String postDate = String("")+"action=login&ac_id=1&user_ip=&nas_ip=&user_mac=&url=&username=+"+usr_name+"&password="+usr_pwd; /*-------------------------------------------------------------- * 通過上網認證,請自己修改postDate中的學號和密碼 ---------------------------------------------------------------*/ int hdulogin(struct http_request ruqest) { WiFiClient client; if (!client.connect(ruqest.host, ruqest.httpPort)) { Use_Serial.println("connection failed"); return 1; } delay(10); if (ruqest.postDate.length() && ruqest.postDate != "0") { String data = (String)ruqest.postDate; int length = data.length(); String postRequest = (String)("POST ") + "/"+ruqest.host_ur+" HTTP/1.1\r\n" + "Host: " +ruqest.host + "\r\n" + "Connection: Keep Alive\r\n" + "Content-Length: " + length + "\r\n" + "Accept: */*\r\n" + "Origin: http://"+ruqest.host+"\r\n" + "Upgrade-Insecure-Requests: 1"+"\r\n" + "Content-Type: application/x-www-form-urlencoded;" + "\r\n" + "User-Agent: zyzandESP8266\r\n" + "Accept-Encoding: gzip, deflate"+"\r\n" + "Accept-Language: zh-CN,zh;q=0.9"+"\r\n" + "\r\n" + data + "\r\n"; client.print(postRequest); delay(600); //處理返回信息 String line = client.readStringUntil('\n'); while (client.available() > 0) { line += "\r\n"+client.readStringUntil('\n'); } Use_Serial.println(line); client.stop(); if (line.indexOf("時間") != -1 || line.indexOf("登陸") != -1) { //認證成功 return 0; Use_Serial.println("time ----------- find "); } else { return 2; } } client.stop(); return 2; } void setup() { Use_Serial.begin(115200); delay(1000); Use_Serial.println("Connecting to "); Use_Serial.println(ssid); //connect to your local wi-fi network WiFi.begin(ssid, password); //check wi-fi is connected to wi-fi network while (WiFi.status() != WL_CONNECTED) { delay(500); Use_Serial.print("."); } Use_Serial.println(""); Use_Serial.println("WiFi connected"); SN = (String )system_get_chip_id(); Serial.println(SN); onoffbutton.setCallback(onoffcallback); mqtt.subscribe(&onoffbutton); // 認證上網 http_request ruqest; ruqest.Referer="http://10.255.44.33/srun_portal_pc.php?ac_id=1&"; ruqest.host = "10.255.44.33"; ruqest.httpPort = 80; ruqest.host_ur = "srun_portal_pc.php"; ruqest.usr_name="1601120383";//賬號 ruqest.usr_pwd="mimaHENFuzb";//密碼 經修改 +1 ruqest.postDate = String("")+"action=login&ac_id=1&user_ip=&nas_ip=&user_mac=&url=&username=+"+ ruqest.usr_name+"&password="+ruqest.usr_pwd; hdulogin(ruqest); } void loop() { if(WiFi.status() == WL_CONNECTED){ MQTT_connect(); mqtt.processPackets(10000); if(! mqtt.ping()) { mqtt.disconnect(); } } else { Serial.print("wifi... failed\n"); } delay(5000); }