After connecting to Blink Server, device seem loosing connection and "Cmd skipped:20" on virtualWrite

Hi,

I have started using Blink yesterday and could make fast improvements here with FreeRTOS. (Thanks @CampHamp for the examples!). But I had problems…

=> I have Blynk connection at beginning (log below), that I suppose does not hold for long and I can not “virtualWrite” to Blynk server.

Well, my device (ESP32-S3 - LillyGo T-Display S3) is connected to 8 Dallas Temperature sensors (One Wire), a SD card (SPI) and a DS3231 RTC (I2C), running a sketch with FreeRTOS quite well.

I tried to implement Blynk to see if I can, besides of locally logging data on SDcard, also monitor it from distance.

The project uses the very precise 1Hz SquareWave of DS3231 in a GPIOpin attached to an Interruption. The ISR notify the task for sensors measurements every second. Another task collect all sensors data and calculate the one minute average which is then logged into a file and release for data show (TFT and Serial).

After introducing Blink in the project, I experience crashes that I could solve putting Blink.run() inside a a proper looptask pinned to Core 0 and the oneWire communication in Core1 with higher priority. The other tasks for data Logging (the highest priority), TFT-display and Serial monitor( lowest priority) are not pinned to any core (the FreeRTOS scheduler decides de core).

Every minute the LogTask, after logging on SD card, try to send 5 Temperatures values (floats) to Blynk server. So, it is not fooding the server with data.

The sketch is big, has many functions and some tasksFuntions, but I post the critical code parts below. Initialisation code seems ok since I get inicial connection.

I hope to have some help!

The serial monitor logs:
// INITIAL CONNECTION SUCCESSFUL

[2212] Connecting to Darwin
[3765] Connected to WiFi
[3765] IP: 10.10.10.56
[3766] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v1.3.2 on ESP32-S3

 #StandWithUkraine    https://bit.ly/swua

[3767] Connecting to blynk.cloud:80
[4013] <[1D|00|01|00|20]eOrm9i5y90gUxQIAQs1XIc0O_M5LIhNR
[4221] >[00|00|01|00|C8]
[4221] Ready (ping: 206ms).
[4221] Free RAM: 280616
[4221] Waiting:66
[4288] <[11|00|02|00]}mcu[00]0.0.0[00]fw-type[00]TMPL20ZbxYhJ7[00]build[00]May[20|20]1[20]2024[20]12:53:04[00]blynk[00]1.3.2[00]h-beat[00]45[00]buff-in[00]1024[00]dev[00]ESP32-S3[00]tmpl[00]TMPL20ZbxYhJ7

DS3231 interruption attached
Blynk Loop Task Started
01 - 30.12  30.38  30.31  30.50  30.31  30.38  30.19  30.06  4.58  
02 - 30.12  30.38  30.31  30.50  30.31  30.38  30.12  30.06  4.58  
03 - 30.12  30.31  30.31  30.50  30.31  30.38  30.19  30.06  4.60  
04 - 30.12  30.38  30.31  30.50  30.31  30.38  30.19  30.06  4.60  
05 - 30.12  30.38  30.31  30.50  30.31  30.38  30.19  30.06  4.55  
[11232] Connecting to blynk.cloud:80                                                    <== LOST CONNECTION?
[11232] Connecting to blynk.cloud:8080
06 - 30.12  30.31  30.31  30.50  30.31  30.38  30.19  30.00  4.58  
07 - 30.12  30.38  30.31  30.50  30.31  30.38  30.12  30.06  4.58 
08 - 30.12  30.31  30.38  30.50  30.31  30.38  30.19  30.06  4.58  
09 - 30.12  30.38  30.31  30.50  30.31  30.38  30.19  30.06  4.58  
10 - 30.12  30.31  30.31  30.50  30.31  30.38  30.12  30.06  4.53 

// AFTER 60 MEASUREMENTS...

55 - 30.19  30.38  30.31  30.44  30.31  30.38  30.19  30.06  4.58
[61242] Connecting to blynk.cloud:80
[61242] Connecting to blynk.cloud:8080
56 - 30.19  30.38  30.31  30.50  30.31  30.38  30.19  30.06  4.58  
57 - 30.19  30.38  30.31  30.50  30.31  30.38  30.19  30.06  4.58  
58 - 30.19  30.38  30.31  30.50  30.31  30.38  30.12  30.06  4.58  
59 - 30.19  30.38  30.31  30.50  30.31  30.38  30.12  30.06  4.60  
60 - 30.19  30.38  30.31  30.50  30.31  30.38  30.19  30.06  4.60  

