Skip to content

Instantly share code, notes, and snippets.

@gsampallo
Last active August 16, 2020 18:30
Show Gist options
  • Save gsampallo/a4139fcdaacf28f3cd087df9900e0227 to your computer and use it in GitHub Desktop.
Save gsampallo/a4139fcdaacf28f3cd087df9900e0227 to your computer and use it in GitHub Desktop.
Simple ejemplo para enviar una notificación push a un telefono por medio de Pushbullet desde NodeMCU.
/*
* Send Push notification from NodeMCU with Pushbullet API
* https://www.gsampallo.com/2020/07/09/envio-de-notificaciones-desde-nodemcu-con-pushbullet/
* Guillermo Sampallo 2020
*/
#include <ESP8266WiFi.h>
const char* ssid = "SSD_NETWORK";
const char* password = "PASSWORD_NETWORK";
int pinBoton = 4;
const char* apiKey = "TOKEN";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid,password);
Serial.print("Conectando");
while(WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(100);
}
Serial.println("Conectado");
Serial.print("Direccion IP: ");
Serial.println(WiFi.localIP());
pinMode(pinBoton,INPUT_PULLUP);
}
void loop() {
if(digitalRead(pinBoton) == LOW) {
Serial.println("Presionado");
enviarMensaje("NodeMCU","Mensaje enviado desde NodeMCU");
delay(2000);
}
delay(100);
}
const char* host = "api.pushbullet.com";
void enviarMensaje(String titulo,String mensaje) {
WiFiClientSecure client;
client.setInsecure();
if(!client.connect(host,443)) {
Serial.println("No se pudo conectar con el servidor");
return;
}
String url = "/v2/pushes";
String message = "{\"type\": \"note\", \"title\": \""+titulo+"\", \"body\": \""+mensaje+"\"}\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 " + apiKey + "\r\n" +
"Content-Type: application/json\r\n" +
"Content-Length: " +
String(message.length()) + "\r\n\r\n");
client.print(message);
delay(2000);
while (client.available() == 0);
while (client.available()) {
String line = client.readStringUntil('\n');
Serial.println(line);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment