0

I've written a simple python server for an IoT electronics school project. A "raspberry pi 3 model B rev 1.2" acts as the local station/server. I'm looking to create an AP which esp32's connect to, and communicate with the server on port 100 using a custom application level protocol.

The AP is created successfully (most of the time) in a separate thread using the command: sudo create_ap -n --no-dns -g 10.0.0.1 wlan0 test test1234

ron@raspberrypi:~/Desktop/livepark $ ./start_server.sh current working dir: /home/ron/Desktop/livepark starting server... WARN: brmfmac driver doesn't work properly with virtual interfaces and it can cause kernel panic. For this reason we disallow virtual interfaces for your adapter. For more info: https://github.com/oblique/create_ap/issues/203 WARN: Your adapter does not fully support AP virtual interface, enabling --no-virt Config dir: /tmp/create_ap.wlan0.conf.JhUuwR5X PID: 3404 Transmitting to channel 1... No Internet sharing hostapd command-line interface: hostapd_cli -p /tmp/create_ap.wlan0.conf.JhUuwR5X/hostapd_ctrl wlan0: interface state UNINITIALIZED->ENABLED wlan0: AP-ENABLED wlan0: INTERFACE-DISABLED wlan0 interface - gateway addr: 10.0.0.1 

After starting the AP I'm trying to bind a listening socket:

print("wlan0 interface - gateway addr: " + WiFi_AP.wait_for_hotspot()) # print the IP address of the hotspot time.sleep(1) cloud_api.init_cloud_connection() print("cloud connection initialized") time.sleep(5) # wait for socket to be ready # opening socket using with statement, so it closes on its own when done with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: # Allow reusing the port sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) # bing server port to hotspot interface and port 100 sock.bind((HOST, PORT)) # opening cloud connection status thread with 5 sec delay status_thread = threading.Thread(target=cloud_api.cloud_status_update, args=5, daemon=True) status_thread.start() listen_for_clients(sock) 

But I get an exception;

OSError: [Errno 99] Cannot assign requested address

I've tried a different local ip range for the gateway, i.e. 192.168.0.1/24, didn't work. Chatgpt suggested executables need special permissions to bind to ports under 1000, which I've given to the interpreter which is symlinked to by the venv.

it seems the issue might lie in "wlan0: INTERFACE-DISABLED" but I don't know how to change that. The OS installed is:

Linux raspberrypi 6.6.51+rpt-rpi-v8 #1 SMP PREEMPT Debian 

P.s. when I create an AP using the same command from the code, open the same python interpreter in the venv my project uses, create a TCP socket and bind to 10.0.0.1:100, it all works. But when I run the python script it fails:

sudo create_ap -n --daemon --no-dns -g 10.0.0.1 wlan0 test test1234 Running as Daemon... ron@raspberrypi:~ $ sudo create_ap --list-running 2524 wlan0 ron@raspberrypi:~ $ cd Desktop/ ron@raspberrypi:~/Desktop $ venv/bin/python3 Python 3.11.2 (main, Nov 30 2024, 21:22:50) [GCC 12.2.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import socket >>> sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) >>> sock.bind(('10.0.0.1', 100)) >>> sock.listen <built-in method listen of socket object at 0x7f82a5a440> >>> sock.listen() 
3
  • @Milliways edited to include os version
    – Ron Kan
    CommentedApr 15 at 14:57
  • You have listed kernel version (not up to date) but not listed OS version .
    – Milliways
    CommentedApr 15 at 23:44
  • You have also failed to explain HOW you created AP or any diagnostics indicating that it is running. You appear to have run some unspecified create_ap. What does it do?
    – Milliways
    CommentedApr 15 at 23:47

1 Answer 1

0

ChatGPT was the culprit, with various wrong suggestions. Using a minimal working script I was able to narrow the problem down to:

WiFi_AP.wait_for_hotspot():

def wait_for_hotspot(timeout=12): """ wait for the hotspot to be created and return the IP address of the wifi interface :param timeout: time to wait for the hotspot to be created :return: IP address of the wifi interface or None if timeout is reached """ for _ in range(timeout * 2): # check every 0.5 seconds so double the timeout # check if the hotspot is created by checking the IP address of the wifi interface output = subprocess.check_output(["ip", "addr", "show", "wlan0"]).decode() match = re.search(r'inet (\d+\.\d+\.\d+\.\d+)', output) # regex to find the IP address addr = match.group(1) if match else None if addr is not None: time.sleep(2) # wait for the interface to be fully up return addr time.sleep(0.5) raise "hotspot timed out" 

And fixing the function create_ap() which turned out to be running the utility create_ap twice; once using subrpocess.Popen, and again using subprocess.run.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.