/*****************************************************
WOKWI GUARANTEED WORKING CONFIG
ESP32 + ILI9341 CAP TOUCH
*****************************************************/
// ===== FORCE TFT CONFIG (DO NOT USE User_Setup.h) =====
#define USER_SETUP_LOADED
#define ILI9341_DRIVER
#define TFT_MOSI 23
#define TFT_MISO 19
#define TFT_SCLK 18
#define TFT_CS 15
#define TFT_DC 2
#define TFT_RST 4
// Stable for Wokwi
#define SPI_FREQUENCY 20000000
#define SPI_READ_FREQUENCY 20000000
#define SPI_TOUCH_FREQUENCY 2500000
#define LOAD_GLCD
#define LOAD_FONT2
#define LOAD_FONT4
#define LOAD_GFXFF
// =======================================================
#include <SPI.h>
#include <TFT_eSPI.h>
#include <Adafruit_FT6206.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <WiFi.h>
// -------- Pins --------
#define PIN_PH 32
#define PIN_TEMP 27
#define PIN_VOLT 34
#define PIN_CURR 35
#define PIN_LDR 33
TFT_eSPI tft = TFT_eSPI();
Adafruit_FT6206 ctp = Adafruit_FT6206();
OneWire oneWire(PIN_TEMP);
DallasTemperature sensors(&oneWire);
bool systemState = true;
bool redraw = true;
// ======================================================
void setup() {
Serial.begin(115200);
delay(1000);
tft.init();
tft.setRotation(1);
tft.fillScreen(TFT_BLACK);
if (!ctp.begin(40)) {
Serial.println("Touch failed");
}
sensors.begin();
WiFi.begin("Wokwi-GUEST", "");
}
// ======================================================
void loop() {
handleTouch();
if (systemState) {
drawActive();
} else {
drawStandby();
}
delay(200);
}
// ======================================================
void handleTouch() {
if (ctp.touched()) {
TS_Point p = ctp.getPoint();
int x = p.x;
int y = p.y;
if (x > 240 && y < 50) {
systemState = !systemState;
redraw = true;
delay(300);
}
}
}
// ======================================================
void drawActive() {
if (redraw) {
tft.fillScreen(TFT_BLACK);
redraw = false;
}
sensors.requestTemperatures();
float tempF = (sensors.getTempCByIndex(0) * 9.0 / 5.0) + 32.0;
float ph = (analogRead(PIN_PH) / 4095.0) * 14.0;
float volt = (analogRead(PIN_VOLT) / 4095.0) * 15.0;
float curr = (analogRead(PIN_CURR) / 4095.0) * 5.0;
int light = map(analogRead(PIN_LDR), 0, 4095, 0, 100);
// ON Button
tft.fillRect(250, 5, 65, 35, TFT_GREEN);
tft.setTextColor(TFT_BLACK, TFT_GREEN);
tft.drawCentreString("ON", 282, 12, 2);
tft.setTextColor(TFT_GOLD, TFT_BLACK);
tft.drawString("GENERATOR", 10, 10, 2);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.setCursor(10, 35);
tft.printf("V: %.1f V I: %.1f A ", volt, curr);
tft.drawFastHLine(0, 80, 320, TFT_DARKGREY);
tft.setTextColor(TFT_CYAN, TFT_BLACK);
tft.drawString("OCEAN SENSORS", 10, 90, 2);
tft.setTextSize(3);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.setCursor(10, 115);
tft.printf("pH: %.1f ", ph);
tft.setTextSize(2);
tft.setCursor(160, 120);
tft.printf("%.0f F ", tempF);
tft.setCursor(10, 170);
tft.setTextColor(TFT_ORANGE, TFT_BLACK);
tft.printf("SUNLIGHT: %d%% ", light);
tft.setTextSize(1);
tft.setCursor(10, 220);
tft.setTextColor(TFT_BLUE, TFT_BLACK);
tft.print("IP: ");
tft.print(WiFi.localIP());
}
// ======================================================
void drawStandby() {
if (redraw) {
tft.fillScreen(TFT_BLACK);
redraw = false;
}
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.drawCentreString("STANDBY MODE", 160, 90, 4);
tft.drawCentreString("Tap Top Right to Wake", 160, 140, 2);
tft.fillRect(250, 5, 65, 35, TFT_RED);
tft.setTextColor(TFT_WHITE, TFT_RED);
tft.drawCentreString("OFF", 282, 12, 2);
}