微信硬件平台(八) 4 ESP8266通过微信公众号给用户推送消息

https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=自己申请微信公众号的TOKEN

输出结果:  由于aRDUINO串口不支持打印中文所以乱码,但是微信端接受正常。

 

代码实现:

#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());
}

 int num=0;
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 = "/cgi-bin/message/custom/send";
  url += "?access_token=";
  url +="19_U0qaL6WclJtNRhFCtbTs6zTVA_edIDPWckzG4CRyfoFJOxdQjLWOtQTNzIt2orXFj_-XpoDn6fS3DvZ0_D9O1N1K8Ke1Wq0_lM1iiZ3XWzc9-85g9zlIA_diYxjbtpN5Pg2jTxq7uEmdQ4A3KEDeABAQKJ";
 // url += "&device_type=";
 // url += "gh_e93c1b3098b9";
 // url += "&device_id=";
 // url += "gh_e93c1b3098b9_dae1c2072212185c";
if(num<100){
  num++;}
  else { num=0;  }
      String data =(String) "{"    
   + " \"touser\":\"ognVI6JsmBGd7lxYTZY4GH29LcNg\"," 
   + " \"msgtype\":\"text\","  
   +  "\"text\" :"  
   +  "{ \"content\":\"这是来自ESP8266发送的"+num+"条消息:\r\n  电量统计: 98\r\n 空气质量: 89\r\n 连接地址:<a href=http://www.qq.com >!\", }"   //中文可以发送,没问题
   + "}"  ;
      int length = data.length();
      
      String postRequest =(String)("POST ") + url + " HTTP/1.1\r\n" +
          "Content-Type: application/json;charset=utf-8\r\n" +
          "Host: " + host + ":" + httpPort + "\r\n" +          
          "Content-Length: " + length + "\r\n" +
          "Connection: Keep Alive \r\n" +
          +"\r\n"
          +data
          +"\r\n";
       Serial.println(postRequest);
       client.print(postRequest);


 
  // This will send the request to the server
//  client.print(postRequest);
    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");
}

遇到问题:

问题一:

一定时间内,有推送消息限制。20条。如果推送了20条用户没有给公众号发送消息或者互动(点击菜单什么的),微信服务器停止推送消息。

所以每当我从公众号接受20条消息,就要手动给公众号回复消息。

问题二:

  1. {
  2.  
    "errcode":45015,
  3.  
    "errmsg":"response out of time limit or subscription is canceled hint: [ZE1Uxa0498age8]"
  4.  
    }
     
     
     原因是当用户微信不活跃时间超过24小时(此时间当前是多少由 腾讯定),不会将信息推送到用户 微信公众号

猜你喜欢

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