This project demonstrates how to use an ESP32 microcontroller to connect to a Wi-Fi network, fetch jokes from a web API, and display them on a TFT display. The example uses several libraries to handle Wi-Fi connectivity, HTTP requests, JSON parsing, and graphics rendering.
- ESP32 Microcontroller: The main controller handling Wi-Fi and HTTP operations.
- Adafruit ILI9341 TFT Display: A 240x320 pixel color display used to show the fetched jokes.
- Wi-Fi Library (
WiFi.h
): Manages Wi-Fi connectivity. - HTTPClient Library (
HTTPClient.h
): Handles HTTP requests and responses. - ArduinoJson Library (
ArduinoJson.h
): Parses JSON responses from the API. - Adafruit GFX and ILI9341 Libraries (
Adafruit_GFX.h
,Adafruit_ILI9341.h
): Provides graphics functions for the TFT display.
-
SSID and Password:
const char* ssid = "Wokwi-GUEST"; const char* password = "";
These are the credentials for the Wi-Fi network the ESP32 will connect to.
-
TFT and Button Pins:
#define BTN_PIN 5 #define TFT_DC 2 #define TFT_CS 15 Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
Defines the pins for the TFT display and the button.
-
Jokes API URL:
const String url = "https://v2.jokeapi.dev/joke/Programming";
URL of the API that provides programming jokes.
-
getJoke()
:- Sends an HTTP GET request to the jokes API.
- Parses the JSON response to extract the joke.
- Handles both single-line jokes and two-part jokes (setup and delivery).
- Returns the joke as a string.
String getJoke() { HTTPClient http; http.useHTTP10(true); http.begin(url); http.GET(); String result = http.getString(); DynamicJsonDocument doc(2048); DeserializationError error = deserializeJson(doc, result); if (error) { Serial.print("deserializeJson() failed: "); Serial.println(error.c_str()); return "<Error>"; } String type = doc["type"].as<String>(); String joke = doc["joke"].as<String>(); String setup = doc["setup"].as<String>(); String delivery = doc["delivery"].as<String>(); http.end(); return type.equals("single") ? joke : setup + " " + delivery; }
-
nextJoke()
:- Displays a loading message.
- Fetches a joke using
getJoke()
. - Prints the joke to the TFT display.
void nextJoke() { tft.setTextColor(ILI9341_WHITE); tft.println("\nLoading Joke....."); String Joke = getJoke(); tft.setTextColor(ILI9341_GREEN); tft.println(Joke); }
-
setup()
:- Configures the button pin.
- Connects to the specified Wi-Fi network.
- Initializes the TFT display.
- Displays the IP address once connected.
- Fetches and displays the first joke.
void setup(void) { pinMode(BTN_PIN, INPUT_PULLUP); WiFi.begin(ssid, password, 6); tft.begin(); tft.setRotation(1); tft.setTextColor(ILI9341_WHITE); tft.setTextSize(2); tft.print("attempt to connect to wifi..."); while (WiFi.status() != WL_CONNECTED) { delay(100); tft.print("."); } tft.print("\nOK IP="); tft.print(WiFi.localIP()); nextJoke(); }
-
loop()
:- Checks if the button is pressed.
- If pressed, clears the screen and fetches a new joke.
void loop(void) { if (digitalRead(BTN_PIN) == LOW) { tft.fillScreen(ILI9341_BLACK); tft.setCursor(0, 0); nextJoke(); } }
This project demonstrates how to use the ESP32 to connect to a Wi-Fi network, send HTTP requests to a jokes API, parse the JSON responses, and display the jokes on a TFT display. It includes handling for both single-line and two-part jokes and allows fetching new jokes by pressing a button.