// WIFI #include const char* ssid = "ssid"; const char* password = "pass"; //password or empty WiFiClient wifi; // websocket #include WebSocketsServer webSocket = WebSocketsServer(80); //change port of WS host here //serial #include #define TXPORT D1 #define RXPORT D2 SoftwareSerial mySerial(RXPORT, TXPORT); //positions int xAxis = 0, yAxis = 0, ZAxis = 0; int stepMove = 10; //Distance It will move ////////// Code //////////// void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) { switch (type) { case WStype_DISCONNECTED: Serial.printf("[%u] Disconnected!\n", num); break; case WStype_CONNECTED: { IPAddress ip = webSocket.remoteIP(num); Serial.printf("[%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload); // send message to client webSocket.sendTXT(num, "Connected"); } break; case WStype_TEXT: { Serial.printf("[%u] get Text: %s\n", num, payload); String _payload = String((char *) &payload[0]); if (_payload.equals("u")) { // up xAxis -= stepMove; mySerial.printf("G1 X%d\r\n", xAxis); Serial.println("moving X to " + xAxis); } if (_payload.equals("d")) { // down xAxis += stepMove; mySerial.printf("G1 X%d\r\n", xAxis); Serial.println("moving X to " + xAxis); } if (_payload.equals("l")) { // left yAxis -= stepMove; mySerial.printf("G1 Y%d\r\n", yAxis); Serial.println("moving Y to " + yAxis); } if (_payload.equals("r")) { // right yAxis += stepMove; mySerial.printf("G1 Y%d\r\n", yAxis); Serial.println("moving Y to " + yAxis); } if (_payload.equals("w")) { // draw mySerial.printf("G1 Z0\r\n"); Serial.println("moving pen down"); } if (_payload.equals("x")) { // release mySerial.printf("G1 Z10\r\n"); Serial.println("moving pen up"); } break; } } } void connect() { Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); /* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default, would try to act as both a client and an access-point and could cause network-issues with your other WiFi-devices on your WiFi-network. */ WiFi.mode(WIFI_STA); if (password == "") WiFi.begin(ssid); else WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); } void setup() { // Init serial Serial.begin(115200); while (!Serial); // set the data rate for the SoftwareSerial port mySerial.begin(115200); // init wifi connect(); //init websocket Serial.println("Start Websocket Server"); webSocket.begin(); webSocket.onEvent(webSocketEvent); //initgrbl mySerial.write("G1 F3000\n"); delay(500); } void loop() { webSocket.loop(); while (mySerial.available()) Serial.write(mySerial.read()); }