Scroll LED over webserver success!

// Code for the Uno R4 WiFi to create a simple webserver with prompts to choose what to display on the onboard 96-LED Matrix. Of course, fill in the “INSERT MESSAGE" fields and use the traditional ArduinoSecrets file to add your WiFi credentials
// Authored by Dave Gettler, October 1, 2024

#include "WiFiS3.h"
#include "arduino_secrets.h"
#include "ArduinoGraphics.h"
#include "Arduino_LED_Matrix.h"

char ssid[] = SECRET_SSID;        // your network SSID (name)
char pass[] = SECRET_PASS;    // your network password (use for WPA, or use as key for WEP)

WiFiServer server(80);
ArduinoLEDMatrix matrix;

int currentMessageIndex = 0; // Index to track the current message

// Add a new message for prompt
const char* messages[] = {
    " CHOOSE A MESSAGE ", // New prompt message, feel free to personalize
    "INSERT LED MESSAGE1",
    "INSERT LED MESSAGE2",
    "INSERT LED MESSAGE3",
    "INSERT LED MESSAGE4"
};
const int numMessages = sizeof(messages) / sizeof(messages[0]);

void setup() {
    Serial.begin(115200);
    while (!Serial) {
        ; // wait for serial port to connect. Needed for native USB port only
    }

    // Connect to WiFi
    WiFi.begin(ssid, pass);
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }
    Serial.println("WiFi connected");

    server.begin();
    Serial.println("HTTP server started");

    matrix.begin();
    // Set to show "CHOOSE A MESSAGE" initially
    currentMessageIndex = 0; // Now points to the "CHOOSE A MESSAGE" prompt
}

void loop() {
    WiFiClient client = server.available(); // Check for incoming clients
    if (client) {
        String request = client.readStringUntil('\r'); // Read the request
        client.flush(); // Clear the client buffer

        // Handle the root request
        if (request.indexOf("GET / ") != -1) {
            handleRoot(client);
        }
        // Handle the message request
        else if (request.indexOf("GET /message?msg=") != -1) {
            handleMessage(request, client);
        }
       
        client.stop(); // Close the connection
    }

    // Draw the current message
    matrix.beginDraw();
    matrix.stroke(0xFFFFFFFF);
    matrix.textScrollSpeed(70);

    const char* text = messages[currentMessageIndex];
    matrix.textFont(Font_5x7);
    matrix.beginText(0, 1, 0xFFFFFF);
    matrix.println(text);
    matrix.endText(SCROLL_LEFT);
   
    matrix.endDraw();
}

void handleRoot(WiFiClient& client) {
    String html = "<html><body><h1>Message Controller</h1>";
    for (int i = 1; i < numMessages; i++) { // Start from 1 to skip the prompt
        html += "<a href='/message?msg=" + String(i) + "'>Message " + String(i) + "</a><br>";
    }
    html += "</body></html>";
   
    client.println("HTTP/1.1 200 OK");
    client.println("Content-Type: text/html");
    client.println("Connection: close");
    client.println();
    client.println(html);
}

void handleMessage(String request, WiFiClient& client) {
    int msgIndex = request.indexOf("msg=") + 4; // Find the message index
    if (msgIndex != -1) {
        String msg = request.substring(msgIndex);
        currentMessageIndex = msg.toInt(); // Get the message index from the URL
        if (currentMessageIndex < 1 || currentMessageIndex >= numMessages) {
            currentMessageIndex = 0; // Default to the prompt if out of bounds
        }
    }
    // Redirect back to the root
    client.println("HTTP/1.1 302 Found");
    client.println("Location: /");
    client.println("Connection: close");
    client.println();
}

void displayMessage(const char* text) {
    matrix.beginDraw();
    matrix.stroke(0xFFFFFFFF);
    matrix.textFont(Font_4x6);
    matrix.beginText(0, 1, 0xFFFFFF);
    matrix.println(text);
    matrix.endText();
    matrix.endDraw();
}
1 Like