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
}