/
NTP_clock_TCA9584A_4OLEDs_2a.ino
238 lines (202 loc) · 6.57 KB
/
NTP_clock_TCA9584A_4OLEDs_2a.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*********
Rui Santos
Complete project details at https://RandomNerdTutorials.com/tca9548a-i2c-multiplexer-esp32-esp8266-arduino/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
v.0 - original version from Rui's site
v.1 - original sketch by Nicu FLORICA (niq_ro)
- NTP clock inspired by https://nicuflorica.blogspot.com/2022/10/ceas-ntp-pe-afisaj-cu-tm1637.html
- hardware switch for DST (Daylight Saving Time) as at https://nicuflorica.blogspot.com/2022/12/ceas-ntp-pe-afisaj-oled-de-096-128x64.html
v.2 - added 7-segment numbers, based in ideea from https://ta-laboratories.com/blog/2018/09/07/recreating-a-7-segment-display-with-adafruit-gfx-ssd1306-oled/
v.2a - small correction in sketch (clean unusefull lines)
*********/
#include <Wire.h>
#include <Adafruit_GFX.h> // https://github.com/adafruit/Adafruit-GFX-Library
#include <Adafruit_SSD1306.h> // https://github.com/adafruit/Adafruit_SSD1306
#include <NTPClient.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
const char *ssid = "niq_ro";
const char *password = "berelaburtica";
const long timezoneOffset = 2*3600; // GMT + seconds (Romania)
#define DSTpin 14 // GPIO14 = D5, see https://randomnerdtutorials.com/esp8266-pinout-reference-gpios/
// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", timezoneOffset);
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
int ora, minut, secunda;
byte DST = 0;
byte DST0;
// define segment truth table for each digit
static const int digit_array[10][7] = {
{1, 1, 1, 1, 1, 1, 0}, // 0
{0, 1, 1, 0, 0, 0, 0}, // 1
{1, 1, 0, 1, 1, 0, 1}, // 2
{1, 1, 1, 1, 0, 0, 1}, // 3
{0, 1, 1, 0, 0, 1, 1}, // 4
{1, 0, 1, 1, 0, 1, 1}, // 5
{1, 0, 1, 1, 1, 1, 1}, // 6
{1, 1, 1, 0, 0, 0, 0}, // 7
{1, 1, 1, 1, 1, 1, 1}, // 8
{1, 1, 1, 1, 0, 1, 1} // 9
};
uint8_t n1 = 40;
uint8_t n3 = 8;
uint8_t n2 = 52;
uint8_t r = 2;
// Select I2C BUS
void TCA9548A(uint8_t bus){
Wire.beginTransmission(0x70); // TCA9548A address
Wire.write(1 << bus); // send byte to select bus
Wire.endTransmission();
Serial.print(bus);
}
void setup() {
pinMode (DSTpin, INPUT);
Serial.begin(115200);
Serial.println (" ");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay (500);
Serial.print (".");
}
Serial.println (" ");
if (digitalRead(DSTpin) == LOW)
DST = 0;
else
DST = 1;
timeClient.setTimeOffset(timezoneOffset + DST*3600);
timeClient.begin();
DST0 = DST;
// Start I2C communication with the Multiplexer
Wire.begin();
// Init OLED display on bus number 2
TCA9548A(2);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.setRotation(1); //
display.setTextColor(WHITE);
display.println("1");
// Clear the buffer
display.clearDisplay();
// Init OLED display on bus number 3
TCA9548A(3);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.setRotation(1); //
display.setTextColor(WHITE);
display.println("2");
// Clear the buffer
display.clearDisplay();
// Init OLED display on bus number 4
TCA9548A(4);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.setRotation(1); //
display.setTextColor(WHITE);
display.println("3");
// Clear the buffer
display.clearDisplay();
// Init OLED display on bus number 5
TCA9548A(5);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.setRotation(1); //
display.setTextColor(WHITE);
display.println("4");
// Clear the buffer
display.clearDisplay();
delay(2000);
}
void loop() {
if (digitalRead(DSTpin) == LOW)
DST = 0;
else
DST = 1;
timeClient.update();
ora = timeClient.getHours();
minut = timeClient.getMinutes();
/*
ora = ora%12; // 12-hour format
if (ora == 0) ora = 12; // 12-hour format
*/
Serial.print(ora);
Serial.print(";");
Serial.println(minut);
// Write to OLED on bus number 2
TCA9548A(2);
display.clearDisplay();
number(0, 0, ora/10, WHITE);
display.display();
// Write to OLED on bus number 3
TCA9548A(3);
display.clearDisplay();
number(0, 0, ora%10, WHITE);
if (secunda == 1)
display.fillCircle (SCREEN_HEIGHT-3-r/2,SCREEN_WIDTH-3-r/2,r+1,WHITE);
display.display();
// Write to OLED on bus number 4
TCA9548A(4);
display.clearDisplay();
number(0, 0, minut/10, WHITE);
display.display();
// Write to OLED on bus number 5
TCA9548A(5);
display.clearDisplay();
number(0, 0, minut%10, WHITE);
display.display();
if (DST != DST0)
{
timeClient.setTimeOffset(timezoneOffset + DST*3600);
timeClient.begin();
DST0 = DST;
}
secunda = (millis()/500)%2;
delay(500);
} // end main loop
void number(uint8_t pos_x, uint8_t pos_y,
uint8_t digit, uint8_t color) {
// loop through 7 segments
for (uint8_t i = 0; i < 7; i++) {
bool seg_on = digit_array[digit][i];
// if seg_on is true draw segment
if (seg_on) {
switch (i) {
case 0:
display.fillRoundRect(n3 + pos_x, 0 + pos_y, n1, n3, r, color); // SEG a // https://learn.adafruit.com/adafruit-gfx-graphics-library/graphics-primitives
break;
case 1:
display.fillRoundRect(n1+n3 + pos_x, n3 + pos_y, n3, n2, r, color); // SEG b
break;
case 2:
display.fillRoundRect(n1+n3 + pos_x, n2+2*n3 + pos_y, n3, n2, r, color); // SEG c
break;
case 3:
display.fillRoundRect(n3 + pos_x, 2*n2+2*n3+ pos_y, n1, n3, r, color); // SEG d
break;
case 4:
display.fillRoundRect(0 + pos_x, n2+2*n3 + pos_y, n3, n2, r, color); // SEG e
break;
case 5:
display.fillRoundRect(0 + pos_x, n3 + pos_y, n3, n2, r, color); // SEG f
break;
case 6:
display.fillRoundRect(n3 + pos_x, n2+n3 + pos_y, n1, n3, r, color); // SEG g
break;
}
seg_on = false;
}
}
}