#include <WiFi.h>
#include <HTTPClient.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// WiFi credentials for Wokwi
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Message endpoint URL
const char* messageURL = "https://pastebin.com/raw/YhQmyjJe";
// For testing, check every 30 seconds instead of 15 minutes
const int sleepSeconds = 30;
String lastMessage = "";
void setup() {
Serial.begin(115200);
delay(1000);
// Initialize I2C
Wire.begin(9, 8); // SDA=GPIO9, SCL=GPIO8
// Initialize display
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println("SSD1306 allocation failed");
for(;;);
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println("Starting...");
display.display();
// Connect to WiFi
connectWiFi();
// Fetch and display message
String message = fetchMessage();
if (message != "" && message != lastMessage) {
displayMessage(message);
lastMessage = message;
}
// In real hardware this would sleep, but for Wokwi we'll loop
Serial.println("Waiting " + String(sleepSeconds) + " seconds...");
delay(sleepSeconds * 1000);
// Reset to check again
ESP.restart();
}
void loop() {
// Never reached - restarts after setup()
}
void connectWiFi() {
Serial.println("Connecting to WiFi...");
display.clearDisplay();
display.setCursor(0, 0);
display.println("Connecting to");
display.println("WiFi...");
display.display();
WiFi.begin(ssid, password);
int attempts = 0;
while (WiFi.status() != WL_CONNECTED && attempts < 20) {
delay(500);
Serial.print(".");
display.print(".");
display.display();
attempts++;
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println("\nWiFi connected!");
display.clearDisplay();
display.setCursor(0, 0);
display.println("WiFi Connected!");
display.display();
delay(1000);
} else {
Serial.println("\nWiFi failed");
display.clearDisplay();
display.setCursor(0, 0);
display.println("WiFi Failed!");
display.display();
}
}
String fetchMessage() {
if (WiFi.status() != WL_CONNECTED) {
return "No WiFi";
}
Serial.println("Fetching message...");
display.clearDisplay();
display.setCursor(0, 0);
display.println("Fetching...");
display.display();
HTTPClient http;
http.begin(messageURL);
int httpCode = http.GET();
String message = "";
if (httpCode == HTTP_CODE_OK) {
message = http.getString();
message.trim();
Serial.println("Message fetched: " + message);
} else {
Serial.println("HTTP error: " + String(httpCode));
message = "HTTP Error " + String(httpCode);
}
http.end();
return message;
}
void displayMessage(String message) {
display.clearDisplay();
display.setCursor(0, 0);
display.setTextSize(2);
// Simple word wrap
int lineHeight = 16;
int currentY = 0;
String word = "";
String currentLine = "";
for (int i = 0; i <= message.length(); i++) {
char c = (i < message.length()) ? message[i] : ' ';
if (c == ' ' || i == message.length()) {
// Test if adding word exceeds line width
String testLine = currentLine + (currentLine.length() > 0 ? " " : "") + word;
int16_t x1, y1;
uint16_t w, h;
display.getTextBounds(testLine, 0, 0, &x1, &y1, &w, &h);
if (w > SCREEN_WIDTH && currentLine.length() > 0) {
// Print current line and move to next
display.setCursor(0, currentY);
display.println(currentLine);
currentY += lineHeight;
currentLine = word;
} else {
currentLine = testLine;
}
word = "";
} else {
word += c;
}
}
// Print last line
if (currentLine.length() > 0) {
display.setCursor(0, currentY);
display.println(currentLine);
}
display.display();
Serial.println("Display updated with: " + message);
}Loading
xiao-esp32-c3
xiao-esp32-c3