Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By Gadjet
#37640 Thanks for posting this code it was just what I was looking for.

I modified it because I want to trigger the pushbullet note from a switch linked to my 3D printer so I get a notification when it's finishes.

All works great, I can trigger the message a few times OK but after say 5 min's I try to trigger it again and the serial output doesn't get past "request sent" I assume it's because it's stuck at the
Code: Select allwhile (client.available() == 0);

but not sure why.

Reset and try again and it works great for a few minutes.

any ideas what I'm doing wrong?

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

const int BUTTON = 4; //button input

// 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
WiFiClientSecure client;
void setup() {
  pinMode(BUTTON, INPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(15, OUTPUT);
  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
 
  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");
  }
}


void loop() {
  if (digitalRead(BUTTON)==LOW){ // Wait for buton press
      String url = "/v2/pushes";
  String messagebody = "{\"type\": \"note\", \"title\": \"3D Printer\", \"body\": \"3D Print Finished\"}\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");
 
    while (client.available() == 0);

  while (client.available()) {
    String line = client.readStringUntil('\n');
    Serial.println(line);
  }
  } 
}
User avatar
By Gadjet
#37704 I think I found the problem, I'd left the https connection code outside the loop in the setup section so I'm assuming it timed out after a period and that's why I couldn't get it to work more than a few times.

I moved the connection code in to the loop that's run when the button is pressed and it works fine now.

Thanks again.

Phil