update mailbox

This commit is contained in:
2021-01-13 13:00:28 +01:00
parent 40a64daf8a
commit c41bddd71b
10 changed files with 124 additions and 70 deletions

BIN
.DS_Store vendored

Binary file not shown.

View File

@@ -26,16 +26,16 @@ void displayOn(void)
void displayDrawBatt(uint16_t voltage, bool sleep) void displayDrawBatt(uint16_t voltage, bool sleep)
{ {
Serial.println("get vbatt");
voltage = powerGetVbatt(); voltage = powerGetVbatt();
uint16_t xstart = 104; uint16_t xstart = 102;
uint16_t ystart = 30; uint16_t ystart = 3;
display->setColor(BLACK); display->setColor(BLACK);
display->fillRect(xstart - 5, ystart, 29, 24); display->fillRect(xstart - 5, ystart, 29, 24);
display->setColor(WHITE); display->setColor(WHITE);
display->drawRect(xstart, ystart, 12, 6); display->drawRect(xstart, ystart, 22, 12);
display->fillRect(xstart + 1, xstart + 2, 1, 2); display->fillRect(xstart + 1, xstart + 2, 1, 2);
display->fillRect(xstart+21,ystart + 3,2,6);
uint16_t v = voltage; uint16_t v = voltage;
if (v < MINBATT) if (v < MINBATT)
@@ -47,14 +47,12 @@ void displayDrawBatt(uint16_t voltage, bool sleep)
v = MAXBATT; v = MAXBATT;
} }
double pct = map(v, MINBATT, MAXBATT, 0, 100); double pct = map(v, MINBATT, MAXBATT, 0, 100);
uint8_t bars = round(pct / 10.0); uint8_t bars = round(pct / 5.5);
display->fillRect(xstart + 1, ystart + 1, bars, 4); display->fillRect(xstart + 2, ystart + 2, bars, 8);
display->setFont(ArialMT_Plain_10); display->setFont(ArialMT_Plain_10);
display->setTextAlignment(TEXT_ALIGN_RIGHT); display->setTextAlignment(TEXT_ALIGN_RIGHT);
display->drawString(127, 5, String((int)round(pct)) + "%"); display->drawString(127, 15, String((double(v)/1000)) + "v");
display->drawString(127, 14, String(round(voltage / 10.0) / 100.0) + "V");
Serial.printf("battery = %i mv\n", voltage);
#if defined(__DEBUG) && __DEBUG > 0 #if defined(__DEBUG) && __DEBUG > 0
static uint8_t c = 0; static uint8_t c = 0;
@@ -93,7 +91,8 @@ void displayDrawNet(void)
void displayDrawSensor(void) void displayDrawSensor(void)
{ {
sprintf(buf, "Dist: %i cm", sensorGetDistance()); sprintf(buf, "Dist: %i cm", sensorGetDistance());
display->drawString(0, 45, buf); display->setTextAlignment(TEXT_ALIGN_LEFT);
display->drawString(1, 50, buf);
} }
void displayDrawMailbox(void) void displayDrawMailbox(void)
@@ -145,8 +144,6 @@ void displayInit(void)
initOK = display->init(); initOK = display->init();
display->setBrightness(128); display->setBrightness(128);
display->flipScreenVertically(); display->flipScreenVertically();
display->setFont(ArialMT_Plain_10);
display->drawString(0, 0, "OLED initial done!");
display->display(); display->display();
delay(1000); delay(1000);
Serial.print("."); Serial.print(".");
@@ -160,4 +157,26 @@ void displayInit(void)
return; return;
} }
Serial.println(" done"); Serial.println(" done");
displayWriteLine("Display init done");
}
void displayWriteLine(String text)
{
static uint8_t index=0;
if(text == "")
{
return;
}
display->setFont(ArialMT_Plain_10);
display->setTextAlignment(TEXT_ALIGN_LEFT);
display->drawString(0,index,text);
index += 10;
display->display();
delay(300);
}
void displayshow(void)
{
display->display();
} }

View File

@@ -8,6 +8,8 @@ void displayDrawBatt(uint16_t voltage, bool sleep);
void displayDrawShutdown(void); void displayDrawShutdown(void);
void displayOff(void); void displayOff(void);
void displayOn(void); void displayOn(void);
void displayWriteLine(String text);
void displayshow(void);

View File

