#include #include #include #include #include "SPIFFS.h" #include const char* ssid = "ESP32CAM"; const char* password = "12345678"; // Create AsyncWebServer object on port 80 AsyncWebServer server(80); // Create a WebSocket object AsyncWebSocket ws("/ws"); // Set LED GPIO const int ledPin1 = 14; const int ledPin2 = 15; String message = ""; String sliderValue1 = "0"; String sliderValue2 = "0"; String sliderValue3 = "0"; int speed1 = 0; int dutyCycle1; int dutyCycle2; // setting PWM properties const int freq = 2000; const int ledChannel1 = 0; const int ledChannel2 = 1; const int resolution = 8; // Initialize SPIFFS void initFS() { if (!SPIFFS.begin()) { Serial.println("An error has occurred while mounting SPIFFS"); } else{ Serial.println("SPIFFS mounted successfully"); } } 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; message = (char*)data; if (message.indexOf("1s") >= 0) { sliderValue1 = message.substring(2); dutyCycle1 = map(sliderValue1.toInt(), -100, 100, 194-speed1, 194+speed1); } if (message.indexOf("2s") >= 0) { sliderValue2 = message.substring(2); dutyCycle2 = map(sliderValue2.toInt(), -100, 100, 194-speed1, 194+speed1); } if (message.indexOf("3s")>= 0){ sliderValue3 = message.substring(2); speed1 = map(sliderValue3.toInt(), 0, 100, 0, 61); } } } void onEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len) { switch (type) { case WS_EVT_CONNECT: Serial.printf("WebSocket client #%u connected from %s\n", client->id(), client->remoteIP().toString().c_str()); break; case WS_EVT_DISCONNECT: Serial.printf("WebSocket client #%u disconnected\n", client->id()); dutyCycle1 = 0; dutyCycle2 = 0; speed1 = 0; 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); } void setup() { Serial.begin(115200); WiFi.softAP(ssid, password); IPAddress miIP = WiFi.softAPIP(); Serial.print("AP IP address: "); Serial.println(miIP); //probar 192.168.4.1 pinMode(ledPin1, OUTPUT); pinMode(ledPin2, OUTPUT); // configure LED PWM functionalitites ledcSetup(ledChannel1, freq, resolution); ledcSetup(ledChannel2, freq, resolution); // attach the channel to the GPIO to be controlled ledcAttachPin(ledPin1, ledChannel1); ledcAttachPin(ledPin2, ledChannel2); initFS(); initWebSocket(); // Web Server Root URL server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ request->send(SPIFFS, "/index.html", "text/html"); }); server.serveStatic("/", SPIFFS, "/"); // Start server server.begin(); } void loop() { ledcWrite(ledChannel1, dutyCycle1); ledcWrite(ledChannel2, dutyCycle2); ws.cleanupClients(); Serial.print(dutyCycle1); Serial.print(F(": ")); Serial.print(dutyCycle2); Serial.print(F(": ")); Serial.println(speed1); delay(200); }