#include <TFT_eSPI.h>
#include <SPI.h> // this is needed for display
#include <HTTPClient.h>
#include <WiFi.h>
// Invoke custom library with default width and height
TFT_eSPI tft = TFT_eSPI();
// The display also uses hardware SPI, plus #9 & #10
#define TFT_CS 15
#define TFT_DC 2
#define TFT_MOSI 23
#define TFT_SCLK 18
// Wi-Fi credentials
const char* ssid = "MEO-346E40"; // Replace with your Wi-Fi SSID
const char* password = "be0a511ad4"; // Replace with your Wi-Fi password
// API URL
const char* apiUrl = "https://random-word-api.herokuapp.com/word?number=1";
void setup() {
// Initialize the display
tft.init();
tft.setRotation(1); // Adjust rotation if needed
tft.fillScreen(TFT_BLACK);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.setTextSize(2);
// Initialize Serial for debugging
Serial.begin(115200);
// Connect to Wi-Fi
tft.setCursor(10, 10);
tft.println("Connecting to Wi-Fi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
tft.print(".");
}
tft.setCursor(10, 30);
tft.println("Wi-Fi connected!");
tft.println("Fetching word...");
// Fetch and display a random word
// fetchAndDisplayWord();
}
void loop() {
// Nothing to do here
}
// void fetchAndDisplayWord() {
// Check Wi-Fi connection
if (WiFi.status() != WL_CONNECTED) {
tft.setCursor(10, 50);
tft.println("Wi-Fi not connected!");
return;
}
// Create an HTTPClient object
HTTPClient http;
// Send GET request to the API
http.begin(apiUrl);
int httpCode = http.GET();
// Check the response code
if (httpCode == HTTP_CODE_OK) {
// Parse the response (JSON format: ["word"])
String payload = http.getString();
payload.replace("[", "");
payload.replace("]", "");
payload.replace("\"", "");
// Display the word
tft.setCursor(10, 50);
tft.println("Random Word:");
tft.setCursor(10, 70);
tft.println(payload);
} else {
// Display error message
tft.setCursor(10, 50);
tft.println("Failed to fetch word!");
tft.setCursor(10, 70);
tft.println("HTTP Error: " + String(httpCode));
}
// Close the connection
http.end();