Search code examples
esp8266nodemcunotifypushbullet

PushBullet Nodemcu


i working on security alarm system and need to push notify to my phon and write code according pushbullet api documents but my code not work :

i use both "https://api.pushbullet.com" and "api.pushbullet.com" for pushbullet_server and use WiFiClientSecure and WiFiClient for post data

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");
}

and

WiFiClientSecure secure_client;

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

i also change

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

url is

String url = "/v2/pushes";

Solution

  • solved

    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);
    
    

    i forget this line of code : "Connection: close\r\n\r\n"

    notification send successful