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) {
sntp_setoperatingmode(SNTP_OPMODE_POLL);
sntp_setservername(0, "de.pool.ntp.org");

2
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 */

58
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();

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

Loading…
Cancel
Save