Skip to content

Commit f41f477

Browse files
committed
Initial NTP clock commit
1 parent ed59c78 commit f41f477

File tree

7 files changed

+831
-0
lines changed

7 files changed

+831
-0
lines changed

esp8266-ledclock/README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# A(nother) NTP based clock for ESP8266 #
2+
3+
This is an LED clock driven by the amazing little ESP8266
4+
device. There are a few people who have made NTP clients
5+
for this device, but this one is thought out properly, and
6+
much easier to setup and use:
7+
8+
* Uses the cheapest ESP8266 module (the ESP-1).
9+
* Drives a 4-digit LED display via SPI.
10+
* Single button to begin configuration.
11+
* Browser based config entry.
12+
13+
The circuit is extremely basic, with the following parts:
14+
15+
* ESP-1 module.
16+
* 7-segment LED display (SPI interface)
17+
* 3.3v regulator (e.g LM1117)
18+
* Bypass cap
19+
* Button + pullup resistor.
20+
21+
The particular LED module I'm using is a Sure Electronics
22+
display. It runs off 5v, however the driver chips are 3.3v
23+
input compatible, obviating the need for a level converter.
24+
25+
## Operation ##
26+
27+
The clock has a simple interface, and does not require any software
28+
changes to set it up for your network.
29+
30+
Setup:
31+
32+
* Power on.
33+
* Within 5s press the button.
34+
* Display shows 'AP'.
35+
* Press the button to display the device IP address.
36+
* Connect to the 'ESP-CLOCK' SSID.
37+
* Point a browser to the device IP.
38+
* Configure Wifi credentials and time/ntp attributes.
39+
* After submitting, the clock connects to the given Wifi network and starts.
40+
41+
During normal operation:
42+
43+
* Press the button to display the device IP address.
44+
* Browse to that address to view the status and change config.
45+
* The lower right decimal point will be lit if NTP synchronisation
46+
is overdue (e.g couldnt connect to server).
47+
48+
## Copyright ##
49+
50+
The design and code is Copyright 2015 Ben Buxton. ([email protected]).
51+
52+
Licenced under GPLv3.
53+

