ESP32-Smart-Thermostat-Blynk-APP
Loading...
Searching...
No Matches
main.cpp File Reference

ESP32 Smart Thermostat with TFT Display and Blynk Integration. More...

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <TFT_eSPI.h>
#include <DHT.h>

Macros

#define BLYNK_TEMPLATE_ID   "TMPLxxxxxxxxx"
#define BLYNK_TEMPLATE_NAME   "My Thermostat"
#define BLYNK_AUTH_TOKEN   "AbCdEfGhIjKlMnOpQrStUvWxYz123456"
#define RELAY_PIN   26
#define SENSOR_PIN   15
#define SENSOR_TYPE   DHT11
#define BACKLIGHT_PIN   13
#define RELAY_ON   LOW
 Relay Logic configuration. CW-020 Relays are usually Low-Level Trigger, meaning pulling the pin to GND turns the relay ON.
#define RELAY_OFF   HIGH
#define COLOR_PANEL_BG   TFT_BLACK
#define COLOR_PANEL_FROST   0x2104
#define COLOR_HEATING   0xFB00
#define COLOR_HUMIDITY   0x3DFF
#define COLOR_GREY_TEXT   0x8410
#define COLOR_TEXT_WHITE   TFT_WHITE

Functions

DHT dht (SENSOR_PIN, SENSOR_TYPE)
 BLYNK_WRITE (V2)
 Blynk App Callback: Target Temperature Slider. Triggered automatically when the user changes the slider on the Blynk App (Virtual Pin 2).
 BLYNK_WRITE (V4)
 Blynk App Callback: Emergency Stop Button. Forces the relay to turn off if the button on Virtual Pin 4 is pressed (value goes to 0).
void setup ()
 Standard Arduino Setup Function. Initializes hardware pins, display, sensors, and network connections.
void loop ()
 Standard Arduino Loop Function. Kept clean. Only processes Blynk cloud tasks and hardware timers.
void readSensorAndControl ()
 Core Application Logic. Reads the DHT sensor, applies calibration offsets, updates the cloud, checks the thermostat rules, and triggers a UI redraw if any data changed.
void checkThermostat ()
 Evaluates the current temperature against the target setpoint. Uses hysteresis to define a "deadband" so the relay doesn't rapidly click on and off when the temperature hovers exactly at the target.
void drawInterface ()
 Renders the Graphical User Interface on the TFT display. Draws the central circle, dynamic sidebar, icons, and text.

Variables

char ssid [] = "YOUR_WIFI_SSID"
char pass [] = "YOUR_WIFI_PASSWORD"
TFT_eSPI tft = TFT_eSPI()
BlynkTimer timer
float currentTemp = 0
float humidity = 0
float targetTemp = 22.0
float hysteresis = 0.5
bool isHeating = false
float lastTemp = -999
float lastHumidity = -999
float lastTarget = -999
bool lastHeatingState = !isHeating
bool forceRedraw = true
const unsigned char icon_drop[] PROGMEM
 XBM Image Data for the Humidity Drop. The PROGMEM keyword forces the microcontroller to store this array in Flash memory instead of RAM, leaving more RAM available for dynamic variables.

Detailed Description

ESP32 Smart Thermostat with TFT Display and Blynk Integration.

  • This firmware reads environmental data from a DHT sensor, controls a heating relay based on a target setpoint with hysteresis, and displays a dynamic, anti-flickering user interface on an SPI TFT screen.

Macro Definition Documentation

◆ BACKLIGHT_PIN

#define BACKLIGHT_PIN   13

◆ BLYNK_AUTH_TOKEN

#define BLYNK_AUTH_TOKEN   "AbCdEfGhIjKlMnOpQrStUvWxYz123456"

◆ BLYNK_TEMPLATE_ID

#define BLYNK_TEMPLATE_ID   "TMPLxxxxxxxxx"

◆ BLYNK_TEMPLATE_NAME

#define BLYNK_TEMPLATE_NAME   "My Thermostat"

◆ COLOR_GREY_TEXT

