From 3f4d22b4aceabc191e72d5deabe25f1823d9267f Mon Sep 17 00:00:00 2001 From: Hendrik Langer Date: Wed, 13 Jun 2018 01:39:43 +0200 Subject: [PATCH] run without wifi --- src/AlarmClock.cpp | 8 +++++++ src/AlarmClock.h | 2 ++ src/main.cpp | 58 ++++++++++++++++++++++++---------------------- src/main.h | 1 + 4 files changed, 41 insertions(+), 28 deletions(-) diff --git a/src/AlarmClock.cpp b/src/AlarmClock.cpp index 3bd78b5..67ed47d 100644 --- a/src/AlarmClock.cpp +++ b/src/AlarmClock.cpp @@ -91,6 +91,14 @@ bool AlarmClock::getRTCTime(struct tm *timeinfo) { } */ +bool AlarmClock::isNTPExpired(void) { + if (lastNTPRequest == 0 || millis() - lastNTPRequest >= 15*60*1000) { + lastNTPRequest = millis(); + return true; + } + return false; +} + bool AlarmClock::updateNTPTime(void) { sntp_setoperatingmode(SNTP_OPMODE_POLL); sntp_setservername(0, "de.pool.ntp.org"); diff --git a/src/AlarmClock.h b/src/AlarmClock.h index 9603a02..7995161 100644 --- a/src/AlarmClock.h +++ b/src/AlarmClock.h @@ -26,6 +26,7 @@ class AlarmClock { bool isTimeValid(void); bool isNight(void); bool getRTCTime(struct tm *timeinfo); + bool isNTPExpired(void); bool updateNTPTime(void); double getSecondsToAlarm(void); void printTimeUnix(whichTime); @@ -33,6 +34,7 @@ class AlarmClock { bool update(void); time_t now; struct tm timeinfo; + uint32_t lastNTPRequest = 0; }; #endif /* _ALARMCLOCK_H */ diff --git a/src/main.cpp b/src/main.cpp index 32cef1f..fa15621 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -165,10 +165,6 @@ void setup() { alarmclock.updateNTPTime(); } - Serial.print(" Alarm time: "); alarmclock.printTimeUnix(TIME_ALARM); - double seconds = alarmclock.getSecondsToAlarm(); - Serial.printf("alarm in %f seconds\n", seconds); - if (!alarmclock.isPending(secondsToSleep)) suspend(); else if (!alarmclock.isPending(5*60)) suspend(5*60); } @@ -189,24 +185,16 @@ void setup() { iot.mqtt.onMessage(onMqttMessage); iot.mqtt.setWill(sensorTopic.c_str(), 0, false, "OFFLINE"); - +/* while (iot.wifi.status() != WL_CONNECTED) { delay(500); Serial.print("."); screen->draw(); } - WiFi.onEvent(WiFiGotIP, WiFiEvent_t::SYSTEM_EVENT_STA_GOT_IP); - - IPAddress localIP = iot.wifi.getIP(); - char ipStr[16]; - localIP.toString().toCharArray(ipStr, 16); - - if (!alarmclock.isTimeValid()) { - ESP_LOGI(TAG, "Time is not set yet. Connecting to WiFi and getting time over NTP."); - alarmclock.updateNTPTime(); - } - alarmclock.getTime(state.timeStr, sizeof(state.timeStr), "%H:%M:%S", TIME_CURRENT); + onWiFiConnect(); +*/ + strcpy(state.timeStr, "--:--:--"); if (alarmclock.isNight()) { Serial.println("night mode"); @@ -223,12 +211,22 @@ void setup() { mp3.begin(); led.setup(); - udp.beginMulticast(udpMulticastAddress, udpMulticastPort); lastButtonPress = millis(); rotary.registerCallback(rotation); rotary.begin(rotaryPinA, rotaryPinB, rotaryPinButton); + lastActive = millis(); +} + + +void onWiFiConnect() { + if (!alarmclock.isTimeValid()) { + ESP_LOGI(TAG, "Time is not set yet. Connecting to WiFi and getting time over NTP."); + alarmclock.updateNTPTime(); + } + + esp_sleep_wakeup_cause_t wakeup_reason = esp_sleep_get_wakeup_cause(); if (wakeup_reason == ESP_SLEEP_WAKEUP_EXT1) { uint64_t wakeup_pinmask = esp_sleep_get_ext1_wakeup_status(); if (wakeup_pinmask & (1ULL << ext_wakeup_pin_1)) { @@ -237,14 +235,7 @@ void setup() { } } - lastActive = millis(); -} - -void WiFiGotIP(WiFiEvent_t event, WiFiEventInfo_t info) -{ - Serial.println("WiFi connected"); - Serial.println("IP address: "); - Serial.println(IPAddress(info.got_ip.ip_info.ip.addr)); + udp.beginMulticast(udpMulticastAddress, udpMulticastPort); } @@ -262,6 +253,10 @@ void onMqttConnect(bool sessionPresent) { void transmitStatus() { DEBUG_PRINTLN(__func__); + if (iot.wifi.status() != WL_CONNECTED) { + return; + } + float temp = bme280.readTemperature(); float humi = bme280.readHumidity(); float pres = bme280.readPressure(); @@ -376,6 +371,13 @@ void loop() if(millis() - lastUpdate >= 1000) { lastUpdate = millis(); + if (iot.wifi.status() == WL_CONNECTED) { + if (alarmclock.isNTPExpired()) { // will return true first time called + Serial.println("Updating NTP Time"); + alarmclock.updateNTPTime(); + } + } + double seconds = alarmclock.getSecondsToAlarm(); bool stayAwake = false; @@ -413,7 +415,7 @@ void loop() int voltage = rom_phy_get_vdd33(); Serial.printf("voltage: %d\n", voltage); - alarmclock.getTime(state.timeStr, sizeof(state.timeStr), "%H:%M:%S", TIME_CURRENT); + if (alarmclock.isTimeValid()) alarmclock.getTime(state.timeStr, sizeof(state.timeStr), "%H:%M:%S", TIME_CURRENT); sprintf(state.string1, "%.1f°C %.1f%% %.0fhPa", bme280.readTemperature(), bme280.readHumidity(), bme280.readPressure()); sprintf(state.string2, "%.1f %dmV %ddBm %ukB %s", temperatureRead(), voltage, WiFi.RSSI(), ESP.getFreeHeap()/1024, alarmclock.isAlarmArmed()?"A":"_"); @@ -449,14 +451,14 @@ void loop() lastTransmit = millis(); transmitStatus(); } - +/* int lenp = udp.parsePacket(); //Serial.println(lenp); while(udp.available()) { int len = udp.read(udprecvbuf, sizeof(udprecvbuf)); Serial.printf("udp received %d bytes\n", len); } - +*/ if (menuChange != eNone) { delete screen; if (menuChange == eMainScreen) screen = new MainScreen(); diff --git a/src/main.h b/src/main.h index 3e50f2d..9170511 100644 --- a/src/main.h +++ b/src/main.h @@ -4,6 +4,7 @@ void resetToFactoryDefaults(); void setup(); void onMqttConnect(bool sessionPresent); +void onWiFiConnect(); void transmitStatus(); void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total); void suspend(uint32_t secondsToSleep);