Pushbullet example
Posted: Sun Dec 13, 2015 11:16 am
Finally got around to try https connection to send messages to my phone through pushbullet. To test it I used the HTTPSRequest example and just changed a few lines of code. Here is the example that sends to pushbullet. Just add your SSID, password and pushbullet API Key:
Code: Select all
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
const char* ssid = ".......";
const char* password = ".......";
const char* host = "api.pushbullet.com";
const int httpsPort = 443;
const char* PushBulletAPIKEY = "..............."; //get it from your pushbullet account
// Use web browser to view and copy
// SHA1 fingerprint of the certificate
const char* fingerprint = "2C BC 06 10 0A E0 6E B0 9E 60 E5 96 BA 72 C5 63 93 23 54 B3"; //got it using https://www.grc.com/fingerprints.htm
void setup() {
Serial.begin(115200);
Serial.println();
Serial.print("connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// Use WiFiClientSecure class to create TLS connection
WiFiClientSecure client;
Serial.print("connecting to ");
Serial.println(host);
if (!client.connect(host, httpsPort)) {
Serial.println("connection failed");
return;
}
if (client.verify(fingerprint, host)) {
Serial.println("certificate matches");
} else {
Serial.println("certificate doesn't match");
}
String url = "/v2/pushes";
String messagebody = "{\"type\": \"note\", \"title\": \"ESP8266\", \"body\": \"Hello World!\"}\r\n";
Serial.print("requesting URL: ");
Serial.println(url);
client.print(String("POST ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Authorization: Bearer " + PushBulletAPIKEY + "\r\n" +
"Content-Type: application/json\r\n" +
"Content-Length: " +
String(messagebody.length()) + "\r\n\r\n");
client.print(messagebody);
Serial.println("request sent");
//print the response
while (client.available() == 0);
while (client.available()) {
String line = client.readStringUntil('\n');
Serial.println(line);
}
}
void loop() {
}