Wlan communication problems
-
Thanks @jcaron, it was the newline problem. Now that the client is working ok, I'll try to fix the server on wipy. I'll report how it goes.
-
For the server I'm trying with this:
from network import WLAN import time import machine import socket wlan = WLAN(mode=WLAN.STA_AP, ssid='test', auth=(WLAN.WPA2,'testpass')) time.sleep(6) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) print("socket created") server_address = socket.getaddrinfo('localhost', 1234)[0][-1] sock.bind(server_address) print("socket bound") sock.listen(1) print("listening") connection, client_address = sock.accept() print("connection from "+str(client_address)) while True: data = connection.recv(16) if data: print("received "+str(data)) connection.sendall("ok!") print("sent response") break connection.close() socket.close() print("end")I guess the ip of the server should be 192.168.4.1? Access point is working and client connects to it ok. No error is reported on the server, on client it crashes on sock.connect(server_address) with OSError: [Errno 104] ECONNRESET. I tried also to use my existing wifi network, so that both client and server connect to it (server not used as an access point), but I'm still receiving the same error. Since the client should be working now, I guess I'm missing something on the server side. Last thing it outputs is "listening", so it's waiting on sock.accept().
-
@lmo never tried having a *Py act as a TCP server, but I don’t think
localhostis the appropriate choice. That should restrict connections to only local ones.You probably want to use either the IP address or
0.0.0.0. Not sure if there is a more appropriate way of writing this.The error should have been ECONNREFUSED rather than ECONNRESET, though, but that may be a minor deviation.
-
Thanks again @jcaron, 0.0.0.0 or ip (192.168.4.1) instead of localhost did resolve the problem, and server responds properly now.
I have one more issue to resolve. About a minute after server is set up, no matter if it's still waiting for a client to connect or if it's already finished with execution (and sockets were closed) it crashes with:Unhandled exception in callback handler Traceback (most recent call last): File "_pybytes.py", line 495, in __smart_config_callback TimeoutError: Connection to AP Timeout!How should this be handled?
-
@lmo I'd say you should disable pybytes. Depending on the firmware version, this is done by either using a different image or modifying settings (see https://forum.pycom.io/topic/5649/new-firmware-release-1-20-2-rc3 for details on the latter).
-
Dear @lmo,
@jcaron said in Wlan communication problems:
disable pybytes using a different image
We are regularly building firmware images not including Pybytes, so the most recent WiPy-1.20.2.rc3-0.8.0-vanilla-squirrel.tar.gz might make you happy.
With kind regards,
Andreas. -
Thanks @jcaron and @andreas, with this vanilla firmware the crash is gone. But I'm having some problems with bluetooth on newer firmware versions. I'll ask about that in another post. For anyone looking for example of just using the wlan, here is the code for a simple one time send and receive a response.
Client:
from network import WLAN import time import machine import socket wlan = WLAN(mode=WLAN.STA) time.sleep(6) wlan.connect('test', auth=(WLAN.WPA2, 'testpass'), timeout=5000) while not wlan.isconnected(): machine.idle() print('wlan connected '+str(wlan.ifconfig())) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_address = socket.getaddrinfo('192.168.4.1', 1234)[0][-1] sock.connect(server_address) message = 'test message' sock.sendall(message) print("sent "+message) bytes_received = 0 while bytes_received < 3: data = sock.recv(3) bytes_received += len(data) print("received "+str(data.decode())) sock.close()Server:
from network import WLAN import time import machine import socket wlan = WLAN(mode=WLAN.STA_AP, ssid='test', auth=(WLAN.WPA2,'testpass')) time.sleep(6) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_address = socket.getaddrinfo('0.0.0.0', 1234)[0][-1] sock.bind(server_address) sock.listen(1) print("listening") connection, client_address = sock.accept() print("connection from "+str(client_address)) while True: data = connection.recv(16) if data: print("received "+str(data)) response = "ok!" connection.sendall(response) print("sent "+response) break connection.close() sock.close() -
@jcaron Thank you! This helped me to figure out that when starting we have to use the scan in a try-except logic inside a while because the firsts attempts of scan get nothing and rather than returning an empty array, it arises an exception like this:
OSError: Scan operation Failed! -
it works on the 1.19.x firmware, but not on the 1.20.x, wl.scan() finds 0 networks.
-
This post is deleted!
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login