Quote of the day get.html help

Hi

I make a coffee timer that counts down, and also display a quote of the day
Quote of the day don't kick in

I have this:

server http://quotes.rest/qod.json
API token MmwxxxxxxxxxxxxxxxxxxxxxxxxxxHexiH9

This code gives this output

Failed to get quote of the day

String getQuoteOfTheDay() {
  String quote;
  HTTPClient http;

  http.begin("http://quotes.rest/qod.json"); // Set the URL for the API endpoint
  int httpCode = http.GET(); // Send the HTTP request

  if (httpCode == HTTP_CODE_OK) { // Check if the HTTP request was successful
    String response = http.getString(); // Get the response from the API
    int startQuote = response.indexOf("quote\":\"") + 8; // Find the start of the quote
    int endQuote = response.indexOf("\",\"length\""); // Find the end of the quote
    quote = response.substring(startQuote, endQuote); // Extract the quote from the response
    quote.replace("\\u2019", "'"); // Replace any special characters in the quote
  } else { // If the HTTP request failed
    quote = "Failed to get quote of the day"; // Set an error message
  }

  http.end(); // Close the HTTP connection
  return quote; // Return the quote of the day
}

Q: As you can see I don't have a API string, can I get some help to place it?

Full code:

#include <Adafruit_SSD1306.h> // OLED library
#include <WiFi.h> // WiFi library
#include <HTTPClient.h> // HTTP client library


#define OLED_SDA 21
#define OLED_SCL 22
#define OLED_RST 23
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define ANALOG_PIN A0
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RST);


const char* ssid1            = "";          // WiFi der mottager skal stå fast
const char* password1        = "";


char apiToken* = "MmxxxlGXpTk";


void setup() {
  Serial.begin(115200); // Start Serial communication
  Wire.begin(OLED_SDA, OLED_SCL); // Initialize I2C communication for OLED
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Initialize OLED screen
  display.clearDisplay(); // Clear the screen
  display.setTextSize(1); // Set text size
  display.setTextColor(SSD1306_WHITE); // Set text color
  display.setCursor(0, 30); // Set cursor position
  display.print("Connecting to WiFi..."); // Display connecting message
  display.display(); // Update the OLED screen
  WiFi.begin(ssid1, password1); // Connect to WiFi
  while (WiFi.status() != WL_CONNECTED) { // Wait for WiFi connection
    delay(1000);
  }
  display.clearDisplay(); // Clear the screen before displaying new data
  display.print("WiFi connected"); // Display connection status
  display.setCursor(0, 10); // Set cursor position
  display.print("IP address: "); // Display IP address
  display.print(WiFi.localIP());
  display.display(); // Update the OLED screen
  delay(2000); // Wait for 2 seconds before clearing the screen
  display.clearDisplay(); // Clear the screen
}

void loop() {
  int count = 6; // Set the initial countdown time
  int analogValue = analogRead(ANALOG_PIN); // Read analog input signal
  while (analogValue < 100) { // Loop until signal is high
    analogValue = analogRead(ANALOG_PIN); // Read analog input signal
  }
  while (count >= 0) { // Loop until countdown reaches 0
    display.clearDisplay(); // Clear the screen before displaying new data
    display.setTextSize(1); // Set text size
    display.setTextColor(SSD1306_WHITE); // Set text color
    display.setCursor(0, 0); // Set cursor position
    display.print("Kaffebrygging ferdig om: "); // Display text
    display.print(count); // Display remaining time
    display.setCursor(0, 30); // Set cursor position for quote of the day
    display.print("Quote of the day:"); // Display text
    display.display(); // Update the OLED screen
    String quote = getQuoteOfTheDay(); // Get today's quote
    Serial.println(quote);
    display.setCursor(0, 40); // Set cursor position for quote of the day
    display.print(quote); // Display today's quote
    delay(1000); // Wait for 1 second
    count--; // Decrement the countdown timer
  }
}

