Skip to content

Commit

Permalink
low power improvements, display and bme now powered off GPIO pins
Browse files Browse the repository at this point in the history
  • Loading branch information
lmarzen committed Dec 17, 2023
1 parent 2ed5f37 commit 5e7ca94
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 13 deletions.
8 changes: 8 additions & 0 deletions platformio/include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,12 @@
// Disable alerts by changing the DISPLAY_ALERTS macro to 0.
#define DISPLAY_ALERTS 1

// STATUS BAR EXTRAS
// Extra information that can be displayed on the status bar. Set to 1 to
// enable.
#define STATUS_BAR_EXTRAS_BAT_VOLTAGE 0
#define STATUS_BAR_EXTRAS_WIFI_RSSI 0

// BATTERY MONITORING
// You may choose to power your whether display with or without a battery.
// Low power behavior can be controlled in config.cpp.
Expand All @@ -223,8 +229,10 @@ extern const uint8_t PIN_EPD_DC;
extern const uint8_t PIN_EPD_SCK;
extern const uint8_t PIN_EPD_MISO;
extern const uint8_t PIN_EPD_MOSI;
extern const uint8_t PIN_EPD_PWR;
extern const uint8_t PIN_BME_SDA;
extern const uint8_t PIN_BME_SCL;
extern const uint8_t PIN_BME_PWR;
extern const uint8_t BME_ADDRESS;
extern const char *WIFI_SSID;
extern const char *WIFI_PASSWORD;
Expand Down
1 change: 1 addition & 0 deletions platformio/include/display_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ const uint8_t *getWindBitmap24(int windDeg);
const char *getHttpResponsePhrase(int code);
const char *getWifiStatusPhrase(wl_status_t status);
void printHeapUsage();
void disableBuiltinLED();

#endif

1 change: 1 addition & 0 deletions platformio/include/renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ void drawMultiLnString(int16_t x, int16_t y, const String &text,
uint16_t max_lines, int16_t line_spacing,
uint16_t color=GxEPD_BLACK);
void initDisplay();
void powerOffDisplay();
void drawCurrentConditions(const owm_current_t &current,
const owm_daily_t &today,
const owm_resp_air_pollution_t &owm_air_pollution,
Expand Down
8 changes: 5 additions & 3 deletions platformio/src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ const uint8_t PIN_EPD_DC = 22;
const uint8_t PIN_EPD_SCK = 18;
const uint8_t PIN_EPD_MISO = 19; // 19 Master-In Slave-Out not used, as no data from display
const uint8_t PIN_EPD_MOSI = 23;
const uint8_t PIN_EPD_PWR = 14; // Irrelevant if directly connected to 3.3V
// I2C Pins used for BME280
const uint8_t PIN_BME_SDA = 17; // 27 for micro-usb firebeetle
const uint8_t PIN_BME_SCL = 16; // 26 for micro-usb firebeetle
const uint8_t BME_ADDRESS = 0x76; // if sensor does not work, try 0x77
const uint8_t PIN_BME_SDA = 17;
const uint8_t PIN_BME_SCL = 16;
const uint8_t PIN_BME_PWR = 4; // Irrelevant if directly connected to 3.3V
const uint8_t BME_ADDRESS = 0x76; // If sensor does not work, try 0x77

// WIFI
const char *WIFI_SSID = "ssid";
Expand Down
12 changes: 12 additions & 0 deletions platformio/src/display_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1667,3 +1667,15 @@ const char *getWifiStatusPhrase(wl_status_t status)
default: return "";
}
} // end getWifiStatusPhrase

/* This function sets the builtin LED to LOW and disables it even during deep
* sleep.
*/
void disableBuiltinLED()
{
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
gpio_hold_en(static_cast<gpio_num_t>(LED_BUILTIN));
gpio_deep_sleep_hold_en();
return;
} // end disableBuiltinLED
24 changes: 16 additions & 8 deletions platformio/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <Arduino.h>
#include <Adafruit_BME280.h>
#include <Adafruit_Sensor.h>
#include <driver/adc.h>
#include <Preferences.h>
#include <time.h>
#include <WiFi.h>
Expand Down Expand Up @@ -127,15 +128,19 @@ void setup()
printHeapUsage();
#endif

disableBuiltinLED();

// Open namespace for read/write to non-volatile storage
prefs.begin(NVS_NAMESPACE, false);

#if BATTERY_MONITORING
// GET BATTERY VOLTAGE
// DFRobot FireBeetle Esp32-E V1.0 has voltage divider (1M+1M), so readings
// are multiplied by 2. Readings are divided by 1000 to convert mV to V.
double batteryVoltage =
static_cast<double>(analogRead(PIN_BAT_ADC)) / 1000.0 * (3.5 / 2.0);
adc_power_acquire();
uint16_t batADC = analogRead(PIN_BAT_ADC);
adc_power_release();
double batteryVoltage = static_cast<double>(batADC) / 1000.0 * (3.5 / 2.0);
// use / 1000.0 * (3.3 / 2.0) multiplier above for firebeetle esp32
// use / 1000.0 * (3.5 / 2.0) for firebeetle esp32-E
Serial.println("Battery voltage: " + String(batteryVoltage, 2));
Expand All @@ -158,7 +163,7 @@ void setup()
{
drawError(battery_alert_0deg_196x196, "Low Battery", "");
} while (display.nextPage());
display.powerOff();
powerOffDisplay();
}

