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

View File

@@ -1,7 +1,7 @@
#ifndef 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 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,40 +2,50 @@
#include "mailbox.h"
#include "sensor.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
uint8_t previousDistance = 0;
uint8_t initialDistance = 0;
uint8_t mailcounter = 0;
bool mailFlag = false;
bool mailDetected = false;
bool mailInitOK = false;
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(BUTTON, INPUT_PULLUP);
pinMode(DOORSW, INPUT_PULLUP);
Serial.println(" Done");
displayWriteLine("Mailbox Init Done");
}
void mailboxhandler(void)
{
uint8_t currentDistance = sensorGetDistance();
if (currentDistance == previousDistance)
if (currentDistance < 1 && !mailInitOK)
{
return;
}
else
{
mailInitOK = true;
}
if (currentDistance <= previousDistance)
{
if (mailFlag)
{
if (mailcounter++ >= MAILCOUNTERFILTER)
{
mailDetected = true;
}
}
else
{
mailcounter = 0;
}
}
else if (currentDistance < (previousDistance - MAILCOUNTERTHRESHOLT))
{
@@ -46,6 +56,20 @@ void mailboxhandler(void)
{
mailFlag = false;
}
//handle mail debounce
if (mailFlag)
{
if (mailcounter++ >= MAILCOUNTERFILTER)
{
mailDetected = true;
}
}
else
{
mailcounter = 0;
}
previousDistance = currentDistance;
}
@@ -56,5 +80,4 @@ bool mailboxGetMailDetected(void)
void mailboxCleared(void)
{
}

View File

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

View File

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

View File

@@ -8,6 +8,7 @@
#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)
#define VOLTAGEADC 0.9245
#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())
@@ -74,6 +75,8 @@ void powerInit(void)
//digitalWrite(VEXT, LOW); // ESP32 Lora v2.1 reads on GPIO37 when GPIO21 is low
//delay(ADC_READ_STABILIZE); // let GPIO stabilize
Serial.println("Power init done");
displayWriteLine("Power Init Done");
}
uint16_t powerGetVbatt(void)
@@ -86,27 +89,27 @@ 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);
}
@@ -117,8 +120,8 @@ 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;
//Serial.println("start read batt");
//int reading = 666;
uint16_t rawVoltage;
#if (defined(HELTEC_V2_1))
@@ -131,12 +134,11 @@ uint16_t ReadVBatt()
#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);
rawVoltage = analogRead(35) * VOLTAGEADC;
//Serial.printf("battery analogread = %i\n", 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
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
@@ -147,7 +149,7 @@ uint16_t ReadVBatt()
#endif
//Serial.printf("battery rawvoltage = %i\n", rawVoltage);
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
return rawVoltage;
@@ -176,9 +178,9 @@ uint16_t Sample()
// ADC read
samp[i] = ReadVBatt();
#if defined(__DEBUG) && __DEBUG > 0
Serial.printf("ADC Raw Reading[%d]: %d", i, voltage);
#endif
//Serial.printf("ADC Raw Reading[%d]: %d", i, voltage);
t += samp[i];
if (++i >= VBATT_SMOOTH)
@@ -186,9 +188,7 @@ uint16_t Sample()
i = 0;
}
uint16_t s = round(((float)t / (float)VBATT_SMOOTH));
#if defined(__DEBUG) && __DEBUG > 0
Serial.printf(" Smoothed of %d/%d = %d\n", t, VBATT_S MOOTH, s);
#endif
Serial.printf("Vbatt = %4.3f Volt\n", (double(s)/1000));
return s;
}

View File

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

View File

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