首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PushBullet节点

PushBullet节点
EN

Stack Overflow用户
提问于 2020-03-12 19:12:24
回答 1查看 231关注 0票数 0

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

对于pushbullet_server,我同时使用"https://api.pushbullet.com“和"api.pushbullet.com”,对于post数据,我使用WiFiClientSecure和WiFiClient

代码语言:javascript
复制
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 secure_client;

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

我也改变了

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

url是

代码语言:javascript
复制
String url = "/v2/pushes";
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-13 15:42:00

已解决

代码语言:javascript
复制
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);

我忘记了这行代码:"Connection: close\r\n\r\n“

通知发送成功

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60652923

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档