added otr support

This commit is contained in:
2021-06-13 14:08:15 +02:00
parent f338ddc386
commit 163ee7b8df
5 changed files with 4332 additions and 73 deletions

1384
default_8MB.csv Normal file

File diff suppressed because one or more lines are too long

1375
huge_app.csv Normal file

File diff suppressed because one or more lines are too long

1384
min_spiffs.csv Normal file

File diff suppressed because one or more lines are too long

View File

@@ -12,4 +12,15 @@
platform = espressif32
board = esp32dev
framework = arduino
upload_port = COM[6]
monitor_speed = 115200
; upload_port = COM[6]
upload_protocol = espota
upload_port = 192.168.2.92
; board_build.partitions = huge_app.csv
board_build.partitions = min_spiffs.csv
; [env:custom_table]
; platform = espressif32
; board = esp32dev
; board_build.partitions = default_8MB.csv

View File

@@ -6,13 +6,30 @@
* Website: www.circuitdigest.com
*/
#include <EEPROM.h>
#define EEPROM_SIZE 1
#include <BLE2902.h>
#include <BLEDevice.h>
#include <BLEServer.h> //Library to use BLE as server
#include <BLEUtils.h>
#include <ArduinoOTA.h>
#include <ESPmDNS.h>
#include <WiFi.h>
#include <WiFiUdp.h>
#define STATUS_LED 13
#define ALARM_LED 33
#define MODE_ADDRESS 0
#define MODE_BLE 0
#define MODE_WIFI 1
//#define TESTMODE 1
const char * ssid = "FRITZ!Box 7362 SL";
const char * password = "SP-248464364";
bool _BLEClientConnected = false;
#define AlarmService BLEUUID((uint16_t)0x1811) // ialert notification 0x1802
@@ -23,7 +40,7 @@ BLEServer * pServer = NULL;
bool deviceConnected = false;
bool oldDeviceConnected = false;
uint32_t value = 0;
uint8_t mode;
class MyServerCallbacks : public BLEServerCallbacks {
void onConnect(BLEServer * pServer) {
deviceConnected = true;
@@ -37,17 +54,30 @@ class MyServerCallbacks : public BLEServerCallbacks {
};
// uint8_t toggle = 0;
uint8_t alarmFired = 0;
int state = 0;
class MyAlarmCallback : public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic * pCharacteristic) {
std::string value = pCharacteristic->getValue();
for (int i = 0; i < value.length(); i++) {
Serial.print(value[i]);
}
Serial.println("");
if (value.compare("X") == 0) {
Serial.println("found X");
alarmFired = 1;
// if (toggle) {
// digitalWrite(32, HIGH);
// toggle = 0;
// }
// else {
// digitalWrite(32, LOW);
// toggle = 1;
// }
state = 0;
}
else if (value.compare("U") == 0) {
// reboot in WifiMode
Serial.println("Found U");
EEPROM.write(MODE_ADDRESS, MODE_WIFI);
EEPROM.commit();
// reboot
ESP.restart();
}
}
};
@@ -87,6 +117,16 @@ void IRAM_ATTR onTimer() {
}
void setup() {
Serial.begin(115200);
EEPROM.begin(EEPROM_SIZE);
// EEPROM.write(MODE_ADDRESS, MODE_BLE);
// EEPROM.commit();
mode = EEPROM.read(MODE_ADDRESS);
if (mode == MODE_BLE) {
// upcounting timer 1MHz
timer = timerBegin(0, 40, true);
timerAttachInterrupt(timer, &onTimer, true);
@@ -94,23 +134,87 @@ void setup() {
timerAlarmEnable(timer);
// blink led
pinMode(32, OUTPUT);
pinMode(ALARM_LED, OUTPUT);
// status led
pinMode(33, OUTPUT);
pinMode(STATUS_LED, OUTPUT);
Serial.begin(115200);
Serial.println("Start BLE");
InitBLE();
}
else if (mode == MODE_WIFI) {
// boot next time into ble mode
EEPROM.write(MODE_ADDRESS, MODE_BLE);
EEPROM.commit();
Serial.println("Booting in Wifimode");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
// Serial.println("Connection Failed! Rebooting...");
delay(1000);
// ESP.restart();
}
ArduinoOTA
.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH)
type = "sketch";
else // U_SPIFFS
type = "filesystem";
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using
// SPIFFS.end()
Serial.println("Start updating " + type);
})
.onEnd([]() {
Serial.println("\nEnd");
// EEPROM.write(MODE_ADDRESS, MODE_BLE);
// EEPROM.commit();
})
.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
})
.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR)
Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR)
Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR)
Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR)
Serial.println("Receive Failed");
else if (error == OTA_END_ERROR)
Serial.println("End Failed");
});
ArduinoOTA.begin();
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
// WiFi.mode(WIFI_STA);
// WiFi.begin(ssid, password);
// while (WiFi.waitForConnectResult() != WL_CONNECTED) {
// delay(3000);
// break;
// }
// ArduinoOTA.begin();
}
std::string x;
int state = 0;
void loop() {
if (mode == MODE_WIFI) {
// ota update mode
ArduinoOTA.handle();
}
else {
// BatteryLevelCharacteristic.setValue(&level, 1);
// BatteryLevelCharacteristic.notify();
// x = AlarmCharacteristic.getValue();
// BLE mode
// occures every 100ms
if (ctr > 0) {
@@ -119,19 +223,19 @@ void loop() {
portEXIT_CRITICAL(&timerMux);
#if defined TESTMODE
if (state % 2 == 0) {
digitalWrite(32, HIGH);
digitalWrite(ALARM_LED, HIGH);
}
else {
digitalWrite(32, LOW);
digitalWrite(ALARM_LED, LOW);
}
state++;
#else
if (alarmFired) {
if (state % 2 == 0) {
digitalWrite(32, HIGH);
digitalWrite(ALARM_LED, HIGH);
}
else {
digitalWrite(32, LOW);
digitalWrite(ALARM_LED, LOW);
}
state++;
}
@@ -146,16 +250,16 @@ void loop() {
}
if (deviceConnected) {
digitalWrite(33, HIGH);
digitalWrite(STATUS_LED, HIGH);
// //pCharacteristic->setValue((uint8_t*)&value, 4);
// pCharacteristic->notify();
// value++;
delay(10); // bluetooth stack will go into congestion, if too many packets are sent, in 6
// hours test i was able to go as low as 3ms
delay(10); // bluetooth stack will go into congestion, if too many packets are sent, in
// 6 hours test i was able to go as low as 3ms
}
// disconnecting
if (!deviceConnected && oldDeviceConnected) {
digitalWrite(33, LOW);
digitalWrite(STATUS_LED, LOW);
delay(500); // give the bluetooth stack the chance to get things ready
pServer->startAdvertising(); // restart advertising
Serial.println("start advertising");
@@ -169,3 +273,4 @@ void loop() {
delay(500);
}
}