Ive been working with my ESP8266 and have encountered an issue that seems to be coming from nowhere and being unsolvanle. At least - to me.
Basically I went on with this tutorial on building a simple server socket.
However my esp suddenly stopped allowing connections to itself. The code works normally until the line 23 of main.py
:
while True:
conn, addr = s.accept()
And simply stops working or moving ahead in the while loop. Its simply stuck in itself!
There are no error messages. AT least Thonny
doesnt show any.
The funny part is - sometimes it does actually accept the connection. With all the same code and hardware and one of 100 tries maybe.
Ive tried to look up for a similar failure here, on stackoverflow and @Techtician has the same, I believe unsolved problem. (link to his post here)
Let's try and simplify the issue.
Use this simple echo client + server and report back the results.
be sure to replace <CONTROLLER_IP>
with the controller's ip address.
the server should be run on the controller, the client - on your pc.
server:
import socket
HOST = '0.0.0.0'
PORT = 5000
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen()
conn, addr = s.accept()
print("Connection from client", addr)
data = conn.recv(1024)
while data:
conn.sendall(data.decode().upper().encode())
data = conn.recv(1024)
s.close()
client:
import socket
HOST = '<CONTROLLER_IP>'
PORT = 5000
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
print(f"Connected to server {HOST}:{PORT}")
while True:
msg = input("Type Message:") # type some text and hit enter
s.sendall(msg.encode())
data = s.recv(1024)
print('GOT: ', data.decode())
It is crucial to run the server before running the client, otherwise the client's attempt to connect will surely fail.
As vpfb mentioned, a TCP connection is created.
until a connection is created, that s.accept()
is blocking. that is a feature, not a bug.