第一版 原型系统
连上西电的网
直接发送上网的认证信息
返回认证结果网页
成功上网
#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);
}