#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#define TFT_CS 15 // TFT CS pin is connected to NodeMCU pin D2
#define TFT_RST 4 // TFT RST pin is connected to NodeMCU pin D3
#define TFT_DC 2 // TFT DC pin is connected to NodeMCU pin D4
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
float temperature = 0.0;
float humidity = 0.0;
void setup() {
Serial.begin(9600);
tft.begin();
tft.setRotation(3); // Adjust rotation based on your display orientation
tft.setTextSize(2); // Adjust text size as needed
tft.setTextColor(ILI9341_WHITE); // Set text color
tft.fillScreen(ILI9341_BLACK); // Clear screen with black color
}
void loop() {
if (Serial.available() > 0) {
String input = Serial.readStringUntil('\n');
Serial.print("Received input: ");
Serial.println(input); // Print the received input for debugging
// Handle temperature input
if (input.startsWith("t:")) {
String tempValue = input.substring(2); // Remove "Temp: "
temperature = tempValue.toFloat();
Serial.print("Temperature set to: ");
Serial.println(temperature);
}
// Handle humidity input
else if (input.startsWith("h:")) {
String humiValue = input.substring(2); // Remove "Humi: "
humidity = humiValue.toFloat();
Serial.print("Humidity set to: ");
Serial.println(humidity);
}
// Clear the screen
tft.fillScreen(ILI9341_BLACK);
// Display temperature and humidity
tft.setCursor(10, 10);
tft.print("Temp: ");
tft.print(temperature);
tft.print(" C");
tft.setCursor(10, 40);
tft.print("Humidity: ");
tft.print(humidity);
tft.print(" %");
// Small delay for stability
delay(1000);
}
}