Browse Source

run without wifi

main
Hendrik Langer 7 years ago
parent
commit
3f4d22b4ac
  1. 8
      src/AlarmClock.cpp
  2. 2
      src/AlarmClock.h
  3. 58
      src/main.cpp
  4. 1
      src/main.h

8
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) { bool AlarmClock::updateNTPTime(void) {
sntp_setoperatingmode(SNTP_OPMODE_POLL); sntp_setoperatingmode(SNTP_OPMODE_POLL);
sntp_setservername(0, "de.pool.ntp.org"); sntp_setservername(0, "de.pool.ntp.org");

2
src/AlarmClock.h

@ -26,6 +26,7 @@ class AlarmClock {
bool isTimeValid(void); bool isTimeValid(void);
bool isNight(void); bool isNight(void);
bool getRTCTime(struct tm *timeinfo); bool getRTCTime(struct tm *timeinfo);
bool isNTPExpired(void);
bool updateNTPTime(void); bool updateNTPTime(void);
double getSecondsToAlarm(void); double getSecondsToAlarm(void);
void printTimeUnix(whichTime); void printTimeUnix(whichTime);
@ -33,6 +34,7 @@ class AlarmClock {
bool update(void); bool update(void);
time_t now; time_t now;
struct tm timeinfo; struct tm timeinfo;
uint32_t lastNTPRequest = 0;
}; };
#endif /* _ALARMCLOCK_H */ #endif /* _ALARMCLOCK_H */

58
src/main.cpp

@ -165,10 +165,6 @@ void setup() {
alarmclock.updateNTPTime(); 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(); if (!alarmclock.isPending(secondsToSleep)) suspend();
else if (!alarmclock.isPending(5*60)) suspend(5*60); else if (!alarmclock.isPending(5*60)) suspend(5*60);
} }
@ -189,24 +185,16 @@ void setup() {
iot.mqtt.onMessage(onMqttMessage); iot.mqtt.onMessage(onMqttMessage);
iot.mqtt.setWill(sensorTopic.c_str(), 0, false, "OFFLINE"); iot.mqtt.setWill(sensorTopic.c_str(), 0, false, "OFFLINE");
/*
while (iot.wifi.status() != WL_CONNECTED) { while (iot.wifi.status() != WL_CONNECTED) {
delay(500); delay(500);
Serial.print("."); Serial.print(".");
screen->draw(); screen->draw();
} }
WiFi.onEvent(WiFiGotIP, WiFiEvent_t::SYSTEM_EVENT_STA_GOT_IP); onWiFiConnect();
*/
IPAddress localIP = iot.wifi.getIP(); strcpy(state.timeStr, "--:--:--");
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);
if (alarmclock.isNight()) { if (alarmclock.isNight()) {
Serial.println("night mode"); Serial.println("night mode");
@ -223,12 +211,22 @@ void setup() {
mp3.begin(); mp3.begin();
led.setup(); led.setup();
udp.beginMulticast(udpMulticastAddress, udpMulticastPort);
lastButtonPress = millis(); lastButtonPress = millis();
rotary.registerCallback(rotation); rotary.registerCallback(rotation);
rotary.begin(rotaryPinA, rotaryPinB, rotaryPinButton); 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) { if (wakeup_reason == ESP_SLEEP_WAKEUP_EXT1) {
uint64_t wakeup_pinmask = esp_sleep_get_ext1_wakeup_status(); uint64_t wakeup_pinmask = esp_sleep_get_ext1_wakeup_status();
if (wakeup_pinmask & (1ULL << ext_wakeup_pin_1)) { if (wakeup_pinmask & (1ULL << ext_wakeup_pin_1)) {
@ -237,14 +235,7 @@ void setup() {
} }
} }
lastActive = millis(); udp.beginMulticast(udpMulticastAddress, udpMulticastPort);
}
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));
} }
@ -262,6 +253,10 @@ void onMqttConnect(bool sessionPresent) {
void transmitStatus() { void transmitStatus() {
DEBUG_PRINTLN(__func__); DEBUG_PRINTLN(__func__);
if (iot.wifi.status() != WL_CONNECTED) {
return;
}
float temp = bme280.readTemperature(); float temp = bme280.readTemperature();
float humi = bme280.readHumidity(); float humi = bme280.readHumidity();
float pres = bme280.readPressure(); float pres = bme280.readPressure();
@ -376,6 +371,13 @@ void loop()
if(millis() - lastUpdate >= 1000) { if(millis() - lastUpdate >= 1000) {
lastUpdate = millis(); 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(); double seconds = alarmclock.getSecondsToAlarm();
bool stayAwake = false; bool stayAwake = false;
@ -413,7 +415,7 @@ void loop()
int voltage = rom_phy_get_vdd33(); int voltage = rom_phy_get_vdd33();
Serial.printf("voltage: %d\n", voltage); 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.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":"_"); 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(); lastTransmit = millis();
transmitStatus(); transmitStatus();
} }
/*
int lenp = udp.parsePacket(); int lenp = udp.parsePacket();
//Serial.println(lenp); //Serial.println(lenp);
while(udp.available()) { while(udp.available()) {
int len = udp.read(udprecvbuf, sizeof(udprecvbuf)); int len = udp.read(udprecvbuf, sizeof(udprecvbuf));
Serial.printf("udp received %d bytes\n", len); Serial.printf("udp received %d bytes\n", len);
} }
*/
if (menuChange != eNone) { if (menuChange != eNone) {
delete screen; delete screen;
if (menuChange == eMainScreen) screen = new MainScreen(); if (menuChange == eMainScreen) screen = new MainScreen();

1
src/main.h

@ -4,6 +4,7 @@
void resetToFactoryDefaults(); void resetToFactoryDefaults();
void setup(); void setup();
void onMqttConnect(bool sessionPresent); void onMqttConnect(bool sessionPresent);
void onWiFiConnect();
void transmitStatus(); void transmitStatus();
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total); void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total);
void suspend(uint32_t secondsToSleep); void suspend(uint32_t secondsToSleep);

Loading…
Cancel
Save