// WIFI #include const char* ssid = "sssid"; const char* password = "password"; WiFiClient wifi; // websocket #include WebSocketsClient webSocket; String WSserver = "agrilabwaag2.azurewebsites.net"; int WSport = 80; //serial #include #define TXPORT D1 #define RXPORT D2 SoftwareSerial mySerial(RXPORT, TXPORT); //positions int xAxis = 0, yAxis = 0, ZAxis = 0; int stepMove = 10; ////////// Code //////////// void webSocketEvent(WStype_t type, uint8_t * payload, size_t lenght) { switch(type) { case WStype_DISCONNECTED: Serial.printf("[WSc] Disconnected!\n"); break; case WStype_CONNECTED: Serial.printf("[WSc] Connected to url: %s\n", payload); break; case WStype_TEXT: { Serial.printf("[Wsc] get Text: %s\n", 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; case WStype_BIN: Serial.printf("[WSc] get binary lenght: %u\n", lenght); hexdump(payload, lenght); break; case WStype_ERROR: Serial.printf("We received an error \n"); break; default: 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 Client"); webSocket.begin(WSserver, WSport); webSocket.onEvent(webSocketEvent); //initgrbl mySerial.write("G1 F3000 \r\n"); delay(500); } void loop() { webSocket.loop(); while (mySerial.available()) Serial.write(mySerial.read()); }