BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Blynk + LCD + WiFi Manager

I built a thermometer with a display using ttgo t-display esp32, DS18b20 and Blynk async_WM.

Everything works fine if wifi is ok.

But when it loses wifi connection and tries to reconnect, the display does not work properly.

If there is no wifi, it is impossible to read on display the temperatures longer.

My code:


#include "defines.h"
#include "Credentials.h"
#include "dynamicParams.h"
#include <DallasTemperature.h>
#include <OneWire.h>
#define ONE_WIRE_BUS 27 // temperatura
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

BlynkTimer timer;


//*** TFT *****

#include <TFT_eSPI.h>
#include "bmp.h"
#include "Free_Fonts.h"
#include <FS.h>
#include <SPI.h>

TFT_eSPI tft = TFT_eSPI();

WidgetLED led1(V1);//ok


//***********************************

bool kropka;
unsigned long  sek=0;
float temperatura0=100;
float temperatura1=100;
float temperatura2=100;

float t0=200;
float t1=200;
float t2=200;

int hcwd=2;







//**** nazwy temperatur *****************

void nazwyTemperatur() {
  


    tft.setFreeFont(FSSB12);

  tft.fillScreen(TFT_BLACK);
  
  tft.setCursor(60, 30);
  tft.setTextColor(TFT_GREEN, TFT_BLACK);
  tft.println("10p:");
  
  tft.setCursor(60, 75);
  tft.setTextColor(TFT_BLUE, TFT_BLACK);
  tft.println("KEG:");
  
  tft.setCursor(60, 120);
  tft.setTextColor(TFT_ORANGE, TFT_BLACK);
  tft.println("H2O:");
  
}


//*************************************************************


void setup() 
{

   Serial.begin(115200);
   while (!Serial);
   tft.begin();
   sensors.begin();
   delay(100);
  tft.setRotation(1);
  tft.setSwapBytes(true);

  delay(200);
  tft.fillScreen(TFT_BLUE);
  delay(500);
  
  nazwyTemperatur();
   sensors.setResolution(12);

    // Set config portal SSID and Password
  Blynk.setConfigPortal("xxxxxxxxxxxx", "xxxxxxxxxxxx");
  // Set config portal IP address
  Blynk.setConfigPortalIP(IPAddress(192, 168, 200, 4));
  // Set config portal channel, default = 1. Use 0 => random channel from 1-13 to avoid conflict
  Blynk.setConfigPortalChannel(0);

  // From v1.0.5, select either one of these to set static IP + DNS
  Blynk.setSTAStaticIPConfig(IPAddress(192, 168, 2, 230), IPAddress(192, 168, 2, 1), IPAddress(255, 255, 255, 0));
  //Blynk.setSTAStaticIPConfig(IPAddress(192, 168, 2, 220), IPAddress(192, 168, 2, 1), IPAddress(255, 255, 255, 0),
  //                           IPAddress(192, 168, 2, 1), IPAddress(8, 8, 8, 8));
  //Blynk.setSTAStaticIPConfig(IPAddress(192, 168, 2, 220), IPAddress(192, 168, 2, 1), IPAddress(255, 255, 255, 0),
  //                           IPAddress(4, 4, 4, 4), IPAddress(8, 8, 8, 8));

  // Use this to default DHCP hostname to ESP8266-XXXXXX or ESP32-XXXXXX
  //Blynk.begin();
  // Use this to personalize DHCP hostname (RFC952 conformed)
  // 24 chars max,- only a..z A..Z 0..9 '-' and no '-' as last char
  //Blynk.begin("ESP32-WM-Config");
  Blynk.begin(HOST_NAME);

  //timer.setInterval(1 * 1000, readAndSendData);

  if (Blynk.connected())
  {
    hcwd=2;
#if USE_SPIFFS
    Serial.println("\nBlynk ESP32 using SPIFFS connected. Board Name : " + Blynk.getBoardName());
#else
    Serial.println("\nBlynk ESP32 using EEPROM connected. Board Name : " + Blynk.getBoardName());
    Serial.printf("EEPROM size = %d bytes, EEPROM start address = %d / 0x%X\n", EEPROM_SIZE, EEPROM_START, EEPROM_START);
#endif
  }

#if USE_DYNAMIC_PARAMETERS
void displayCredentials(void)
{
  Serial.println("\nYour stored Credentials :");

  for (int i = 0; i < NUM_MENU_ITEMS; i++)
  {
    Serial.println(String(myMenuItems[i].displayName) + " = " + myMenuItems[i].pdata);
  }
}
#endif


}

void loop() 
{


  Blynk.run();



 if((millis()-sek)>1000)
     { 


//******* wifi  ***********************************

          if (Blynk.connected())
            {
             tft.pushImage(5,5,30,30,wifi);
             Blynk.virtualWrite(2,temperatura0);
             Blynk.virtualWrite(3,temperatura1);
             Blynk.virtualWrite(4,temperatura2);


             
            } 
             else
            {
            tft.fillRect(5,5,30,30,TFT_BLACK);
          
            }

// ***** czerwona kropka *************************

       if(kropka==1)
          {
          tft.fillRect(4,104,33,33,TFT_BLACK);
          tft.pushImage(5,105,30,30,rec);
          led1.on();
          }
        else
          {
            tft.fillRect(4,104,33,33,TFT_BLACK);
           led1.off();
          }
        //***************************************

      sensors.requestTemperatures();
      temperatura0 = sensors.getTempCByIndex(0);
      temperatura1 = sensors.getTempCByIndex(1);
      temperatura2 = sensors.getTempCByIndex(2);


      tft.setFreeFont(FSSB18);
     if(temperatura0 != t0)
     {
      tft.fillRect(120, 0, 120, 39, TFT_BLACK);
      tft.setCursor(120, 30);
      tft.setTextColor(TFT_GREEN, TFT_BLACK);
      tft.println(temperatura0);
      t0=temperatura0;
     }

     if(temperatura1 != t1)
     {
      tft.fillRect (120, 45, 120, 40, TFT_BLACK);
      tft.setCursor(120, 75);
      tft.setTextColor(TFT_BLUE, TFT_BLACK);
      tft.println(temperatura1);
      t1=temperatura1;
     } 

     if(temperatura2 != t2)
     {
       tft.fillRect (120, 90, 120, 40, TFT_BLACK);
       tft.setCursor(120, 120);
       tft.setTextColor(TFT_ORANGE, TFT_BLACK);
       tft.println(temperatura2);
       t2=temperatura2;
     }


        
      sek=millis();
      kropka=!kropka;


     }
}

First of all, your void loop is far too messy and this will lead to disconnections anyway.
You should read this…

Secondly, adding-in some serial print messages will allow you to understand your program flow better, and allow you to see what happens when Blynk is disconnected and you call Blynk.run

BTW, I deleted your other post where you put the same information on to the end of an existing topic.

Pete.

I tried every way.
This code gives the same result:

#include "defines.h"
#include "Credentials.h"
#include "dynamicParams.h"
#include <DallasTemperature.h>
#include <OneWire.h>
#define ONE_WIRE_BUS 27 // temperatura
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

BlynkTimer timer;


//*** TFT *****

#include <TFT_eSPI.h>
#include "bmp.h"
#include "Free_Fonts.h"
#include <FS.h>
#include <SPI.h>

TFT_eSPI tft = TFT_eSPI();

WidgetLED led1(V1);//ok



//***********************************

bool kropka;

float temperatura0=100;
float temperatura1=100;
float temperatura2=100;

float t0=200;
float t1=200;
float t2=200;









//**** nazwy temperatur *****************

void nazwyTemperatur() {
  


    tft.setFreeFont(FSSB12);

  tft.fillScreen(TFT_BLACK);
  
  tft.setCursor(60, 30);
  tft.setTextColor(TFT_GREEN, TFT_BLACK);
  tft.println("10p:");
  
  tft.setCursor(60, 75);
  tft.setTextColor(TFT_BLUE, TFT_BLACK);
  tft.println("KEG:");
  
  tft.setCursor(60, 120);
  tft.setTextColor(TFT_ORANGE, TFT_BLACK);
  tft.println("H2O:");
  
}


//*************************************************************


void setup() 
{

   Serial.begin(115200);
   while (!Serial);

   timer.setInterval(1*1000,co1sek);

   
   tft.begin();
   sensors.begin();
   delay(100);
  tft.setRotation(1);
  tft.setSwapBytes(true);

  delay(200);
  tft.fillScreen(TFT_BLUE);
  delay(500);
  
  nazwyTemperatur();
   sensors.setResolution(12);

    // Set config portal SSID and Password
  Blynk.setConfigPortal("xxxxxxxxxxxx", "xxxxxxxxxxxx");
  // Set config portal IP address
  Blynk.setConfigPortalIP(IPAddress(192, 168, 200, 4));
  // Set config portal channel, default = 1. Use 0 => random channel from 1-13 to avoid conflict
  Blynk.setConfigPortalChannel(0);

  // From v1.0.5, select either one of these to set static IP + DNS
  Blynk.setSTAStaticIPConfig(IPAddress(192, 168, 2, 230), IPAddress(192, 168, 2, 1), IPAddress(255, 255, 255, 0));
  //Blynk.setSTAStaticIPConfig(IPAddress(192, 168, 2, 220), IPAddress(192, 168, 2, 1), IPAddress(255, 255, 255, 0),
  //                           IPAddress(192, 168, 2, 1), IPAddress(8, 8, 8, 8));
  //Blynk.setSTAStaticIPConfig(IPAddress(192, 168, 2, 220), IPAddress(192, 168, 2, 1), IPAddress(255, 255, 255, 0),
  //                           IPAddress(4, 4, 4, 4), IPAddress(8, 8, 8, 8));

  // Use this to default DHCP hostname to ESP8266-XXXXXX or ESP32-XXXXXX
  //Blynk.begin();
  // Use this to personalize DHCP hostname (RFC952 conformed)
  // 24 chars max,- only a..z A..Z 0..9 '-' and no '-' as last char
  //Blynk.begin("ESP32-WM-Config");
  Blynk.begin(HOST_NAME);

  //timer.setInterval(1 * 1000, readAndSendData);

  if (Blynk.connected())
  {

#if USE_SPIFFS
    Serial.println("\nBlynk ESP32 using SPIFFS connected. Board Name : " + Blynk.getBoardName());
#else
    Serial.println("\nBlynk ESP32 using EEPROM connected. Board Name : " + Blynk.getBoardName());
    Serial.printf("EEPROM size = %d bytes, EEPROM start address = %d / 0x%X\n", EEPROM_SIZE, EEPROM_START, EEPROM_START);
#endif
  }

#if USE_DYNAMIC_PARAMETERS
void displayCredentials(void)
{
  Serial.println("\nYour stored Credentials :");

  for (int i = 0; i < NUM_MENU_ITEMS; i++)
  {
    Serial.println(String(myMenuItems[i].displayName) + " = " + myMenuItems[i].pdata);
  }
}
#endif


}

void loop() 
{


  Blynk.run();
  timer.run();
}


 

void co1sek() //co 1 sek
{

      sensors.requestTemperatures();
      temperatura0 = sensors.getTempCByIndex(0);
      temperatura1 = sensors.getTempCByIndex(1);
      temperatura2 = sensors.getTempCByIndex(2);

      
//******* wifi  ***********************************

          if (Blynk.connected())
            {
              
             tft.pushImage(5,5,30,30,wifi);
             Blynk.virtualWrite(2,temperatura0);
             Blynk.virtualWrite(3,temperatura1);
             Blynk.virtualWrite(4,temperatura2);


             
            } 
             else
            {
            tft.fillRect(5,5,30,30,TFT_BLACK);
          
            }

// ***** czerwona kropka *************************

       if(kropka==1)
          {
          tft.fillRect(4,104,33,33,TFT_BLACK);
          tft.pushImage(5,105,30,30,rec);
          led1.on();
          }
        else
          {
            tft.fillRect(4,104,33,33,TFT_BLACK);
           led1.off();
          }
        //***************************************




      tft.setFreeFont(FSSB18);
     if(temperatura0 != t0)
     {
      tft.fillRect(120, 0, 120, 39, TFT_BLACK);
      tft.setCursor(120, 30);
      tft.setTextColor(TFT_GREEN, TFT_BLACK);
      tft.println(temperatura0);
      Serial.println(temperatura0);
      t0=temperatura0;
     }

     if(temperatura1 != t1)
     {
      tft.fillRect (120, 45, 120, 40, TFT_BLACK);
      tft.setCursor(120, 75);
      tft.setTextColor(TFT_BLUE, TFT_BLACK);
      tft.println(temperatura1);
      Serial.println(temperatura1);
      t1=temperatura1;
     } 

     if(temperatura2 != t2)
     {
       tft.fillRect (120, 90, 120, 40, TFT_BLACK);
       tft.setCursor(120, 120);
       tft.setTextColor(TFT_ORANGE, TFT_BLACK);
       tft.println(temperatura2);
       Serial.println(temperatura2);
       t2=temperatura2;
     }
      kropka=!kropka;


   

}

Look at this please:

17:32:20.511 -> 23.94
17:32:21.491 -> 23.87
17:32:24.490 -> 23.94
17:32:25.515 -> 23.87
17:32:34.488 -> 24.06
17:32:35.516 -> 24.00
17:32:44.508 -> 23.94
17:32:45.492 -> 23.87
17:32:48.493 -> 24.06
17:32:49.473 -> 24.00
17:33:14.499 -> 23.94
17:33:15.490 -> 23.87
17:33:37.489 -> 24.06
17:33:38.491 -> 24.00
17:33:50.501 -> 23.94
17:33:51.486 -> 23.87
17:33:53.503 -> 23.94
17:33:54.484 -> 23.87
17:34:43.634 -> [392539] run: WiFi lost. Reconnect WiFi+Blynk
17:34:43.634 -> [392539] Connecting MultiWifi...
17:35:25.820 -> [434719] WiFi not connected
17:35:25.820 -> [434719] Connecting to blynk.abcXXXXXXXXXXX:8080
17:35:25.961 -> [434881] run: WiFi lost. Reconnect WiFi+Blynk
17:35:25.961 -> [434881] Connecting MultiWifi...
17:36:13.165 -> [482074] WiFi not connected
17:36:13.165 -> [482074] Connecting to blynk.abcXXXXXXXXXX:8080
17:36:13.351 -> [482236] run: WiFi lost. Reconnect WiFi+Blynk
17:36:13.351 -> [482236] Connecting MultiWifi...
17:37:00.540 -> [529429] WiFi not connected
17:37:00.540 -> [529429] Connecting to blynk.abcXXXXXXXXXXX:8080
17:37:00.680 -> [529591] run: WiFi lost. Reconnect WiFi+Blynk
17:37:00.680 -> [529591] Connecting MultiWifi...
17:37:48.078 -> [576984] WiFi not connected
17:37:48.078 -> [576984] Connecting to blynk.abcXXXXXXXXX:8080
17:37:48.267 -> 23.81
17:37:48.267 -> [577150] run: WiFi lost. Reconnect WiFi+Blynk
17:37:48.267 -> [577150] Connecting MultiWifi...
17:38:37.840 -> [626724] WiFi connected after time: 10
17:38:37.840 -> [626724] SSID:neo,RSSI=-47
17:38:37.840 -> [626724] Channel:3,IP address:192.168.0.213
17:38:37.840 -> [626724] run: WiFi reconnected. Connect to Blynk
17:38:37.840 -> [626727] 
17:38:37.840 ->     ___  __          __
17:38:37.840 ->    / _ )/ /_ _____  / /__
17:38:37.840 ->   / _  / / // / _ \/  '_/
17:38:37.840 ->  /____/_/\_, /_//_/_/\_\
17:38:37.840 ->         /___/ v0.6.1 on ESP32
17:38:37.840 -> 
17:38:37.840 -> [626740] Connecting to :8080
17:38:42.851 -> [631741] Connecting to :8080
17:38:47.831 -> [636740] 
17:38:47.831 ->     ___  __          __
17:38:47.831 ->    / _ )/ /_ _____  / /__
17:38:47.831 ->   / _  / / // / _ \/  '_/
17:38:47.831 ->  /____/_/\_, /_//_/_/\_\
17:38:47.831 ->         /___/ v0.6.1 on ESP32
17:38:47.831 -> 
17:38:47.831 -> [636741] Connecting to blynk.abc-XXXXXXXXXX:8080
17:38:52.849 -> [641742] Connecting to blynk.abc-XXXXXXXXX:8080
17:38:57.855 -> [646741] Blynk not connected
17:38:57.996 -> 23.81
17:38:58.043 -> 23.87
17:38:58.043 -> [646909] run: Blynk lost. Connect Blynk
17:38:58.043 -> [646910] 
17:38:58.043 ->     ___  __          __
17:38:58.043 ->    / _ )/ /_ _____  / /__
17:38:58.043 ->   / _  / / // / _ \/  '_/
17:38:58.043 ->  /____/_/\_, /_//_/_/\_\
17:38:58.043 ->         /___/ v0.6.1 on ESP32
17:38:58.043 -> 
17:38:58.043 -> [646915] Connecting to :8080
17:39:03.001 -> [651916] Connecting to :8080
17:39:08.009 -> [656915] 
17:39:08.009 ->     ___  __          __
17:39:08.009 ->    / _ )/ /_ _____  / /__
17:39:08.009 ->   / _  / / // / _ \/  '_/
17:39:08.009 ->  /____/_/\_, /_//_/_/\_\
17:39:08.009 ->         /___/ v0.6.1 on ESP32
17:39:08.009 -> 
17:39:08.009 -> [656916] Connecting to blynk.abcXXXXXXXXXXXXXXl:8080
17:39:28.149 -> [677065] Blynk not connected
17:39:28.149 -> [677065] Connecting to blynk XXXXXXXXXXXXXXXX:8080
17:39:28.385 -> 23.87
17:39:28.385 -> [677265] run: Blynk lost. Connect Blynk
17:39:28.385 -> [677265] 
17:39:28.385 ->     ___  __          __
17:39:28.385 ->    / _ )/ /_ _____  / /__
17:39:28.385 ->   / _  / / // / _ \/  '_/
17:39:28.385 ->  /____/_/\_, /_//_/_/\_\
17:39:28.385 ->         /___/ v0.6.1 on ESP32
17:39:28.385 -> 
17:39:28.385 -> [677272] Connecting to :8080
17:39:33.388 -> [682273] Connecting to :8080
17:39:38.390 -> [687272] 
17:39:38.390 ->     ___  __          __
17:39:38.390 ->    / _ )/ /_ _____  / /__
17:39:38.390 ->   / _  / / // / _ \/  '_/
17:39:38.390 ->  /____/_/\_, /_//_/_/\_\
17:39:38.390 ->         /___/ v0.6.1 on ESP32
17:39:38.390 -> 
17:39:38.390 -> [687273] Connecting to blynk.abcXXXXXXXXXX:8080
17:39:38.437 -> [687345] Ready (ping: 32ms).
17:39:38.530 -> [687413] Connected to Blynk Server = blynk.abcXXXXXX, Token = 654392d9XXXXXXXXXXXXXXXX
17:39:38.530 -> [687413] run: Blynk reconnected
17:39:38.858 -> 23.81
17:39:39.516 -> 23.87
17:39:40.496 -> 23.81
17:39:40.496 -> 23.81
17:39:41.479 -> 23.87
17:39:42.508 -> 23.87
17:39:43.489 -> 23.81
17:39:43.489 -> 23.81
17:39:44.518 -> 23.87
17:39:44.518 -> 23.94
17:39:45.502 -> 23.87
17:39:45.502 -> 24.00
17:39:46.486 -> 23.81
17:39:46.486 -> 23.81
17:39:48.509 -> 23.87
17:39:49.494 -> 23.87
17:39:50.484 -> 23.81

THX a lot.

Your topic title says “+ WiFi Manager” but the serial output mentions MultiWiFi, which as far as I know are different things.
As you’ve not included any of the included .h files such as defines.h its difficult to know exactly what you are doing.

Pete.

Of course, I already share !

defines.h

/****************************************************************************************************************************
   defines.h
   For ESP32 boards

   Blynk_Async_WM is a library, using AsyncWebServer instead of (ESP8266)WebServer for the ESP8266/ESP32 to enable easy
   configuration/reconfiguration and autoconnect/autoreconnect of WiFi/Blynk.
   
   Based on and modified from Blynk library v0.6.1 (https://github.com/blynkkk/blynk-library/releases)
   Built by Khoi Hoang (https://github.com/khoih-prog/Blynk_Async_WM)
   Licensed under MIT license
   Version: 1.0.16

   Version    Modified By   Date      Comments
   -------    -----------  ---------- -----------
    1.0.16    K Hoang      25/08/2020 Initial coding to use (ESP)AsyncWebServer instead of (ESP8266)WebServer. 
                                      Bump up to v1.0.16 to sync with Blynk_WM v1.0.16
 *****************************************************************************************************************************/

#ifndef defines_h
#define defines_h

#ifndef ESP32
  #error This code is intended to run on the ESP32 platform! Please check your Tools->Board setting.
#endif

#define BLYNK_PRINT Serial

#define DOUBLERESETDETECTOR_DEBUG     false
#define BLYNK_WM_DEBUG                0

// Not use #define USE_SPIFFS  => using EEPROM for configuration data in WiFiManager
// #define USE_SPIFFS    false => using EEPROM for configuration data in WiFiManager
// #define USE_SPIFFS    true  => using SPIFFS for configuration data in WiFiManager
// Be sure to define USE_SPIFFS before #include <BlynkSimpleEsp8266_WM.h>

#define USE_SPIFFS                  true
//#define USE_SPIFFS                  false

#if (!USE_SPIFFS)
  // EEPROM_SIZE must be <= 2048 and >= CONFIG_DATA_SIZE (currently 172 bytes)
  #define EEPROM_SIZE    (2 * 1024)
  // EEPROM_START + CONFIG_DATA_SIZE must be <= EEPROM_SIZE
  #define EEPROM_START   0
#endif

// Force some params in Blynk, only valid for library version 1.0.1 and later
#define TIMEOUT_RECONNECT_WIFI                    10000L
#define RESET_IF_CONFIG_TIMEOUT                   true
#define CONFIG_TIMEOUT_RETRYTIMES_BEFORE_RESET    2
// Those above #define's must be placed before #include <BlynkSimpleEsp8266_Async_WM.h>

//You have to download Blynk WiFiManager Blynk_Async_WM library at //https://github.com/khoih-prog/Blynk_Async_WM
// In order to enable (USE_BLYNK_WM = true). Otherwise, use (USE_BLYNK_WM = false)
#define USE_BLYNK_WM   true
//#define USE_BLYNK_WM   false


//#define USE_SSL     true
#define USE_SSL     false

#if USE_BLYNK_WM
  #if USE_SSL
    #include <BlynkSimpleEsp32_SSL_Async_WM.h>        //https://github.com/khoih-prog/Blynk_Async_WM
  #else
    #include <BlynkSimpleEsp32_Async_WM.h>            //https://github.com/khoih-prog/Blynk_Async_WM
  #endif

  #include "Credentials.h"
  #include "dynamicParams.h"

#else
  #if USE_SSL
    #include <BlynkSimpleEsp32_SSL.h>
    #define BLYNK_HARDWARE_PORT     9443
  #else
    #include <BlynkSimpleEsp32.h>
    #define BLYNK_HARDWARE_PORT     8080
  #endif
#endif

#if !USE_BLYNK_WM

  #ifndef LED_BUILTIN
    #define LED_BUILTIN       2         // Pin D2 mapped to pin GPIO2/ADC12 of ESP32, control on-board LED
  #endif
  
  #define USE_LOCAL_SERVER    true
  //#define USE_LOCAL_SERVER    false
  
  // If local server
  #if USE_LOCAL_SERVER
    char blynk_server[]   = "yourname.duckdns.org";
  #endif
  
  char auth[]     = "***";
  char ssid[]     = "***";
  char pass[]     = "***";

#endif

#define PIN_D22   22            // Pin D22 mapped to pin GPIO22/SCL of ESP32

#define DHT_PIN     PIN_D22     // pin DATA @ D22 / GPIO22
#define DHT_TYPE    DHT11

#define HOST_NAME   "ESP32-DHT11-Async"

#endif      //defines_h

dynamicParams.h

/****************************************************************************************************************************
   dynamicParams.h
   For ESP32 boards

   Blynk_Async_WM is a library, using AsyncWebServer instead of (ESP8266)WebServer for the ESP8266/ESP32 to enable easy
   configuration/reconfiguration and autoconnect/autoreconnect of WiFi/Blynk.
   
   Based on and modified from Blynk library v0.6.1 (https://github.com/blynkkk/blynk-library/releases)
   Built by Khoi Hoang (https://github.com/khoih-prog/Blynk_Async_WM)
   Licensed under MIT license
   Version: 1.0.16

   Version    Modified By   Date      Comments
   -------    -----------  ---------- -----------
    1.0.16    K Hoang      25/08/2020 Initial coding to use (ESP)AsyncWebServer instead of (ESP8266)WebServer. 
                                      Bump up to v1.0.16 to sync with Blynk_WM v1.0.16
 *****************************************************************************************************************************/

#ifndef dynamicParams_h
#define dynamicParams_h

#define USE_DYNAMIC_PARAMETERS    false

/////////////// Start dynamic Credentials ///////////////

//Defined in <BlynkSimpleEsp32_Async_WM.h> and <BlynkSimpleEsp32_SSL_Async_WM.h>
/**************************************
  #define MAX_ID_LEN                5
  #define MAX_DISPLAY_NAME_LEN      16

  typedef struct
  {
  char id             [MAX_ID_LEN + 1];
  char displayName    [MAX_DISPLAY_NAME_LEN + 1];
  char *pdata;
  uint8_t maxlen;
  } MenuItem;
**************************************/

#if USE_DYNAMIC_PARAMETERS

  #define MAX_MQTT_SERVER_LEN      34
  char MQTT_Server  [MAX_MQTT_SERVER_LEN + 1]   = "default-mqtt-server";
  
  #define MAX_MQTT_PORT_LEN        6
  char MQTT_Port   [MAX_MQTT_PORT_LEN + 1]  = "1883";
  
  #define MAX_MQTT_USERNAME_LEN      34
  char MQTT_UserName  [MAX_MQTT_USERNAME_LEN + 1]   = "default-mqtt-username";
  
  #define MAX_MQTT_PW_LEN        34
  char MQTT_PW   [MAX_MQTT_PW_LEN + 1]  = "default-mqtt-password";
  
  #define MAX_MQTT_SUBS_TOPIC_LEN      34
  char MQTT_SubsTopic  [MAX_MQTT_SUBS_TOPIC_LEN + 1]   = "default-mqtt-SubTopic";
  
  #define MAX_MQTT_PUB_TOPIC_LEN       34
  char MQTT_PubTopic   [MAX_MQTT_PUB_TOPIC_LEN + 1]  = "default-mqtt-PubTopic";
  
  MenuItem myMenuItems [] =
  {
    { "mqtt", "MQTT Server",      MQTT_Server,      MAX_MQTT_SERVER_LEN },
    { "mqpt", "Port",             MQTT_Port,        MAX_MQTT_PORT_LEN   },
    { "user", "MQTT UserName",    MQTT_UserName,    MAX_MQTT_USERNAME_LEN },
    { "mqpw", "MQTT PWD",         MQTT_PW,          MAX_MQTT_PW_LEN },
    { "subs", "Subs Topics",      MQTT_SubsTopic,   MAX_MQTT_SUBS_TOPIC_LEN },
    { "pubs", "Pubs Topics",      MQTT_PubTopic,    MAX_MQTT_PUB_TOPIC_LEN },
  };
  
  uint16_t NUM_MENU_ITEMS = sizeof(myMenuItems) / sizeof(MenuItem);  //MenuItemSize;

#else

  MenuItem myMenuItems [] = {};
  
  uint16_t NUM_MENU_ITEMS = 0;
#endif


/////// // End dynamic Credentials ///////////

#endif      //dynamicParams_h

Credentials.h

/****************************************************************************************************************************
   Credentials.h
   For ESP32 boards

   Blynk_Async_WM is a library, using AsyncWebServer instead of (ESP8266)WebServer for the ESP8266/ESP32 to enable easy
   configuration/reconfiguration and autoconnect/autoreconnect of WiFi/Blynk.
   
   Based on and modified from Blynk library v0.6.1 (https://github.com/blynkkk/blynk-library/releases)
   Built by Khoi Hoang (https://github.com/khoih-prog/Blynk_Async_WM)
   Licensed under MIT license
   Version: 1.0.16

   Version    Modified By   Date      Comments
   -------    -----------  ---------- -----------
    1.0.16    K Hoang      25/08/2020 Initial coding to use (ESP)AsyncWebServer instead of (ESP8266)WebServer. 
                                      Bump up to v1.0.16 to sync with Blynk_WM v1.0.16
 *****************************************************************************************************************************/

#ifndef Credentials_h
#define Credentials_h

/// Start Default Config Data //////////////////

/*
  // Defined in <BlynkSimpleEsp32_Async_WM.h> and <BlynkSimpleEsp32_SSL_Async_WM.h>

  #define SSID_MAX_LEN      32
  #define PASS_MAX_LEN      64
  
  typedef struct
  {
  char wifi_ssid[SSID_MAX_LEN];
  char wifi_pw  [PASS_MAX_LEN];
  }  WiFi_Credentials;

  #define BLYNK_SERVER_MAX_LEN      32
  #define BLYNK_TOKEN_MAX_LEN       36

  typedef struct
  {
  char blynk_server[BLYNK_SERVER_MAX_LEN];
  char blynk_token [BLYNK_TOKEN_MAX_LEN];
  }  Blynk_Credentials;

  #define NUM_WIFI_CREDENTIALS      2
  #define NUM_BLYNK_CREDENTIALS     2

  typedef struct Configuration
  {
  char header         [16];
  WiFi_Credentials  WiFi_Creds  [NUM_WIFI_CREDENTIALS];
  Blynk_Credentials Blynk_Creds [NUM_BLYNK_CREDENTIALS];
  int  blynk_port;
  char board_name     [24];
  int  checkSum;
  } Blynk_WM_Configuration;

*/

//bool LOAD_DEFAULT_CONFIG_DATA = true;
bool LOAD_DEFAULT_CONFIG_DATA = false;

Blynk_WM_Configuration defaultConfig =
{
  //char header[16], dummy, not used
#if USE_SSL  
  "SSL",
#else
  "NonSSL",
#endif
  //WiFi_Credentials  WiFi_Creds  [NUM_WIFI_CREDENTIALS]
  //WiFi_Creds.wifi_ssid and WiFi_Creds.wifi_pw
  "SSID1", "password1",
  "xxx", "Kasxxxxx02580",
  // Blynk_Credentials Blynk_Creds [NUM_BLYNK_CREDENTIALS];
  // Blynk_Creds.blynk_server and Blynk_Creds.blynk_token
  "blynk-cloud.com",     "token",
  "blynk.abcXXXXXXXXXX",  "token1", 
  //int  blynk_port;
#if USE_SSL
  9443,
#else
  8080,
#endif
  //char board_name     [24];
  "TERMOMETR-ESP",
  //int  checkSum, dummy, not used
  0
};

/////////// End Default Config Data /////////////


#endif    //Credentials_h

@PeteKnight

Thanks for taking care of everything. So sorry I have been busy recently.

@robert4you

Even you clean up your loop() nicely, there are still issues because your WiFi is so much unreliable. I suggest you have to fix that first to use Blynk effectively.

The Blynk.run() has to do a lot of works, and in your case, to repeatedly reconnect WiFi as well as Blynk back. And the job is blocking, by design.

That’s why the temp. measurement and time-consuming TFT display are so flaky.

To fix the issue, considering your current WiFi situation, I rewrite your program using ESP32’s built-in FreeRTOS tasks and it’s working well in all situations, good or flaky WiFi/Blynk.

I’ll include the examples using the similar solution in future releases of ESP32’s Blynk libraries

  1. Blynk_WM
  2. BlynkAsync_WM. Updated to v1.1.0
  3. BlynkESP32_BT_WF
  4. BlynkGSM_Manager
  5. Blynk_Async_ESP32_BT_WF
  6. Blynk_Async_GSM_Manager

Hereafter is the modified code, with Google English translation to help other users.

#include "defines.h"
#include "Credentials.h"
#include "dynamicParams.h"

#include <ESP32Ping.h>

#include <DallasTemperature.h>
#include <OneWire.h>

#define ONE_WIRE_BUS 27 // Temperature
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

//BlynkTimer timer;

//*** TFT *****

#include <TFT_eSPI.h>
#include "bmp.h"              // ./ArduinoMenu_library/examples/adafruitGfx_eTFT/TFT_eSPI/ArduinoMenu_LilyGo_TTGO_T-display_demo/bmp.h
#include "Free_Fonts.h"       // https://github.com/Bodmer/TFT_eSPI/blob/master/examples/320%20x%20240/Free_Font_Demo/Free_Fonts.h
#include <FS.h>
#include <SPI.h>

TFT_eSPI tft = TFT_eSPI();

WidgetLED led1(V1);

bool isOnline         = false;
bool isBlynkConnected = false;

//***********************************

bool toggle;

float Temperature0 = 100;
float Temperature1 = 100;
float Temperature2 = 100;

float t0 = 200;
float t1 = 200;
float t2 = 200;

//**** displayLocation *****************

void displayLocation() 
{
  tft.setFreeFont(FSSB12);

  tft.fillScreen(TFT_BLACK);

  tft.setCursor(60, 30);
  tft.setTextColor(TFT_GREEN, TFT_BLACK);
  tft.println("#01:");

  tft.setCursor(60, 75);
  tft.setTextColor(TFT_BLUE, TFT_BLACK);
  tft.println("#02:");

  tft.setCursor(60, 120);
  tft.setTextColor(TFT_ORANGE, TFT_BLACK);
  tft.println("#03:");
}

void displayTemp()
{
  // *********** LED toggle **************

  if (toggle == 1)
  {
    tft.fillRect(4, 104, 33, 33, TFT_BLACK);
    tft.pushImage(5, 105, 30, 30, rec);
    led1.on();
    digitalWrite(LED_BUILTIN, 1);
  }
  else
  {
    tft.fillRect(4, 104, 33, 33, TFT_BLACK);
    led1.off();
    digitalWrite(LED_BUILTIN, 0);
  }
  //***************************************

  tft.setFreeFont(FSSB18);

// Define true to simulate DS18B20s for testing if you don't have real DS18B20
#define SIMULATE_ONLY   true
  
#if SIMULATE_ONLY

  float curTime = millis() % 100000;

  Serial.println("=================");
  Serial.printf("Time = %ld ms\n", millis());
  
  Temperature0 = 25.0 + curTime / 100000;
  Temperature1 = 26.0 + curTime / 125000;
  Temperature2 = 27.0 + curTime / 150000;

#endif
  
  if (Temperature0 != t0)
  {
    tft.fillRect(120, 0, 120, 39, TFT_BLACK);
    tft.setCursor(120, 30);
    tft.setTextColor(TFT_GREEN, TFT_BLACK);
    
    tft.println(Temperature0);
    Serial.printf("T0 = %5.2f\n", Temperature0);
    
    t0 = Temperature0;
  }

  if (Temperature1 != t1)
  {
    tft.fillRect (120, 45, 120, 40, TFT_BLACK);
    tft.setCursor(120, 75);
    tft.setTextColor(TFT_BLUE, TFT_BLACK);
    
    tft.println(Temperature1);
    Serial.printf("T1 = %5.2f\n", Temperature1);
    
    t1 = Temperature1;
  }

  if (Temperature2 != t2)
  {
    tft.fillRect (120, 90, 120, 40, TFT_BLACK);
    tft.setCursor(120, 120);
    tft.setTextColor(TFT_ORANGE, TFT_BLACK);
    
    tft.println(Temperature2);
    Serial.printf("T2 = %5.2f\n", Temperature2);
    
    t2 = Temperature2;
  }
  
  toggle = !toggle;
}

void readTemp()
{
  sensors.requestTemperatures();
  Temperature0 = sensors.getTempCByIndex(0);
  Temperature1 = sensors.getTempCByIndex(1);
  Temperature2 = sensors.getTempCByIndex(2);
  
  //******* wifi  ***********************************

  if (Blynk.connected())
  {
    tft.pushImage(5, 5, 30, 30, wifi);
    Blynk.virtualWrite(V2, Temperature0);
    Blynk.virtualWrite(V3, Temperature1);
    Blynk.virtualWrite(V4, Temperature2);
  }
  else
  {
    tft.fillRect(5, 5, 30, 30, TFT_BLACK);
  }
}

//*************************************************************

void DisplayEveryNSec( void * pvParameters )
{
#define DISPLAY_INTERVAL_MS       1000L

  for (;;)
  {
    displayTemp();
    vTaskDelay(DISPLAY_INTERVAL_MS / portTICK_PERIOD_MS);
  }
}

void SensorReadEveryNSec( void * pvParameters )
{
#define SENSOR_READ_INTERVAL_MS       5000L
  
  for (;;)
  {
    readTemp();
    vTaskDelay(SENSOR_READ_INTERVAL_MS / portTICK_PERIOD_MS);
  }
}

void InternetBlynkStatus( void * pvParameters )
{
// Check Internet connection every PING_INTERVAL_MS if already Online
#define PING_INTERVAL_MS      60000L
// Check Internet connection every REPING_INTERVAL_MS if Offline
#define REPING_INTERVAL_MS    10000L

  for (;;)
  {
    if (Ping.ping("google.com"))
    {
      isOnline = true;

      if (Blynk.connected())
      {
        isBlynkConnected = true;
        Serial.println("Blynk Online");
      }
      else
      {
        isBlynkConnected = false;
        Serial.println("Internet OK. Blynk Offline");
      }
      
      vTaskDelay(PING_INTERVAL_MS / portTICK_PERIOD_MS);
      continue;
    }
    else
    {
      isOnline = false;
      Serial.println("Internet Offline");
      vTaskDelay(REPING_INTERVAL_MS / portTICK_PERIOD_MS);
      continue;
    }
  }
}

void BlynkRun( void * pvParameters )
{
#define BLYNK_RUN_INTERVAL_MS    250L

  for (;;)
  {
    Blynk.run();
    vTaskDelay(BLYNK_RUN_INTERVAL_MS / portTICK_PERIOD_MS);
  }
}

//**************************************************************

#define USING_CORE_1      0
#define USING_CORE_2      1

#define DisplayEveryNSec_Priority           ( 3 | portPRIVILEGE_BIT )
#define SensorReadEveryNSec_Priority        ( 4 | portPRIVILEGE_BIT )
#define InternetBlynkStatus_Priority        ( 2 | portPRIVILEGE_BIT )
#define BlynkRun_Priority                   ( 2 | portPRIVILEGE_BIT )

void setup()
{
  Serial.begin(115200);
  while (!Serial);

  Serial.println("\nStart BlynkAsync_Temp_MultiTask on " + String(ARDUINO_BOARD));

  pinMode(LED_BUILTIN,  OUTPUT);

  tft.begin();
  sensors.begin();
  delay(100);
  tft.setRotation(1);
  tft.setSwapBytes(true);

  delay(200);
  tft.fillScreen(TFT_BLUE);
  delay(500);

  displayLocation();
  sensors.setResolution(12);

  // Set config portal SSID and Password
  Blynk.setConfigPortal("xxxxxxxxxxxx", "xxxxxxxxxxxx");
  // Set config portal IP address
  Blynk.setConfigPortalIP(IPAddress(192, 168, 200, 4));
  // Set config portal channel, default = 1. Use 0 => random channel from 1-13 to avoid conflict
  Blynk.setConfigPortalChannel(0);

  // From v1.0.5, select either one of these to set static IP + DNS
  Blynk.setSTAStaticIPConfig(IPAddress(192, 168, 2, 230), IPAddress(192, 168, 2, 1), IPAddress(255, 255, 255, 0));
  //Blynk.setSTAStaticIPConfig(IPAddress(192, 168, 2, 220), IPAddress(192, 168, 2, 1), IPAddress(255, 255, 255, 0),
  //                           IPAddress(192, 168, 2, 1), IPAddress(8, 8, 8, 8));
  //Blynk.setSTAStaticIPConfig(IPAddress(192, 168, 2, 220), IPAddress(192, 168, 2, 1), IPAddress(255, 255, 255, 0),
  //                           IPAddress(4, 4, 4, 4), IPAddress(8, 8, 8, 8));

  // Use this to default DHCP hostname to ESP8266-XXXXXX or ESP32-XXXXXX
  //Blynk.begin();
  // Use this to personalize DHCP hostname (RFC952 conformed)
  // 24 chars max,- only a..z A..Z 0..9 '-' and no '-' as last char
  //Blynk.begin("ESP32-WM-Config");
  Blynk.begin(HOST_NAME);

  //timer.setInterval(1 * 1000, readAndSendData);
  //timer.setInterval(1 * 1000, displayTemp);
  //timer.setInterval(5 * 1000, readTemp);

  if (Blynk.connected())
  {
    isBlynkConnected = true;
    
#if USE_SPIFFS
    Serial.println("\nBlynk ESP32 using SPIFFS connected. Board Name : " + Blynk.getBoardName());
#else
    Serial.println("\nBlynk ESP32 using EEPROM connected. Board Name : " + Blynk.getBoardName());
    Serial.printf("EEPROM size = %d bytes, EEPROM start address = %d / 0x%X\n", EEPROM_SIZE, EEPROM_START, EEPROM_START);
#endif
  }

  xTaskCreatePinnedToCore( DisplayEveryNSec,      "DisplayEveryNSec",     20000,  NULL, DisplayEveryNSec_Priority,      NULL, USING_CORE_2);
  xTaskCreatePinnedToCore( SensorReadEveryNSec,   "SensorReadEveryNSec",  20000,  NULL, SensorReadEveryNSec_Priority,   NULL, USING_CORE_2);
  xTaskCreatePinnedToCore( InternetBlynkStatus,   "InternetBlynkStatus",  5000,   NULL, InternetBlynkStatus_Priority,   NULL, USING_CORE_2);
  xTaskCreatePinnedToCore( BlynkRun,              "BlynkRun",             20000,  NULL, BlynkRun_Priority,              NULL, USING_CORE_2);
}

#if USE_DYNAMIC_PARAMETERS
  void displayCredentials(void)
  {
    Serial.println("\nYour stored Credentials :");

    for (int i = 0; i < NUM_MENU_ITEMS; i++)
    {
      Serial.println(String(myMenuItems[i].displayName) + " = " + myMenuItems[i].pdata);
    }
  }
#endif  

void loop()
{
  // No more Blynk.run() and timer.run() here. All is tasks
}

Next will be the terminal output to show that the display and measurement are still OK while reconnecting to WiFi/Blynk.

1 Like

Terminal output

Start BlynkAsync_Temp_MultiTask on ESP32_DEV
[1355] ======= Start Default Config Data =======
[1355] Hdr=NonSSL,BrdName=ESP32-Async-Blynk
[1355] SSID=HueNet1,PW=12345678
[1355] SSID1=HueNet1,PW1=12345678
[1358] Server=account.ddns.net,Token=blynk_token
[1364] Server1=account.duckdns.org,Token1=blynk_token
[1370] Port=8080
[1372] ======= End Config Data =======
[1448] Hostname=ESP32-Async-Controller
[1493] SaveCfgFile 
[1493] WCSum=0x3551
[1497] OK
[1500] SaveBkUpCfgFile 
[1504] OK
[1506] SaveCredFile 
[1506] CW1:pdata=default-mqtt-server,len=34
[1506] CW1:pdata=1883,len=6
[1507] CW1:pdata=default-mqtt-username,len=34
[1509] CW1:pdata=default-mqtt-password,len=34
[1513] CW1:pdata=default-mqtt-SubTopic,len=34
[1517] CW1:pdata=default-mqtt-PubTopic,len=34
[1523] OK
[1523] CrWCSum=0x29a6
[1526] SaveBkUpCredFile 
[1526] CW2:pdata=default-mqtt-server,len=34
[1530] CW2:pdata=1883,len=6
[1533] CW2:pdata=default-mqtt-username,len=34
[1537] CW2:pdata=default-mqtt-password,len=34
[1541] CW2:pdata=default-mqtt-SubTopic,len=34
[1545] CW2:pdata=default-mqtt-PubTopic,len=34
[1551] OK
[1551] ======= Start Loaded Config Data =======
[1554] Hdr=ESP32,BrdName=ESP32-Async-Blynk
[1558] SSID=HueNet1,PW=12345678
[1561] SSID1=HueNet1,PW1=12345678
[1564] Server=account.ddns.net,Token=blynk_token
[1570] Server1=account.duckdns.org,Token1=blynk_token
[1576] Port=8080
[1578] ======= End Config Data =======
[1582] bg: noConfigPortal = true
[1585] Connecting MultiWifi...
[7922] WiFi connected after time: 1
[7922] SSID:HueNet1,RSSI=-32
[7922] Channel:2,IP address:192.168.2.101
[7922] bg: WiFi OK. Try Blynk
[7923] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.6.1 on ESP32

[7936] BlynkArduinoClient.connect: Connecting to account.ddns.net:8080
[7980] Ready (ping: 8ms).
[8048] Connected to Blynk Server = account.ddns.net, Token = blynk_token
[8048] bg: WiFi+Blynk OK

Blynk ESP32 using SPIFFS connected. Board Name : ESP32-Async-Blynk
=================
Time = 8869 ms
T0 = 25.09
T1 = 26.07
T2 = 27.06
=================
Time = 10022 ms
T0 = 25.10
T1 = 26.08
T2 = 27.07
...
Online
=================
Time = 15911 ms
T0 = 25.16
T1 = 26.13
T2 = 27.11
=================
Time = 16927 ms
T0 = 25.17
T1 = 26.14
T2 = 27.11
=================
Time = 17942 ms
T0 = 25.18
T1 = 26.14
T2 = 27.12
...
=================
Time = 23894 ms
T0 = 25.24
T1 = 26.19
T2 = 27.16
[24848] run: WiFi lost. Reconnect WiFi+Blynk
[24848] Connecting MultiWifi...
=================
Time = 24911 ms                 <=== Still displaying while reconnecting to WiFi
T0 = 25.25
T1 = 26.20
T2 = 27.17
=================
Time = 26534 ms
T0 = 25.27
T1 = 26.21
T2 = 27.18
=================
Time = 27549 ms
T0 = 25.28
T1 = 26.22
T2 = 27.18
=================
Time = 28564 ms
T0 = 25.29
T1 = 26.23
T2 = 27.19
...
=================
Time = 65106 ms
T0 = 25.65
T1 = 26.52
T2 = 27.43
=================
Time = 66798 ms
T0 = 25.67
T1 = 26.53
T2 = 27.45
=================
Time = 67813 ms
T0 = 25.68
T1 = 26.54
T2 = 27.45
=================
Time = 68828 ms
T0 = 25.69
T1 = 26.55
T2 = 27.46
=================
Time = 69843 ms
T0 = 25.70
T1 = 26.56
T2 = 27.47
[70647] WiFi connected after time: 10              <=== Reconnected to WiFi
[70647] SSID:HueNet1,RSSI=-33
[70647] Channel:2,IP address:192.168.2.101
[70647] run: WiFi reconnected. Connect to Blynk
[70650] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.6.1 on ESP32

[70663] BlynkArduinoClient.connect: Connecting to account.ddns.net:8080
[70712] Ready (ping: 12ms).
[70781] Connected to Blynk Server = account.ddns.net, Token = blynk_token
[70781] run: WiFi+Blynk reconnected
=================
Time = 70860 ms
T0 = 25.71
T1 = 26.57
T2 = 27.47
=================
Time = 72618 ms
T0 = 25.73
T1 = 26.58
T2 = 27.48
=================
Time = 73702 ms
T0 = 25.74
T1 = 26.59
T2 = 27.49
...
=================
Time = 93405 ms
T0 = 25.93
T1 = 26.75
T2 = 27.62
=================
Time = 94422 ms
T0 = 25.94
T1 = 26.76
T2 = 27.63

Thank you very much, I will test your solution.

Earlier I also tried to use dual core & multi task but the wchatdog kill me.

For anyone interested in the solution, check the full and latest code at

Async_ESP32_MultiTask.

There are more new MultiTask-related examples in

ESP32_MultiTask Directory

Thx a lot.!

Hi!

The code works great.
But…

If we have a display, it would be nice to show that the device has turned on own wifi network for configuration. What variable can I use for this?