[Arduino Portenta H7] OSError: [Errno 1] EPERM when running network code

Discussion and questions about boards that can run MicroPython but don't have a dedicated forum.
Target audience: Everyone interested in running MicroPython on other hardware.
Post Reply
kevalshah2005
Posts: 4
Joined: Sun Jun 12, 2022 10:55 pm

[Arduino Portenta H7] OSError: [Errno 1] EPERM when running network code

Post by kevalshah2005 » Sun Jun 12, 2022 11:03 pm

I'm having an issue with the below code. It's giving me the error "OSError: [Errno 1] EPERM" even though this is exactly what the OpenMV documentation instructs me to do. Specifically, the error is happening on line 11 ( wlan.connect(SSID, KEY) ).

Code: Select all

from network import WLAN
 
SSID  = "[redacted]" # Network SSID
KEY  = '[redacted]' # Network key
HOST = '' # Use first available interface
PORT = 8080 # Arbitrary non-privileged port
 
# Init wlan module and connect to network
print("Trying to connect... (may take a while)...")
wlan = WLAN(network.STA_IF)
wlan.connect(SSID, KEY)
 
# We should have a valid IP now via DHCP
print(wlan.ifconfig())
Let me know if you need any more information, I'll be happy to provide it.

Shards
Posts: 39
Joined: Fri Jun 25, 2021 5:14 pm
Location: Milton Keynes, UK

Re: [Arduino Portenta H7] OSError: [Errno 1] EPERM when running network code

Post by Shards » Mon Jun 13, 2022 7:08 am

I can see a couple of problems with the code.

You only import WLAN from network so 'network.STA_IF' is invalid. However, the real issue is that that you need 'wlan.active(True) after the assignment and before the connection.

I'm using an ESP32 but the network interface is pretty consistent across builds.

kevalshah2005
Posts: 4
Joined: Sun Jun 12, 2022 10:55 pm

Re: [Arduino Portenta H7] OSError: [Errno 1] EPERM when running network code

Post by kevalshah2005 » Mon Jun 13, 2022 8:00 pm

Ah, that makes sense, thanks! I pasted a small portion of the code I was using, so that's why the network import wasn't there (I forgot to include it in the above code). The above error was fixed, but I'm having a different issue now with a different part of my code. I combined the example code for streaming a JPEG image (changed WINC to WLAN) with my code for distinguishing between two different kinds of playing cards to get the below code. It keeps coming up with "Waiting for connections.." but when I check the availahble WiFi networks I don't see the Arduino's network.

Code: Select all

# Card Distinguisher - By Keval Shah

import sensor, image, time, tf, pyb
import network, usocket, sys

model_file = "ei-test-project-transfer-learning-tensorflow-lite-int8-quantized-model.lite" # TensorFlow Lite Model to read from

labels = ["pokemon_card", "regular_card"] # Labels to distinguish between

ledRed = pyb.LED(1) # Red LED colour
ledGreen = pyb.LED(2) # Green LED colour

SSID  = "[redacted]" # Network SSID
KEY  = '[redacted]' # Network key
HOST = '' # Use first available interface
PORT = 8080 # Arbitrary non-privileged port

# Set up sensor
sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.QVGA)
sensor.set_windowing((96, 96))
sensor.skip_frames(time = 2000)

# Set up clock
clock = time.clock()

# Init wlan module and connect to network
print("Trying to connect... (may take a while)...")
wlan = network.WLAN(network.AP_IF)
wlan.active(True)
# wlan.connect(SSID, KEY)

# We should have a valid IP now via DHCP
print(wlan.ifconfig())

# Create server socket
s = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM)

# Bind and listen
s.bind([HOST, PORT])
s.listen(5)

# Set server socket to blocking
s.setblocking(True)

def start_streaming(s):
    print ('Waiting for connections..')
    client, addr = s.accept()
    # set client socket timeout to 2s
    client.settimeout(2.0)
    print ('Connected to ' + addr[0] + ':' + str(addr[1]))

    # Read request from client
    data = client.recv(1024)
    # Should parse client request here

    # Send multipart header
    client.send("HTTP/1.1 200 OK\r\n" \
                "Server: OpenMV\r\n" \
                "Content-Type: multipart/x-mixed-replace;boundary=openmv\r\n" \
                "Cache-Control: no-cache\r\n" \
                "Pragma: no-cache\r\n\r\n")

    # FPS clock
    clock = time.clock()

    # Start streaming images
    # NOTE: Disable IDE preview to increase streaming FPS.
    frame = sensor.snapshot()
    cframe = frame.compressed(quality=35)
    header = "\r\n--openmv\r\n" \
             "Content-Type: image/jpeg\r\n"\
             "Content-Length:"+str(cframe.size())+"\r\n\r\n"
    client.send(header)
    client.send(cframe)

