https://api.weixin.qq.com/device/get_openid?access_token=自己申请微信token&device_type=gh_e93c1b3098b9&device_id=gh_e93c1b3098b9_dae1c2072212185c
ESP8266代码实现
#include <ESP8266WiFi.h> const char* ssid = "HUAWEI-H3VBKZ"; const char* password = "13991320169" //-1; const char* host = "api.weixin.qq.com"; void setup() { Serial.begin(115200); delay(10); // We start by connecting to a WiFi network Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); //works! while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); } void loop() { delay(10000); Serial.print("connecting to "); Serial.println(host); // Use WiFiClient class to create TCP connections WiFiClient client; const int httpPort = 80; if (!client.connect(host, httpPort)) { //works! Serial.println("connection failed"); return; } // We now create a URI for the request String url = "/device/get_openid"; url += "?access_token="; url +="19_PwEib-mFrCmjdMfhehHgRJXF9TUEUr8ewWDUBAgbQUOojVjwg3lGXD3ei73O1blBZvOWqXwxTSM4kLUJru1ZFFQy8-6yL1a7hhhFZp-huEIJSGLhmbvbsKdIbo_hB8GiRmQiYnuhdtr1l1z3MNKgAAAGHE";// 有效期2个小时 url += "&device_type="; url += "gh_e93c1b3098b9"; url += "&device_id="; url += "gh_e93c1b3098b9_dae1c2072212185c"; // Serial.print("Requesting URL: "); // Serial.println(url); // This will send the request to the server client.print(String("GET ") + url + " HTTP/1.1 " + "Host: " + host + " " + "Connection: close "); delay(600); //处理返回信息 String line = client.readStringUntil(' '); while (client.available() > 0) { line += client.readStringUntil(' '); line +=' '; } Serial.println(line); client.stop(); Serial.println(); Serial.println("closing connection"); }
改进版本
自动将获取得到的用户ID组分割成数组逐一保存
#include <ESP8266WiFi.h> #include <ArduinoJson.h> /* { "errcode": 42001, "errmsg": "access_token expired" } */ #define WEIXIN_TOKEN "19_uIzAk_rDxS4CLZoAk5016hvXmrjGw-jnH3zPQnZwnZhcw_H7PpfQvWl2KX4vZtl5ykxCJKiRk5ED-rSZs88g-cvq2qkHFKEwSh8ga_phKkg8VNfJbf_u8oB2R7cGhksmBUXpukZ6UhyM729BWXYhAIAPQR" #define PRODUCT_TYPE "gh_e93c1b3098b9" //#define PRODUCT_ID "gh_e93c1b3098b9_dae1c2072212185c" #define PRODUCT_ID "gh_e93c1b3098b9_cc8c4f4cd693972f" #define host "api.weixin.qq.com" #define httpPort 80 #define ssid "HUAWEI-H3VBKZ" #define password "13991320168" void setup() { Serial.begin(115200); delay(10); // We start by connecting to a WiFi network 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()); } /* 功能:5-0 请求设备绑定的用户ID 输入: String UESRID 微信用户ID String CONTENT 要发送的内容 输出: 无 */ /* "errcode":42001,"errmsg":"access_token expired"} { "open_id": [ "ognVI6JsmBGd7lxYTZY4GH29LcNg", "ognVI6GpDeseo6Qe_S7hGPgwlt8E", "ognVI6CC8_HsPH5zgydb-PZFmxqU", "ognVI6FxhqhGVuGhsZbmDyutgsMQ" ], "resp_msg": { "ret_code": 0, "error_info": "ok" } } */ String RequestUserId(){ Serial.println("/**************************************************/"); Serial.println("5 request userID! "); Serial.print("connecting to "); Serial.println(host); // Use WiFiClient class to create TCP connections WiFiClient client; if (!client.connect(host, httpPort)) { //works! Serial.println("connection failed"); return "fail"; } // We now create a URI for the request String url = "/device/get_openid"; url += "?access_token="; url += WEIXIN_TOKEN; url += "&device_type="; url += PRODUCT_TYPE; url += "&device_id="; url += PRODUCT_ID; // This will send the request to the server client.print(String("GET ") + url + " HTTP/1.1 " + "Host: " + host + " " + "Connection: close "); delay(600); Serial.println(); //处理返回信息 String line = client.readStringUntil(' '); while (client.available() > 0) { line += client.readStringUntil(' '); line +=' '; } // Serial.println(line); client.stop(); if(line.indexOf("access_token")>0){ Serial.println("access_token expired"); return "fail"; } //datStart = s.indexOf(timesign) + strlen(timesign); if(line.indexOf("open_id")>0){ //{"open_id":["ognVI6JsmBGd7lxYTZY4GH29LcNg","ognVI6GpDeseo6Qe_S7hGPgwlt8E","ognVI6CC8_HsPH5zgydb-PZFmxqU","ognVI6FxhqhGVuGhsZbmDyutgsMQ"],"resp_msg":{"ret_code":0,"error_info":"ok"} int datebegin= line.indexOf("{"); // int dateend=line.length(); int dateend=line.lastIndexOf("}"); String datajson=line.substring(datebegin,dateend); Serial.println(datajson); return datajson; } else{ Serial.println("fail"); return "fail"; } Serial.println(); Serial.println("closing connection "); } /* 功能:5-1 对用户ID组进行解析拆分 输入: String json 用户ID组 "ognVI6JsmBGd7lxYTZY4GH29LcNg","ognVI6GpDeseo6Qe_S7hGPgwlt8E","ognVI6CC8_HsPH5zgydb-PZFmxqU","ognVI6FxhqhGVuGhsZbmDyutgsMQ", String json_ruselt[] 拆分存放的数组 int sizejson 拆分存放的数组大小 默认10 输出: 成功返回1 */ bool RequestUserId_json(String json,String json_ruselt[],int sizejson){ // if(json.length()<1){return 0;} // DynamicJsonDocument jsonBuffer(json.length()); // deserializeJson(jsonBuffer, json); // JsonObject root = jsonBuffer.as<JsonObject>(); // // String ueserid1 = root[String("open_id")][0]; // String ueserid2 = root[String("open_id")][1]; // String ueserid3 = root[String("open_id")][2]; // String ueserid4 = root[String("open_id")][3]; // Serial.println(); // Serial.println(ueserid1); // Serial.println(ueserid2); // Serial.println(ueserid3); // Serial.println(ueserid4); int datebegin= json.indexOf("[")+1; int dateend=json.indexOf("]"); String datajson=json.substring(datebegin,dateend); datajson= datajson+','; // Serial.println(datajson); /* ognVI6JsmBGd7lxYTZY4GH29LcNg dongdong ognVI6GpDeseo6Qe_S7hGPgwlt8E ognVI6CC8_HsPH5zgydb-PZFmxqU ognVI6FxhqhGVuGhsZbmDyutgsMQ */ int i=0; while(datajson.length()>0){ int idbegin=0; int idend=datajson.indexOf(","); String id=datajson.substring(idbegin,idend); id=id.substring(1,id.length()-1); if(i<sizejson){ json_ruselt[i]=id; i++;} // Serial.println(id); datajson=datajson.substring(idend+1,datajson.length()); } return 1; } void loop() { delay(10000); String json= RequestUserId(); // 得到用户ID组 String json_ruselt[10]; // 解析的存放数组 bool user_num_ok= RequestUserId_json(json,json_ruselt,10); if(!user_num_ok) return; else{ for(int i=0;i<10;i++){ if(json_ruselt[i]!="") Serial.println(json_ruselt[i]); } } }