/************************************************************************************************* PROGRAMMINFO ************************************************************************************************** Funktion: ESP32 WEB Server für die 28BYJ-48 Stepper Motor Ansteuerung ************************************************************************************************** Version: 21.10.2021 ************************************************************************************************** Board: ESP32vn IoT UNO V1.0.4 ************************************************************************************************** C++ Arduino IDE V1.8.13 ************************************************************************************************** Einstellungen: https://dl.espressif.com/dl/package_esp32_index.json http://dan.drown.org/stm32duino/package_STM32duino_index.json https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_dev_index.json ************************************************************************************************** **************************************************************************************************/ #include #include #include #include #include "SPIFFS.h" #include const int stepsPerRevolution = 2048; // change this to fit the number of steps per revolution #define IN1 19 #define IN2 18 #define IN3 5 #define IN4 17 Stepper myStepper(stepsPerRevolution, IN1, IN3, IN2, IN4); String message = ""; // Replace with your network credentials const char* ssid = "xxx"; const char* password = "xxx"; // Create AsyncWebServer object on port 80 AsyncWebServer server(80); // Create a WebSocket object AsyncWebSocket ws("/ws"); //Variables to save values from HTML form String direction ="STOP"; String steps; bool newRequest = false; // Initialize SPIFFS void initSPIFFS() { if (!SPIFFS.begin(true)) { Serial.println("An error has occurred while mounting SPIFFS"); } else{ Serial.println("SPIFFS mounted successfully"); } } // Initialize WiFi 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(String state) { ws.textAll(state); } 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; steps = message.substring(0, message.indexOf("&")); direction = message.substring(message.indexOf("&")+1, message.length()); Serial.print("steps"); Serial.println(steps); Serial.print("direction"); Serial.println(direction); notifyClients(direction); newRequest = true; } } 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()); //Notify client of motor current state when it first connects notifyClients(direction); 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); } void setup() { // Serial port for debugging purposes Serial.begin(115200); initWiFi(); initWebSocket(); initSPIFFS(); myStepper.setSpeed(5); // Web Server Root URL server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ request->send(SPIFFS, "/index.html", "text/html"); }); server.serveStatic("/", SPIFFS, "/"); server.begin(); } void loop() { if (newRequest){ if (direction == "CW"){ myStepper.step(steps.toInt()); Serial.print("CW"); } else{ myStepper.step(-steps.toInt()); } newRequest = false; notifyClients("stop"); } ws.cleanupClients(); }