diff options
Diffstat (limited to 'src/main.cpp')
-rw-r--r-- | src/main.cpp | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..3683a20 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,90 @@ +#include <Arduino.h> + +#include <Wire.h> +#include <Timezone.h> // https://github.com/JChristensen/Timezone +#include <TimeLib.h> // https://github.com/PaulStoffregen/Time +#include <DS1307RTC.h> // https://github.com/PaulStoffregen/DS1307RTC +#include <NTPClient.h> // https://github.com/arduino-libraries/NTPClient +#include <TM1637Display.h> // https://github.com/avishorp/TM1637 +#include <WiFiManager.h>//https://github.com/tzapu/WiFiManager + +#include <ESP8266WiFi.h> +#include <ESP8266WiFiMulti.h> +#include <WiFiUdp.h> +#include <ArduinoOTA.h> + +// Central European Time (Frankfurt, Paris) +TimeChangeRule CEST = {"CEST", Last, Sun, Mar, 2, 120}; // Central European Summer Time +TimeChangeRule CET = {"CET ", Last, Sun, Oct, 3, 60}; // Central European Standard Time +Timezone CE(CEST, CET); + +// NTP +WiFiUDP ntpUDP; +NTPClient timeClient(ntpUDP, "europe.pool.ntp.org", 0, 600000); + +// timelib +time_t utc; +time_t local; + +// display +//TM1637Display display(1, 3); +TM1637Display display(D8, D7); // pinClk, pinDIO + +uint8_t data[] = { 0xff, 0xff, 0xff, 0xff }; +uint8_t blank[] = { 0x00, 0x00, 0x00, 0x00 }; + +char buf[4]; +char prev[4]; + +void setup() { + WiFi.mode(WIFI_STA); + + WiFiManager wifiManager; + wifiManager.setTimeout(600); + + if(!wifiManager.autoConnect("clock")) { + ESP.reset(); + } + + ArduinoOTA.begin(); + ESP.wdtEnable(WDTO_8S); + + // put your setup code here, to run once: + //Wire.begin(0,2); // 00-sda,02-sdl + Wire.begin(D2,D1); + timeClient.begin(); + setSyncProvider(RTC.get); + display.clear(); +} + +void loop() { + ESP.wdtFeed(); + ArduinoOTA.handle(); + + yield(); + + // put your main code here, to run repeatedly: + if(timeClient.update()) { + RTC.set(timeClient.getEpochTime()); + } + + yield(); + + utc = now(); + local = CE.toLocal(utc); + + yield(); + + sprintf(buf, "%.2d%.2d",hour(local), minute(local)); + data[0] = display.encodeDigit(buf[0] - '0'); + data[1] = display.encodeDigit(buf[1] - '0'); + data[2] = display.encodeDigit(buf[2] - '0'); + data[3] = display.encodeDigit(buf[3] - '0'); + + if(strcmp(buf, prev) != 0) { + display.setSegments(data); + strcpy(prev , buf); + } + + yield(); +} |