Skip to content

Instantly share code, notes, and snippets.

@uberscientist
Created January 3, 2019 03:14
Show Gist options
  • Save uberscientist/d98649527703cc9645e53d3d243a8c80 to your computer and use it in GitHub Desktop.
Save uberscientist/d98649527703cc9645e53d3d243a8c80 to your computer and use it in GitHub Desktop.
Arduino alarm clock
#include "SSD1306.h" // alias for `#include "SSD1306Wire.h"`
#include <SPI.h>
#include <ezTime.h>
#include <SPIFFS.h>
#include <ESPAsyncWebServer.h>
#include <WiFi.h>
AsyncWebServer server(80);
SSD1306 display(0x3c, 4, 15);
#define SS 18
#define RST 14
#define DI0 26
Timezone myTZ;
time_t current_time;
// Hour and Minute
byte alarm_times[128][2] = {
{ 6, 0 },
{ 8, 30 },
{ 11, 0 },
{ 13, 30 },
{ 16, 0 },
{ 18, 30 },
{ 23, 0 }
};
int freq = 2000;
int channel = 0;
int resolution = 8;
volatile bool alarm_stop_flag = false;
void setup() {
pinMode(16,OUTPUT);
digitalWrite(16, LOW); // set GPIO16 low to reset OLED
delay(50);
digitalWrite(16, HIGH);
display.init();
display.flipScreenVertically();
display.setFont(ArialMT_Plain_10);
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.drawString(0, 0, "Connecting...");
display.display();
Serial.begin(115200);
while (!Serial); //if just the the basic function, must connect to a computer
if(!SPIFFS.begin(true)){
Serial.println("An Error has occured mounting SPIFFS");
return;
}
ledcSetup(channel, freq, resolution);
ledcAttachPin(18, channel);
WiFi.begin("secret ssid", "secret password");
waitForSync();
display.drawString(0, 10, "Connected!");
server.on("/alarm_times", HTTP_GET, [](AsyncWebServerRequest *request) {
String json_response = "[";
for(int i = 0; i < sizeof(alarm_times) / sizeof(alarm_times[0]); i++) {
if(alarm_times[i] == 0) continue;
json_response += "[\"" + String(alarm_times[i][0]) + "\"," + String(alarm_times[i][0]) + "], ";
}
json_response += "\"\"]";
request->send(200, "application/json", json_response);
});
server.serveStatic("/", SPIFFS, "/www/");
server.begin();
myTZ.setLocation("America/Phoenix");
myTZ.setDefault();
current_time = myTZ.now();
display.drawString(0, 20, String(current_time));
display.display();
secondPoll();
pinMode(36, INPUT);
attachInterrupt(digitalPinToInterrupt(36), stopAlarm, CHANGE);
}
void loop() {
events();
}
void activateAlarm() {
ledcWrite(channel, 125);
alarm_stop_flag = false;
while (not alarm_stop_flag) {
for(int i = 0; i < 2; i++){
ledcWriteTone(channel, 700);
delay(300);
ledcWrite(channel, 0);
delay(300);
}
}
ledcWrite(channel, 0);
}
void stopAlarm() {
alarm_stop_flag = true;
}
void checkAlarm(tmElements_t current_time) {
for(int i = 0; i < sizeof(alarm_times) / sizeof(alarm_times[0]); i++) {
if(current_time.Hour == alarm_times[i][0] and current_time.Minute == alarm_times[i][1]){
activateAlarm();
}
}
}
void secondPoll() {
int hour_12hr;
tmElements_t tm;
breakTime(myTZ.now(), tm);
setEvent(secondPoll, current_time + 1);
display.clear();
display.drawString(1, 1, myTZ.dateTime("m/d/y g:i:s A"));
display.display();
if (tm.Second == 0) {
checkAlarm(tm);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment