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

时间:2022-11-25 07:55:16

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

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

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

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

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

代码实现:

#include <ESP8266WiFi.h>

#define WEIXIN_TOKEN  "19_qLywZOTSRQsE3NhNthVSL-MCFtpgC26QZZlard0yjaXAxW3G3TtNCnoTneMQrQtK-CcpjsruX084iVuLFBsuVRmJJgYKCSlJhcASOH5To_dHPe7jPj30HpGBIif22Pn3be77Hu8Z56KVs8LTOREbAIAYBO"
#define PRODUCT_TYPE "gh_e93c1b3098b9"
#define PRODUCT_ID "gh_e93c1b3098b9_dae1c2072212185c"
#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); //works! while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
} Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
} /*
输入:
String UESRID 微信用户ID
String CONTENT 要发送的内容
输出: 无
*/
void SendMsgToUser(String UESRID,String CONTENT){ String data =(String) "{"
+ " \"touser\":\""+UESRID+"\","
+ " \"msgtype\":\"text\","
+ "\"text\" :"
+ "{ \"content\":\""+CONTENT+"\", }"
+ "}" ; Serial.println("/**************************************************/");
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;
} // We now create a URI for the request
String url = "/cgi-bin/message/custom/send";
url += "?access_token=";
url += WEIXIN_TOKEN; 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); delay(600);
//处理返回信息
String line = client.readStringUntil('\n'); while (client.available() > 0) {
line +='\n';
line += client.readStringUntil('\n'); } Serial.println(line);
client.stop(); Serial.println();
Serial.println("closing connection!"); } int msgnum=0;
void loop() { if(msgnum<100){
msgnum++;}
else { msgnum=0; } delay(10000); String UESRID="ognVI6JsmBGd7lxYTZY4GH29LcNg";
String CONTENT=(String)"这是来自ESP8266发送的第"+msgnum+"条消息:\r\n 电量统计: 98\r\n 空气质量: 89\r\n 连接地址:<a href=http://www.qq.com >!"; SendMsgToUser(UESRID,CONTENT);
}

  

遇到问题:

问题一:

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

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

问题二:

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