Skip to content

Instantly share code, notes, and snippets.

@justinian
Created February 20, 2017 21:16
Show Gist options
  • Save justinian/05f23ee87d2a28cea2ab7be86c643fc7 to your computer and use it in GitHub Desktop.
Save justinian/05f23ee87d2a28cea2ab7be86c643fc7 to your computer and use it in GitHub Desktop.
Pillminder Arduino Code
#include <Adafruit_LEDBackpack.h>
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <Wire.h>
const char* ssid = "xxxxxx";
const char* password = "xxxxxxx";
const char* host = "api.pushover.net";
const int httpsPort = 443;
const char* token = "apixxxxxx";
const char* user = "xxxxxx";
#define DIVISOR 1 // Raise this for debugging purposes
#define SECONDS_PER_HOUR (60*60)
const unsigned long limit_seconds = LIMIT_HOURS * SECONDS_PER_HOUR;
// Use web browser to view and copy
// SHA1 fingerprint of the certificate
const char* fingerprint = "1E D5 B7 68 BB 25 AD A3 E0 96 78 A4 68 48 08 4F 07 E4 8D AB";
Adafruit_7segment matrix = Adafruit_7segment();
unsigned long lastStart = 0;
bool alerted = false;
#define LIMIT_HOURS 23
void resetClock() {
alerted = false;
matrix.setBrightness(2);
matrix.blinkRate(0);
lastStart = millis();
}
void alert() {
alerted = true;
matrix.setBrightness(5);
matrix.blinkRate(2);
WiFiClientSecure client;
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");
return;
}
String url = "/1/messages.json";
String payload = String("token=") + token + "&" +
"title=Take%20Your%20Pill&" +
"message=It+has+been+" + String(LIMIT_HOURS) + "+hours&" +
"user=" + user;
client.print(String("POST ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Content-Type: application/x-www-form-urlencoded\r\n" +
"Content-Length: " + String(payload.length()) + "\r\n" +
"Connection: close\r\n\r\n" +
payload
);
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
break;
}
}
String line = client.readStringUntil('\n');
}
void setup() {
Serial.begin(115200);
Serial.println();
Wire.begin(13,12);
matrix.begin(0x70);
Serial.print("connecting to ");
Serial.println(ssid);
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());
}
bool dot = true;
void loop() {
unsigned long delta = millis() - lastStart;
unsigned long seconds = delta / DIVISOR;
if(delta % DIVISOR == 0) {
unsigned long minutes = (seconds / 60) % 60;
unsigned long hours = seconds / (60*60);
matrix.print((hours*100) + minutes, DEC);
matrix.writeDigitNum(0, (hours/10) % 10, hours > 100);
matrix.writeDigitNum(1, (hours % 10));
matrix.drawColon(true);
matrix.writeDigitNum(3, minutes / 10);
matrix.writeDigitNum(4, minutes % 10, dot);
matrix.writeDisplay();
dot = !dot;
}
if(seconds > limit_seconds && !alerted)
alert();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment