diff --git a/src/hardware.h b/src/hardware.h index 26cfb60..7b54e47 100644 --- a/src/hardware.h +++ b/src/hardware.h @@ -8,7 +8,7 @@ static constexpr uint8_t buttonPin = 0; static constexpr uint8_t sensorPin = 0; static constexpr uint8_t rotaryPinA = 39; static constexpr uint8_t rotaryPinB = 38; -static constexpr uint8_t rotaryPinButton = 22; +static constexpr uint8_t rotaryPinButton = 17; static constexpr uint8_t ext_wakeup_pin_1 = 0; static constexpr uint8_t ext_wakeup_pin_2 = 0; @@ -20,11 +20,11 @@ static constexpr uint32_t secondsToSleep = 3600; * 5V O O 5V * 3V3 O O 3V3 * GND O < 36 only input, no pullups - * RX * (3) ? * 17 + * RX * (3) < 37 only input, no pullups * TX * (1) < 38 ROTARY_A only input, no pullups * RST * BUTTON < 39 ROTARY_B only input, no pullups * 0 * KEY_BUILTIN < 34 only input, no pullups - * ROTARY_BTN 22 * < 35 only input, no pullups + * 22 * ? < 35 only input, no pullups * BME280_SDO 19 * LoRa_MISO ? * 32 * BME280_CS 23 * ? * 33 * 18 x LoRa_CS * 25 MAX98_DIN @@ -32,13 +32,13 @@ static constexpr uint32_t secondsToSleep = 3600; * 15 * OLED_SCL LoRa_MOSI * 27 BME280_SDA/SDI * 2 * LED_B LoRa_RST * 14 * 4 * OLED_SDA * 12 MAX98_BCLK - * 17 * * 13 MAX98_LRC - * 16 * OLED_RST * 21 + * ROTARY_BTN 17 * * 13 MAX98_LRC + * 16 * OLED_RST * 21 WS2812B_DATA(?) */ /* Rotary Encoder * 1 LED - * pinA A 2 LED + * pinA A 2 LED pinA und PinB mit externen Pullups * GND C 3 BTN -| * pinB B 4 LED / * 5 Vin -| diff --git a/src/main.cpp b/src/main.cpp index 1be0329..b29caaf 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,8 +15,9 @@ #include #endif -#include -#include +#include +#include +#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()); } diff --git a/src/main.h b/src/main.h index fb251d8..0e7feb7 100644 --- a/src/main.h +++ b/src/main.h @@ -8,3 +8,4 @@ void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties void suspend(); void loop(); void rotation(int i, int direction, int buttonPressed); +void obtain_time(void);