@@ -1,7 +1,7 @@
#ifndef HALH #ifndef HALH
#define HALH #define HALH
#define MAXBATT 4200 // The default Lipo is 4200mv when the battery is fully charged. #define MAXBATT 4100 // The default Lipo is 4200mv when the battery is fully charged.
#define LIGHT_SLEEP_VOLTAGE 3750 // Point where start light sleep #define LIGHT_SLEEP_VOLTAGE 3750 // Point where start light sleep
#define MINBATT 3200 // The default Lipo is 3200mv when the battery is empty...this WILL be low on the 3.3v rail specs!!! #define MINBATT 3200 // The default Lipo is 3200mv when the battery is empty...this WILL be low on the 3.3v rail specs!!!

View File

@@ -2,29 +2,62 @@
#include "mailbox.h" #include "mailbox.h"
#include "sensor.h" #include "sensor.h"
#include "hal.h" #include "hal.h"
#include "display.h"
#define MAILCOUNTERFILTER 10 //minimum samples should be equal before triggering mail alert #define MAILCOUNTERFILTER 4 //minimum samples should be equal before triggering mail alert
#define MAILCOUNTERTHRESHOLT 2 //minimum delta distance in cm #define MAILCOUNTERTHRESHOLT 2 //minimum delta distance in cm
uint8_t previousDistance = 0; uint8_t previousDistance = 0;
uint8_t initialDistance = 0;
uint8_t mailcounter = 0; uint8_t mailcounter = 0;
bool mailFlag = false; bool mailFlag = false;
bool mailDetected = false; bool mailDetected = false;
bool mailInitOK = false;
void mailboxInit(void) void mailboxInit(void)
{ {
Serial.print("Mailbox Init");
mailInitOK = true;
initialDistance = sensorGetDistance();
if(initialDistance < 1)
{
Serial.print(" Error: detection < 1cm");
mailInitOK = false;
}
pinMode(MAILLED, OUTPUT); pinMode(MAILLED, OUTPUT);
pinMode(BUTTON, INPUT_PULLUP); pinMode(BUTTON, INPUT_PULLUP);
pinMode(DOORSW, INPUT_PULLUP); pinMode(DOORSW, INPUT_PULLUP);
Serial.println(" Done");
displayWriteLine("Mailbox Init Done");
} }
void mailboxhandler(void) void mailboxhandler(void)
{ {
uint8_t currentDistance = sensorGetDistance(); uint8_t currentDistance = sensorGetDistance();
if (currentDistance < 1 && !mailInitOK)
if (currentDistance == previousDistance)
{ {
return;
}
else
{
mailInitOK = true;
}
if (currentDistance <= previousDistance)
{
}
else if (currentDistance < (previousDistance - MAILCOUNTERTHRESHOLT))
{
mailFlag = true;
mailcounter = 0;
}
else
{
mailFlag = false;
}
//handle mail debounce
if (mailFlag) if (mailFlag)
{ {
if (mailcounter++ >= MAILCOUNTERFILTER) if (mailcounter++ >= MAILCOUNTERFILTER)
@@ -36,16 +69,7 @@ void mailboxhandler(void)
{ {
mailcounter = 0; mailcounter = 0;
} }
}
else if (currentDistance < (previousDistance - MAILCOUNTERTHRESHOLT))
{
mailFlag = true;
mailcounter = 0;
}
else
{
mailFlag = false;
}
previousDistance = currentDistance; previousDistance = currentDistance;
} }
@@ -56,5 +80,4 @@ bool mailboxGetMailDetected(void)
void mailboxCleared(void) void mailboxCleared(void)
{ {
} }

View File

@@ -22,9 +22,11 @@ void setup()
serialInit(); serialInit();
displayInit(); displayInit();
sensorInit(); sensorInit();
//netInit(); netInit();
powerInit(); powerInit();
sleepInit(TIME_TO_SLEEP); sleepInit(TIME_TO_SLEEP);
displayshow();
delay(2000);
displayUpdate(); displayUpdate();
} }

View File

@@ -1,6 +1,7 @@
#include "Arduino.h" #include "Arduino.h"
#include "net.h" #include "net.h"
#include <WiFi.h> #include <WiFi.h>
#include "display.h"
// Replace with your network credentials // Replace with your network credentials
@@ -20,6 +21,8 @@ void netInit(void)
delay(500); delay(500);
} }
Serial.println("done"); Serial.println("done");
displayWriteLine("Network Init Done");
} }
bool netIsConnected( void ) bool netIsConnected( void )

