logo elektroda
logo elektroda
X
logo elektroda

Web Socket for ESP32 - How to read and send information to a Python server using asyncio

robo1973 828 5
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 20413945
    robo1973
    Level 15  
    Hello !!!

    I am looking for a working web cosked on esp32 to read and send information to the following server in python

    
    import asyncio
    import websockets
    
    async def echo(websocket):
        async for message in websocket:
            print(message)
            await websocket.send("left")
    async def main():
        async with websockets.serve(echo, "192.168.178.69",8084):
            await asyncio.Future()  # run forever
    


    Hello !!!
  • ADVERTISEMENT
  • ADVERTISEMENT
  • ADVERTISEMENT
  • #4 20414738
    ex-or
    Level 28  
    There is a link to an example there.
  • ADVERTISEMENT
  • #5 20415553
    robo1973
    Level 15  
    ex-or wrote:
    There is a link to the example.

    I don't really see the example pos esp23 Can you provide a link ?

    I apply the following code But nothing comes from the python server from the first post to the client ?
    #include <WiFi.h>
    #include <WebSocketClient.h>
    
    const char* ssid     = "SSID HERE";
    const char* password = "PASSWORD HERE";
    char path[] = "/";
    char host[] = "192.168.178.69";
      
    WebSocketClient webSocketClient;
    
    // Use WiFiClient class to create TCP connections
    WiFiClient client;
    
    void setup() {
      Serial.begin(115200);
      delay(10);
    
      // We start by connecting to a WiFi network
    
      Serial.println();
      Serial.println();
      Serial.print("Connecting to ");
      Serial.println(ssid);
      
      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);
      
    
      // Connect to the websocket server
      if (client.connect("192.168.178.69", 8084)) {
        Serial.println("Connected");
      } else {
        Serial.println("Connection failed.");
        while(1) {
          // Hang on failure
        }
      }
    
      // Handshake with the server
      webSocketClient.path = path;
      webSocketClient.host = host;
      if (webSocketClient.handshake(client)) {
        Serial.println("Handshake successful");
      } else {
        Serial.println("Handshake failed.");
        while(1) {
          // Hang on failure
        }  
      }
    
    }
    
    
    void loop() {
      String data;
    
      if (client.connected()) {
        
        webSocketClient.getData(data);
        if (data.length() > 0) {
          Serial.print("Received data: ");
          Serial.println(data);
        }
        
        // capture the value of analog 1, send it along
        pinMode(1, INPUT);
        data = String(analogRead(1));
        
        webSocketClient.sendData(data);
        
      } else {
        Serial.println("Client disconnected.");
        while (1) {
          // Hang on disconnect.
        }
      }
      
      // wait to fully let the client disconnect
      delay(3000);
      
    }
    .
ADVERTISEMENT