esp8266-ledclock/display.ino

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
2+
// ======== Display constants.
3+
#define _A_ 0x80
4+
#define _B_ 0x40
5+
#define _C_ 0x20
6+
#define _D_ 0x10
7+
#define _E_ 0x08
8+
#define _F_ 0x04
9+
#define _G_ 0x02
10+
#define _DP_ 0x01
11+
12+
13+
#define DATA 0
14+
#define CLOCK 1
15+
#define BLANK 2
16+
17+
const char segments[] = {
18+
_A_|_B_|_C_|_D_|_E_|_F_, // 0
19+
_B_|_C_, // 1
20+
_A_|_B_|_G_|_D_|_E_, // 2
21+
_A_|_B_|_C_|_D_|_G_, // 3
22+
_B_|_C_|_F_|_G_, // 4
23+
_A_|_C_|_D_|_F_|_G_, // 5
24+
_A_|_C_|_D_|_E_|_F_|_G_, // 6
25+
_A_|_B_|_C_, // 7
26+
_A_|_B_|_C_|_D_|_E_|_F_|_G_, // 8
27+
_A_|_B_|_C_|_D_|_F_|_G_, // 9
28+
29+
_A_|_B_|_C_|_E_|_F_|_G_, // A
30+
_C_|_D_|_E_|_F_|_G_, // b
31+
_D_|_E_|_G_, // c
32+
_B_|_C_|_D_|_E_|_G_, // d
33+
_A_|_D_|_E_|_F_|_G_, //e
34+
_A_|_E_|_F_|_G_, // f
35+
36+
_A_|_B_|_E_|_F_|_G_, // P [0x10]
37+
38+
_G_, // - [0x11]
39+
40+
_A_, _B_, _C_, _D_, _E_, _F_, // 0x12 - 0x17
41+
42+
0, // 0x18
43+
};
44+
45+
char digits[4];
46+
char decimals;
47+
48+
#define PULSE digitalWrite(CLOCK, LOW); delayMicroseconds(10) ; digitalWrite(CLOCK, HIGH); delayMicroseconds(10) ;
49+
50+
void clear() {
51+
char i;
52+
digitalWrite(DATA, LOW);
53+
for (i = 0 ; i < 32 ; i++) {
54+
PULSE;
55+
}
56+
}
57+
58+
void display() {
59+
char i, d, digit;
60+
61+
digitalWrite(BLANK, LOW);
62+
63+
clear();
64+
for (d = 0 ; d < 4 ; d++) {
65+
digit = segments[digits[d]];
66+
67+
if ((decimals >> d & 0x1) == 0x1) digit |= _DP_;
68+
69+
for (i = 0 ; i < 8 ; i++) {
70+
digitalWrite(DATA, digit & 0x1 ? HIGH : LOW);
71+
digit >>= 1;
72+
PULSE;
73+
}
74+
}
75+
digitalWrite(BLANK, HIGH);
76+
}
77+
78+
void displayAP() {
79+
digits[0] = 0x10;
80+
digits[1] = 0xA;
81+
digits[2] = 0x18;
82+
digits[3] = 0x18;
83+
display();
84+
}
85+
86+
void displayDash() {
87+
digits[0] = digits[1] = digits[2] = digits[3] = 0x11;
88+
display();
89+
}
90+
91+
void clearDigits() {
92+
digits[0] = digits[1] = digits[2] = digits[3] = 0x18;
93+
}
94+
95+
// Twirler handler.
96+
Ticker ticker;
97+
98+
volatile char busySegment = 0x12;
99+
volatile char busyDigit;
100+
101+
void displayBusy(char digit) {
102+
busyDigit = digit;
103+
ticker.attach(0.1, _displayBusy);
104+
}
105+
106+
void stopDisplayBusy() {
107+
ticker.detach();
108+
}
109+
110+
void _displayBusy() {
111+
if (busySegment > 0x17) {
112+
busySegment = 0x12;
113+
}
114+
clearDigits();
115+
digits[busyDigit] = busySegment++;
116+
display();
117+
}
118+
119+
// End twirler handler.
120+
121+
// IP Display handler.
122+
volatile signed char dispOctet = -1;
123+
124+
char displayIP() {
125+
if (dispOctet > -1) {
126+
return 1;
127+
}
128+
if (digitalRead(SETUP_PIN) == 1) return 0;
129+
dispOctet = 0;
130+
ticker.attach(1.0, _displayIP);
131+
return 0;
132+
}
133+
134+
void _displayIP() {
135+
if (dispOctet > 3) {
136+
ticker.detach();
137+
dispOctet = -1;
138+
clockMode == MODE_CLOCK ? displayClock() : displayAP();
139+
return;
140+
}
141+
clearDigits();
142+
uint8_t octet = (uint32_t(clockMode == MODE_CLOCK ? WiFi.localIP() : WiFi.softAPIP()) >> (8 * dispOctet++)) & 0xff;
143+
uint8_t d = 0;
144+
for (; octet > 99 ; octet -= 100) d++;
145+
digits[2] = d;
146+
d = 0;
147+
for (; octet > 9 ; octet -= 10) d++;
148+
digits[1] = d;
149+
digits[0] = octet;
150+
decimals = 0x1;
151+
display();
152+
}
153+
154+
// end Ip display handler.
155+
156+
void displayClock() {
157+
int h = hour();
158+
int m = minute();
159+
digits[0] = digits[1] = digits[2] = digits[3] = decimals = 0;
160+
161+
if (h > 19) digits[3] = 2;
162+
else if (h > 9) digits[3] = 1;
163+
digits[2] = h % 10;
164+
165+
if (m > 49) digits[1] = 5;
166+
else if (m > 39) digits[1] = 4;
167+
else if (m > 29) digits[1] = 3;
168+
else if (m > 19) digits[1] = 2;
169+
else if (m > 9) digits[1] = 1;
170+
171+
digits[0] = m % 10;
172+
173+
if (second() & 0x1) decimals = 0x4;
174+
if (timeStatus() != timeSet) decimals |= 0x1;
175+
display();
176+
}
177+
178+
void setupDisplay() {
179+
pinMode(DATA, OUTPUT);
180+
pinMode(CLOCK, OUTPUT);
181+
pinMode(BLANK, OUTPUT);
182+
displayDash();
183+
}
184+

0 commit comments

Comments
 (0)