Ok, i was being ambiguous. Thank you for your reply.
So, in blocking mode everything is ok. Changing to non-blocking will require you to do some polling. My problem was that i couldn't find a way to poll. I will explain the problem and the solution i found, in the same time.
First of all, this is what happens when you print the socket:
Code: Select all
>>> print(s)
<socket state=0 timeout=0 incoming=0 off=0>
You (being the client, ESP being the server) initiate a connection and incoming becomes a value larger than 0 (i didn't check what that hex value is), so i was polling the length of this string.
Here comes my problem: You accept the connection and incoming becomes 0 and socket_state becomes 0. But you now don't check s socket anymore, instead, you will check cl socket. Which has socket_state = 2, meaning it's connected.
You send actual data and socket_state of cl becomes 3 and now it's time to extract data.
ESP now send back a response to you and immediately closes cl socket.
Code: Select all
#Start TCP server
addr = socket.getaddrinfo('192.168.0.121', 868)[0][-1]
s = socket.socket()
s.setblocking(False)
s.bind(addr)
s.listen(1)
while True:
start = time.ticks_ms()
socket = (str)(s)
if len(socket) > 43:
cl, addr2 = s.accept()
cl_str = (str)(cl)
while cl_str[14] == "2":
cl_str = (str)(cl)
data = cl.recv(32)
print(data)
cl.close()
delta = time.ticks_diff(time.ticks_ms(), start)
print(delta)
In the meanwhile you can do some other short things, in my case i want to update the status of a PWM pin once 50mS.