#include <SPI.h>
#include <TFT_eSPI.h>
#include <SD.h>
#include <DHT.h>
#include <Adafruit_NeoPixel.h>
#include <XPT2046_Touchscreen.h>
// Pin definitions
#define TFT_LED 32
#define SD_CS 15
#define LDR_PIN 34
#define GAS_PIN 36
#define DHT_PIN 27
#define DHT_TYPE DHT22
#define ENCODER_A 33
#define ENCODER_B 25
#define ENCODER_SW 26
#define NEOPIXEL_PIN 5
#define NUM_PIXELS 8
#define BUZZER_PIN 13
#define TOUCH_CS 14 // assuming default in Wokwi part
TFT_eSPI tft = TFT_eSPI();
DHT dht(DHT_PIN, DHT_TYPE);
Adafruit_NeoPixel strip(NUM_PIXELS, NEOPIXEL_PIN, NEO_GRB + NEO_KHZ800);
XPT2046_Touchscreen touch(TOUCH_CS);
int relayState = LOW;
unsigned long lastLog = 0;
void setup(){
Serial.begin(115200);
tft.init(); tft.setRotation(1);
pinMode(TFT_LED, OUTPUT); analogWrite(TFT_LED, 255);
if (!SD.begin(SD_CS)){
Serial.println("SD card failed!");
} else Serial.println("SD card ready");
dht.begin();
pinMode(ENCODER_A, INPUT_PULLUP);
pinMode(ENCODER_B, INPUT_PULLUP);
pinMode(ENCODER_SW, INPUT_PULLUP);
strip.begin(); strip.show();
pinMode(BUZZER_PIN, OUTPUT); digitalWrite(BUZZER_PIN, LOW);
}
void loop(){
float temp = dht.readTemperature(), hum = dht.readHumidity();
int light = analogRead(LDR_PIN), gas = analogRead(GAS_PIN);
int b = map(light, 0, 4095, 255, 10);
analogWrite(TFT_LED, b);
if (millis() - lastLog > 5000){
File log = SD.open("/log.txt", FILE_APPEND);
if (log){
log.printf("%lu,%.1f,%.1f,%d,%d\n", millis(), temp, hum, light, gas);
log.close();
}
lastLog = millis();
}
tft.fillScreen(TFT_BLACK);
tft.setTextSize(2);
tft.setTextColor(TFT_YELLOW, TFT_BLACK);
tft.setCursor(10, 20);
tft.println("Temp: " + String(temp,1) + "C");
tft.println("Hum : " + String(hum,1) + "%");
tft.println("Lght: " + String(light));
tft.println("Gas : " + String(gas));
tft.println("Bright: " + b);
if (digitalRead(ENCODER_SW)==LOW){
relayState = !relayState;
tone(BUZZER_PIN, 1000, 100);
for (int i=0;i<strip.numPixels();i++){
strip.setPixelColor(i, relayState ? strip.Color(0,255,0) : strip.Color(255,0,0));
}
strip.show();
delay(300);
}
if (touch.touched()){
TS_Point p = touch.getPoint();
tft.setCursor(10, 140);
tft.setTextColor(TFT_CYAN);
tft.print("Touch at ");
tft.print(p.x); tft.print(","); tft.print(p.y);
}
delay(1000);
}