summaryrefslogtreecommitdiff
path: root/src/main.cpp
blob: e20c0ed589c8e203ec51e37d35fc95d1e8b56d0d (plain)
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
#include <Arduino.h>

#include <Wire.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>

#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>//https://github.com/tzapu/WiFiManager

#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

// 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, 3600000);

// timelib
time_t utc;
time_t local;

// display
//TM1637Display display(1, 3);
TM1637Display display(D6, D5); // pinClk, pinDIO

uint8_t data[] = { 0xff, 0xff, 0xff, 0xff };
uint8_t blank[] = { 0x00, 0x00, 0x00, 0x00 };

char buf[5];
char prev[5] = { '0','0','0','0' };

void setup() {
  WiFiManager wifiManager;
  wifiManager.setTimeout(180);

  if(!wifiManager.autoConnect("clock")) {
    ESP.reset();
    delay(5000);
  }

  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); // link to hardware clock
  display.clear();
  display.setBrightness(0x0a);
  display.setSegments(data);
}

void loop() {
  ESP.wdtFeed();
  ArduinoOTA.handle();

  yield();
  delay(100);

  // 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));

  if(second(local) % 2 == 0) {
    display.showNumberDecEx(hour(local), 0b10000000, true, 2, 0);
  } else {
    display.showNumberDecEx(hour(local), 0b11100000, true, 2, 0);
  }

  if(strcmp(buf, prev) != 0) {
    display.showNumberDecEx(hour(local), 0b11100000, true, 2, 0);
    display.showNumberDecEx(minute(local), 0b11100000, true, 2, 2);
    strcpy(prev , buf);
  }

  yield();
}