|
|
@ -15,8 +15,9 @@ |
|
|
|
#include <Wire.h> |
|
|
|
#endif |
|
|
|
|
|
|
|
#include <WiFiUdp.h> |
|
|
|
#include <NTPClient.h> |
|
|
|
#include <time.h> |
|
|
|
#include <sys/time.h> |
|
|
|
#include "apps/sntp/sntp.h" |
|
|
|
|
|
|
|
#include "main.h" |
|
|
|
#include "hardware.h" |
|
|
@ -28,15 +29,13 @@ |
|
|
|
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ 16, /* clock=*/ 15, /* data=*/ 4); |
|
|
|
|
|
|
|
|
|
|
|
char timeStr[20]; |
|
|
|
char timeStr[64]; |
|
|
|
char weatherStr[32]; |
|
|
|
RTC_DATA_ATTR static int boot_count = 0; |
|
|
|
|
|
|
|
//Create a new Basecamp instance called iot
|
|
|
|
Basecamp iot; |
|
|
|
|
|
|
|
WiFiUDP ntpUDP; |
|
|
|
NTPClient timeClient(ntpUDP, "de.pool.ntp.org", 3600, 60000); |
|
|
|
BME280 bme280; |
|
|
|
MP3 mp3; |
|
|
|
Rotary rotary; |
|
|
@ -60,12 +59,15 @@ void setup() { |
|
|
|
|
|
|
|
boot_count++; |
|
|
|
|
|
|
|
/* time_t now;
|
|
|
|
time_t now; |
|
|
|
struct tm timeinfo; |
|
|
|
time(&now); |
|
|
|
setenv("TZ", "GMT+1", 1); |
|
|
|
setenv("TZ", "Europe/Berlin", 1); |
|
|
|
tzset(); |
|
|
|
localtime_r(&now, &timeinfo);*/ |
|
|
|
localtime_r(&now, &timeinfo); |
|
|
|
|
|
|
|
//Initialize Basecamp
|
|
|
|
iot.begin(); |
|
|
|
|
|
|
|
esp_sleep_wakeup_cause_t wakeup_reason = esp_sleep_get_wakeup_cause(); |
|
|
|
switch(wakeup_reason) { |
|
|
@ -89,8 +91,11 @@ void setup() { |
|
|
|
break; |
|
|
|
} |
|
|
|
|
|
|
|
//Initialize Basecamp
|
|
|
|
iot.begin(); |
|
|
|
char strftime_buf[64]; |
|
|
|
strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo); |
|
|
|
Serial.println(strftime_buf); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
u8g2.begin(); |
|
|
|
// delay(50);
|
|
|
@ -122,8 +127,12 @@ void setup() { |
|
|
|
char ipStr[16]; |
|
|
|
localIP.toString().toCharArray(ipStr, 16); |
|
|
|
|
|
|
|
timeClient.begin(); |
|
|
|
timeClient.update(); |
|
|
|
if (timeinfo.tm_year < (2016 - 1900)) { |
|
|
|
ESP_LOGI(TAG, "Time is not set yet. Connecting to WiFi and getting time over NTP."); |
|
|
|
obtain_time(); |
|
|
|
// update 'now' variable with current time
|
|
|
|
time(&now); |
|
|
|
} |
|
|
|
|
|
|
|
mp3.begin(); |
|
|
|
|
|
|
@ -187,6 +196,25 @@ void suspend() { |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void obtain_time(void) { |
|
|
|
sntp_setoperatingmode(SNTP_OPMODE_POLL); |
|
|
|
sntp_setservername(0, "de.pool.ntp.org"); |
|
|
|
sntp_init(); |
|
|
|
|
|
|
|
// wait for time to be set
|
|
|
|
time_t now = 0; |
|
|
|
struct tm timeinfo = { 0 }; |
|
|
|
int retry = 0; |
|
|
|
const int retry_count = 10; |
|
|
|
while(timeinfo.tm_year < (2016 - 1900) && ++retry < retry_count) { |
|
|
|
ESP_LOGI(TAG, "Waiting for system time to be set... (%d/%d)", retry, retry_count); |
|
|
|
vTaskDelay(2000 / portTICK_PERIOD_MS); |
|
|
|
time(&now); |
|
|
|
localtime_r(&now, &timeinfo); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void rotation(int i, int direction, int buttonPressed) { |
|
|
|
if(millis() - lastButtonPress >= 300) { |
|
|
|
lastButtonPress = millis(); |
|
|
@ -201,8 +229,16 @@ void loop() |
|
|
|
{ |
|
|
|
if(millis() - lastUpdate >= 1000) { |
|
|
|
lastUpdate = millis(); |
|
|
|
timeClient.update(); |
|
|
|
timeClient.getFormattedTime().toCharArray(timeStr, 50); |
|
|
|
|
|
|
|
time_t now; |
|
|
|
struct tm timeinfo; |
|
|
|
time(&now); |
|
|
|
setenv("TZ", "Europe/Berlin", 1); |
|
|
|
tzset(); |
|
|
|
localtime_r(&now, &timeinfo); |
|
|
|
|
|
|
|
strftime(timeStr, sizeof(timeStr), "%c", &timeinfo); |
|
|
|
Serial.println(timeStr); |
|
|
|
|
|
|
|
sprintf(weatherStr, "%.1f°C %.1f%% %.0fhPa", bme280.readTemperature(), bme280.readHumidity(), bme280.readPressure()); |
|
|
|
} |
|
|
|