HELP! Connecting Two Pico W's One in AP the Other in Station Issues

The Raspberry Pi is a series of credit card-sized single-board computers developed in the United Kingdom by the Raspberry Pi Foundation to promote the teaching of basic computer science in schools and developing countries.

Post Reply
User avatar
/RaspberryPi
Corporate
Posts: 2449
Joined: Wed Jun 05, 2019 1:29 am

HELP! Connecting Two Pico W's One in AP the Other in Station Issues

Post by /RaspberryPi »


Hello Raspberry Pi Community,

Running into an issue that I have not found anyone online able to solve.

I have a system of 2 Raspberry Pi Pico W's that are powered by the same computer, however, I am using separate instances of Thonny to run client and server code on each one respectively.

The first Pico W is running the following code

import network import time import socket import gc import rp2 # Set the country code to the United States rp2.country('US') gc.collect() # if you do not see the network you may have to power cycle # unplug your pico w for 10 seconds and plug it in again def ap_mode(ssid, password): """ Description: This is a function to activate AP mode Parameters: ssid[str]: The name of your internet connection password[str]: Password for your internet connection Returns: Nothing """ # Just making our internet connection ap = network.WLAN(network.AP_IF) ap.config(essid=ssid, password=password) ap.active(True) while ap.active() == False: pass import time print('AP Mode Is Active, You can Now Connect') print('IP Address To Connect to:: ' + ap.ifconfig()[0]) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #creating socket object s.bind(('', 80)) s.listen(5) while True: conn, addr = s.accept() print('Got a connection from %s' % str(addr)) request = conn.recv(1024).decode('utf-8') print('Content = %s' % str(request)) response = 'true' conn.send(response) conn.close() ap_mode('NAME', 'PASSWORD') While the second one is connected to the AP Mode Raspberry Pi Pico W as follows

import socket import time import network import urequests import gc import rp2 # Set the country code to the United States rp2.country('US') gc.collect() def connect_to_pico_w(): try: wlan = network.WLAN(network.STA_IF) wlan.active(True) wlan.connect("NAME", "PASSWORD") while not wlan.isconnected(): pass print('connected') socket_host = "192.168.4.1" ai = socket.getaddrinfo(socket_host, 80) # Address of Web Server response = urequests.get('http://192.168.4.1:80') response.close() except Exception as e: print("Error occurred:", str(e)) connect_to_pico_w() Now what happens is, when I first boot the system (turn on both devices by plugging them in), I can send the string across the WebSocket. However the issue arises when I try to rerun the station mode Pico W to send the same message, it simply times out with an "error occurred: [errno 103]"

This is strange that I cannot do this more than once, although I close the connection on the server side Pico W.

To get it to work again I need to powercycle the Pico Ws and rerun the code, but obviously, that is not realistic. Sometimes it manages to be able to run twice in a row, but not more than that as far as I am seen.

Why is it refusing to connect to the socket multiple times? Has anyone had experience with sending information between two Pico W's, one in AP Mode and the other in Station Mode? Thought this would be a simple thing but looks like many people online have been having issues with this with no resolution and I want to get to the bottom of it. Could be simple and I am just overlooking something. All help is highly appreciated!
submitted by /u/QuietRing5299
[link] [comments]

Source: https://www.reddit.com/r/raspberry_pi/c ... the_other/
/RaspberryPi
Post Reply

Return to “Raspberry Pi Forum”