#define COLOR_GREY_TEXT   0x8410

◆ COLOR_HEATING

#define COLOR_HEATING   0xFB00

◆ COLOR_HUMIDITY

#define COLOR_HUMIDITY   0x3DFF

◆ COLOR_PANEL_BG

#define COLOR_PANEL_BG   TFT_BLACK

◆ COLOR_PANEL_FROST

#define COLOR_PANEL_FROST   0x2104

◆ COLOR_TEXT_WHITE

#define COLOR_TEXT_WHITE   TFT_WHITE

◆ RELAY_OFF

#define RELAY_OFF   HIGH

◆ RELAY_ON

#define RELAY_ON   LOW

Relay Logic configuration. CW-020 Relays are usually Low-Level Trigger, meaning pulling the pin to GND turns the relay ON.

◆ RELAY_PIN

#define RELAY_PIN   26

◆ SENSOR_PIN

#define SENSOR_PIN   15

◆ SENSOR_TYPE

#define SENSOR_TYPE   DHT11

Function Documentation

◆ BLYNK_WRITE() [1/2]

BLYNK_WRITE ( V2 )

Blynk App Callback: Target Temperature Slider. Triggered automatically when the user changes the slider on the Blynk App (Virtual Pin 2).

◆ BLYNK_WRITE() [2/2]

BLYNK_WRITE ( V4 )

Blynk App Callback: Emergency Stop Button. Forces the relay to turn off if the button on Virtual Pin 4 is pressed (value goes to 0).

◆ checkThermostat()

void checkThermostat ( )

Evaluates the current temperature against the target setpoint. Uses hysteresis to define a "deadband" so the relay doesn't rapidly click on and off when the temperature hovers exactly at the target.

◆ dht()

DHT dht ( SENSOR_PIN ,
SENSOR_TYPE  )

◆ drawInterface()

void drawInterface ( )

Renders the Graphical User Interface on the TFT display. Draws the central circle, dynamic sidebar, icons, and text.

◆ loop()

void loop ( )

Standard Arduino Loop Function. Kept clean. Only processes Blynk cloud tasks and hardware timers.

◆ readSensorAndControl()

void readSensorAndControl ( )

Core Application Logic. Reads the DHT sensor, applies calibration offsets, updates the cloud, checks the thermostat rules, and triggers a UI redraw if any data changed.

◆ setup()

void setup ( )

Standard Arduino Setup Function. Initializes hardware pins, display, sensors, and network connections.

Variable Documentation

◆ currentTemp

float currentTemp = 0

◆ forceRedraw

bool forceRedraw = true

Forces a full screen wipe and redraw on the first boot cycle

◆ humidity

float humidity = 0

◆ hysteresis

float hysteresis = 0.5

Temperature gap to prevent rapid relay switching

◆ isHeating

bool isHeating = false

◆ lastHeatingState

bool lastHeatingState = !isHeating

◆ lastHumidity

float lastHumidity = -999

◆ lastTarget

float lastTarget = -999

◆ lastTemp

float lastTemp = -999

◆ pass

char pass[] = "YOUR_WIFI_PASSWORD"

◆ PROGMEM

const unsigned char icon_flame [] PROGMEM
Initial value:
= {
0x00, 0x00, 0x00, 0x01, 0x80, 0x03, 0xc0, 0x07, 0xe0, 0x0f, 0xf0, 0x1f,
0xf8, 0x3f, 0xfc, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x3f, 0xfc, 0x1f,
0xf8, 0x0f, 0xf0, 0x03, 0xc0, 0x00, 0x00, 0x00
}

XBM Image Data for the Humidity Drop. The PROGMEM keyword forces the microcontroller to store this array in Flash memory instead of RAM, leaving more RAM available for dynamic variables.

XBM Image Data for the Heating Flame.

◆ ssid

char ssid[] = "YOUR_WIFI_SSID"

◆ targetTemp

float targetTemp = 22.0

◆ tft

TFT_eSPI tft = TFT_eSPI()

◆ timer

BlynkTimer timer