#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
// TFT LCD pin mapping
#define TFT_DC 17
#define TFT_CS 5
#define TFT_RST 16 // ใช้ GPIO16 เป็น Reset (ถ้าไม่ใช้ ก็ต่อ RST ไป 3.3V ได้)
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
// WiFi
const char* ssid = "Wokwi-GUEST"; // ใส่ชื่อ WiFi ของคุณ
const char* password = ""; // ใส่รหัสผ่าน WiFi ของคุณ
// Google Apps Script URL (Web App ที่อ่าน A1,A2 จาก Google Sheet)
String serverName = "https://script.google.com/macros/s/AKfycbzjXoNUOJ7dSuFAEmit1Nk6SP7pe2FwJk-67Zwb-MGm9piUfr4cfXQo2i6eOmpDdvBl/exec";
// global variable
String text = "Loading...";
int mode = 5;
// setup
void setup() {
Serial.begin(115200);
tft.begin();
tft.setRotation(1); // หมุนหน้าจอแนวนอน
tft.fillScreen(ILI9341_BLACK);
// connect WiFi
WiFi.begin(ssid, password);
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi connected!");
}
// loop
void loop() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(serverName);
int httpResponseCode = http.GET();
if (httpResponseCode == 200) {
String payload = http.getString();
Serial.println(payload);
StaticJsonDocument<256> doc;
DeserializationError error = deserializeJson(doc, payload);
if (!error) {
text = doc["text"].as<String>();
mode = doc["mode"].as<int>();
showText(text, mode);
}
} else {
Serial.print("Error HTTP request: ");
Serial.println(httpResponseCode);
}
http.end();
}
delay(5000); // update ทุก 5 วินาที
}
// ฟังก์ชันแสดงข้อความตามโหมด
void showText(String msg, int mode) {
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_YELLOW);
tft.setTextSize(2);
switch (mode) {
case 1: // บนลงล่าง
for (int y = 0; y < 240; y += 5) {
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(20, y);
tft.print(msg);
delay(30);
}
break;
case 2: // ล่างขึ้นบน
for (int y = 240; y > 0; y -= 5) {
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(20, y);
tft.print(msg);
delay(30);
}
break;
case 3: // ซ้ายไปขวา
for (int x = 0; x < 320; x += 5) {
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(x, 100);
tft.print(msg);
delay(30);
}
break;
case 4: // ขวาไปซ้าย
for (int x = 320; x > 0; x -= 5) {
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(x, 100);
tft.print(msg);
delay(30);
}
break;
case 5: // แสดงตรงกลาง
{
int16_t x1, y1;
uint16_t w, h;
tft.getTextBounds(msg, 0, 0, &x1, &y1, &w, &h);
tft.setCursor((320 - w) / 2, (240 - h) / 2);
tft.print(msg);
}
break;
case 0: // สุ่มตำแหน่ง
for (int i = 0; i < 20; i++) {
int x = random(0, 280);
int y = random(0, 220);
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(x, y);
tft.print(msg);
delay(200);
}
break;
}
}