View File

@@ -8,6 +8,7 @@
#if (ENVIRONMENT == TTGO_T18) #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 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) #define VOLTAGEREF 3787 // measured 2,0474 / 2214 ticks (mv)
#define VOLTAGEADC 0.9245
#else #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()) #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())
@@ -74,6 +75,8 @@ void powerInit(void)
//digitalWrite(VEXT, LOW); // ESP32 Lora v2.1 reads on GPIO37 when GPIO21 is low //digitalWrite(VEXT, LOW); // ESP32 Lora v2.1 reads on GPIO37 when GPIO21 is low
//delay(ADC_READ_STABILIZE); // let GPIO stabilize //delay(ADC_READ_STABILIZE); // let GPIO stabilize
Serial.println("Power init done"); Serial.println("Power init done");
displayWriteLine("Power Init Done");
} }
uint16_t powerGetVbatt(void) uint16_t powerGetVbatt(void)
@@ -86,27 +89,27 @@ void powerHandler(void)
voltage = Sample(); voltage = Sample();
//displayDrawBatt(voltage, voltage < LIGHT_SLEEP_VOLTAGE); //displayDrawBatt(voltage, voltage < LIGHT_SLEEP_VOLTAGE);
// if (voltage < MINBATT) if (voltage < MINBATT)
// { // Low Voltage cut off shut down to protect battery as long as possible { // Low Voltage cut off shut down to protect battery as long as possible
// displayDrawShutdown(); displayDrawShutdown();
// delay(2000); delay(2000);
// #if defined(__DEBUG) && __DEBUG > 0 #if defined(__DEBUG) && __DEBUG > 0
// Serial.printf(" !! Shutting down...low battery volotage: %dmV.\n", voltage); Serial.printf(" !! Shutting down...low battery volotage: %dmV.\n", voltage);
// delay(10); delay(10);
// #endif #endif
// esp_sleep_enable_timer_wakeup(LO_BATT_SLEEP_TIME); esp_sleep_enable_timer_wakeup(LO_BATT_SLEEP_TIME);
// esp_deep_sleep_start(); esp_deep_sleep_start();
// } }
// else if (voltage < LIGHT_SLEEP_VOLTAGE) else if (voltage < LIGHT_SLEEP_VOLTAGE)
// { // Use light sleep once on battery { // Use light sleep once on battery
// uint64_t s = VBATT_SAMPLE; uint64_t s = VBATT_SAMPLE;
// #if defined(__DEBUG) && __DEBUG > 0 #if defined(__DEBUG) && __DEBUG > 0
// Serial.printf(" - Light Sleep (%dms)...battery volotage: %dmV.\n", (int)s, voltage); Serial.printf(" - Light Sleep (%dms)...battery volotage: %dmV.\n", (int)s, voltage);
// delay(20); delay(20);
// #endif #endif
// esp_sleep_enable_timer_wakeup(s * 1000); // Light Sleep does not flush buffer esp_sleep_enable_timer_wakeup(s * 1000); // Light Sleep does not flush buffer
// esp_light_sleep_start(); esp_light_sleep_start();
// } }
delay(ADC_READ_STABILIZE); delay(ADC_READ_STABILIZE);
} }
@@ -117,8 +120,8 @@ void powerHandler(void)
// Poll the proper ADC for VBatt on Heltec Lora 32 with GPIO21 toggled // Poll the proper ADC for VBatt on Heltec Lora 32 with GPIO21 toggled
uint16_t ReadVBatt() uint16_t ReadVBatt()
{ {
Serial.println("start read batt"); //Serial.println("start read batt");
int reading = 666; //int reading = 666;
uint16_t rawVoltage; uint16_t rawVoltage;
#if (defined(HELTEC_V2_1)) #if (defined(HELTEC_V2_1))
@@ -131,12 +134,11 @@ uint16_t ReadVBatt()
#elif (ENVIRONMENT == TTGO_T18) #elif (ENVIRONMENT == TTGO_T18)
pinMode(VBATT, ANALOG); // ADC GPIO13 pinMode(VBATT, ANALOG); // ADC GPIO13
reading = analogRead(35); rawVoltage = analogRead(35) * VOLTAGEADC;
Serial.printf("battery analogread = %i\n", reading); //Serial.printf("battery analogread = %i\n", reading);
rawVoltage = float(VOLTAGEREF / 4096) * reading; //Serial.printf("raw voltage = %i\n", rawVoltage);
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 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) #elif (ENVIRONMENT == LOLIN32)
digitalWrite(VEXT, LOW); // ESP32 Lora v2.1 reads on GPIO37 when GPIO21 is low digitalWrite(VEXT, LOW); // ESP32 Lora v2.1 reads on GPIO37 when GPIO21 is low
delay(ADC_READ_STABILIZE); // let GPIO stabilize delay(ADC_READ_STABILIZE); // let GPIO stabilize
@@ -147,7 +149,7 @@ uint16_t ReadVBatt()
#endif #endif
//Serial.printf("battery rawvoltage = %i\n", rawVoltage); //Serial.printf("battery rawvoltage = %i\n", rawVoltage);
rawVoltage *= VOLTAGE_DIVIDER; rawVoltage *= VOLTAGE_DIVIDER;
Serial.printf("battery sample = %i\n", rawVoltage); //Serial.printf("battery voltage = %4.2f\n", rawVoltage/1000);
//digitalWrite(VEXT, HIGH); // ESP32 Lora v2.1 reads on GPIO37 when GPIO21 is low //digitalWrite(VEXT, HIGH); // ESP32 Lora v2.1 reads on GPIO37 when GPIO21 is low
return rawVoltage; return rawVoltage;
@@ -176,9 +178,9 @@ uint16_t Sample()
// ADC read // ADC read
samp[i] = ReadVBatt(); samp[i] = ReadVBatt();
#if defined(__DEBUG) && __DEBUG > 0
Serial.printf("ADC Raw Reading[%d]: %d", i, voltage); //Serial.printf("ADC Raw Reading[%d]: %d", i, voltage);
#endif
t += samp[i]; t += samp[i];
if (++i >= VBATT_SMOOTH) if (++i >= VBATT_SMOOTH)
@@ -186,9 +188,7 @@ uint16_t Sample()
i = 0; i = 0;
} }
uint16_t s = round(((float)t / (float)VBATT_SMOOTH)); uint16_t s = round(((float)t / (float)VBATT_SMOOTH));
#if defined(__DEBUG) && __DEBUG > 0 Serial.printf("Vbatt = %4.3f Volt\n", (double(s)/1000));
Serial.printf(" Smoothed of %d/%d = %d\n", t, VBATT_S MOOTH, s);
#endif
return s; return s;
} }