if (batteryVoltage <= CRIT_LOW_BATTERY_VOLTAGE)
Expand Down Expand Up @@ -225,7 +230,7 @@ void setup()
drawError(wifi_x_196x196, "WiFi Connection", "Failed");
} while (display.nextPage());
}
display.powerOff();
powerOffDisplay();
beginDeepSleep(startTime, &timeInfo);
}

Expand All @@ -241,7 +246,7 @@ void setup()
{
drawError(wi_time_4_196x196, "Time Synchronization", "Failed");
} while (display.nextPage());
display.powerOff();
powerOffDisplay();
beginDeepSleep(startTime, &timeInfo);
}

Expand All @@ -266,7 +271,7 @@ void setup()
{
drawError(wi_cloud_down_196x196, statusStr, tmpStr);
} while (display.nextPage());
display.powerOff();
powerOffDisplay();
beginDeepSleep(startTime, &timeInfo);
}
rxStatus = getOWMairpollution(client, owm_air_pollution);
Expand All @@ -280,12 +285,14 @@ void setup()
{
drawError(wi_cloud_down_196x196, statusStr, tmpStr);
} while (display.nextPage());
display.powerOff();
powerOffDisplay();
beginDeepSleep(startTime, &timeInfo);
}
killWiFi(); // WiFi no longer needed

// GET INDOOR TEMPERATURE AND HUMIDITY, start BME280...
pinMode(PIN_BME_PWR, OUTPUT);
digitalWrite(PIN_BME_PWR, HIGH);
float inTemp = NAN;
float inHumidity = NAN;
Serial.print("Reading from BME280... ");
Expand Down Expand Up @@ -317,6 +324,7 @@ void setup()
statusStr = "BME not found"; // check wiring
Serial.println(statusStr);
}
digitalWrite(PIN_BME_PWR, LOW);

String refreshTimeStr;
getRefreshTimeStr(refreshTimeStr, timeConfigured, &timeInfo);
Expand All @@ -337,7 +345,7 @@ void setup()
#endif
drawStatusBar(statusStr, refreshTimeStr, wifiRSSI, batteryVoltage);
} while (display.nextPage());
display.powerOff();
powerOffDisplay();

// DEEP-SLEEP
beginDeepSleep(startTime, &timeInfo);
Expand Down
20 changes: 18 additions & 2 deletions platformio/src/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ void drawMultiLnString(int16_t x, int16_t y, const String &text,
*/
void initDisplay()
{
pinMode(PIN_EPD_PWR, OUTPUT);
digitalWrite(PIN_EPD_PWR, HIGH);
display.init(115200, true, 2, false);
SPI.begin(PIN_EPD_SCK,
PIN_EPD_MISO,
Expand All @@ -237,6 +239,16 @@ void initDisplay()
// display.fillScreen(GxEPD_WHITE);
display.setFullWindow();
display.firstPage(); // use paged drawing mode, sets fillScreen(GxEPD_WHITE)
return;
} // end initDisplay

/* Power-off e-paper display
*/
void powerOffDisplay()
{
display.powerOff();
digitalWrite(PIN_EPD_PWR, LOW);
return;
} // end initDisplay

/* This function is responsible for drawing the current conditions and
Expand Down Expand Up @@ -1085,8 +1097,10 @@ void drawStatusBar(const String &statusStr, const String &refreshTimeStr,
if (batVoltage < BATTERY_WARN_VOLTAGE) {
dataColor = ACCENT_COLOR;
}
dataStr = String(batPercent) + "% ("
+ String( round(100.0 * batVoltage) / 100.0, 2 ) + "v)";
dataStr = String(batPercent) + "%";
#if STATUS_BAR_EXTRAS_BAT_VOLTAGE
dataStr += " (" + String( round(100.0 * batVoltage) / 100.0, 2 ) + "v)";
#endif
drawString(pos, DISP_HEIGHT - 1 - 2, dataStr, RIGHT, dataColor);
pos -= getStringWidth(dataStr) + 25;
display.drawInvertedBitmap(pos, DISP_HEIGHT - 1 - 17,
Expand All @@ -1097,10 +1111,12 @@ void drawStatusBar(const String &statusStr, const String &refreshTimeStr,
// WiFi
dataStr = String(getWiFidesc(rssi));
dataColor = rssi >= -70 ? GxEPD_BLACK : ACCENT_COLOR;
#if STATUS_BAR_EXTRAS_WIFI_RSSI
if (rssi != 0)
{
dataStr += " (" + String(rssi) + "dBm)";
}
#endif
drawString(pos, DISP_HEIGHT - 1 - 2, dataStr, RIGHT, dataColor);
pos -= getStringWidth(dataStr) + 19;
display.drawInvertedBitmap(pos, DISP_HEIGHT - 1 - 13, getWiFiBitmap16(rssi),
Expand Down
Binary file modified showcase/wiring_diagram_despi-c02.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified showcase/wiring_diagram_waveshare_rev22.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 5e7ca94

Please sign in to comment.