从txt获取token
#include
#define host_token "www.dongvdong.top"
#define httpPort 80
#define ssid "dongdong"
#define password "dongdong"
void setup() {
Serial.begin(115200);
delay(10);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
/*
功能: 1从自己的服务器上txt获取微信token
输入: 无
输出: token
*/
//
//{"errcode":42001,"errmsg":"access_token expired"}
String RequestWeixinToken(){
Serial.println("/**************************************************/");
Serial.println("1 request token! ");
Serial.print("connecting to ");
Serial.println(host_token);
Serial.println();
WiFiClient client;
if (!client.connect(host_token, httpPort)) { //works!
Serial.println("connection failed");
return "fail";
}
String str = "GET /WexinToken/token.txt \r\n\r\n\r\n"; // GET 请求网页服务器默认文件夹下的文件。 根据实际路径设置
client.print(str);
delay(600);
String token;
if (client.available())
{
token = client.readStringUntil('\r'); // 读一行
Serial.println(token); //串口打印出来
}
client.stop();
Serial.println();
Serial.println("closing connection \n");
if(token.length()>10){
return token;}
else{
return "fail";
}
}
void loop() {
delay(10000);
String token= RequestWeixinToken(); // 得到用户ID组
}