View File

@@ -2,6 +2,7 @@
#include "sensor.h" #include "sensor.h"
#include <Ultrasonic.h> #include <Ultrasonic.h>
#include "hal.h" #include "hal.h"
#include "display.h"
RTC_DATA_ATTR int prevDistance = 0; RTC_DATA_ATTR int prevDistance = 0;
@@ -13,13 +14,15 @@ void sensorInit(void)
{ {
sensorUpdateDistance(); sensorUpdateDistance();
Serial.println("sensor Init done"); Serial.println("sensor Init done");
displayWriteLine("Sensor Init Done");
} }
void sensorUpdateDistance(void) void sensorUpdateDistance(void)
{ {
distance = ultrasonic.read(); distance = ultrasonic.read();
Serial.printf("distance= %i CM\n",distance); Serial.printf("Distance = %i CM\n",distance);
} }
int sensorGetDistance(void) int sensorGetDistance(void)
@@ -27,7 +30,7 @@ int sensorGetDistance(void)
return distance; return distance;
} }
int sensorGetPreviousDistance(void) // int sensorGetPreviousDistance(void)
{ // {
return prevDistance; // return prevDistance;
} // }

View File

@@ -55,6 +55,8 @@ void sleepInit(unsigned int duration)
esp_sleep_enable_timer_wakeup(duration * uS_TO_S_FACTOR); esp_sleep_enable_timer_wakeup(duration * uS_TO_S_FACTOR);
Serial.println("Setup ESP32 to sleep for every " + String(duration) + Serial.println("Setup ESP32 to sleep for every " + String(duration) +
" Seconds"); " Seconds");
displayWriteLine("Sleep Init Done");
} }
void sleepCallback(void) void sleepCallback(void)