while(True):
    clock.tick() # Advances clock
    img = sensor.snapshot() # Gets image from sensor
    img.set(h_mirror = True) # Mirrors image
    objs = tf.classify(model_file, img)  # Classifies image based on model
    predictions = objs[0].output() # Provides predictions based on classifications

    max_val = max(predictions) # Finds the most probable label
    max_index = predictions.index(max_val) # Finds the index of the most probable label

    if max_index == 0: # If the index is the first label then turn red LED off and turn green LED on
        ledRed.off()
        ledGreen.on()
    else: # If the index is not the first label then turn red LED on and turn green LED off
        ledRed.on()
        ledGreen.off()

    img.draw_string( # Draws text onto image showing the classification and value
        0,
        0,
        labels[max_index] + "\n{:.2f}".format(round(max_val, 2)),
        mono_space = False,
        scale=1
    )

    print("-----")

    for i, label in enumerate(labels): # Prints classification and value to console for debugging purposes
       print(str(label) + ": " + str(predictions[i]))

    print("FPS:", clock.fps()) # Prints FPS for debugging purposes

    try:
        start_streaming(s)
    except OSError as e:
        print("socket error: ", e)
        #sys.print_exception(e)
    except Exception as e:
        print("unknown error: " + str(e))

Shards
Posts: 39
Joined: Fri Jun 25, 2021 5:14 pm
Location: Milton Keynes, UK

Re: [Arduino Portenta H7] OSError: [Errno 1] EPERM when running network code

Post by Shards » Tue Jun 14, 2022 1:38 pm

Not sure why you are using AP_IF. Any device connected to an IP network must have an IP address which you can see using ifconfig . I have a web server running on my ESP32s using code virtually identical to yours for the basic socket stuff. I just do a normal STA_IF connection and it works just fine.

If you are connecting to the server if you use port 8080 you will need to specify that to connect. I use port 80 which is a default HTTP port so just the IP address works.

I'd suggest simplifying your code right down, removing all the sensor code and creating a minimal web page. Get that working and you can then add in the sensor code safely.

kevalshah2005
Posts: 4
Joined: Sun Jun 12, 2022 10:55 pm

Re: [Arduino Portenta H7] OSError: [Errno 1] EPERM when running network code

Post by kevalshah2005 » Wed Jun 15, 2022 6:03 am

I'm trying to transmit an image from my Portenta H7 to my computer wirelessly and from what I read in the OpenMV documentation, AP mode seemed like the way to go. Also, I was trying STA_IF first but it also came up with the same problem, so I switched to AP_IF in an attempt to fix it.

Switching to port 80 didn't seem to help the problem either. Do I need to specify something in the HOST field and if so, what do I put there?

Shards
Posts: 39
Joined: Fri Jun 25, 2021 5:14 pm
Location: Milton Keynes, UK

Re: [Arduino Portenta H7] OSError: [Errno 1] EPERM when running network code

Post by Shards » Wed Jun 15, 2022 6:52 am

An empty HOST string should work. Are you still getting an error or does it just hang?

Your code looks OK as far as making a connection goes. I'm not so sure about the HTML you are generating but if you try connecting to your devices IP address via a browser you should at least see a connection if you run the code from the REPL.

I'd reiterate that it's worth building things up slowly. Forget about images until you can serve a simple, text only, web page.

For what it's worth here is my minimal webserver code:

Code: Select all

import network
from socket import socket

from net_config import SSID, PSWD

station = network.WLAN(network.STA_IF)
station.active(True)

while True:
	try:
		station.connect(SSID,PSWD)
	except:
		pass	
	if station.isconnected():
		break
print('Connection successful')
print(station.ifconfig())

s = socket()
s.bind(('', 80))
s.listen(5)

while True:
	
	conn, addr = s.accept()
	print('Got a connection from' + str(addr))
	request = conn.recv(1024)
	conn.send('HTTP/1.1 200 OK\n')
	conn.send('Content-Type: text/html\n')
	conn.send('Connection: close\n\n')
	conn.sendall('Hello New World')
	conn.close()
And the output from the program when connecting via a browser to the IP address:

Code: Select all

Connection successful
('192.168.1.187', '255.255.255.0', '192.168.1.1', '192.168.1.1')
Got a connection from('192.168.1.131', 39494)


kevalshah2005
Posts: 4
Joined: Sun Jun 12, 2022 10:55 pm

Re: [Arduino Portenta H7] OSError: [Errno 1] EPERM when running network code

Post by kevalshah2005 » Thu Jun 16, 2022 7:12 am

It just hangs. It gets stuck at "Waiting for Connections.." and never progresses past that. The IP also shows up at 0.0.0.0, which seems strange.
Image

I tried out the code that you sent (replacing SSID and PSWD with my own SSID and PSWD) but it doesn't output anything and after a while just shows up with a bunch of these messages:
Image

Since it never prints "Connection successful", it seems that something is wrong inside of the while True loop, or in other words with station.connect(). That may be where I'm having the problem in my code as well.

Shards
Posts: 39
Joined: Fri Jun 25, 2021 5:14 pm
Location: Milton Keynes, UK

Re: [Arduino Portenta H7] OSError: [Errno 1] EPERM when running network code

Post by Shards » Thu Jun 16, 2022 2:21 pm

Without an IP address you won't get anywhere.

I'm not sure I have any more to add as it looks like the problem is with your specific hardware. The code I listed works on both ESP8266 and a range of ESP32 mcu.

Hopefully someone with some experience of your board can help. You might also get a more focused response from the Open MV forum.

Post Reply