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


輸出結果: 由於aRDUINO串口不支持打印中文所以亂碼,但是微信端接受正常。

代碼實現:
#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條消息,就要手動給公眾號回復消息。
問題二:
-
{
-
"errcode":45015,
-
"errmsg":"response out of time limit or subscription is canceled hint: [ZE1Uxa0498age8]"
-
