Building a joke machine with an ESP8266

Posted on

I had a few ESP8266’s lying around and some OLED Displays and wanted to do a project with them. So, I decided to put a joke API (http://official-joke-api.appspot.com) I discovered earlier to use.

Wire up your display to the ESP8266 according to the following schematics:

ESP8266 Arduino OLED Display Circuit Schematic
https://randomnerdtutorials.com/esp8266-0-96-inch-oled-display-with-arduino-ide/

Then add the following code to your ESP8266 with the Arduino Application. You need to edit in the Wireless SSID and Password on line 4 and 5.

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

const char* ssid = "Wifi SSID";
const char* password =  "Wifi Password";

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <ArduinoJson.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);


void setup () {

  Serial.begin(115200);

  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
    Serial.println(F("SSD1306 allocation failed"));
    for (;;);
  }

  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 10);
  // Display static text
  display.println("Waiting for Joke");

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {

    delay(1000);
    Serial.println("Connecting..");

  }

  Serial.println('\n');
  Serial.print("Connected to ");
  Serial.println(WiFi.SSID());              // Tell us what network we're connected to
  Serial.print("IP address:\t");
  Serial.println(WiFi.localIP());

}

void loop() {
  Serial.println("Loop");

  if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status

    HTTPClient http;  //Declare an object of class HTTPClient

    http.begin("http://official-joke-api.appspot.com/random_joke");  //Specify request destination
    int httpCode = http.GET();                                  //Send the request

    if (httpCode > 0) { //Check the returning code

      String payload = http.getString();   //Get the request response payload
      Serial.println(payload);

      DynamicJsonDocument doc(2048);
      deserializeJson(doc, payload);

      const char* joke = doc["setup"];
      const char* punchline = doc["punchline"];
      Serial.println(joke);

      display.clearDisplay();

      display.setCursor(0, 10);
      // Display static text
      display.println(joke);
      display.display();
      delay(4000);
      display.clearDisplay();

      display.setCursor(0, 10);
      // Display static text
      display.println(punchline);
      display.display();

    }

    http.end();   //Close connection

  }

  delay(6000);    //Send a request every 30 seconds
}

Also, the following Libraries need to be installed via the Library Manager in the Arduino Software. (under Tools, Library Manager)

ArduinoJson, Adafruit_SSD1306, Adafruit-GFX-Library

Also, you need to add the URL “http://arduino.esp8266.com/stable/package_esp8266com_index.json” in the preferences and install esp8266 platform (Open Boards Manager from Tools > Board menu).

Don’t forget to select the ESP8266 board from Tools > Board menu after the installation.

And voilà, your joke machine should be finished and will be showing first a setup and then a few seconds later the punchline.

Leave a Reply