Skip to content

Instantly share code, notes, and snippets.

@SukkoPera
Created June 20, 2018 21:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SukkoPera/5d891f036c949b84f7ae69cac9ad1e68 to your computer and use it in GitHub Desktop.
Save SukkoPera/5d891f036c949b84f7ae69cac9ad1e68 to your computer and use it in GitHub Desktop.
Arduino client for the PushBullet notification service (via PushingBox)
#include <WiFiEsp.h>
#include "SoftwareSerial.h"
SoftwareSerial swSerial (10, 11); // RX, TX
#define PUSHINGBOX_DEVID "xxx"
#define BUTTON_PIN 2
char ssid[] = "ssid"; // your network SSID (name)
char pass[] = "password"; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio's status
// Initialize the Ethernet client object
WiFiEspClient client;
void setup() {
Serial.begin (9600);
swSerial.begin (9600);
WiFi.init (&swSerial);
// check for the presence of the shield
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println(F("WiFi shield not present"));
// don't continue
while (true);
}
// attempt to connect to WiFi network
while ( status != WL_CONNECTED) {
Serial.print(F("Attempting to connect to WPA SSID: "));
Serial.println(ssid);
// Connect to WPA/WPA2 network
status = WiFi.begin(ssid, pass);
}
// you're connected now, so print out the data
Serial.println(F("You're connected to the network"));
printWifiStatus();
pinMode (BUTTON_PIN, INPUT_PULLUP);
}
#define BUFSIZE 32
// http://hardwarefun.com/tutorials/url-encoding-in-arduino
void clientPrintEncoded (const char* s) {
static const char *hex = "0123456789abcdef";
char buf[BUFSIZE];
int n = 0;
for (; *s; s++) {
if (('a' <= *s && *s <= 'z')
|| ('A' <= *s && *s <= 'Z')
|| ('0' <= *s && *s <= '9')) {
buf[n++] = *s;
} else {
buf[n++] = '%';
buf[n++] = hex[*s >> 4];
buf[n++] = hex[*s & 15];
}
if (n > BUFSIZE - 3) {
client.write ((byte *) buf, n);
n = 0;
}
}
if (n > 0)
client.write ((byte *) buf, n);
}
size_t strlenEncoded (const char* s) {
size_t l = 0;
for (; *s; s++) {
if (('a' <= *s && *s <= 'z')
|| ('A' <= *s && *s <= 'Z')
|| ('0' <= *s && *s <= '9')) {
l++;
} else {
l += 3;
}
}
return l;
}
boolean sendNotification (const char* title, const char* body) {
if (client.connect ("api.pushingbox.com", 80)) {
Serial.println (F("Connected to server"));
// Make a HTTP request
#if 1
// Use POST method
client.println (F("POST /pushingbox HTTP/1.1"));
client.println (F("Host: api.pushingbox.com"));
//client.println (F("User-Agent: Arduino"));
//client.println (F("Connection: close"));
client.println (F("Content-Type: application/x-www-form-urlencoded"));
client.print (F("Content-Length: "));
client.println (6 + strlen (PUSHINGBOX_DEVID) + 7 + strlenEncoded (title) + 6 + strlenEncoded (body));
client.println ();
client.print (F("devid="));
client.print (F(PUSHINGBOX_DEVID));
client.print (F("&title="));
clientPrintEncoded (title);
client.print (F("&body="));
clientPrintEncoded (body);
client.println ();
#else
// Use GET method
client.print (F("POST /pushingbox?"));
client.print (F("devid="));
client.print (F(PUSHINGBOX_DEVID));
client.print (F("&title="));
clientPrintEncoded (title);
client.print (F("&body="));
clientPrintEncoded (body);
client.println (F(" HTTP/1.1"));
client.println (F("Host: api.pushingbox.com"));
//client.println (F("User-Agent: Arduino"));
//client.println (F("Connection: close"));
client.println ();
#endif
// This doesn't seem to detect all errors, still it's better than nothing
boolean err = false;
while (client.connected ()) {
if (client.available ()) {
char c = client.read ();
//repbuf[replen++] = c;
Serial.print (c);
#if 0
buf.push (c);
if (buf.endsWith ("error")) {
// :(
err = true;
}
#endif
}
}
Serial.println ();
Serial.println (F("Disconnecting from server..."));
client.stop ();
if (err) {
Serial.println (F("Send failed"));
} else {
Serial.println (F("Message sent"));
}
return !err;
} else {
Serial.println (F("Connection failed"));
}
return false;
}
void loop () {
if (digitalRead (BUTTON_PIN) == HIGH) {
// ALARM!
sendNotification ("ALARM!!!", "Everything's not so alright tonight...");
//sendNotification ("Bah", "From Arduino with POST");
while (digitalRead (BUTTON_PIN) == HIGH)
;
}
}
void printWifiStatus() {
// print the SSID of the network you're attached to
Serial.print(F("SSID: "));
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address
IPAddress ip = WiFi.localIP();
Serial.print(F("IP Address: "));
Serial.println(ip);
// print the received signal strength
long rssi = WiFi.RSSI();
Serial.print(F("Signal strength (RSSI):"));
Serial.print(rssi);
Serial.println(F(" dBm"));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment