Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
AlarmClock code with EZTime library
#include <ESP8266WiFi.h>
#include <TM1637Display.h>
#include <ezTime.h>
// Both can be installed from library manager
char ssid[] = "SSID"; // your network SSID (name)
char password[] = "password"; // your network password
// Module connection pins (Digital Pins)
#define CLK D6
#define DIO D5
TM1637Display display(CLK, DIO);
boolean dotsOn;
unsigned long oneSecondLoopDue = 0;
Timezone myTZ;
const uint8_t LETTER_A = SEG_A | SEG_B | SEG_C | SEG_E | SEG_F | SEG_G;
const uint8_t LETTER_B = SEG_C | SEG_D | SEG_E | SEG_F | SEG_G;
const uint8_t LETTER_C = SEG_A | SEG_D | SEG_E | SEG_F;
const uint8_t LETTER_D = SEG_B | SEG_C | SEG_D | SEG_E | SEG_G;
const uint8_t LETTER_E = SEG_A | SEG_D | SEG_E | SEG_F | SEG_G;
const uint8_t LETTER_F = SEG_A | SEG_E | SEG_F | SEG_G;
const uint8_t LETTER_O = SEG_C | SEG_D | SEG_E | SEG_G;
const uint8_t SEG_BOOT[] = {
LETTER_B, // b
LETTER_O, // o
LETTER_O, // o
SEG_D | SEG_E | SEG_F | SEG_G // t - kinda
};
void setup() {
Serial.begin(115200);
display.setBrightness(0xff);
display.setSegments(SEG_BOOT);
// Set WiFi to station mode and disconnect from an AP if it was Previously
// connected
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
// Attempt to connect to Wifi network:
Serial.print("Connecting Wifi: ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi connected");
// Uncomment the line below to see what it does behind the scenes
// setDebug(INFO);
waitForSync();
Serial.println();
Serial.println("UTC: " + UTC.dateTime());
// Provide official timezone names
// https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
//myTZ.setLocation(F("Pacific/Auckland"));
//Serial.print(F("New Zealand: "));
//Serial.println(myTZ.dateTime());
// Wait a little bit to not trigger DDoS protection on server
// See https://github.com/ropg/ezTime#timezonedropnl
//delay(5000);
// Or country codes for countries that do not span multiple timezones
myTZ.setLocation(F("ie"));
Serial.print(F("Time in Ireland: "));
Serial.println(myTZ.dateTime());
}
void displayTime(bool dotsVisible) {
String timeHour = myTZ.dateTime("H");
String timeMinutes = myTZ.dateTime("i");
uint8_t data[4];
data[0] = display.encodeDigit(timeHour.substring(0,1).toInt());
data[1] = display.encodeDigit(timeHour.substring(1).toInt());
if (dotsVisible) {
// Turn on double dots
data[1] = data[1] | B10000000;
}
data[2] = display.encodeDigit(timeMinutes.substring(0,1).toInt());
data[3] = display.encodeDigit(timeMinutes.substring(1).toInt());
display.setSegments(data);
}
void loop() {
events();
unsigned long now = millis();
if (now > oneSecondLoopDue) {
displayTime(dotsOn);
dotsOn = !dotsOn;
// checkForAlarm();
// if (buttonPressed) {
// alarmHandled = true;
// buttonPressed = false;
// }
oneSecondLoopDue = now + 1000;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment