websockets.exceptions.ConnectionClosedError: code = 1006 (connection closed abnormally [internal]), no reason Python 3.7 websockets

Viewed 1661

I have the same problem as this, 1006 Connection closed abnormally error with python 3.7 websockets but my use case is different. So please help me out on this.

I am running a python websocket server. This is my code,

import asyncio
import websockets

async def echo(websocket, path):
    async for message in websocket:
        await websocket.send(message)

start_server = websockets.serve(echo, "192.168.#.##", 5000)

asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()

I connect to my server using a ESP32. The connection goes well and the server echoes back the message sent from the ESP32 for about 30 seconds and the server stops at once. This happens regularly. This is the error I receive websockets.exceptions.ConnectionClosedError: code = 1006 (connection closed abnormally [internal]), no reason

My ESP32 code is as follows

#include <WiFi.h>
#include <WebSocketClient.h>
#include <ArduinoJson.h> 
#include <ESP32Servo.h> 
const char* ssid     = "####";
const char* password = "####";
 
char path[] = "/";
char host[] = "192.168.#.##";

int M1A = 16; //motor drive input pins
int M1B = 17;
int M2A = 18;
int M2B = 27;
 
WebSocketClient webSocketClient;
WiFiClient client;

void connnect(){
   if (client.connect(host, 5000)) {
    Serial.println("Connected");
  } else {
    Serial.println("Connection failed.");
  }
 
  webSocketClient.path = path;
  webSocketClient.host = host;
  if (webSocketClient.handshake(client)) {
    Serial.println("Handshake successful");
  } else {
    Serial.println("Handshake failed.");
  }
}
void setup() {
  Serial.begin(115200);
 
  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());
 
  delay(5000);
 
 connnect();
 pinMode(M1A, OUTPUT); //configure as inputs
  pinMode(M1B, OUTPUT);
  pinMode(M2A, OUTPUT);
  pinMode(M2B, OUTPUT);
 
}
 
void loop() {
   String data;
  
    if (client.connected()) {
   
    webSocketClient.sendData("right");
    webSocketClient.getData(data);
    if (data!= NULL){
      if (data =="right"){
        Serial.println("right");
        analogWrite(M1A, 160);//pwm signal
        analogWrite(M1B, 0);
        analogWrite(M2A, 255);
        analogWrite(M2B, 255);
        delay(100);
      }else if (data =="left"){
        Serial.println("left");
        analogWrite(M1A, 255);//pwm signal
        analogWrite(M1B, 255);
        analogWrite(M2A, 160);
        analogWrite(M2B, 0);
        
      }else if (data =="forward"){
        Serial.println("forward");
        analogWrite(M1A, 160);//pwm signal
        analogWrite(M1B, 0);
        analogWrite(M2A, 160);
        analogWrite(M2B, 0);
        
      }else{
         Serial.println("stop");
         analogWrite(M1A, 255);//pwm signal
         analogWrite(M1B, 255);
         analogWrite(M2A, 255);
         analogWrite(M2B, 255);
      }
    }
 
  } else {
    Serial.println("Client disconnected.");
    connnect();
  }
 
  delay(3000);
 
}

Thanks in advance

0 Answers
Related