#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() {
}
Moderator: igrr
Thank you for this i have been looking for a way to send notifications to my phone from esp and its working perfect.
Just one thing is there a way to send variables too? lets say i have the temperature in a float and want to send that?
i have been trying a few different things but I'm afraid i don't know what I'm doing or how the result should look like, any help/tip on how to do this?
I was also looking for this code for some time but only found people looking for it but no one actually posting about how exactly to do it. So I read up on the API, SSL and the HTTP protocol and to my surprise got it working pretty quickly.
Sure you can send dynamic values, that was the whole point of this exercise Sorry to not include it in the example code.
Just a warning: I found that the SSL encryption uses pretty much all of the available ram of the ESP, so as soon as you add too much code to your sketch, it will just crash when trying to access the secure API.
To modify the code to send danymic data try this:
int16_t mySensorvalue = analogRead(A0); //a dummy example of a sensor value
String messagebody = "{\"type\": \"note\", \"title\": \"Dynamic sensorvalue from the ESP\", \"body\": \"The measured value is: ";
messagebody += String(mySensorvalue);
messagebody += "\"}\r\n";
Let me know if it works, I have not tried it yet since I have no access to a compiler nor an ESP at the moment.
And thank you for the warning abut the memory, the nrf24l01 code I'm using for this is pretty low in memory use but i will keep it in mind when i add more code.
Again Big thank you for this code!!