Hi @gilles99,
You have to change the Partition Scheme to “No OTA (Large APP)” as you can see it in my first post screen.
But in case your partition setup is not good, you shouldn’t be able to upload the sketch (so you shouldn’t even have a CPU restart).
I noticed some crash events, or device stuck issues while using the codes from the first post.
You will find below the last version code I use, it is working well for 1 week now without any problem. All you need to do is to use BLEClient.cpp from Chegewara which fixes connection issues : https://gist.github.com/chegewara/d3ed0e3365407f6d3abcffc486b2c462
#define BLYNK_PRINT Serial // Defines the object that is used for printing
#define BLYNK_DEBUG // Optional, this enables more detailed prints
#include "BLEDevice.h"
#define FLORA_ADDR "c4:7c:8d:66:e9:11"
#define BLYNK_NO_BUILTIN // Disable built-in analog & digital pin operations
#include <Time.h>
#include <TimeLib.h>
#include "WiFi.h"
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP 120 /* Time ESP32 will go to sleep (in seconds) */
BlynkTimer timer;
BLEClient* pClient;
WidgetBridge bridge1(V10); //Initiating Bridge Widget on V1 of Device LolinD32
//**********TOKEN (OK)**********//
char auth[] = "****************************";
//**********WIFI (OK)**********//
char ssid[] = "*************************";
char pass[] = "************************";
// The remote service we wish to connect to.
static BLEUUID serviceUUID("00001204-0000-1000-8000-00805f9b34fb");
// The characteristic of the remote service we are interested in.
static BLEUUID uuid_sensor_data("00001a01-0000-1000-8000-00805f9b34fb");
static BLEUUID uuid_write_mode("00001a00-0000-1000-8000-00805f9b34fb");
static BLEAddress floraAddress(FLORA_ADDR);
static BLERemoteCharacteristic* pRemoteCharacteristic;
float temp;
float moisture;
float light;
int conductivity;
int freeHeapstart;
int freeHeapstop;
int c;
void getSensorData(BLEAddress pAddress){
btStart();
timer.setTimeout(30000L, deepsleep);
Serial.print("=================== Forming a connection to Flora device ====================");
Serial.println(pAddress.toString().c_str());
Serial.println(" - Connection to Flora");
if (!pClient->connect(pAddress)){
pClient->disconnect();
delay(500);
Serial.println(" - Cannot connect to Flora");
ESP.restart();
} else {
Serial.println(" - Connected to Flora");
}
// Obtain a reference to the service we are after in the remote BLE server.
BLERemoteService* pRemoteService = pClient->getService(serviceUUID);
if (pRemoteService == nullptr) {
Serial.print("Failed to find our service UUID: ");
Serial.println(serviceUUID.toString().c_str());
} else {
Serial.println(" - Found our service");
}
pRemoteCharacteristic = pRemoteService->getCharacteristic(uuid_write_mode);
uint8_t buf[2] = {0xA0, 0x1F};
pRemoteCharacteristic->writeValue(buf, 2, true);
// Obtain a reference to the characteristic in the service of the remote BLE server.
pRemoteCharacteristic = pRemoteService->getCharacteristic(uuid_sensor_data);
Serial.println(pRemoteService->toString().c_str());
if (pRemoteCharacteristic == nullptr) {
Serial.print("Failed to find our characteristic UUID: ");
Serial.println(uuid_sensor_data.toString().c_str());
} else {
Serial.println(" - Found our characteristic");
}
// Read the value of the characteristic.
std::string value = pRemoteCharacteristic->readValue();
Serial.print("The characteristic value was: ");
const char *val = value.c_str();
Serial.print("Hex: ");
for (int i = 0; i < 16; i++) {
Serial.print((int)val[i], HEX);
Serial.print(" ");
}
Serial.println(" ");
temp = (val[0] + val[1] * 256) / ((float)10.0);
moisture = val[7];
light = val[3] + val[4] * 256;
conductivity = val[8] + val[9] * 256;
char buffer[64];
temp = float(temp);
moisture = float(moisture);
light = float(light);
conductivity = float(conductivity);
Serial.print("Temperature: ");
Serial.println(temp);
Serial.print("Moisture: ");
Serial.println(moisture);
Serial.print("Light: ");
Serial.println(light);
Serial.print("Conductivity: ");
Serial.println(conductivity);
pClient->disconnect();
delay(500);
btStop();
delay(500);
sendSensorData();
}
void sendSensorData() {
Blynk.begin(auth, ssid, pass);
do
{
Blynk.connect();
} while (!Blynk.connected());
bridge1.setAuthToken("****************************");
Blynk.virtualWrite(V6, temp);
Blynk.virtualWrite(V7, moisture);
bridge1.virtualWrite(V11, temp);
bridge1.virtualWrite(V12, moisture);
delay(1000);
Blynk.disconnect();
WiFi.mode(WIFI_OFF);
freeHeapstop = ESP.getFreeHeap();
Serial.print("FREEHEAP END : ");
Serial.println(freeHeapstop);
ESP.restart();
}
void getSensorData1(){
getSensorData(floraAddress);
}
void deepsleep(){
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
delay(200);
esp_deep_sleep_start();
}
void setup() {
Serial.begin(115200);
Serial.println("Starting Flora client...");
delay(500);
BLEDevice::init("");
pClient = BLEDevice::createClient();
delay(500);
WiFi.mode(WIFI_OFF);
timer.setTimeout(102000L, getSensorData1);
}
void loop() {
timer.run();
}