Skip to content

Commit 8ef4b32

Browse files
Update Horloge.ino
1 parent 7a311c2 commit 8ef4b32

File tree

1 file changed

+9
-283
lines changed

1 file changed

+9
-283
lines changed

Horloge/Horloge.ino

Lines changed: 9 additions & 283 deletions
Original file line numberDiff line numberDiff line change
@@ -19,295 +19,21 @@
1919
* Contact: marc@sibert.fr
2020
*/
2121

22-
///> @see https://github.com/arduino-libraries/NTPClient
23-
#include <NTPClient.h>
24-
#include <WiFiUdp.h>
22+
// #define DEBUG 1
2523

26-
///> @see https://arduino-esp8266.readthedocs.io/en/latest/index.html
27-
#include <ESP8266WiFi.h>
24+
#include "horloge.h"
2825

29-
///> @see https://github.com/adafruit/Adafruit_LED_Backpack
30-
#include <Adafruit_LEDBackpack.h>
26+
#ifdef DEBUG
27+
#include <GDBStub.h>
28+
#endif
3129

32-
#include <LittleFS.h>
33-
34-
#include "web.h"
35-
36-
template<
37-
const long BAUDRATE = 115200,
38-
const unsigned I2C_ADDR = 0x70
39-
> class App {
40-
public:
41-
App() :
42-
alpha4(Adafruit_AlphaNum4()),
43-
url(F("pool.ntp.org")),
44-
ntpUDP(),
45-
timeClient(ntpUDP, url.c_str(), utcOffsetInSeconds)
46-
{
47-
WiFi.setAutoReconnect(true);
48-
49-
uint8_t mac[6];
50-
WiFi.macAddress(mac);
51-
char host[20];
52-
snprintf_P(host, sizeof(host), PSTR("horloge-%02x%02x%02x"), mac[3], mac[4], mac[5]);
53-
hostname = host;
54-
}
55-
56-
inline
57-
void setup() {
58-
Serial.begin(BAUDRATE);
59-
alpha4.begin(I2C_ADDR);
60-
Serial.print(F("LittleFS starts: "));
61-
Serial.println(LittleFS.begin() ? "OK." : "Failed!");
62-
63-
alpha4.clear();
64-
alpha4.setBrightness(0);
65-
66-
alpha4.writeDigitRaw(0, 0x0008);
67-
alpha4.writeDigitRaw(3, 0x0008);
68-
alpha4.writeDisplay();
69-
for (byte i = 0; i < 4; ++i) {
70-
alpha4.setBrightness(i);
71-
delay(200);
72-
}
73-
alpha4.writeDigitRaw(0, 0x00C0);
74-
alpha4.writeDigitRaw(3, 0x00C0);
75-
alpha4.writeDisplay();
76-
for (byte i = 4; i < 8; ++i) {
77-
alpha4.setBrightness(i);
78-
delay(200);
79-
}
80-
alpha4.writeDigitRaw(0, 0x00E2);
81-
alpha4.writeDigitRaw(3, 0x00E2);
82-
alpha4.writeDisplay();
83-
for (byte i = 8; i < 12; ++i) {
84-
alpha4.setBrightness(i);
85-
delay(200);
86-
}
87-
alpha4.writeDigitRaw(0, 0x00E3);
88-
alpha4.writeDigitRaw(3, 0x00E3);
89-
alpha4.writeDisplay();
90-
for (byte i = 12; i <= 15; ++i) {
91-
alpha4.setBrightness(i);
92-
delay(200);
93-
}
94-
alpha4.writeDigitRaw(1, 0x0008);
95-
alpha4.writeDigitRaw(2, 0x0008);
96-
alpha4.writeDisplay();
97-
delay(200);
98-
99-
alpha4.writeDigitRaw(3, 0x07E3);
100-
alpha4.writeDisplay();
101-
delay(200);
102-
103-
alpha4.writeDigitRaw(3, 0x00E3);
104-
alpha4.writeDisplay();
105-
delay(1000);
106-
107-
// display(F("HORLOGE 1,0 (c) Marc SIBERT"));
108-
display(F("HORLOGE 1,0"));
109-
delay(500);
110-
// display(String(F("COMPILE ")) + __DATE__ + F(" - ") + __TIME__);
111-
// delay(1000);
112-
113-
114-
setupWiFi();
115-
116-
web.setup();
117-
118-
}
119-
120-
/**
121-
* Méthode appelée perpétuellement.
122-
*/
123-
inline
124-
void loop() {
125-
static auto oldSec = 100;
126-
127-
if (!WiFi.isConnected()) setupWiFi();
128-
web.handleClient();
129-
130-
timeClient.update();
131-
const auto sec = timeClient.getSeconds();
132-
if (sec != oldSec) {
133-
oldSec = sec;
134-
const auto hh = timeClient.getHours();
135-
alpha4.writeDigitAscii(0, hh < 10 ? ' ' : ('0' + hh / 10));
136-
alpha4.writeDigitAscii(1, '0' + hh % 10, (sec & 1));
137-
const auto mn = timeClient.getMinutes();
138-
alpha4.writeDigitAscii(2, '0' + mn / 10);
139-
alpha4.writeDigitAscii(3, '0' + mn % 10);
140-
const auto sensorValue = analogRead(A0);
141-
alpha4.setBrightness( constrain(_BV( map(sensorValue, 500, 1023, 0, 3) ), 1, 15) );
142-
143-
alpha4.writeDisplay();
144-
}
145-
146-
}
147-
148-
protected:
149-
/**
150-
* Affiche les chaînes de caractères sur l'écran.
151-
* @param str La chaîne de caractère.
152-
*/
153-
void display(const String& str) {
154-
const byte digits = 4;
155-
if (str.length() <= digits) {
156-
for (byte i = 0; i < digits; ++i) {
157-
alpha4.writeDigitAscii(i, i < str.length() ? str[i] : ' ');
158-
}
159-
} else {
160-
for (unsigned i = 0; i < str.length() - (digits - 1); ++i) {
161-
display(str.substring(i, i + digits));
162-
}
163-
}
164-
alpha4.writeDisplay();
165-
delay(200);
166-
}
167-
168-
String statusMessage(const int status) {
169-
switch (status) {
170-
case WL_IDLE_STATUS :
171-
return F("Status change...");
172-
break;
173-
case WL_NO_SSID_AVAIL :
174-
return F("SSID not reachable!");
175-
break;
176-
case WL_CONNECTED :
177-
return F("Connected.");
178-
break;
179-
case WL_CONNECT_FAILED :
180-
return F("Connection failed!");
181-
break;
182-
case WL_CONNECTION_LOST :
183-
return F("Connection lost...");
184-
break;
185-
case WL_DISCONNECTED :
186-
return F("Disconnected...");
187-
break;
188-
default :
189-
Serial.print("WL_IDLE_STATUS ="); Serial.println(WL_IDLE_STATUS);
190-
Serial.print("WL_NO_SSID_AVAIL ="); Serial.println(WL_NO_SSID_AVAIL);
191-
Serial.print("WL_SCAN_COMPLETED ="); Serial.println(WL_SCAN_COMPLETED);
192-
Serial.print("WL_CONNECTED ="); Serial.println(WL_CONNECTED);
193-
Serial.print("WL_CONNECT_FAILED ="); Serial.println(WL_CONNECT_FAILED);
194-
Serial.print("WL_CONNECTION_LOST ="); Serial.println(WL_CONNECTION_LOST);
195-
Serial.print("WL_DISCONNECTED ="); Serial.println(WL_DISCONNECTED);
196-
197-
Serial.print("Erreur wl_status_t : ");
198-
Serial.println(status);
199-
200-
return String(F("Other error #")) + String(status);
201-
break;
202-
}
203-
}
204-
205-
boolean waitForConnexion(const unsigned timeout = 30) {
206-
auto s = WiFi.status();
207-
for(const auto t = millis(); millis() < t + (timeout * 1000UL); s = WiFi.status()) {
208-
display(statusMessage(s));
209-
yield();
210-
if (s == WL_CONNECTED) return true;
211-
}
212-
return false;
213-
}
214-
215-
/**
216-
* Setup WiFi connection or
217-
*/
218-
void setupWiFi() {
219-
WiFi.mode(WIFI_STA);
220-
221-
WiFi.hostname(hostname);
222-
WiFi.begin();
223-
224-
display(String(F("WIFI SSID ")) + WiFi.SSID());
225-
if (waitForConnexion()) {
226-
display(String(F("IP address ")) + WiFi.localIP().toString());
227-
delay(200);
228-
return;
229-
}
230-
display(F("No WiFi found!"));
231-
delay(200);
232-
233-
display(F("Start WPS "));
234-
delay(200);
235-
if (WiFi.beginWPSConfig()) {
236-
if (waitForConnexion()) {
237-
display(F("WPS succeded - Reboot"));
238-
delay(200);
239-
ESP.restart();
240-
} else {
241-
display(F("WPS timed out!"));
242-
delay(200);
243-
}
244-
} else {
245-
display(F("WPS failed!"));
246-
delay(200);
247-
}
248-
249-
display(F("Start SmartConfig"));
250-
delay(200);
251-
if (WiFi.beginSmartConfig()) {
252-
if (waitForConnexion()) {
253-
display(F("SmartConfig succeded - Reboot"));
254-
delay(200);
255-
ESP.restart();
256-
} else {
257-
display(F("Connection timed out!"));
258-
delay(200);
259-
}
260-
} else {
261-
display(F("SmartConfig failed!"));
262-
delay(200);
263-
}
264-
265-
}
266-
267-
/**
268-
* @see http://howardhinnant.github.io/date_algorithms.html
269-
*/
270-
void displayDate(const unsigned long& day) {
271-
const unsigned long z = day + 719468;
272-
const unsigned long era = z / 146097;
273-
const unsigned doe = static_cast<unsigned>(z - era * 146097); // [0, 146096]
274-
const unsigned yoe = (doe - doe/1460 + doe/36524 - doe/146096) / 365; // [0, 399]
275-
const unsigned doy = doe - (365*yoe + yoe/4 - yoe/100); // [0, 365]
276-
const byte mp = (5*doy + 2)/153; // [0, 11]
277-
const byte d = doy - (153*mp+2)/5 + 1; // [1, 31]
278-
const byte m = mp + (mp < 10 ? 3 : -9); // [1, 12]
279-
const unsigned y = static_cast<unsigned>(yoe) + era * 400 + (m <= 2);
280-
281-
static const String jours[] = {"Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi"};
282-
static const String mois[] = {"Janvier", "Fevrier", "Mars", "Avril", "Mai", "Juin", "Juillet", "Aout", "Septembre", "Octobre", "Novembre", "Decembre"};
283-
284-
display(String(jours[(day+4) % 7]) + " " + String(d) + " " + mois[mp] + " " + String(y));
285-
delay(1000);
286-
}
287-
288-
private:
289-
290-
291-
///> Accès à l'afficheur.
292-
Adafruit_AlphaNum4 alpha4;
293-
294-
///> Outils WiFi & ntp.
295-
String url;
296-
const long utcOffsetInSeconds = 3600;
297-
WiFiUDP ntpUDP;
298-
NTPClient timeClient;
299-
300-
String hostname;
301-
302-
Web web;
303-
};
304-
305-
306-
// constexpr char url[] = "europe.pool.ntp.org";
307-
App<> app;
30+
Horloge app;
30831

30932
void setup() {
31033
app.setup();
34+
#ifdef DEBUG
35+
gdbstub_init();
36+
#endif
31137
}
31238

31339
void loop() {

0 commit comments

Comments
 (0)