added otr support
This commit is contained in:
1384
default_8MB.csv
Normal file
1384
default_8MB.csv
Normal file
File diff suppressed because one or more lines are too long
1375
huge_app.csv
Normal file
1375
huge_app.csv
Normal file
File diff suppressed because one or more lines are too long
1384
min_spiffs.csv
Normal file
1384
min_spiffs.csv
Normal file
File diff suppressed because one or more lines are too long
@@ -12,4 +12,15 @@
|
|||||||
platform = espressif32
|
platform = espressif32
|
||||||
board = esp32dev
|
board = esp32dev
|
||||||
framework = arduino
|
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
|
||||||
|
|||||||
249
src/main.cpp
249
src/main.cpp
@@ -6,13 +6,30 @@
|
|||||||
* Website: www.circuitdigest.com
|
* Website: www.circuitdigest.com
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <EEPROM.h>
|
||||||
|
#define EEPROM_SIZE 1
|
||||||
|
|
||||||
#include <BLE2902.h>
|
#include <BLE2902.h>
|
||||||
#include <BLEDevice.h>
|
#include <BLEDevice.h>
|
||||||
#include <BLEServer.h> //Library to use BLE as server
|
#include <BLEServer.h> //Library to use BLE as server
|
||||||
#include <BLEUtils.h>
|
#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
|
//#define TESTMODE 1
|
||||||
|
|
||||||
|
const char * ssid = "FRITZ!Box 7362 SL";
|
||||||
|
const char * password = "SP-248464364";
|
||||||
|
|
||||||
bool _BLEClientConnected = false;
|
bool _BLEClientConnected = false;
|
||||||
|
|
||||||
#define AlarmService BLEUUID((uint16_t)0x1811) // ialert notification 0x1802
|
#define AlarmService BLEUUID((uint16_t)0x1811) // ialert notification 0x1802
|
||||||
@@ -23,7 +40,7 @@ BLEServer * pServer = NULL;
|
|||||||
bool deviceConnected = false;
|
bool deviceConnected = false;
|
||||||
bool oldDeviceConnected = false;
|
bool oldDeviceConnected = false;
|
||||||
uint32_t value = 0;
|
uint32_t value = 0;
|
||||||
|
uint8_t mode;
|
||||||
class MyServerCallbacks : public BLEServerCallbacks {
|
class MyServerCallbacks : public BLEServerCallbacks {
|
||||||
void onConnect(BLEServer * pServer) {
|
void onConnect(BLEServer * pServer) {
|
||||||
deviceConnected = true;
|
deviceConnected = true;
|
||||||
@@ -37,17 +54,30 @@ class MyServerCallbacks : public BLEServerCallbacks {
|
|||||||
};
|
};
|
||||||
// uint8_t toggle = 0;
|
// uint8_t toggle = 0;
|
||||||
uint8_t alarmFired = 0;
|
uint8_t alarmFired = 0;
|
||||||
|
int state = 0;
|
||||||
class MyAlarmCallback : public BLECharacteristicCallbacks {
|
class MyAlarmCallback : public BLECharacteristicCallbacks {
|
||||||
void onWrite(BLECharacteristic * pCharacteristic) {
|
void onWrite(BLECharacteristic * pCharacteristic) {
|
||||||
alarmFired = 1;
|
std::string value = pCharacteristic->getValue();
|
||||||
// if (toggle) {
|
|
||||||
// digitalWrite(32, HIGH);
|
for (int i = 0; i < value.length(); i++) {
|
||||||
// toggle = 0;
|
Serial.print(value[i]);
|
||||||
// }
|
}
|
||||||
// else {
|
Serial.println("");
|
||||||
// digitalWrite(32, LOW);
|
|
||||||
// toggle = 1;
|
if (value.compare("X") == 0) {
|
||||||
// }
|
Serial.println("found X");
|
||||||
|
alarmFired = 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,85 +117,160 @@ void IRAM_ATTR onTimer() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
// upcounting timer 1MHz
|
|
||||||
timer = timerBegin(0, 40, true);
|
|
||||||
timerAttachInterrupt(timer, &onTimer, true);
|
|
||||||
timerAlarmWrite(timer, 100000, true);
|
|
||||||
timerAlarmEnable(timer);
|
|
||||||
|
|
||||||
// blink led
|
|
||||||
pinMode(32, OUTPUT);
|
|
||||||
// status led
|
|
||||||
pinMode(33, OUTPUT);
|
|
||||||
|
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
Serial.println("Start BLE");
|
|
||||||
InitBLE();
|
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);
|
||||||
|
timerAlarmWrite(timer, 100000, true);
|
||||||
|
timerAlarmEnable(timer);
|
||||||
|
|
||||||
|
// blink led
|
||||||
|
pinMode(ALARM_LED, OUTPUT);
|
||||||
|
// status led
|
||||||
|
pinMode(STATUS_LED, OUTPUT);
|
||||||
|
|
||||||
|
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;
|
std::string x;
|
||||||
int state = 0;
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
if (mode == MODE_WIFI) {
|
||||||
|
// ota update mode
|
||||||
|
ArduinoOTA.handle();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
|
||||||
// BatteryLevelCharacteristic.setValue(&level, 1);
|
// BLE mode
|
||||||
// BatteryLevelCharacteristic.notify();
|
|
||||||
|
|
||||||
// x = AlarmCharacteristic.getValue();
|
// occures every 100ms
|
||||||
|
if (ctr > 0) {
|
||||||
// occures every 100ms
|
portENTER_CRITICAL(&timerMux);
|
||||||
if (ctr > 0) {
|
ctr--;
|
||||||
portENTER_CRITICAL(&timerMux);
|
portEXIT_CRITICAL(&timerMux);
|
||||||
ctr--;
|
|
||||||
portEXIT_CRITICAL(&timerMux);
|
|
||||||
#if defined TESTMODE
|
#if defined TESTMODE
|
||||||
if (state % 2 == 0) {
|
|
||||||
digitalWrite(32, HIGH);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
digitalWrite(32, LOW);
|
|
||||||
}
|
|
||||||
state++;
|
|
||||||
#else
|
|
||||||
if (alarmFired) {
|
|
||||||
if (state % 2 == 0) {
|
if (state % 2 == 0) {
|
||||||
digitalWrite(32, HIGH);
|
digitalWrite(ALARM_LED, HIGH);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
digitalWrite(32, LOW);
|
digitalWrite(ALARM_LED, LOW);
|
||||||
}
|
}
|
||||||
state++;
|
state++;
|
||||||
}
|
#else
|
||||||
|
if (alarmFired) {
|
||||||
|
if (state % 2 == 0) {
|
||||||
|
digitalWrite(ALARM_LED, HIGH);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
digitalWrite(ALARM_LED, LOW);
|
||||||
|
}
|
||||||
|
state++;
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
if (state == 22) {
|
if (state == 22) {
|
||||||
state = 0;
|
state = 0;
|
||||||
#if defined TESTMODE
|
#if defined TESTMODE
|
||||||
ctr = -20;
|
ctr = -20;
|
||||||
#endif
|
#endif
|
||||||
alarmFired = 0;
|
alarmFired = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (deviceConnected) {
|
if (deviceConnected) {
|
||||||
digitalWrite(33, HIGH);
|
digitalWrite(STATUS_LED, HIGH);
|
||||||
// //pCharacteristic->setValue((uint8_t*)&value, 4);
|
// //pCharacteristic->setValue((uint8_t*)&value, 4);
|
||||||
// pCharacteristic->notify();
|
// pCharacteristic->notify();
|
||||||
// value++;
|
// value++;
|
||||||
delay(10); // bluetooth stack will go into congestion, if too many packets are sent, in 6
|
delay(10); // bluetooth stack will go into congestion, if too many packets are sent, in
|
||||||
// hours test i was able to go as low as 3ms
|
// 6 hours test i was able to go as low as 3ms
|
||||||
}
|
}
|
||||||
// disconnecting
|
// disconnecting
|
||||||
if (!deviceConnected && oldDeviceConnected) {
|
if (!deviceConnected && oldDeviceConnected) {
|
||||||
digitalWrite(33, LOW);
|
digitalWrite(STATUS_LED, LOW);
|
||||||
delay(500); // give the bluetooth stack the chance to get things ready
|
delay(500); // give the bluetooth stack the chance to get things ready
|
||||||
pServer->startAdvertising(); // restart advertising
|
pServer->startAdvertising(); // restart advertising
|
||||||
Serial.println("start advertising");
|
Serial.println("start advertising");
|
||||||
oldDeviceConnected = deviceConnected;
|
oldDeviceConnected = deviceConnected;
|
||||||
}
|
}
|
||||||
// connecting
|
// connecting
|
||||||
if (deviceConnected && !oldDeviceConnected) {
|
if (deviceConnected && !oldDeviceConnected) {
|
||||||
// do stuff here on connecting
|
// do stuff here on connecting
|
||||||
oldDeviceConnected = deviceConnected;
|
oldDeviceConnected = deviceConnected;
|
||||||
}
|
}
|
||||||
|
|
||||||
delay(500);
|
delay(500);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user