Avg  30.15 30.36 30.32 30.49 30.31 30.37 30.17 30.06  (°C) 2024-05-01T16:06:47

 - Blink Data - 
[65800] Cmd skipped:20
[65800] Cmd skipped:20
[65800] Cmd skipped:20
[65800] Cmd skipped:20
[65800] Cmd skipped:20
  - Blynk data - .

SOME CODE

// SETUP CODE
void setup() {
  Serial.begin(115200);
  delay(2000);
  Blynk.begin(BLYNK_AUTH_TOKEN, client_ssid, client_password);
  while (!Blynk.connected());
  if (!Blynk.connected()){
    debugln();
    debugln ("Blynk not connected");
  } else {
    debugln();
    debugln(" Blynk Connected!");
  }

  //PINS
  pinMode(LCD_POWER_ON, OUTPUT);
  digitalWrite(LCD_POWER_ON, HIGH);
  pinMode(BUTTON_1, INPUT_PULLUP);
  pinMode(BUTTON_2, INPUT_PULLUP);
  pinMode(BAT_VOLT, INPUT);
  
  //ADC at GPIO_NUM_4 -> ADC_CHANNEL_3
  adc1_config_width(ADC_WIDTH_BIT_12);
  adc1_config_channel_atten(ADC1_CHANNEL_3, ADC_ATTEN_11db);
  esp_adc_cal_characterize(ADC_UNIT_1, ADC_ATTEN_11db, ADC_WIDTH_BIT_12, 1100, &adc_chars);
  

  SplashSerialScreen(); // ESP32 infos
  setup_DS3231_RTC(); //Real Time Clock and 1Hz square wave generator
  
  // Connect to Wifi and set the time. Just once
  //WiFi_ConnectAsClient();
  //Adjust_RTC_byNPT();
  WiFi_Disconnect();
 
  //PSRAM init
  if(psramInit()) debugln(" PSRAM - ok");  
  else debugln("\nPSRAM NOT ok");
        
  // Screen
  tftSetup();
  TFTdimming(4);

  //SD-Card
  Setup_SD_FS ();
  Create_Log_File();

  //Dallas Sensors DS18B20
  setupTemperatureSensors(); 

  //RTOS tasks
  egTheEvents_h = xEventGroupCreate();
  xTaskCreatePinnedToCore(BlynkLoop, "blynk", 100000, NULL, 1, NULL, 0);
  xTaskCreatePinnedToCore(xReadTempSensors, "ReadTempSensors", 5*512, NULL, 5, &ReadTempSensors_h, 1); // Use address of handle = &handles
  xTaskCreate(xAverage, "Avergage Sensors", 2*512, NULL, 6, &xAverage_h );
  xTaskCreate(xDrawScreen, "DrawScreen", 6*512, NULL, 2, &xDrawScreen_h); 
  xTaskCreate(xPrintTemperatures, "PrintTemperatures", 10*512, NULL, 1, &xPrintTemperatures_h);
  xTaskCreate(xLogData, "LogData", 5*1024, NULL, 4, &LogData_h);  
  
  // Interrupts for DS3231 at GPIO_NUM_44
  attachInterrupt(GPIO_NUM_44,  OneSecondTimer_ISR, FALLING); // External Hardware timer:
  onRecord = true;
  // Blink timer produced the same errors, so I prefer the Interrupt synch

  // Button handling using OnButton Library
  Button1.attachClick(Button1_Click);
  Button2.attachClick(Button2_Click);
  Button2.attachLongPressStop(Button2_LongClick);
} 

void loop() { //spooling the buttons
  Button1.tick();
  Button2.tick();
}

// ISR LINKED to DS3231 SQW at pin 44

void IRAM_ATTR OneSecondTimer_ISR() { 
  
  BaseType_t task_woken = pdFALSE;
  // notify the task xReadTempSensors via ReadTempSensors_h handle
  vTaskNotifyGiveFromISR(ReadTempSensors_h, &task_woken ); 

} 

//Tasks Functions
void BlynkLoop (void * pvParameters) {
  (void) pvParameters;

  Serial.println("Blynk Loop Task Started");
    
  for(;;) {
      Blynk.run();
      //timer.run(); // not in use,  same erros and lost of synch with ext HWtimer
      vTaskDelay(1); //feed the watchdog, thanks to @CampHamp
    }
}

