Created
August 18, 2019 07:35
-
-
Save en129/7163a1451d275352d19da6908ae3dff2 to your computer and use it in GitHub Desktop.
Send push notification from ESP32 using Pushbullet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <WiFiClientSecure.h> | |
#include <string.h> | |
WiFiClientSecure client; | |
/**************************/ | |
//WiFi config | |
/**************************/ | |
const char* ssid = "XXXXXXXXXXXXX"; // your network SSID (name of wifi network) | |
const char* password = "XXXXXXXXXXXXX"; // your network password | |
/**************************/ | |
//Pushbullet config | |
/**************************/ | |
const char* accessToken="XXXXXXXXXXXXXXXXXX"; // your pushbullet access token | |
const char* pushTitle="test"; | |
const char* pushMsg="hoge"; | |
// DSTRootCAX3.crt | |
const char* test_root_ca= \ | |
"-----BEGIN CERTIFICATE-----\n" \ | |
"MIIDSjCCAjKgAwIBAgIQRK+wgNajJ7qJMDmGLvhAazANBgkqhkiG9w0BAQUFADA/\n" \ | |
"MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT\n" \ | |
"DkRTVCBSb290IENBIFgzMB4XDTAwMDkzMDIxMTIxOVoXDTIxMDkzMDE0MDExNVow\n" \ | |
"PzEkMCIGA1UEChMbRGlnaXRhbCBTaWduYXR1cmUgVHJ1c3QgQ28uMRcwFQYDVQQD\n" \ | |
"Ew5EU1QgUm9vdCBDQSBYMzCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB\n" \ | |
"AN+v6ZdQCINXtMxiZfaQguzH0yxrMMpb7NnDfcdAwRgUi+DoM3ZJKuM/IUmTrE4O\n" \ | |
"rz5Iy2Xu/NMhD2XSKtkyj4zl93ewEnu1lcCJo6m67XMuegwGMoOifooUMM0RoOEq\n" \ | |
"OLl5CjH9UL2AZd+3UWODyOKIYepLYYHsUmu5ouJLGiifSKOeDNoJjj4XLh7dIN9b\n" \ | |
"xiqKqy69cK3FCxolkHRyxXtqqzTWMIn/5WgTe1QLyNau7Fqckh49ZLOMxt+/yUFw\n" \ | |
"7BZy1SbsOFU5Q9D8/RhcQPGX69Wam40dutolucbY38EVAjqr2m7xPi71XAicPNaD\n" \ | |
"aeQQmxkqtilX4+U9m5/wAl0CAwEAAaNCMEAwDwYDVR0TAQH/BAUwAwEB/zAOBgNV\n" \ | |
"HQ8BAf8EBAMCAQYwHQYDVR0OBBYEFMSnsaR7LHH62+FLkHX/xBVghYkQMA0GCSqG\n" \ | |
"SIb3DQEBBQUAA4IBAQCjGiybFwBcqR7uKGY3Or+Dxz9LwwmglSBd49lZRNI+DT69\n" \ | |
"ikugdB/OEIKcdBodfpga3csTS7MgROSR6cz8faXbauX+5v3gTt23ADq1cEmv8uXr\n" \ | |
"AvHRAosZy5Q6XkjEGB5YGV8eAlrwDPGxrancWYaLbumR9YbK+rlmM6pZW87ipxZz\n" \ | |
"R8srzJmwN0jP41ZL9c8PDHIyh8bwRLtTcm1D9SZImlJnt1ir/md2cXjbDaJWFBM5\n" \ | |
"JDGFoqgCWjBH4d1QB7wCCZAA62RjYJsWvIjJEubSfZGL+T0yjWW06XyxV3bqxbYo\n" \ | |
"Ob8VZRzI9neWagqNdwvYkQsEjgfbKbYK7p2CNTUQ\n" \ | |
"-----END CERTIFICATE-----\n"; | |
const char* server = "api.pushbullet.com"; // Server URL | |
void setup() { | |
//Initialize serial and wait for port to open: | |
Serial.begin(115200); | |
delay(100); | |
Serial.print("Attempting to connect to SSID: "); | |
Serial.println(ssid); | |
WiFi.begin(ssid, password); | |
// attempt to connect to Wifi network: | |
while (WiFi.status() != WL_CONNECTED) { | |
Serial.print("."); | |
// wait 1 second for re-trying | |
delay(1000); | |
} | |
Serial.print("Connected to "); | |
Serial.println(ssid); | |
client.setCACert(test_root_ca); | |
Serial.println("\nStarting connection to server..."); | |
if (!client.connect(server, 443)) | |
{ | |
Serial.println("Connection failed!"); | |
} | |
else | |
{ | |
Serial.println("Connected to server!"); | |
//HTTP REQUEST | |
client.println("POST /v2/pushes HTTP/1.0"); | |
//HTTP HEADER | |
client.println("Host: api.pushbullet.com"); | |
client.println("Content-Type: application/json"); | |
client.println("Connection: close"); | |
client.print("Access-Token:"); | |
client.println(accessToken); | |
client.print("Content-Length: "); | |
client.println(41+strlen(pushTitle)+strlen(pushMsg)); | |
client.println(); | |
//HTTP BODY | |
client.print("{\"type\": \"note\", \"title\": \""); | |
client.print(pushTitle); | |
client.print("\", \"body\": \""); | |
client.print(pushMsg); | |
client.println("\"}"); | |
while (client.connected()) { | |
String line = client.readStringUntil('\n'); | |
if (line == "\r") { | |
Serial.println("headers received"); | |
break; | |
} | |
} | |
// if there are incoming bytes available | |
// from the server, read them and print them: | |
while (client.available()) | |
{ | |
char c = client.read(); | |
Serial.write(c); | |
} | |
client.stop(); | |
} | |
} | |
void loop() { | |
// do nothing | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment