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;
}
}