/*
Scrolling API Menu V3.0
=== Scrolling API Menu V3.0 ===
ESP32 Chuck Norris Jokes
by R. Pelayo
Tutorial: https://www.teachmemicro.com/esp32-restful-api
*/
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <U8g2lib.h>
#include "config.h"
bool apiFetched = false; // start false
String apiReply; // stores result from API
unsigned long lastScroll = 0;
int scrollInterval = 0; // ms between scroll steps (~60 FPS)
u8g2_uint_t replyWidth = 0;
// Constructor for U8g2lib
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0);
// === Start generated bitmaps from image2cpp ===
// 'Sunrise_Sunset_Icon', 16x16px
const unsigned char Sunrise_Sunset_Icon[] PROGMEM = { // 'PROGMEM" places this variable in Flash (32k) memory as opposed to SRAM (2k)
0x88, 0x00, 0x72, 0x02, 0x8c, 0x01, 0x05, 0x05, 0x02, 0x02, 0x02, 0x02, 0x02, 0x42, 0x05, 0x45,
0x8c, 0xc1, 0x72, 0xc2, 0x88, 0xe0, 0x00, 0xf0, 0x00, 0x78, 0x00, 0x7c, 0xc0, 0x3f, 0x00, 0x0f
};
// 'Dad_Jokes_Icon', 16x16px
const unsigned char Dad_Jokes_Icon[] PROGMEM = {
0xc0, 0x03, 0x30, 0x0c, 0x0c, 0x30, 0x04, 0x20, 0x72, 0x4e, 0x22, 0x44, 0x01, 0x80, 0x01, 0x80,
0x01, 0x80, 0x19, 0x98, 0xf2, 0x4f, 0xe2, 0x47, 0x84, 0x21, 0x0c, 0x30, 0x30, 0x0c, 0xc0, 0x03
};
// 'Useless_Facts_Icon', 16x16px
const unsigned char Useless_Facts_Icon[] PROGMEM = {
0x00, 0x00, 0x00, 0x40, 0x00, 0x60, 0x00, 0x30, 0x00, 0x18, 0x00, 0x0c, 0x00, 0x0e, 0x00, 0x07,
0x80, 0x03, 0x87, 0x03, 0xcf, 0x01, 0xde, 0x01, 0xfc, 0x00, 0xf8, 0x00, 0x70, 0x00, 0x20, 0x00
};
// 'Quote_Icon', 16x16px
const unsigned char Quote_Icon[] PROGMEM = {
0xcf, 0x03, 0xcf, 0x03, 0xcf, 0x03, 0xc3, 0x00, 0xc3, 0x00, 0x86, 0x01, 0x0c, 0x03, 0x00, 0x00,
0x00, 0x00, 0xc0, 0xf3, 0xc0, 0xf3, 0xc0, 0xf3, 0x00, 0xc3, 0x00, 0xc3, 0x80, 0x61, 0xc0, 0x30
};
// 'Chuck_Icon_new', 16x16px
const unsigned char Chuck_Icon[] PROGMEM = {
0x00, 0x00, 0x42, 0x42, 0xa2, 0x45, 0x16, 0x68, 0xfc, 0x3f, 0x08, 0x10, 0x68, 0x16, 0x08, 0x10,
0x08, 0x11, 0x88, 0x11, 0x08, 0x10, 0xc8, 0x13, 0x28, 0x14, 0x08, 0x10, 0x50, 0x0a, 0xa0, 0x05
};
// Array of all icon bitmaps.
const unsigned char* menu_icon[5] = {
Sunrise_Sunset_Icon,
Dad_Jokes_Icon,
Useless_Facts_Icon,
Quote_Icon,
Chuck_Icon
};
// 'Boarder', 128x20px
const unsigned char Boarder[] PROGMEM = {
0xf8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01,
0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
0xf8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01
};
// 'Scroll Bar', 8x64px
const unsigned char Scroll_Bar[] PROGMEM = {
0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40,
0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40,
0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40,
0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40
};
// 2D array of strings for menu items
const int NUM_ITEMS = 5;
char menu_item[NUM_ITEMS][20] = {
{"Sunrise Sunset"},
{"Dad Jokes"},
{"Useless Facts"},
{"Quotes"},
{"Chuck Jokes"}
};
// === End generated bitmaps from image2cpp ===
// === Start OLED menu variables ===
int item_sel_previous; // menu item before selected
int item_selected = 0; // selected menu item
int item_sel_next; // menu item after selected
#define BUTTON_UP_PIN 27 // pin for UP button
#define BUTTON_SELECT_PIN 25 // pin for SELECT button
#define BUTTON_DOWN_PIN 26 // pin for DOWN button
int button_up_clicked = 0; // only perform action when button is clicked, and wait until another press
int button_select_clicked = 0; // same as above
int button_down_clicked = 0; // same as above
int current_screen = 0; // 0 = menu, 1 = screenshot, 2 = qr code
// === End OLED menu variables ===
// === Start scrolling text variables ===
u8g2_uint_t offset = u8g2.getDisplayWidth(); // start text just off the right edge
//u8g2_uint_t offset; // Tracks the current horizontal position for scrolling animation
u8g2_uint_t width; // Stores the total pixel width of the text string
// === End scrolling text variables ===
void setup(void) {
Serial.begin(9600);
// === Start u8g2 setup ===
u8g2.begin();
u8g2.setBitmapMode(1); // background mode 0 solid, mode 1 transparent
u8g2.setColorIndex(1); // set the color to white
u8g2.setFontMode(0); // enable transparent mode, which is faster
// === End u8g2 setup ===
// === Start WiFi connection ===
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_7x14_tf);
u8g2.drawStr(0,15,"WiFi connecting");
} while (u8g2.nextPage());
delay(1000);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password, 6);
Serial.println(""); // adds a space to serial window
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(50);
Serial.print(".");
}
Serial.print("WiFi connected with IP: ");
Serial.println(WiFi.localIP());
// === End WiFi connection ===
// === Button setup ===
pinMode(BUTTON_UP_PIN, INPUT_PULLUP); // up button
pinMode(BUTTON_DOWN_PIN, INPUT_PULLUP); // down button
pinMode(BUTTON_SELECT_PIN, INPUT_PULLUP); // select button
// === End button setup ===
}
// NEW VOID LOOP
void loop(void) {
// === Handle menu navigation ===
/*
When UP button is pushed, if1, if2 and if5 are true.
When DOWN button is pushed if3, if4, and if6 are true.
*/
if (current_screen == 0) {
if ((digitalRead(BUTTON_UP_PIN) == LOW) && (button_up_clicked == 0)) {
item_selected--;
button_up_clicked = 1;
Serial.println("if1");
if (item_selected < 0) item_selected = NUM_ITEMS - 1;
Serial.println("if2");
}
if ((digitalRead(BUTTON_DOWN_PIN) == LOW) && (button_down_clicked == 0)) {
item_selected++;
button_down_clicked = 1;
Serial.println("if3");
if (item_selected >= NUM_ITEMS) item_selected = 0;
Serial.println("if4");
}
if ((digitalRead(BUTTON_UP_PIN) == HIGH) && (button_up_clicked == 1)) {
button_up_clicked = 0;
Serial.println("if5");
}
if ((digitalRead(BUTTON_DOWN_PIN) == HIGH) && (button_down_clicked == 1)) {
button_down_clicked = 0;
Serial.println("if6");
}
}
// === Handle select button ===
/*
When menu item is selected and ENTER button is pushed,
if7, if8, if10 and if11 are true.
With second press of ENTER button,
if7, if9, if10 and if11 are true.
With third press of ENTER button,
if7, if10 and if11 are true.
When 'Chuck' is selected and ENTER button is pushed,
if7, if8, if10 are true, API is called and then if11 is true.
*/
if ((digitalRead(BUTTON_SELECT_PIN) == LOW) && (button_select_clicked == 0)) {
button_select_clicked = 1;
Serial.println("if7");
if (current_screen == 0) {
current_screen = 1;
Serial.println("if8");
}
else if (current_screen == 1) {
current_screen = 2;
Serial.println("if9");
}
else current_screen = 0;
Serial.println("if10");
}
if ((digitalRead(BUTTON_SELECT_PIN) == HIGH) && (button_select_clicked == 1)) {
button_select_clicked = 0;
Serial.println("if11");
}
// === Compute prev/next items ===
item_sel_previous = item_selected - 1;
if (item_sel_previous < 0) item_sel_previous = NUM_ITEMS - 1;
item_sel_next = item_selected + 1;
if (item_sel_next >= NUM_ITEMS) item_sel_next = 0;
// === Fetch API if needed ===
if (current_screen == 1 && item_selected == 4 && !apiFetched) {
HTTPClient http;
http.begin("https://api.chucknorris.io/jokes/random");
int httpCode = http.GET();
String payload;
if (httpCode == 200) {
payload = http.getString();
Serial.println(payload);
}
http.end();
DynamicJsonDocument doc(1024);
DeserializationError error = deserializeJson(doc, payload);
if (!error) {
apiReply = doc["value"].as<String>();
Serial.println(apiReply);
} else {
apiReply = "JSON error!";
}
apiFetched = true;
//offset = 0; // reset marquee offset
offset = u8g2.getDisplayWidth();
replyWidth = u8g2.getUTF8Width(apiReply.c_str()); // get pixel width of string
}
// === Marquee scrolling logic (replaces old "Control scrolling speed") ===
if (current_screen == 1 && item_selected == 4 && apiFetched) {
offset -= 3; // step size (higher = faster)
if ((u8g2_uint_t)offset < (u8g2_uint_t) - replyWidth) {
offset = u8g2.getDisplayWidth(); // reset when fully scrolled off
}
}
// === Draw everything in one page cycle ===
u8g2.firstPage();
do {
if (current_screen == 0) {
// Menu
u8g2.drawXBMP(0, 22, 128, 20, Boarder);
u8g2.setFont(u8g2_font_7x14_tf);
u8g2.drawStr(26, 15, menu_item[item_sel_previous]);
u8g2.drawXBMP(4, 2, 16, 16, menu_icon[item_sel_previous]);
u8g2.setFont(u8g2_font_7x14B_tf);
u8g2.drawStr(25, 37, menu_item[item_selected]);
u8g2.drawXBMP(4, 24, 16, 16, menu_icon[item_selected]);
u8g2.setFont(u8g2_font_7x14_tf);
u8g2.drawStr(26, 59, menu_item[item_sel_next]);
u8g2.drawXBMP(4, 46, 16, 16, menu_icon[item_sel_next]);
u8g2.drawXBMP(120, 0, 8, 64, Scroll_Bar);
u8g2.drawBox(125, 64 / NUM_ITEMS * item_selected, 3, 64 / NUM_ITEMS);
}
/*
else if (current_screen == 1 && item_selected == 4 && apiFetched) {
// ✅ Joke text with marquee effect
u8g2.setFont(u8g2_font_ncenB08_tr);
int x = offset;
do {
u8g2.drawUTF8(x, 32, apiReply.c_str());
x += replyWidth; // repeat text so it wraps
} while (x < u8g2.getDisplayWidth());
}
*/
else if (current_screen == 1 && item_selected == 4) {
if (apiFetched) {
// Joke text with marquee effect
u8g2.setFont(u8g2_font_ncenB08_tr);
int x = offset;
do {
u8g2.drawUTF8(x, 32, apiReply.c_str());
x += replyWidth + 20; // add some spacing between repeats
} while (x < u8g2.getDisplayWidth() + replyWidth); // extend the loop condition
} else {
// Show loading message while API is being fetched
u8g2.setFont(u8g2_font_7x14_tf);
u8g2.drawStr(20, 32, "Loading joke...");
}
}
else if (current_screen == 2) {
// placeholder for QR code screen, etc.
}
} while (u8g2.nextPage());
// Reset API flag if leaving joke screen
if (current_screen != 1) {
apiFetched = false;
}
}
// END NEW VOID LOOP
/*
// VOID LOOP OLD
void loop(void) {
// === Start API menu ===
if (current_screen == 0) { // MENU SCREEN
if ((digitalRead(BUTTON_UP_PIN) == LOW) && (button_up_clicked == 0)) { // up button pressed
item_selected = item_selected - 1; // select previous item
button_up_clicked = 1; // set button to clicked to only perform the action once
if (item_selected < 0) { // if first item was selected, jump to last item
item_selected = NUM_ITEMS - 1;
}
}
if ((digitalRead(BUTTON_DOWN_PIN) == LOW) && (button_down_clicked == 0)) { // down button pressed
item_selected = item_selected + 1; // select next item
button_down_clicked = 1; // set button to clicked to only perform the action once
if (item_selected >= NUM_ITEMS) { // last item was selected, jump to first menu item
item_selected = 0;
}
}
if ((digitalRead(BUTTON_UP_PIN) == HIGH) && (button_up_clicked == 1)) { // unclick
button_up_clicked = 0;
}
if ((digitalRead(BUTTON_DOWN_PIN) == HIGH) && (button_down_clicked == 1)) { // unclick
button_down_clicked = 0;
}
}
// This section toggles through enter button selections by changing variable 'current_screen' value to 0, 1 or 2
if ((digitalRead(BUTTON_SELECT_PIN) == LOW) && (button_select_clicked == 0)) { // select button clicked, jump between screens
button_select_clicked = 1; // set button to clicked to only perform the action once
if (current_screen == 0) {current_screen = 1;} // menu items screen --> screenshots screen
else if (current_screen == 1) {current_screen = 2;} // screenshots screen --> qr codes screen
else {current_screen = 0;} // qr codes screen --> menu items screen
}
if ((digitalRead(BUTTON_SELECT_PIN) == HIGH) && (button_select_clicked == 1)) { // unclick
button_select_clicked = 0;
}
// set correct values for the previous and next items
item_sel_previous = item_selected - 1; // = 0 - 1 or -1
if (item_sel_previous < 0) { // Yes, -1 is < 0
item_sel_previous = NUM_ITEMS - 1; // NUM_ITEMS(4) - 1 = 3
}
item_sel_next = item_selected + 1; // = 0 + 1 or 1
if (item_sel_next >= NUM_ITEMS) { // No, 1 is not > 4
item_sel_next = 0; // next item would be after last = make it the first
}
// === Start of oled menu screen ===
u8g2.firstPage(); // required for page drawing, mode 1
do { // do current_screen == 0
if (current_screen == 0) { // MENU SCREEN
//u8g2.drawXBMP()
//x: X-position.
//y: Y-position.
//w: Width of the bitmap.
//h: Height of the bitmap.
//bitmap: Pointer to the start of the bitmap.
// selected menu item boarder
u8g2.drawXBMP( 0, 22, 128, 20, Boarder);
// draw previous item as icon + label
u8g2.setFont(u8g2_font_7x14_tf);
u8g2.drawStr(26, 15, menu_item[item_sel_previous]);
// From top left corner - over, down, horz size, vert size)
// u8g2.drawXBMP(, over, down, horz, vert);
u8g2.drawXBMP(4, 2, 16, 16, menu_icon[item_sel_previous]);
// draw selected item as icon + label in bold font
u8g2.setFont(u8g2_font_7x14B_tf);
u8g2.drawStr(25, 37, menu_item[item_selected]);
u8g2.drawXBMP(4, 24, 16, 16, menu_icon[item_selected]);
// draw next item as icon + label
u8g2.setFont(u8g2_font_7x14_tf);
u8g2.drawStr(26, 59, menu_item[item_sel_next]);
u8g2.drawXBMP(4, 46, 16, 16, menu_icon[item_sel_next]);
// draw scrollbar background
u8g2.drawXBMP(120, 0, 8, 64, Scroll_Bar);
// draw scrollbar handle
u8g2.drawBox(125, 64/NUM_ITEMS * item_selected, 3, 64/NUM_ITEMS);
}
// === Start http request and response ===
else if (current_screen == 1 && item_selected == 4) { // MENU OPEN SCREEN
if (!apiFetched) {
// Only do this once!
HTTPClient http; // Initiate HTTP client
http.begin("https://api.chucknorris.io/jokes/random"); // The API URL
int httpCode = http.GET();
String payload;
if (httpCode == 200) {
payload = http.getString(); // raw JSON string
Serial.println(payload);
}
http.end();
DynamicJsonDocument doc(1024);
DeserializationError error = deserializeJson(doc, payload); // DeserializationError converts to boolean
if (!error) {
// apiReply = doc["value"]; // try this
apiReply = doc["value"].as<String>();
Serial.println(apiReply);
} else {
apiReply = "JSON error!";
}
apiFetched = true; // ✅ Don’t fetch again until reset
x = 128; // reset to start from the right edge
}
/*
// === Run 'Scrolling Quote' Test ===
u8g2_uint_t xx; // declare a local variable x for horizontal positioning
u8g2.firstPage();
do {
// draw the scrolling text at current offset
xx = offset;
u8g2.setFont(u8g2_font_10x20_mr); // set the target font
do { // repeated drawing of the scrolling text...
u8g2.drawUTF8(xx, 30, text); // draw the scolling text
xx += width; // add the pixel width of the scrolling text
} while (xx < u8g2.getDisplayWidth()); // draw again until the complete display is filled
u8g2.setFont(u8g2_font_10x20_mr); // draw the current pixel width
u8g2.setCursor(0, 58);
u8g2.print(width); // this value must be lesser than 128 unless U8G2_16BIT is set
} while (u8g2.nextPage());
offset -= 10; // speed adjust, lower number slower, higher number faster
if ((u8g2_uint_t) offset < (u8g2_uint_t) - width)
offset = 0; // start over again
}
// === End 'Scrolling Quote' Test ===
*/
/*
// START using delay() to scroll
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_ncenB08_tr);
// draw at position x
//u8g2.drawStr(x, 32, myChar); // only works with const char*
u8g2.drawStr(x, 32, apiReply.c_str()); // only works with String
} while (u8g2.nextPage());
// move left each frame
// counts down from 128 to -90
x -= 3;
// Serial.print("x: "); Serial.println(x);
// getStrWidth = -90
// only works with const char*
//if (x < -u8g2.getStrWidth(myChar)) { // only works with const char*
if (x < -u8g2.getStrWidth(apiReply.c_str())) { // only works with String
x = 128; // reset to right side
}
delay(0); // controls scroll speed
// END using delay() to scroll
*/
/*
// START using millis() to scroll
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_ncenB08_tr);
// draw at position x
u8g2.drawStr(x, 32, apiReply.c_str()); // only works with String
} while (u8g2.nextPage());
//if (x < -u8g2.getStrWidth(apiReply.c_str())) { // only works with String
//x = 128; // reset to right side
//}
if (millis() - lastScroll >= scrollInterval) {
lastScroll = millis();
x--; // move left one pixel
if (x < -u8g2.getStrWidth(apiReply.c_str())) { // only works with String
x = 128; // reset to right side
}
}
// END using millis() to scroll
*/
/*
} // end of 'else if' MENU OPEN SCREEN
// === End http request and response ===
if (current_screen != 1) {
apiFetched = false;
}
} while (u8g2.nextPage()); // do current_screen == 0
// === End of oled menu screen ===
//else if (current_screen == 2) { // SUB MENU OPEN SCREEN
// Do something - "Display second item"
//}
// delay(0);
//} // end of void loop()
// END VOID LOOP OLD
*/
/*
// START V1.0
HTTPClient http; // Initiate HTTP client
String request = "https://api.chucknorris.io/jokes/random"; // The API URL
http.begin(request); // Start the request
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_t0_11_mr);
u8g2.setCursor(0, 10);
u8g2.print("HTTP request...");
//u8g2.drawStr(0, 15, "HTTP request...");
Serial.println("Making HTTP request...");
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
u8g2.setCursor(0, 20);
u8g2.print("HTTP successful!");
//u8g2.drawStr(0, 25, "HTTP successful!");
Serial.println("HTTP request successful!");
} else {
Serial.print("HTTP request failed, error: ");
Serial.println(httpResponseCode);
}
} while (u8g2.nextPage());
http.GET(); // Use HTTP GET request
String response = http.getString(); // Response from server
// Parse JSON, read error if any
DeserializationError error = deserializeJson(myDoc, response); // DeserializationError converts to boolean
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str());
}
else {
Serial.print(F("deserializeJson() passed: "));
Serial.println(error.f_str());
}
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_7x14_tf);
String value = myDoc["value"];
Serial.println(value);
u8g2.setCursor(0, 30);
u8g2.print(value);
// Close connection
http.end();
// END V1.0
*/
/*
// === Start http request and response ===
// V2.0
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_ncenB08_tr); // Pick a readable font
u8g2.setCursor(0, 20); // Set X=0, Y=20
u8g2.print("HTTP request..."); // Display first message
} while (u8g2.nextPage());
// --- Here is where you'd perform the actual HTTP request ---
delay(2000); // (Temporary) pause to simulate request time
// After request completes:
u8g2.firstPage(); // firstPage() clears the screen
do {
u8g2.setFont(u8g2_font_ncenB08_tr);
u8g2.setCursor(0, 40);
u8g2.print("HTTP successful!"); // Display success message
} while (u8g2.nextPage());
delay(2000);
// END V2.0
*/
/*
// START V2.1
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_ncenB08_tr);
// draw at position x
//u8g2.drawStr(x, 32, myChar); // only works with const char*
u8g2.drawStr(x, 32, myString.c_str()); // only works with String
} while (u8g2.nextPage());
// move left each frame
// counts down from 128 to -90
x--;
Serial.print("x: "); Serial.println(x);
// getStrWidth = -90
// only works with const char*
//if (x < -u8g2.getStrWidth(myChar)) { // only works with const char*
if (x < -u8g2.getStrWidth(myString.c_str())) { // only works with String
x = 128; // reset to right side
}
delay(0); // controls scroll speed
// END V2.1
*/
/*
// === Run 'Scrolling Quote' Test ===
u8g2_uint_t x; // declare a local variable x for horizontal positioning
u8g2.firstPage();
do {
// draw the scrolling text at current offset
x = offset;
u8g2.setFont(u8g2_font_10x20_mr); // set the target font
do { // repeated drawing of the scrolling text...
u8g2.drawUTF8(x, 30, text); // draw the scolling text
x += width; // add the pixel width of the scrolling text
} while (x < u8g2.getDisplayWidth()); // draw again until the complete display is filled
u8g2.setFont(u8g2_font_10x20_mr); // draw the current pixel width
u8g2.setCursor(0, 58);
u8g2.print(width); // this value must be lesser than 128 unless U8G2_16BIT is set
} while (u8g2.nextPage());
offset -= 10; // speed adjust, lower number slower, higher number faster
if ((u8g2_uint_t) offset < (u8g2_uint_t) - width)
offset = 0; // start over again
}
// === End 'Scrolling Quote' Test ===
*/