How do you use the ESPAsyncWebServer with templates on a SD card I can get templates working fine on the ESP32 but not from SD card.
Welcome to the forum
Please post your best effort at getting it to work, any error messages that you receive when compiling or if no errors occur describe what happens
Now I have the templates working but the websockets no longer work the server sees the /ws
// Import required libraries
#include <Arduino.h>
#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include "FS.h"
#include "SD.h"
#include "SPI.h"
// Replace with your network credentials
//const char* ssid = "myssid";
//const char* password = "password";
bool ledState = 0;
const int ledPin = 2;
char Str1[12];
// Create AsyncWebServer object on port 80
AsyncWebServer server(80);
AsyncWebSocket ws("/ws");
void initSDCard(){
Serial.println("Card Mount");
if(!SD.begin()){
Serial.println("Card Mount Failed");
return;
}
uint8_t cardType = SD.cardType();
if(cardType == CARD_NONE){
Serial.println("No SD card attached");
return;
}
Serial.print("SD Card Type: ");
if(cardType == CARD_MMC){
Serial.println("MMC");
} else if(cardType == CARD_SD){
Serial.println("SDSC");
} else if(cardType == CARD_SDHC){
Serial.println("SDHC");
} else {
Serial.println("UNKNOWN");
}
uint64_t cardSize = SD.cardSize() / (1024 * 1024);
Serial.printf("SD Card Size: %lluMB\n", cardSize);
}
void initWiFi() {
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi ..");
while (WiFi.status() != WL_CONNECTED) {
Serial.print('.');
delay(1000);
}
Serial.println(WiFi.localIP());
}
void notifyClients() {
ws.textAll(String(ledState));
}
void handleWebSocketMessage(void *arg, uint8_t *data, size_t len) {
AwsFrameInfo *info = (AwsFrameInfo*)arg;
if (info->final && info->index == 0 && info->len == len && info->opcode == WS_TEXT) {
data[len] = 0;
if (strcmp((char*)data, "toggle") == 0) {
ledState = !ledState;
notifyClients();
}
}
}
void onEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type,
void *arg, uint8_t *data, size_t len) {
Serial.printf("WebSocket");
switch (type) {
case WS_EVT_CONNECT:
Serial.printf("WebSocket client #%u connected from %s\n", client->id(), client->remoteIP().toString().c_str());
//os_printf("ws[%s][%u] connect\n", server->url(), client->id());
client->printf("Hello Client %u :)", client->id());
client->ping();
break;
case WS_EVT_DISCONNECT:
Serial.printf("WebSocket client #%u disconnected\n", client->id());
break;
case WS_EVT_DATA:
handleWebSocketMessage(arg, data, len);
break;
case WS_EVT_PONG:
case WS_EVT_ERROR:
break;
}
}
void initWebSocket() {
ws.onEvent(onEvent);
server.addHandler(&ws);
}
String processor(const String& var){
Serial.println(var);
if(var == "STATE"){
if (ledState){
return F("ON");
return String();
}
else{
return F("OFF");
return String();
}
}
//return String();
}
void setup(){
// Serial port for debugging purposes
Serial.begin(115200);
initWiFi();
initSDCard();
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
// Start server
server.begin();
server.on("/index.html", HTTP_GET, [](AsyncWebServerRequest *request){
Serial.println("index.html");
request->send(SD,"/index.html", String(), false, processor);
//});
//this showed /ws was seen
// server.on("/ws", HTTP_GET, [](AsyncWebServerRequest *request){
// Serial.println("/ws sent");
//request->send(SD,"/home.html")
});
}
void loop() {
ws.cleanupClients();
digitalWrite(ledPin, ledState);
}
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.