Hello! I want to make a digital temperature using a ESP32 and a DHT22, but the problem is that the display that I want to use doesn't work. Before I connected the TFT LCD Touchscreen (320x240px) to ESP32, I saw in the Serial Monitor the inside temperature, humidity and outside Temperature properly. But when I connect the display to ESP32 the problems arise.
Initially when I put cable from Vcc(On TFT Display) to 3.3(On ESP32), GND to GND and the others cables, the display is Powered On and has a permanently white screen. But when I upload the code in Arduino IDE on the ESP32 chip, the TFT display has whitescreen and nothing happens. I don't have any errors.
I tried some examples codes from FILE->Examples->Basics and only the Blink code worked for my display, I don't know what can be the problem.
Is it a connection problem or is the display defect?
I used ChatGPT mostly times to give me some ideas.
I'll atach some photos with connections and how it looks the display.
I'll put my codes here that I have.
I want to say that you have to put your Network username and password where appear "abcd" to function properly the code, if you want to test it.
Sorry for my bad english if isn't so correct.
#include <WiFi.h>
#include <HTTPClient.h>
#include <DHT.h>
#include <ArduinoJson.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
// DHT22 sensor configuration
DHT dht(25, DHT22); // Pin 25 is connected to the DHT22 sensor
// OpenWeatherMap API configuration
const char* apiKey = "ece88bfc93243c9014c1940be0258a69"; // Your API key
float externalTemperature = 0.0;
String cityName = ""; // Variable to store the city name
// WiFi configuration
const char* ssid = "abcd"; // Replace with your WiFi SSID
const char* password = "abcd"; // Replace with your WiFi password
// TFT display configuration
#define TFT_CS 15
#define TFT_RST 3
#define TFT_DC 4
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
void setupWiFi() {
Serial.println("Connecting to WiFi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected to WiFi!");
}
void setup() {
Serial.begin(9600);
setupWiFi();
dht.begin(); // Initialize the DHT22 sensor
// Initialize TFT display
tft.begin();
tft.setRotation(1); // Set landscape orientation for display
tft.fillScreen(ILI9341_BLACK); // Clear the screen
tft.setTextSize(2);
tft.setTextColor(ILI9341_WHITE);
tft.setCursor(10, 10);
tft.println("System started!");
delay(2000); // Delay for the start message
tft.fillScreen(ILI9341_BLACK); // Clear the screen for sensor data
}
void loop() {
// Read temperature and humidity from the DHT22 sensor
float internalTemperature = dht.readTemperature();
float humidity = dht.readHumidity();
if (isnan(internalTemperature) || isnan(humidity)) {
Serial.println("Sensor reading error!");
tft.fillScreen(ILI9341_RED); // Red background for error
tft.setCursor(10, 10);
tft.println("Sensor Error!");
} else {
// Display on Serial Monitor
Serial.print("Internal Temperature: ");
Serial.print(internalTemperature);
Serial.print(" °C ");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println(" %");
// Display on TFT
tft.fillScreen(ILI9341_BLACK); // Clear the screen
tft.setCursor(10, 10);
tft.print("Internal Temp: ");
tft.print(internalTemperature);
tft.println(" C");
tft.setCursor(10, 40);
tft.print("Humidity: ");
tft.print(humidity);
tft.println(" %");
}
// Get location coordinates via IP
String locationData = getLocation();
float latitude = 0.0, longitude = 0.0;
if (!locationData.isEmpty()) {
DynamicJsonDocument doc(2048);
DeserializationError error = deserializeJson(doc, locationData);
if (!error) {
latitude = doc["lat"];
longitude = doc["lon"];
cityName = doc["city"].as<String>(); // Get the city name from the JSON response
} else {
Serial.println("Error parsing location!");
}
}
// Get external temperature
if (latitude != 0.0 && longitude != 0.0) {
getExternalTemperature(latitude, longitude);
if (cityName != "") {
// Display on Serial Monitor
Serial.print("External Temperature at ");
Serial.print(cityName);
Serial.print(": ");
Serial.print(externalTemperature);
Serial.println(" °C");
// Display on TFT
tft.setCursor(10, 70);
tft.print("City: ");
tft.println(cityName);
tft.setCursor(10, 100);
tft.print("External Temp: ");
tft.print(externalTemperature);
tft.println(" C");
}
}
delay(10000); // Wait for 10 seconds before the next update
}
// Function to get location dynamically via IP
String getLocation() {
HTTPClient http;
http.begin("http://ip-api.com/json"); // Location API
int httpResponseCode = http.GET();
if (httpResponseCode == 200) {
String payload = http.getString();
http.end();
return payload;
} else {
Serial.println("Error getting location!");
http.end();
return "";
}
}
// Function to get external temperature from OpenWeatherMap
void getExternalTemperature(float latitude, float longitude) {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String requestURL = "https://api.openweathermap.org/data/2.5/weather?lat=" +
String(latitude) + "&lon=" + String(longitude) +
"&units=metric&appid=" + String(apiKey);
http.begin(requestURL);
int httpResponseCode = http.GET();
if (httpResponseCode == 200) {
String payload = http.getString();
DynamicJsonDocument doc(2048);
DeserializationError error = deserializeJson(doc, payload);
if (!error) {
externalTemperature = doc["main"]["temp"];
} else {
Serial.println("Error parsing JSON response!");
}
} else {
Serial.println("Error connecting to OpenWeatherMap API!");
}
http.end();
} else {
Serial.println("WiFi is not connected!");
}
}