-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclock.ino
122 lines (92 loc) · 2.78 KB
/
clock.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <ESP8266WiFi.h>
#include <Time.h>
#include "secrets.h"
/* =====CONSTANTS===== */
// This only works if this is high on powerup
const int enablePin = 13;
// Got from I2C scanner, google it
const byte oledAddress = 0x3C;
// Replace with your network name&pass
const char* ssid = NETWORK_SSID;
const char* password = NETWORK_PASS;
// Timezone & Daylight Savings correction
const long utcOffsetInSeconds = 7200;
const int daylightOffsetInSeconds = 3600;
// period in ms to fetch the local time (not ntp!) and update the screen
const long pollingInterval = 100;
/* DON'T CHANGE BEYOND THIS POINT */
Adafruit_SSD1306 display(128,32);
// this is set at the end of setup(), to make sure it ran smoothly before looping
bool enabled = false;
// used for the non intrusive delay system
unsigned long previousMillis = 0;
time_t rawtime;
void setup() {
/* Verify enable switch */
pinMode(enablePin, INPUT);
if (!digitalRead(enablePin)) return;
/* Serial */
Serial.begin(115200);
while (!Serial);
Serial.println("Serial good");
/* I2C */
Serial.print("Setting up OLED Display...");
Wire.begin();
// Verify good connection to OLED
Wire.beginTransmission(oledAddress);
if (Wire.endTransmission() > 0) {
Serial.println("Failed");
Serial.println("Can't connect to OLED on channel 0x3C, Make sure SCL -> D1, SDA -> D2");
// for (;;) {}
return;
}
Serial.println("Done!");
/* Get current time from network */
// Connect to WIFI
Serial.print("Connecting to wifi");
WiFi.begin(ssid, password);
while ( WiFi.status() != WL_CONNECTED ) {
delay (500);
Serial.print ( "." );
}
Serial.println("Done!");
// Synchronize with NTP
configTime(utcOffsetInSeconds, daylightOffsetInSeconds, "pool.ntp.org");
Serial.println("Synchronized time with NTP server.");
/* Display setup */
Serial.print("Initializing display...");
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.setTextSize(3);
display.setTextColor(WHITE);
display.clearDisplay();
display.display();
delay(1);
Serial.println("Done!");
Serial.println("Setup complete.");
enabled = true;
}
void loop() {
// Make sure this doesn't run if setup fails
if (!enabled) return;
// Poll only every interval (100ms).
unsigned long currentMillis = millis();
if (currentMillis - previousMillis < pollingInterval) return;
// Fetch current time
time(&rawtime);
struct tm* timeinfo = localtime(&rawtime);
// Format time
char buf[16];
strftime(buf, 16, "%H:%M:%S", timeinfo);
// Display time
display.clearDisplay();
display.setCursor(0, 0);
display.write(buf);
display.display();
delay(1);
// Setup the next poll in pollingInterval ms
previousMillis = currentMillis;
}