86 lines
2.0 KiB
Arduino
86 lines
2.0 KiB
Arduino
#include <WiFi.h>
|
|
#include <WebServer.h>
|
|
#include <ArduinoJson.h>
|
|
|
|
const char* ssid = "PPIA";
|
|
const char* password = "pawelpdaldonejta";
|
|
|
|
WebServer server(80);
|
|
|
|
// Declare the new_image buffer to temporarily store uploaded image
|
|
struct Pixel {
|
|
unsigned short x;
|
|
unsigned short y;
|
|
uint32_t color;
|
|
};
|
|
|
|
Pixel new_image[16][16];
|
|
|
|
void handleRoot() {
|
|
server.send(200, "text/html", index_html);
|
|
}
|
|
|
|
void handleUpload() {
|
|
if (server.hasArg("plain") == false) {
|
|
server.send(400, "text/plain", "body not received");
|
|
return;
|
|
}
|
|
String body = server.arg("plain");
|
|
DynamicJsonDocument doc(8192);
|
|
deserializeJson(doc, body);
|
|
JsonArray arr = doc.as<JsonArray>();
|
|
int row = 0;
|
|
for (JsonVariant val : arr) {
|
|
int col = 0;
|
|
for (JsonVariant val2 : val.as<JsonArray>()) {
|
|
unsigned long color = strtoul(val2.as<const char*>(), NULL, 16);
|
|
new_image[row][col] = { (unsigned short)col, (unsigned short)row, (uint32_t)color };
|
|
col++;
|
|
}
|
|
row++;
|
|
}
|
|
|
|
// Save the new image to the next slot
|
|
if (saved_images_count < 11) { // Ensure we don't overflow (max index is 11)
|
|
saved_images_count++;
|
|
}
|
|
|
|
// Copy new_image data to saved_imaged array
|
|
for (int r = 0; r < 16; r++) {
|
|
for (int c = 0; c < 16; c++) {
|
|
saved_imaged[saved_images_count][r][c] = new_image[r][c].color;
|
|
}
|
|
}
|
|
|
|
pixels.clear();
|
|
drawImageFromSaved(0, 0, saved_images_count);
|
|
pixels.show();
|
|
server.send(200, "text/plain", "OK");
|
|
}
|
|
|
|
void handleShowSaved() {
|
|
pixels.clear();
|
|
drawImageFromSaved(0, 0, 0);
|
|
pixels.show();
|
|
server.send(200, "text/plain", "OK");
|
|
}
|
|
|
|
void start_server() {
|
|
WiFi.begin(ssid, password);
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
delay(1000);
|
|
Serial.println("Connecting to WiFi...");
|
|
}
|
|
Serial.println("Connected to WiFi");
|
|
Serial.println(WiFi.localIP());
|
|
|
|
server.on("/", handleRoot);
|
|
server.on("/show-saved", handleShowSaved);
|
|
server.on("/upload", HTTP_POST, handleUpload);
|
|
server.begin();
|
|
}
|
|
|
|
void handle_server() {
|
|
server.handleClient();
|
|
}
|