* Converted EEPROM library to use nvs instead of partition. Removed eeprom partition from all partition table CSV files.
+* Changed variable names, added some comments, formatting as per me-no-dev's requests
+* Checks for memory on malloc
+* Moved include nvs.h from header to code
+* Reworked the extra example to make it more clear how to actually use the library and persist data
+
+
+ You can’t perform that action at this time.
+
+
+
+
+ You signed in with another tab or window. Reload to refresh your session.
+ You signed out in another tab or window. Reload to refresh your session.
+
* Converted EEPROM library to use nvs instead of partition. Removed eeprom partition from all partition table CSV files.
+* Changed variable names, added some comments, formatting as per me-no-dev's requests
+* Checks for memory on malloc
+* Moved include nvs.h from header to code
+* Reworked the extra example to make it more clear how to actually use the library and persist data
+
+
+ You can’t perform that action at this time.
+
+
+
+
+ You signed in with another tab or window. Reload to refresh your session.
+ You signed out in another tab or window. Reload to refresh your session.
+
* Converted EEPROM library to use nvs instead of partition. Removed eeprom partition from all partition table CSV files.
+* Changed variable names, added some comments, formatting as per me-no-dev's requests
+* Checks for memory on malloc
+* Moved include nvs.h from header to code
+* Reworked the extra example to make it more clear how to actually use the library and persist data
+
+
+ You can’t perform that action at this time.
+
+
+
+
+ You signed in with another tab or window. Reload to refresh your session.
+ You signed out in another tab or window. Reload to refresh your session.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/platformio.ini b/platformio.ini
index 225c53c..0d0476e 100644
--- a/platformio.ini
+++ b/platformio.ini
@@ -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
diff --git a/src/main.cpp b/src/main.cpp
index 1981157..bf3ad2f 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -6,13 +6,30 @@
* Website: www.circuitdigest.com
*/
+#include
+#define EEPROM_SIZE 1
+
#include
#include
#include //Library to use BLE as server
#include
+#include
+#include
+#include
+#include
+
+#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) {
- alarmFired = 1;
- // if (toggle) {
- // digitalWrite(32, HIGH);
- // toggle = 0;
- // }
- // else {
- // digitalWrite(32, LOW);
- // toggle = 1;
- // }
+ 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;
+ 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() {
- // 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.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;
-int state = 0;
+
void loop() {
+ if (mode == MODE_WIFI) {
+ // ota update mode
+ ArduinoOTA.handle();
+ }
+ else {
- // BatteryLevelCharacteristic.setValue(&level, 1);
- // BatteryLevelCharacteristic.notify();
+ // BLE mode
- // x = AlarmCharacteristic.getValue();
-
- // occures every 100ms
- if (ctr > 0) {
- portENTER_CRITICAL(&timerMux);
- ctr--;
- portEXIT_CRITICAL(&timerMux);
+ // occures every 100ms
+ if (ctr > 0) {
+ portENTER_CRITICAL(&timerMux);
+ ctr--;
+ portEXIT_CRITICAL(&timerMux);
#if defined TESTMODE
- if (state % 2 == 0) {
- digitalWrite(32, HIGH);
- }
- else {
- digitalWrite(32, 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++;
- }
+#else
+ if (alarmFired) {
+ if (state % 2 == 0) {
+ digitalWrite(ALARM_LED, HIGH);
+ }
+ else {
+ digitalWrite(ALARM_LED, LOW);
+ }
+ state++;
+ }
#endif
- if (state == 22) {
- state = 0;
+ if (state == 22) {
+ state = 0;
#if defined TESTMODE
- ctr = -20;
+ ctr = -20;
#endif
- alarmFired = 0;
+ alarmFired = 0;
+ }
}
- }
- if (deviceConnected) {
- digitalWrite(33, 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
- }
- // disconnecting
- if (!deviceConnected && oldDeviceConnected) {
- digitalWrite(33, LOW);
- delay(500); // give the bluetooth stack the chance to get things ready
- pServer->startAdvertising(); // restart advertising
- Serial.println("start advertising");
- oldDeviceConnected = deviceConnected;
- }
- // connecting
- if (deviceConnected && !oldDeviceConnected) {
- // do stuff here on connecting
- oldDeviceConnected = deviceConnected;
- }
+ if (deviceConnected) {
+ 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
+ }
+ // disconnecting
+ if (!deviceConnected && oldDeviceConnected) {
+ digitalWrite(STATUS_LED, LOW);
+ delay(500); // give the bluetooth stack the chance to get things ready
+ pServer->startAdvertising(); // restart advertising
+ Serial.println("start advertising");
+ oldDeviceConnected = deviceConnected;
+ }
+ // connecting
+ if (deviceConnected && !oldDeviceConnected) {
+ // do stuff here on connecting
+ oldDeviceConnected = deviceConnected;
+ }
- delay(500);
+ delay(500);
+ }
}
\ No newline at end of file