String getQuoteOfTheDay() {
  String quote;
  HTTPClient http;

  http.begin("http://quotes.rest/qod.json"); // Set the URL for the API endpoint
  int httpCode = http.GET(); // Send the HTTP request

  if (httpCode == HTTP_CODE_OK) { // Check if the HTTP request was successful
    String response = http.getString(); // Get the response from the API
    int startQuote = response.indexOf("quote\":\"") + 8; // Find the start of the quote
    int endQuote = response.indexOf("\",\"length\""); // Find the end of the quote
    quote = response.substring(startQuote, endQuote); // Extract the quote from the response
    quote.replace("\\u2019", "'"); // Replace any special characters in the quote
  } else { // If the HTTP request failed
    quote = "Failed to get quote of the day"; // Set an error message
  }

  http.end(); // Close the HTTP connection
  return quote; // Return the quote of the day
}

I borrowed a code from wokwi

for other coffee enthusiasts, code not optimized if get joke takes long time

Input A0 == High starts countdown and serves a joke

/*
   ESP32 HTTPClient Jokes API Example

  https://wokwi.com/projects/342032431249883731

  Copyright (C) 2022, Uri Shaked
*/
#include <Adafruit_SSD1306.h> // OLED library
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <Adafruit_GFX.h>


const char* ssid1            = "";          // WiFi der mottager skal stå fast
const char* password1        = "";


const String url = "https://v2.jokeapi.dev/joke/Any";   //"https://v2.jokeapi.dev/joke/Programming";

#define ANALOG_PIN A0

#define OLED_SDA 21
#define OLED_SCL 22
#define OLED_RST 23
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RST);

int count;
int bryggetid = 24;

void setup() {
  Serial.begin(115200); // Start Serial communication
  Wire.begin(OLED_SDA, OLED_SCL); // Initialize I2C communication for OLED
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Initialize OLED screen
  display.clearDisplay(); // Clear the screen
  display.setTextSize(1); // Set text size
  display.setTextColor(SSD1306_WHITE); // Set text color
  display.setCursor(0, 30); // Set cursor position
  display.print("Connecting to WiFi..."); // Display connecting message
  display.display(); // Update the OLED screen
  WiFi.begin(ssid1, password1); // Connect to WiFi
  while (WiFi.status() != WL_CONNECTED) { // Wait for WiFi connection
    delay(1000);
  }
  display.clearDisplay(); // Clear the screen before displaying new data
  display.print("WiFi connected"); // Display connection status
  display.setCursor(0, 10); // Set cursor position
  display.print("IP address: "); // Display IP address
  display.print(WiFi.localIP());
  display.display(); // Update the OLED screen
  delay(2000); // Wait for 2 seconds before clearing the screen
  display.clearDisplay(); // Clear the screen
}


void loop() {
  count = bryggetid;
  String joke = getJoke();
 while ((analogRead(ANALOG_PIN) == HIGH) && (count >= 0)) { **Bruk denne
     display.clearDisplay();
    display.setTextColor(WHITE);
    display.setTextSize(1);
    display.setCursor(3, 0);  // Set cursor position
    display.print("Kaffebrygging ferdig om: "); // Display text
    display.print(count);     // Display remaining time
    display.setCursor(0, 30); // Set cursor position for quote of the day
    display.print("Quote of the day:"); // Display text
    display.setCursor(0, 55); // Set cursor position for quote of the day  (y,x)  0,0 topp venstre
    display.print(joke);      // Display today's quote
    display.display();        // Update the OLED screen
    Serial.println(joke);
    delay(1000);              // Wait for 1 second
    count--;
  }
  display.clearDisplay();
  display.setTextColor(WHITE);
  display.setTextSize(1);
  display.setCursor(3, 0);  // Set cursor position
  display.print("Kaffebrygging ferdig! Nyt kaffien"); // Display text
}


String getJoke() {
  HTTPClient http;
  http.useHTTP10(true);
  http.begin(url);
  http.GET();
  String result = http.getString();

  DynamicJsonDocument doc(2048);
  DeserializationError error = deserializeJson(doc, result);

  // Test if parsing succeeds.
  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;
}

on their web site they state:

They Said So Quotes API offers a complete feature rich REST API access to its quotes platform. This is the documentation for the world famous quotes API. If you are a subscriber and you are trying this from a console you can use Bearer token with your api key as the token. You can test and play with the API right here on this web page. Please note recently we closed down public access without api key to prevent abuse. The public routes are still available to use free of charge but requires an api token. You can get one for free at our website. For using the private end points and subscribing to the API please visit https://theysaidso.com/api.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.