Compare commits

...

5 Commits

Author SHA1 Message Date
dc530fa477 prepare for mqtt and add wifi disconnect 2021-01-13 21:46:39 +01:00
c41bddd71b update mailbox 2021-01-13 13:00:28 +01:00
40a64daf8a added ttgo board en pins 2021-01-12 16:53:11 +01:00
e8724575f0 fix compile bug (include in display module) 2021-01-12 11:31:27 +01:00
f7841ff42d added TTGO board (need to update pins) 2021-01-12 11:30:11 +01:00
14 changed files with 253 additions and 67 deletions

BIN
.DS_Store vendored

Binary file not shown.

BIN
CAD/t18_v3.0.pdf Normal file

Binary file not shown.

View File

@@ -9,7 +9,14 @@
; https://docs.platformio.org/page/projectconf.html
[platformio]
default_envs = heltec_wifi_lora_32_V2
default_envs = ttgo-t18
[env]
lib_deps =
ericksimoes/Ultrasonic@^3.0.0
http://192.168.2.3/Bonobo.Git.Server/OLEDDisplay.git
knolleary/PubSubClient @ ^2.8
[env:heltec_wifi_lora_32_V2]
platform = espressif32
@@ -17,11 +24,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,8 +32,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
monitor_speed = 115200

View File

@@ -7,22 +7,35 @@
#include <wifi.h>
#include "hal.h"
#include "power.h"
#include "mailbox.h"
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)
{
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)
@@ -34,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;
@@ -80,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)
@@ -130,10 +142,8 @@ 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!");
display->display();
delay(1000);
Serial.print(".");
@@ -147,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

@@ -6,6 +6,10 @@ void displayInit(void);
void displayUpdate(void);
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!!!
@@ -12,7 +12,9 @@
#define HELTECv2 1
#define LOLIN32 2
#define HELTECv21 3
#define TTGO_T18 3
#define HELTECv21 4
#if ENVIRONMENT == HELTECv2 //HELTEC WIFI LORA V2
@@ -48,4 +50,22 @@
#endif
#if ENVIRONMENT == TTGO_T18 //LOLIN32
#define SDA_OLED 15
#define SCL_OLED 4
#define RST_OLED 17
#define US100 //alternative sonar
#define TRIGGER 13
#define ECHO 14
#define VBATT ADC1_CHANNEL_7
#define VEXT 21
#define MAILLED 21
#define BUTTON 22
#define DOORSW 23
#endif
#endif //HALH

View File

@@ -1,32 +1,51 @@
#include "Arduino.h"
#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))
{
@@ -37,10 +56,28 @@ void mailboxhandler(void)
{
mailFlag = false;
}
//handle mail debounce
if (mailFlag)
{
if (mailcounter++ >= MAILCOUNTERFILTER)
{
mailDetected = true;
}
}
else
{
mailcounter = 0;
}
previousDistance = currentDistance;
}
bool mailboxGetMailDetected(void)
{
return mailDetected;
}
void mailboxCleared(void)
{
}

View File

@@ -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)
{
@@ -22,9 +22,11 @@ void setup()
serialInit();
displayInit();
sensorInit();
//netInit();
netInit();
powerInit();
//sleepInit(TIME_TO_SLEEP);
sleepInit(TIME_TO_SLEEP);
displayshow();
delay(2000);
displayUpdate();
}
@@ -34,6 +36,6 @@ void loop()
sensorUpdateDistance();
displayUpdate();
mailboxhandler();
//sleepStart();
sleepStart();
delay(1000);
}

9
src/mqtt.cpp Normal file
View File

@@ -0,0 +1,9 @@
#include "Arduino.h"
#include "mqtt.h"
#include "display.h"
void mqttInit(void)
{
displayWriteLine("MQTT init done");
}

7
src/mqtt.h Normal file
View File

@@ -0,0 +1,7 @@
#ifndef MQTTH
#define MQTTH
void mqttInit(void);
#endif //MQTTH

View File

@@ -1,25 +1,36 @@
#include "Arduino.h"
#include "net.h"
#include <WiFi.h>
#include <esp_wifi.h>
#include "display.h"
// Replace with your network credentials
const char *ssid = "poes";
const char *password = "Rijnstraat214";
void netInit(void)
{
Serial.print("Inet init ");
Serial.print("Net: init ");
WiFi.begin(ssid, password);
delay(10);
int counter = 30;
while (WiFi.status() != WL_CONNECTED && counter)
while (WiFi.status() != WL_CONNECTED && counter--)
{
Serial.print(".");
delay(500);
}
Serial.println("done");
if(WiFi.status() != WL_CONNECTED)
{
WiFi.mode(WIFI_OFF);
Serial.println("done");
displayWriteLine("Network: Init Done");
}
else
{
Serial.println("FAILED");
displayWriteLine("Network: Init FAILED");
}
}
bool netIsConnected( void )
@@ -31,3 +42,27 @@ const char* netGetSSID( void )
{
return ssid;
}
bool netConnect(void)
{
return WiFi.mode(WIFI_STA);
WiFi.begin(ssid,password);
if(WiFi.status() != WL_CONNECTED)
{
WiFi.mode(WIFI_OFF);
Serial.println("Net: reconnected");
}
else
{
Serial.println("FAILED");
displayWriteLine("Net: reconncet FAILED");
}
}
bool netDisconnect(void)
{
return WiFi.mode(WIFI_OFF);
}

View File

@@ -5,7 +5,16 @@
#include <driver/adc.h>
#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)
#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())
#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 +24,6 @@
uint16_t Sample();
esp_adc_cal_characteristics_t *adc_chars;
uint16_t voltage = 666;
@@ -27,6 +35,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));
@@ -62,8 +75,9 @@ 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)
{
@@ -99,7 +113,6 @@ void powerHandler(void)
delay(ADC_READ_STABILIZE);
}
// Heltec WiFi LoRa V2 battery read example
// by Jeff McClain jeff@themcclains.net
//
@@ -107,27 +120,39 @@ void powerHandler(void)
// Poll the proper ADC for VBatt on Heltec Lora 32 with GPIO21 toggled
uint16_t ReadVBatt()
{
int reading = 666;
//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
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
#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 voltage = %4.2f\n", rawVoltage/1000);
//digitalWrite(VEXT, HIGH); // ESP32 Lora v2.1 reads on GPIO37 when GPIO21 is low
return rawVoltage;
}
// Use a buffer to average/sample ADC
@@ -153,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)
@@ -163,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

@@ -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 */
@@ -54,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)
@@ -65,7 +68,12 @@ void sleepStart()
Serial.flush();
Serial.println("Going to sleep now");
Serial.flush();
displayOff();
delay(100);
esp_light_sleep_start();
displayOn();
Serial.println("Woke");
}