diff --git a/sender/src/eink.cpp b/sender/src/eink.cpp index 285e1ab..df520a8 100644 --- a/sender/src/eink.cpp +++ b/sender/src/eink.cpp @@ -48,13 +48,26 @@ void Eink::show_temp(float temperature) { logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "EINK", "Show temp: %f", temperature); - int textX = 40; + int textX = 50; //u8g2Fonts.setFont(u8g2_font_fur42_tf); - u8g2Fonts.setFont(u8g2_font_inr46_mf); + u8g2Fonts.setFont(u8g2_font_inb46_mf); drawString(textX, 70, String(temperature, 1) + "°", LEFT); drawMercury(temperature); } + +void Eink::show_internal_temp(int x, int y, float temperature) +{ + logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "EINK", "Show internal temp: %f", temperature); + u8g2Fonts.setFont(u8g2_font_7x13_tf); + drawString(x + 60, y - 11, String(temperature, 1) + "°", LEFT); +} +void Eink::show_count(int x, int y, float cnt, String unit) +{ + logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "EINK", "Show cnt: %f", cnt); + u8g2Fonts.setFont(u8g2_font_7x13_tf); + drawString(x + 60, y - 11, unit + String(cnt, 1), LEFT); +} void Eink::drawSignalBars(int x, int y, int percentage) { // Define stapler properties @@ -99,7 +112,9 @@ void Eink::drawMercury(float temperature) int y = 30; int height = 60; int width = 10; - int mercuryLevel = map(temperature, 0.0, 100.0, 20, height); + int mercuryLevel = map(temperature, 5.0, 35.0, 10, height); + logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "EINK", "mercuryLevel: %d", mercuryLevel); + int radius = 14; eink.drawRect(x, y - radius + 2, width, height, GxEPD_BLACK); @@ -125,9 +140,6 @@ void Eink::drawBattery(int x, int y, int percentage) void Eink::drawString(int x, int y, String text, alignmentType alignment) { - logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "EINK drawstring", "x: %d", y); - logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "EINK drawstring", "y: %d", x); - int16_t x1, y1; // the bounds of x,y and w and h of the variable 'text' in pixels. uint16_t w, h; eink.setTextWrap(false); diff --git a/sender/src/eink.hpp b/sender/src/eink.hpp index 13b29a8..84308d2 100644 --- a/sender/src/eink.hpp +++ b/sender/src/eink.hpp @@ -9,9 +9,10 @@ class Eink { public: Eink(int csPin = EPD_CS, int dcPin = EPD_DC, int rstPin = EPD_RSET, int busyPin = EPD_BUSY); - void setup_eink(); void show_temp(float temperature); + void show_internal_temp(int x, int y, float temperature); + void show_count(int x, int y, float cnt, String unit); void show_display(String header, int wait=100); void drawBattery(int x, int y, int percentage=100); void drawSignalBars(int x, int y, int numBars); @@ -20,13 +21,10 @@ public: private: U8G2_FOR_ADAFRUIT_GFX u8g2Fonts; enum alignmentType {LEFT, RIGHT, CENTER}; - void drawMercury(float temperature); void drawString(int x, int y, String text, alignmentType alignment); void drawCircleSegment(int x0, int y0, int radius, int startAngle, int endAngle); - // GxEPD2_213_BN epd; GxEPD2_BW eink; - }; diff --git a/sender/src/io.cpp b/sender/src/io.cpp index 91cf32b..a2b26ca 100644 --- a/sender/src/io.cpp +++ b/sender/src/io.cpp @@ -2,64 +2,96 @@ #include "io.hpp" #include "logger.h" #include "pins.hpp" -#include "WiFi.h" +#include "WiFi.h" #include "driver/adc.h" #include #include - extern logging::Logger logger; -IO::IO() { -} -void IO::setup_io() { - oneWire.begin(DI_DALLAS); - sensors.begin(); - sensors.setResolution(10); +IO::IO() : oneWire(DI_DALLAS), sensors(&oneWire) { + // Constructor body (if needed) } -void IO::set_low_power() { - digitalWrite ( DI_LED, LOW ); - setCpuFrequencyMhz(80); - WiFi.mode(WIFI_OFF); - btStop(); +#define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds */ +#define TIME_TO_SLEEP 600 +void IO::setup_io() +{ + logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "IO", "SETUP IO"); + oneWire.begin(DI_DALLAS); + logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "IO", "oneWire.begin"); + sensors.begin(); + logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "IO", "sensors.begi"); + sensors.setResolution(12); +} + +void IO::set_low_power() +{ + logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "IO", "Set lower power attrs"); + + pinMode(DI_LED, OUTPUT); + digitalWrite(DI_LED, LOW); // TODO check if pin 22(ledbuiltin 2) is a led + setCpuFrequencyMhz(80); + WiFi.mode(WIFI_OFF); + btStop(); esp_wifi_stop(); esp_bt_controller_disable(); - - + esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR); + adc_power_on(); } -uint8_t IO::read_battery() { +void IO::set_deep_sleep() +{ + logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "IO", "Going to sleep"); + esp_deep_sleep_start(); + logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "IO", "Will not reach"); +} + +uint8_t IO::read_battery() +{ uint8_t percentage = 100; float voltage = analogRead(ADC_BATTERY) / 4096.0 * 7.46; - //float voltage = analogRead(35) / 4096.0 * 7.46; - if (voltage > 1 ) { // Only display if there is a valid reading + // float voltage = analogRead(35) / 4096.0 * 7.46; + if (voltage > 1) + { // Only display if there is a valid reading percentage = 2836.9625 * pow(voltage, 4) - 43987.4889 * pow(voltage, 3) + 255233.8134 * pow(voltage, 2) - 656689.7123 * voltage + 632041.7303; - if (voltage >= 4.20) percentage = 100; - if (voltage <= 3.50) percentage = 0; + if (voltage >= 4.20) + percentage = 100; + if (voltage <= 3.50) + percentage = 0; logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "io", "Voltage: %f", voltage); logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "io", "Percentage: %d", percentage); } return percentage; - } -float IO::read_temp() { - +float IO::read_temp() +{ + logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "IO", "read_temp"); sensors.requestTemperatures(); // Request temperature readings + delay(1000); float tempC = sensors.getTempCByIndex(0); - if (tempC != DEVICE_DISCONNECTED_C) + if (tempC != DEVICE_DISCONNECTED_C) { logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "io", "Temp: %f", tempC); - }else{ - logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "io", "Couldn't read temp"); } + else if(tempC == -127.00) { + logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "io", "Couldn't read temp"); + } + else + { + logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "io", "Couldn't read temp"); + } + return tempC; +} + +float IO::read_internal_temp() +{ float chipTemp = temperatureRead(); logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "io", "chipTemp: %f", chipTemp); - return tempC; - + return chipTemp; } \ No newline at end of file diff --git a/sender/src/io.hpp b/sender/src/io.hpp index d60da20..044eaaa 100644 --- a/sender/src/io.hpp +++ b/sender/src/io.hpp @@ -11,12 +11,13 @@ public: IO(); void setup_io(); void set_low_power(); + void set_deep_sleep(); float read_temp(); + float read_internal_temp(); uint8_t read_battery(); private: OneWire oneWire; DallasTemperature sensors; - }; #endif /* IO_H */ \ No newline at end of file diff --git a/sender/src/main.cpp b/sender/src/main.cpp index b4f4286..12499e6 100644 --- a/sender/src/main.cpp +++ b/sender/src/main.cpp @@ -7,35 +7,60 @@ logging::Logger logger; Eink eink; IO io; +RTC_DATA_ATTR int bootCount = 0; +RTC_DATA_ATTR int updateCount = 0; +RTC_DATA_ATTR float last_temp = 20; +RTC_DATA_ATTR int last_battery = 0; +bool update = false; + void setup() { Serial.begin(115200); -// TODO check if pin 22(ledbuiltin 2) is a led + logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "MAIN", "Boot number: %i", bootCount); + update = false; #ifndef DEBUG logger.setDebugLevel(logging::LoggerLevel::LOGGER_LEVEL_INFO); #endif - //delay(100); - io.set_low_power(); io.setup_io(); + io.set_low_power(); + uint8_t battery = io.read_battery(); + if (abs(battery - last_battery) >= 2) + { + last_battery = battery; + update = true; + } + logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "MAIN", "Battery: %i", battery); + float temp = io.read_temp(); - eink.setup_eink(); - // delay(100); + + logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "MAIN", "TEMP: %f", temp); + if (abs(temp - last_temp) >= 0.5 && temp > -120) + { + last_temp = temp; + update = true; + } - eink.drawBattery(186, 14, battery); // eink width 250 - drawbattery width - eink.drawSignalBars(180 - 24 - 4, 12, 84); // drawbattery width - drawsignal - eink.show_temp(30.0); - eink.display(); - + float internal_temp = io.read_internal_temp(); + eink.setup_eink(); + + ++bootCount; + if (update) + { + ++updateCount; + eink.drawBattery(186, 14, battery); // eink width 250 - drawbattery width + eink.drawSignalBars(180 - 24 - 4, 12, 84); // drawbattery width - drawsignal + logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "MAIN", "TEMP: %f", temp); + eink.show_temp(temp); + eink.show_internal_temp(180 - 24 - 4 - 100, 12, internal_temp); + eink.show_count(180 - 24 - 4 - 100 - 60, 12, (float)bootCount, "c: "); + eink.show_count(180 - 24 - 4 - 100 - 60 - 50, 12, (float)bootCount, "u: "); + eink.display(true); + } + io.set_deep_sleep(); } void loop() { - - - - eink.show_temp(30.0); - eink.display(true); - sleep(10); } diff --git a/sender/src/pins.hpp b/sender/src/pins.hpp index e6670d1..5fd32ce 100644 --- a/sender/src/pins.hpp +++ b/sender/src/pins.hpp @@ -20,9 +20,9 @@ const int RADIO_RST = 14; const int RADIO_IRQ = 26; // IRQ --> DIO0 // Battery pin -const int ADC_BATTERY = 12; //builtin = 35 +const int ADC_BATTERY = 35; //builtin = 35 const int DI_DALLAS = 32; // Builtin LED -const int DI_LED = 22; +const int DI_LED = LED_BUILTIN; #endif /* PINS_H */ \ No newline at end of file