【问题标题】:PushBullet NodemcuPushBullet Nodemcu
【发布时间】:2020-03-12 11:12:24
【问题描述】:

我正在开发安全警报系统,需要将通知推送到我的手机 并根据 pushbullet api 文档编写代码,但我的代码不起作用:

我两个都用 “https://api.pushbullet.com”和 pushbullet_server 的“api.pushbullet.com” 并使用 WiFiClientSecure 和 WiFiClient 发布数据

void PushBullet::sendNotification(const String title ,const String body){

  String url = "/v2/pushes";

  String msg =  String("{\"body\":\"") + body + String("\",\"title\":\"")+ title + String("\",\"type\":\"note\"}");
  Serial.println(msg);
  String request = String("POST ") + url + "HTTP/1.1\r\n" + 
                   "Host: " + pushbullet_server + "\r\n" +
                   "User-Agent: ESP8266/NodeMCU 0.9\r\n" +
                   "Accept: */*\r\n" +
                   "Content-Type: application/json\r\n" +
                   "Content-Length: "+ msg.length() +"\r\n" +
                   "Access-Token: "+ this->access_token +"\r\n\r\n" +
                   msg;


  Serial.println(request); 
  Serial.println("- connecting to pushing server: " + String(pushbullet_server));
  secure_client.setInsecure();
  if (!secure_client.connect(pushbullet_server, 443)) {
    Serial.println("faile to connect " + pushbullet_server);
      Serial.println(secure_client.readStringUntil('\n'));
    return;
  }

  secure_client.print(request);


  secure_client.stop();

  while(secure_client.connected() && !secure_client.available()) delay(1); //waits for data
  Serial.println(secure_client.readStringUntil('\n'));

  Serial.println("- stopping the client");
}

WiFiClientSecure 安全客户端;

String pushbullet_server = "https://api.pushbullet.com";

我也变了

String("POST ") + url + "HTTP/1.1\r\n" 来自 String("GET ") + url + "HTTP/1.1\r\n"

网址是

String url = "/v2/pushes";

【问题讨论】:

    标签: esp8266 nodemcu notify pushbullet


    【解决方案1】:

    解决了

    String url = "/v2/pushes";
    
      String msg = String("{\"body\":\"") + body + String("\",\"title\":\"")+ title + String("\",\"type\":\"note\"}");
    
      String request = String("POST ") + url + " HTTP/1.1\r\n" +
                       "Host: " + this->pushbullet_server + "\r\n" +
                       "User-Agent: ESP8266\r\n" +
                       "Authorization: Bearer " + "o.75WBTSImyZuqQgxgdbE3Tud6ADRzEc6n" + "\r\n" +
                       "Content-Type: application/json\r\n" +
                       "Content-length: " + String(msg.length()) + "\r\n" + 
                       "Connection: close\r\n\r\n";
    
      //"Access-Token: "+ "o.75WBTSImyZuqQgxgdbE3Tud6ADRzEc6n" +"\r\n" +
    
      Serial.println(request);
    
      secure_client.setInsecure();
    
      Serial.println("- connecting to pushing server: " + String(pushbullet_server));
    
      if (!secure_client.connect(pushbullet_server, 443)) {
        Serial.println("faile to connect " + pushbullet_server);
          Serial.println(secure_client.readStringUntil('\n'));
        return;
      }
    
      Serial.println("------------------Connect  Succesfull--------------------------------------");
    
      secure_client.print(request);
      secure_client.print(msg);
    
    

    我忘记了这行代码: "连接:关闭\r\n\r\n"

    通知发送成功

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-15
      • 2016-10-27
      相关资源
      最近更新 更多