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


    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
    " +
              "Content-Type: application/json;charset=utf-8
    " +
              "Host: " + host + ":" + httpPort + "
    " +          
              "Content-Length: " + length + "
    " +
              "Connection: Keep Alive 
    " +
              +"
    "
              +data
              +"
    ";
          // Serial.println(postRequest);
           client.print(postRequest);
    
        delay(600);
        //处理返回信息
        String line = client.readStringUntil('
    ');
      
        while (client.available() > 0) {
          line +='
    ';
          line += client.readStringUntil('
    ');
         
        }
        
        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+"条消息:
      电量统计: 98
     空气质量: 89
     连接地址:<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小时(此时间当前是多少由腾讯定),不会将信息推送到用户微信公众号
  • 相关阅读:
    2013-8-14大一大二暑期组队训练赛
    注重实效的程序员——哲学篇
    读《企业应用架构模式》-锁
    OpenCV 编码样式指南
    Offer_1
    μC/OS学习资料(附Ebook)
    poj 1990
    POJ 2455 网络流 基础题 二分+网络流 dicnic 以及 sap算法
    ViewPageAsImage
    win7下wordPress本地搭建博客详解(深度亲测整理---傻瓜式详细教程)
  • 原文地址:https://www.cnblogs.com/kekeoutlook/p/10464070.html
Copyright © 2020-2023  润新知