From 74d82d405b0375c37e1a2136e3f0ff41cdf4a835 Mon Sep 17 00:00:00 2001 From: Hendrik Langer Date: Wed, 7 Feb 2018 21:54:30 +0100 Subject: [PATCH] transmitStatus on standby --- src/hardware.h | 2 +- src/main.cpp | 23 ++++++++++++++++------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/hardware.h b/src/hardware.h index f572fc2..2456348 100644 --- a/src/hardware.h +++ b/src/hardware.h @@ -16,7 +16,7 @@ static constexpr uint8_t PROGMEM NUM_LEDS = 10; static constexpr uint8_t ext_wakeup_pin_1 = 0; static constexpr uint8_t ext_wakeup_pin_2 = 0; -static constexpr uint32_t secondsToSleep = 30*60; +static constexpr uint32_t secondsToSleep = 15*60; static constexpr int batteryLimit = 2800; diff --git a/src/main.cpp b/src/main.cpp index e77cdc2..62422f2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -65,9 +65,9 @@ uint32_t lastUpdate = 0; uint32_t lastTransmit = 0; //Variables for the mqtt packages and topics -uint16_t statusPacketIdSub = 0; String commandTopic; String bme280Topic; +String batteryTopic; void setup() { // gpio configuration @@ -82,6 +82,11 @@ void setup() { //Initialize Basecamp iot.begin(); + //Configure the MQTT topics + commandTopic = "esp32-node/cmd/" + iot.hostname + "/play"; + bme280Topic = "esp32-node/stat/" + iot.hostname + "/bme280"; + batteryTopic = "esp32-node/stat/" + iot.hostname + "/battery"; + esp_sleep_wakeup_cause_t wakeup_reason = esp_sleep_get_wakeup_cause(); switch(wakeup_reason) { case ESP_SLEEP_WAKEUP_EXT0: @@ -122,6 +127,9 @@ void setup() { Serial.print("Current time: "); Serial.println(strftime_buf); if (wakeup_reason == ESP_SLEEP_WAKEUP_TIMER) { + for (int i=0; i<5 && iot.wifi.status() != WL_CONNECTED; i++) delay(500); + if (iot.wifi.status() == WL_CONNECTED) transmitStatus(); + double seconds = difftime(now, mktime(&alarmTime)); Serial.printf("alarm in %f seconds\n", seconds); strftime(strftime_buf, sizeof(strftime_buf), "%c", &alarmTime); @@ -141,10 +149,6 @@ void setup() { bme280.begin(); bme280.printValues(); - //Configure the MQTT topics - commandTopic = "esp32-node/cmd/" + iot.hostname + "/play"; - bme280Topic = "esp32-node/stat/" + iot.hostname + "/bme280"; - //Set up the Callbacks for the MQTT instance. Refer to the Async MQTT Client documentation // TODO: We should do this actually _before_ connecting the mqtt client... iot.mqtt.onConnect(onMqttConnect); @@ -198,8 +202,12 @@ void transmitStatus() { char sensorBuf[root.measureLength()+1]; root.printTo(sensorBuf, sizeof(sensorBuf)); - statusPacketIdSub = iot.mqtt.publish(bme280Topic.c_str(), 0, false, sensorBuf); + uint16_t statusPacketIdSub = iot.mqtt.publish(bme280Topic.c_str(), 0, false, sensorBuf); // statusPacketIdSub = iot.mqtt.publish(bme280Topic.c_str(), 1, true, sensorBuf); + int voltage = rom_phy_get_vdd33(); + char batteryBuf[9]; + sprintf(batteryBuf, "%d", voltage); + statusPacketIdSub = iot.mqtt.publish(batteryTopic.c_str(), 0, false, batteryBuf); } @@ -239,12 +247,13 @@ void suspend(uint32_t secondsToSleep) { if (!displaySleep) { u8g2.begin(); + u8g2.setPowerSave(0); u8g2.clearBuffer(); u8g2.sendBuffer(); u8g2.setFont(u8g2_font_inb19_mf); u8g2.drawStr(0, 20, timeStr); // ToDo: set rtc gpio resistors - rtc_gpio_hold_en((gpio_num_t)16); + //rtc_gpio_hold_en((gpio_num_t)16); esp_sleep_enable_timer_wakeup(1000000LL * 60); }