'Error when recieving data from a websocket server in python
Im trying to send information from my esp8266 to my python code through a tcp/ip websocket. When I start the code, in the esp serial appears this message:
GET / HTTP/1.1
Upgrade: websocket
Host: 192.168.0.67:23
Origin: http://192.168.0.67:23
Sec-WebSocket-Key: P0WRkkqOUSv05V1sTn8Lxw==
Sec-WebSocket-Version: 13
Connection: Upgrade
I dont have any idea of what does that means but up to there the code works normally. The problem is sending data between each other (The esp is the server and the python code is the client) because if I try to send data from python to the esp, the esp wont recieve anything, and when I try to send info from the esp to the python code, the connection closes with this message:
Traceback (most recent call last):
File "C:\Users\toada\AppData\Local\Programs\Python\Python310\testlel.py", line 3, in <module>
ws.connect("ws://192.168.0.67:23")
File "C:\Users\toada\AppData\Local\Programs\Python\Python310\lib\site-packages\websocket\_core.py", line 248, in connect
self.handshake_response = handshake(self.sock, url, *addrs, **options)
File "C:\Users\toada\AppData\Local\Programs\Python\Python310\lib\site-packages\websocket\_handshake.py", line 57, in handshake
status, resp = _get_resp_headers(sock)
File "C:\Users\toada\AppData\Local\Programs\Python\Python310\lib\site-packages\websocket\_handshake.py", line 146, in _get_resp_headers
status, resp_headers, status_message = read_headers(sock)
File "C:\Users\toada\AppData\Local\Programs\Python\Python310\lib\site-packages\websocket\_http.py", line 321, in read_headers
status = int(status_info[1])
IndexError: list index out of range
Can someone tell me whats wrong please? I just want to have the input of the esp as a string
This is the code that I just tested:
import websocket
ws = websocket.WebSocket()
ws.connect("ws://echo.websocket.events")
print(ws.recv())
ws.close()
And here is the arduino code for the NodeMCU:
/*
* Copyright (c) 2018, circuits4you.com
* All rights reserved.
* Create a TCP Server on ESP8266 NodeMCU.
* TCP Socket Server Send Receive Demo
*/
#include <ESP8266WiFi.h>
#define SendKey 0 //Button to send data Flash BTN on NodeMCU
int port = 23; //Port number
WiFiServer server(port);
//Server connect to WiFi Network
const char *ssid = "e"; //Enter your wifi SSID
const char *password = "e"; //Enter your wifi Password
int count=0;
//=======================================================================
// Power on setup
//=======================================================================
void setup()
{
Serial.begin(115200);
pinMode(SendKey,INPUT_PULLUP); //Btn to send data
Serial.println();
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password); //Connect to wifi
// Wait for connection
Serial.println("Connecting to Wifi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
Serial.print("Open Telnet and connect to IP:");
Serial.print(WiFi.localIP());
Serial.print(" on port ");
Serial.println(port);
}
//=======================================================================
// Loop
//=======================================================================
void loop()
{
WiFiClient client = server.available();
if (client) {
if(client.connected())
{
Serial.println("Client Connected");
}
while(client.connected()){
while(client.available()>0){
// read data from the connected client
Serial.write(client.read());
}
//Send Data to connected client
while(Serial.available()>0)
{
client.write(Serial.read());
}
}
client.stop();
Serial.println("Client disconnected");
}
}
//=======================================================================
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
