koenieee / PushBullet-ESP8266

PushBullet library to send and receive notifications/sms from your ESP8266 microcontroller.
GNU General Public License v2.0
29 stars 10 forks source link

got pushbullet-ESP8266 to work #7

Closed larryn46 closed 4 years ago

larryn46 commented 4 years ago

I had to make some changes to example.ino to work. Sorry, the indentations don't show up in HTML


/*
* downloaded from https://www.hackster.io/Nouira/send-notifications-to-your-phone-or-pc-using-nodemcu-cf7c43
* https://github.com/ koenieee/ PushBullet-ESP8266/ issues/7
*/
#include "/home/fido/Arduino/credentials.h" // includes password and ssid
//need to change directory
#include < ESP8266WiFi.h > // import ESP8266 WiFi library
#include < WiFiClientSecure.h > //import client library
const char* programName = "pushbullet_note_link";
//const char* ssid = "crepe et gaufre";//SSID for your local wireless
//const char* password = "crepes@gaufres"; //Password

const char* host = "api.pushbullet.com";
const int httpsPort = 443; // the required port
const char* PushBulletAPIKEY = "o.xxxx...xxxx"; //get it from your pushbullet account
// api.pushbullet.com FA C0 BE 4E 64 B8 80 C7 B7 EC B6 D9 01 FF 61 0D AC 92 F5 A6
const char* fingerprint = "FA C0 BE 4E 64 B8 80 C7 B7 EC B6 D9 01 FF 61 0D AC 92 F5 A6";
void setup() {
Serial.begin(115200);
while(!Serial){} // delay
Serial.println();
Serial.println(programName);
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;
// bool setFingerprint(const char *fpStr);
client.setFingerprint(fingerprint);
Serial.print("setup() line "); Serial.print(__LINE__); Serial.print(" connecting to ");
Serial.println(host);
unsigned long connectTimeOut = 30000; // 30 seconds
unsigned long connectTimer = millis();
while(!client.connect(host, httpsPort))
{
Serial.print(".");
delay(1000);
if((millis() - connectTimer) > connectTimeOut){
Serial.print(" setup() line "); Serial.print(__LINE__); Serial.println(" connection timed out");
while(1); // halt
} // end if
} // end while
Serial.println();
Serial.print("setup() line "); Serial.print(__LINE__);Serial.println("connected");


String url = "/v2/pushes";
String messagebody_note = "{\"type\": \"note\", \"title\": \"Hello !\", \"body\": \"My Name is Ahmed And You ?\"}\r\n";
String messagebody_link = "{\"type\": \"link\", \"title\": \"My Youtube\", \"body\": \"Best Channel^^!\",
\"url\": \"https://www.youtube.com/channel/UCZnkC2WQf-LcH9EOmHjlExA?
view_as=subscriber\"}\r\n";
Serial.print("requesting URL: ");
Serial.println(url);
//send a simple note
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_note.length()) + "\r\n\r\n");
client.print(messagebody_note);
delay(10000); // wait 10 s
//send a link
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_link.length()) + "\r\n\r\n");
client.print(messagebody_link);

Serial.println("request sent");
//print the response

while (client.available() == 0);

while (client.available()) {
String line = client.readStringUntil('\n');
Serial.println(line);
} // end while loop

}// end setup
void loop() {
}// end loop()