diff --git a/.DS_Store b/.DS_Store index 5172429..918c73c 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/CAD/t18_v3.0.pdf b/CAD/t18_v3.0.pdf new file mode 100644 index 0000000..ec901e0 Binary files /dev/null and b/CAD/t18_v3.0.pdf differ diff --git a/platformio.ini b/platformio.ini index c81915e..7f6a34a 100644 --- a/platformio.ini +++ b/platformio.ini @@ -9,7 +9,16 @@ ; https://docs.platformio.org/page/projectconf.html [platformio] -default_envs = heltec_wifi_lora_32_V2 +default_envs = ttgo-t18 + +[env] +lib_deps = + #heltecautomation/Heltec ESP32 Dev-Boards@^1.1.0 + ericksimoes/Ultrasonic@^3.0.0 + 256dpi/MQTT@^2.4.8 + http://192.168.2.3/Bonobo.Git.Server/OLEDDisplay.git + bluerobotics/ping-arduino@^0.0.2 + [env:heltec_wifi_lora_32_V2] platform = espressif32 @@ -17,11 +26,6 @@ board = heltec_wifi_lora_32_V2 tasks = framework = arduino build_flags = -D ENVIRONMENT=1 -lib_deps = - #heltecautomation/Heltec ESP32 Dev-Boards@^1.1.0 - ericksimoes/Ultrasonic@^3.0.0 - 256dpi/MQTT@^2.4.8 - http://192.168.2.3/Bonobo.Git.Server/OLEDDisplay.git monitor_speed = 115200 @@ -30,19 +34,12 @@ platform = espressif32 board = lolin32 framework = arduino build_flags = -D ENVIRONMENT=2 -lib_deps = - ericksimoes/Ultrasonic@^3.0.0 - 256dpi/MQTT@^2.4.8 - http://192.168.2.3/Bonobo.Git.Server/OLEDDisplay.git monitor_speed = 115200 + [env:ttgo-t18] platform = espressif32 board = ttgo-t1 framework = arduino build_flags = -D ENVIRONMENT=3 -lib_deps = - ericksimoes/Ultrasonic@^3.0.0 - 256dpi/MQTT@^2.4.8 - http://192.168.2.3/Bonobo.Git.Server/OLEDDisplay.git monitor_speed = 115200 \ No newline at end of file diff --git a/src/display.cpp b/src/display.cpp index 1062a09..bb8a55b 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -13,8 +13,20 @@ SSD1306Wire *display; bool initOK = false; char buf[128]; +void displayOff(void) +{ + //display->setBrightness(64); + //display->displayOff(); +} + +void displayOn(void) +{ + //display->displayOn(); +} + void displayDrawBatt(uint16_t voltage, bool sleep) { + Serial.println("get vbatt"); voltage = powerGetVbatt(); uint16_t xstart = 104; uint16_t ystart = 30; @@ -131,7 +143,7 @@ void displayInit(void) return; } initOK = display->init(); - display->setBrightness(255); + display->setBrightness(128); display->flipScreenVertically(); display->setFont(ArialMT_Plain_10); display->drawString(0, 0, "OLED initial done!"); diff --git a/src/display.h b/src/display.h index 4f7d097..b8a9ef3 100644 --- a/src/display.h +++ b/src/display.h @@ -6,6 +6,8 @@ void displayInit(void); void displayUpdate(void); void displayDrawBatt(uint16_t voltage, bool sleep); void displayDrawShutdown(void); +void displayOff(void); +void displayOn(void); diff --git a/src/hal.h b/src/hal.h index 936edb1..7e262cf 100644 --- a/src/hal.h +++ b/src/hal.h @@ -56,11 +56,16 @@ #define SCL_OLED 4 #define RST_OLED 17 +#define US100 //alternative sonar #define TRIGGER 13 #define ECHO 14 -#define VBATT ADC2_CHANNEL_1 +#define VBATT ADC1_CHANNEL_7 #define VEXT 21 + +#define MAILLED 21 +#define BUTTON 22 +#define DOORSW 23 #endif #endif //HALH \ No newline at end of file diff --git a/src/mailbox.cpp b/src/mailbox.cpp index 05a74b4..e77dae3 100644 --- a/src/mailbox.cpp +++ b/src/mailbox.cpp @@ -1,6 +1,7 @@ #include "Arduino.h" #include "mailbox.h" #include "sensor.h" +#include "hal.h" #define MAILCOUNTERFILTER 10 //minimum samples should be equal before triggering mail alert #define MAILCOUNTERTHRESHOLT 2 //minimum delta distance in cm @@ -10,6 +11,14 @@ uint8_t mailcounter = 0; bool mailFlag = false; bool mailDetected = false; +void mailboxInit(void) +{ + + pinMode(MAILLED, OUTPUT); + pinMode(BUTTON, INPUT_PULLUP); + pinMode(DOORSW, INPUT_PULLUP); +} + void mailboxhandler(void) { uint8_t currentDistance = sensorGetDistance(); @@ -43,4 +52,9 @@ void mailboxhandler(void) bool mailboxGetMailDetected(void) { return mailDetected; +} + +void mailboxCleared(void) +{ + } \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 7da367f..488989e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,7 +7,7 @@ #include "power.h" #include "mailbox.h" -#define TIME_TO_SLEEP 1 /* Time ESP32 will go to sleep (in seconds) */ +#define TIME_TO_SLEEP 10 /* Time ESP32 will go to sleep (in seconds) */ void serialInit(void) { @@ -24,7 +24,7 @@ void setup() sensorInit(); //netInit(); powerInit(); - //sleepInit(TIME_TO_SLEEP); + sleepInit(TIME_TO_SLEEP); displayUpdate(); } @@ -34,6 +34,6 @@ void loop() sensorUpdateDistance(); displayUpdate(); mailboxhandler(); - //sleepStart(); + sleepStart(); delay(1000); } diff --git a/src/power.cpp b/src/power.cpp index 66bc41a..50a1124 100644 --- a/src/power.cpp +++ b/src/power.cpp @@ -5,7 +5,15 @@ #include #include "display.h" -#define VOLTAGE_DIVIDER 2.08 // Lora has 220k/100k voltage divider so need to reverse that reduction via (220k+100k)/100k on vbat GPIO37 or ADC1_1 (early revs were GPIO13 or ADC2_4 but do NOT use with WiFi.begin()) +#if (ENVIRONMENT == TTGO_T18) + #define VOLTAGE_DIVIDER 2 // ttgo has 100/100k voltage divider so need to reverse that reduction via (220k+100k)/100k on vbat GPIO37 or ADC1_1 (early revs were GPIO13 or ADC2_4 but do NOT use with WiFi.begin()) + #define VOLTAGEREF 3787 // measured 2,0474 / 2214 ticks (mv) +#else + + #define VOLTAGE_DIVIDER 2.08 // Lora has 220k/100k voltage divider so need to reverse that reduction via (220k+100k)/100k on vbat GPIO37 or ADC1_1 (early revs were GPIO13 or ADC2_4 but do NOT use with WiFi.begin()) + +#endif + #define DEFAULT_VREF 1100 // Default VREF use if no e-fuse calibration #define VBATT_SAMPLE 500 // Battery sample rate in ms #define VBATT_SMOOTH 20 // Number of averages in sample @@ -15,7 +23,6 @@ uint16_t Sample(); - esp_adc_cal_characteristics_t *adc_chars; uint16_t voltage = 666; @@ -27,6 +34,11 @@ void powerInit(void) esp_adc_cal_value_t val_type = esp_adc_cal_characterize(ADC_UNIT_1, ADC_ATTEN_DB_6, ADC_WIDTH_BIT_12, DEFAULT_VREF, adc_chars); adc1_config_width(ADC_WIDTH_BIT_12); adc1_config_channel_atten(VBATT, ADC_ATTEN_DB_6); +#elif (ENVIRONMENT == TTGO_T18) + //nothing + adc_chars = (esp_adc_cal_characteristics_t *)calloc(1, sizeof(esp_adc_cal_characteristics_t)); + esp_adc_cal_value_t val_type = esp_adc_cal_characterize(ADC_UNIT_1, ADC_ATTEN_DB_6, ADC_WIDTH_BIT_12, DEFAULT_VREF, adc_chars); + adc1_config_channel_atten(VBATT, ADC_ATTEN_DB_6); #else // Use this for older V2.0 with VBatt reading wired to GPIO13 adc_chars = (esp_adc_cal_characteristics_t *)calloc(1, sizeof(esp_adc_cal_characteristics_t)); @@ -64,7 +76,6 @@ void powerInit(void) Serial.println("Power init done"); } - uint16_t powerGetVbatt(void) { return voltage; @@ -75,31 +86,30 @@ void powerHandler(void) voltage = Sample(); //displayDrawBatt(voltage, voltage < LIGHT_SLEEP_VOLTAGE); - if (voltage < MINBATT) - { // Low Voltage cut off shut down to protect battery as long as possible - displayDrawShutdown(); - delay(2000); -#if defined(__DEBUG) && __DEBUG > 0 - Serial.printf(" !! Shutting down...low battery volotage: %dmV.\n", voltage); - delay(10); -#endif - esp_sleep_enable_timer_wakeup(LO_BATT_SLEEP_TIME); - esp_deep_sleep_start(); - } - else if (voltage < LIGHT_SLEEP_VOLTAGE) - { // Use light sleep once on battery - uint64_t s = VBATT_SAMPLE; -#if defined(__DEBUG) && __DEBUG > 0 - Serial.printf(" - Light Sleep (%dms)...battery volotage: %dmV.\n", (int)s, voltage); - delay(20); -#endif - esp_sleep_enable_timer_wakeup(s * 1000); // Light Sleep does not flush buffer - esp_light_sleep_start(); - } +// if (voltage < MINBATT) +// { // Low Voltage cut off shut down to protect battery as long as possible +// displayDrawShutdown(); +// delay(2000); +// #if defined(__DEBUG) && __DEBUG > 0 +// Serial.printf(" !! Shutting down...low battery volotage: %dmV.\n", voltage); +// delay(10); +// #endif +// esp_sleep_enable_timer_wakeup(LO_BATT_SLEEP_TIME); +// esp_deep_sleep_start(); +// } +// else if (voltage < LIGHT_SLEEP_VOLTAGE) +// { // Use light sleep once on battery +// uint64_t s = VBATT_SAMPLE; +// #if defined(__DEBUG) && __DEBUG > 0 +// Serial.printf(" - Light Sleep (%dms)...battery volotage: %dmV.\n", (int)s, voltage); +// delay(20); +// #endif +// esp_sleep_enable_timer_wakeup(s * 1000); // Light Sleep does not flush buffer +// esp_light_sleep_start(); +// } delay(ADC_READ_STABILIZE); } - // Heltec WiFi LoRa V2 battery read example // by Jeff McClain jeff@themcclains.net // @@ -107,27 +117,40 @@ void powerHandler(void) // Poll the proper ADC for VBatt on Heltec Lora 32 with GPIO21 toggled uint16_t ReadVBatt() { + Serial.println("start read batt"); int reading = 666; + uint16_t rawVoltage; +#if (defined(HELTEC_V2_1)) digitalWrite(VEXT, LOW); // ESP32 Lora v2.1 reads on GPIO37 when GPIO21 is low delay(ADC_READ_STABILIZE); // let GPIO stabilize -#if (defined(HELTEC_V2_1)) pinMode(VBATT, OPEN_DRAIN); // ADC GPIO37 reading = adc1_get_raw(VBATT); pinMode(VBATT, INPUT); // Disconnect ADC before GPIO goes back high so we protect ADC from direct connect to VBATT (i.e. no divider) -#else + uint16_t rawVoltage = esp_adc_cal_raw_to_voltage(reading, adc_chars); + +#elif (ENVIRONMENT == TTGO_T18) + pinMode(VBATT, ANALOG); // ADC GPIO13 + reading = analogRead(35); + Serial.printf("battery analogread = %i\n", reading); + rawVoltage = float(VOLTAGEREF / 4096) * reading; + Serial.printf("raw voltage = %i\n", rawVoltage); + + //pinMode(VBATT, INPUT); // Disconnect ADC before GPIO goes back high so we protect ADC from direct connect to VBATT (i.e. no divider +#elif (ENVIRONMENT == LOLIN32) + digitalWrite(VEXT, LOW); // ESP32 Lora v2.1 reads on GPIO37 when GPIO21 is low + delay(ADC_READ_STABILIZE); // let GPIO stabilize pinMode(VBATT, OPEN_DRAIN); // ADC GPIO13 adc2_get_raw(VBATT, ADC_WIDTH_BIT_12, &reading); pinMode(VBATT, INPUT); // Disconnect ADC before GPIO goes back high so we protect ADC from direct connect to VBATT (i.e. no divider + rawVoltage = esp_adc_cal_raw_to_voltage(reading, adc_chars); #endif - uint16_t rawVoltage = esp_adc_cal_raw_to_voltage(reading, adc_chars); //Serial.printf("battery rawvoltage = %i\n", rawVoltage); rawVoltage *= VOLTAGE_DIVIDER; - //Serial.printf("battery sample = %i\n", rawVoltage); + Serial.printf("battery sample = %i\n", rawVoltage); //digitalWrite(VEXT, HIGH); // ESP32 Lora v2.1 reads on GPIO37 when GPIO21 is low return rawVoltage; - } // Use a buffer to average/sample ADC diff --git a/src/sleep.cpp b/src/sleep.cpp index a2551b7..005ee04 100644 --- a/src/sleep.cpp +++ b/src/sleep.cpp @@ -1,5 +1,6 @@ #include "arduino.h" #include "sleep.h" +#include "display.h" #define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds */ @@ -65,7 +66,12 @@ void sleepStart() Serial.flush(); Serial.println("Going to sleep now"); Serial.flush(); + displayOff(); delay(100); esp_light_sleep_start(); + + + displayOn(); Serial.println("Woke"); + } \ No newline at end of file