OSError: [Errno 1] EPERM in raspberry pi pico

265 Views Asked by At

So, I'm making a project called smart house and it uses http server to get user inputs. But when I run the code, it shows OSError: [Errno 1] EPERM.

Here's some info:

  • OS - Windows 11 Home 64 bit
  • IDE - Thonny
  • Full model of pico - Raspberry pi pico W (Yes, It's wireless)
  • USB port works fine, and so is my network.
  • Tried the whole thing in a virtual machine, but same error
  • Removed the HTML code for stackoverflow, it's there in my actual source code

Here's the source code:

import network
import usocket as socket
from machine import Pin, PWM

pico_led = Pin("LED", Pin.OUT)

html_content = """
<! The HTML Code>
"""


def control_robot(direction):
    print(f"Command received: {direction}")
    # The if else stuff
    if direction == 'light':
        pico_led.toggle()

def parse_request(request):
    method, path, *_ = request.split("\r\n")[0].split(" ")
    return method, path

def start_webserver():
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)

    # Replace with your WiFi credentials
    wlan.connect("SSID", "PASSWORD")
    
    if wlan.isconnected():
        print("Connected")
    
    while not wlan.isconnected():
        pass

    print("Connection successful")
    print("IP Address:", wlan.ifconfig()[0])

    addr = socket.getaddrinfo('0.0.0.0', 8080)[0][-1]

    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind(addr)
    s.listen(1)

    print("Waiting for connections...")

    while True:
        conn, addr = s.accept()
        print("Connection from:", addr)
        request = conn.recv(1024)

        if request:
            request_str = request.decode()
            method, path = parse_request(request_str)

            if method == "GET" and path == "/":
                response = "HTTP/1.1 200 OK\n"
                response += "Content-Type: text/html\n"
                response += "Connection: close\n\n"
                response += html_content
            elif method == "POST" and path == "/control":
                form_data = request_str.split("\r\n")[-1]
                direction = form_data.split("=")[-1]
                control_robot(direction)

                response = "HTTP/1.1 200 OK\n"
                response += "Content-Type: text/plain\n"
                response += "Connection: close\n\n"
                response += f"Command sent: {direction}"
            else:
                response = "HTTP/1.1 400 Bad Request\n"
                response += "Content-Type: text/plain\n"
                response += "Connection: close\n\n"
                response += "400 Bad Request"

            conn.sendall(response.encode())

        conn.close()

if __name__ == "__main__":
    start_webserver()

And here's the full error:

Traceback (most recent call last):
  File "<stdin>", line 163, in <module>
  File "<stdin>", line 110, in start_webserver
OSError: [Errno 1] EPERM

I would appreciate you a lot if I get answer within 3rd January.

0

There are 0 best solutions below