微信硬件平台(八) 3 ESP8266向微信服务器请求设备绑定的用户

 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\r\n" +
               "Host: " + host + "\r\n" + 
               "Connection: close\r\n\r\n");
    delay(600);
    //处理返回信息
    String line = client.readStringUntil('\n');
    while (client.available() > 0) {
      line += client.readStringUntil('\n');
      line +='\n';
    }
    Serial.println(line);
    client.stop();

    
  Serial.println();
  Serial.println("closing connection");
}

  

猜你喜欢

转载自www.cnblogs.com/kekeoutlook/p/10464065.html