-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWiFiConnect.ino
More file actions
65 lines (58 loc) · 1.96 KB
/
WiFiConnect.ino
File metadata and controls
65 lines (58 loc) · 1.96 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
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#define WIFI_SSID "WHUT-DORM" // 这里写你宿舍里wifi的名称,如我这里是WHUT-DORM
const char *username = "your_username"; // 你的上网用户名
const char *password = "your_password"; // 你的上网密码
static WiFiClient client;
const int LED = LED_BUILTIN;
void setup() {
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW);
wifiInit(WIFI_SSID); // 连接wifi获取dhcp分配的ip
connectWiFi(); // 抓包获取的api认证
}
void loop() {
// 在这里写你的联网逻辑
}
void wifiInit(const char *ssid) {
WiFi.begin(ssid);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("WiFi not Connect");
}
Serial.println("Connected to AP");
Serial.print("IP is:");
Serial.println(WiFi.localIP());
}
void connectWiFi() {
const char *keys[] = { "Location" };
HTTPClient http;
http.collectHeaders(keys, 1);
http.begin(client, "http://captive.apple.com");
String url = http.header("Location");
// String url = "http://172.30.21.100/api/r/" + nasId + "?userip=" + WiFi.localIP().toString() + "&wlanacname=&acip=172.30.1.220&acname=WHUT-YQ-Bras-ME60";
http.begin(client, url);
http.GET();
http.end();
HTTPClient http1;
String url1 = "http://172.30.21.100/api/account/login";
http1.begin(client, url1);
http1.addHeader("Content-Type", "application/x-www-form-urlencoded");
http1.addHeader("X-Requested-With", "XMLHttpRequest");
char *query_string = new char[83 + 1 + strlen(password)];
char *tmp = "userIpv4=&userMac=&captcha=&captchaId=&switchip=&username=";
strcat(tmp, username);
strcat(tmp, "&password=");
strcat(tmp, password);
const char *nasId = "&nasId=" + url[27] + url[28];
strcat(tmp, nasId);
strcpy(query_string, tmp);
int httpCode = http1.POST(query_string);
if (httpCode > 0) {
if (httpCode == HTTP_CODE_OK) {
Serial.println("Portal verifying passed.");
digitalWrite(LED, HIGH);
}
}
http1.end();
}