void xReadTempSensors(void *pvParameters) {
  (void) pvParameters;
  EventBits_t myEventBits;
  char timeStamp[] = "DD MMM hh:mm:ss";
  for (;;) {

    //myEventBits = xEventGroupWaitBits(egTheEvents_h, TimerExpired_BIT, pdTRUE, pdTRUE, portMAX_DELAY);
    FreeSensorRead = uxTaskGetStackHighWaterMark(ReadTempSensors_h);
    
    // Wait for timer expired
    //Unblock and reset the notification 
    ulTaskGenericNotifyTake(tskDEFAULT_INDEX_TO_NOTIFY, pdTRUE, portMAX_DELAY);

  //read all sensors at once: temperatures, rtc time and ADC battery voltage
void xReadTempSensors(void *pvParameters) {
  (void) pvParameters;
  EventBits_t myEventBits;
  for (;;) {

    //myEventBits = xEventGroupWaitBits(egTheEvents_h, TimerExpired_BIT, pdTRUE, pdTRUE, portMAX_DELAY);
    FreeSensorRead = uxTaskGetStackHighWaterMark(ReadTempSensors_h);
    
    //Unblock at falling edge DS3231 sqw wave and reset the notification (<4us)
    ulTaskGenericNotifyTake(tskDEFAULT_INDEX_TO_NOTIFY, pdTRUE, portMAX_DELAY);


  
    for (uint8_t i = 0; i < sensorsNumber; i++){ 
      sTemp[i] = sensors.getTempC(sensor[i]);
    }
    now = rtc.now();
    SampleN = count;
    count++;
    if (count > 59) count = 0;
    
    // ADC battery voltage is HW (ESP32-S3) and SW corrected
    BattVolt = 0.19 + (float)esp_adc_cal_raw_to_voltage(adc1_get_raw(ADC1_CHANNEL_3), &adc_chars) * 0.002;

    xEventGroupSetBits(egTheEvents_h, hasMeasured_BIT);
    sensors.requestTemperatures();
  }
}

// Log Data. HERE I CALL BLINK FUNCTION FOR virutalWrite()
void xLogData (void *pvParameters){
  (void) pvParameters;    
  EventBits_t myEventBits;
  char dataBuffer [256];

  for(;;) {
    // Task synchronization
    myEventBits = xEventGroupWaitBits(egTheEvents_h, LogData_BIT, pdTRUE, pdTRUE, portMAX_DELAY);
    FreeLog = uxTaskGetStackHighWaterMark(xPrintTemperatures_h);
    
    // Log Data
    const char * format = "%02u/%02u/%02u %02u:%02u:%02u, %5.2f, %5.2f, %5.2f, %5.2f, %5.2f, %5.2f, %5.2f, %5.2f\r\n";
    sprintf (dataBuffer,  format, now.day(), now.month(), now.year()
                              , now.hour(), now.minute(), now.second() // data stamp
                              , TempAverage [0]
                              , TempAverage [1]
                              , TempAverage [2]
                              , TempAverage [3]
                              , TempAverage [4]
                              , TempAverage [5]
                              , TempAverage [6]
                              , TempAverage [7]
                              );
  //Append the data to file

  appendFile(SD, logFileName, dataBuffer);
  Blynk_SendData();
  }
}

//SEND data to BLYNK SERVER - NEED A TIME STAMP at beginGroup (unix time?)
void Blynk_SendData(){
  debugln("\n - Blink Data - "); 
  Blynk.beginGroup();
  Blynk.virtualWrite(V1, TempAverage[0]);
  Blynk.virtualWrite(V2, TempAverage[1]);
  Blynk.virtualWrite(V3, TempAverage[2]);
  Blynk.virtualWrite(V4, TempAverage[4]);
  Blynk.virtualWrite(V5, TempAverage[4]);
  Blynk.endGroup();
  debugln("  - Blynk data - \n");
}

I found the problem reviewing my post…

  // Connect to Wifi and set the time. Just once
  //WiFi_ConnectAsClient();
  //Adjust_RTC_byNPT();
  WiFi_Disconnect();            <===== I FORGOT TO COMMENT THIS LINE...

I was here before I introduce blink in the sketch… just to set the the RTC by the NTP.
The project is working now… and collecting data.
I also type cast the float data before sending:

Blynk.virtualWrite(V1, (double)(TempAverage[0]));