Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By DedeHai
#36333 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() {
}
Last edited by DedeHai on Tue Dec 22, 2015 5:25 am, edited 1 time in total.
User avatar
By swe-dude
#36701 Hello!
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?
User avatar
By DedeHai
#36947 You are very welcome.
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:

Code: Select allint16_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.
User avatar
By swe-dude
#37158 Thank you it works perfect, now i am getting notifications when i get snail mail and when the inside temp get too high or low, now to figure out what else might be useful to send out...
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!!