Socket error .send() inside function... same .send() works outside function

Hi - I’m tracking an april tag and sending AGV commands over a socket. I’m unit testing the code and for the life of me can’t get the HTTP GET that works at the top of the program to work inside a function… Code is below:

Line 34 works and moves the vehicle. Line 59 is caught as exception

---- terminal output----
Trying to connect... (may take a while)...
('', '', '', '')
Connected to
GET /jsData.html?x=-100&y=150 HTTP/1.1
b'HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: 51\r\nConnection: close\r\n\r\n'

b'GET /jsData.html?x=-91&y=150 HTTP/1.1\r\nHost:\r\n\r\n'
b'GET /jsData.html?x=-93&y=150 HTTP/1.1\r\nHost:\r\n\r\n'
socket error: [Errno 22] EINVAL
Traceback (most recent call last):
File "<stdin>", line 65, in <module>
File "<stdin>", line 59, in start_following
OSError: [Errno 22] EINVAL
b'GET /jsData.html?x=-94&y=150 HTTP/1.1\r\nHost:\r\n\r\n'
socket error: [Errno 22] EINVAL
------------- end terminal output ------------

The code:

import sensor, image, time, network, usocket, sys , ubinascii, math, socket

sensor.set_framesize(sensor.HVGA) # we run out of memory if the resolution is much bigger...
sensor.set_windowing((180, 140)) # Look at center 160x120 pixels of the VGA resolution.
sensor.set_auto_gain(False)  # must turn this off to prevent image washout...
sensor.set_auto_whitebal(False)  # must turn this off to prevent image washout...
sensor.skip_frames(time = 2000)
clock = time.clock()

SSID="SSID" # Network SSID
KEY="PWD"  # Network key
HOST =''     # Use first available interface
PORT = 80  # Arbitrary non-privileged port

# Init wlan module and connect to network
print("Trying to connect... (may take a while)...")
wlan = network.WINC()
wlan.connect(SSID, key=KEY, security=wlan.WPA_PSK)

# We should have a valid IP now via DHCP

# Create a new socket and connect to addr
addr = usocket.getaddrinfo('', 80)[0][-1]
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print ('Connected to ' + addr[0] + ':' + str(addr[1]))
x_offset = -100
y_offset = 150
print("GET /jsData.html?x=%s&y=%s HTTP/1.1\r\nHost: %s\r\n\r\n"%(x_offset,y_offset,addr[0]))
client.send(bytes("GET /jsData.html?x=%s&y=%s HTTP/1.1\r\nHost: %s\r\n\r\n"%(x_offset,y_offset,addr[0]),'utf8')) #THIS WORKS AND AGV STARTS MOVING

def map_range(x, in_min, in_max, out_min, out_max):
  return (x - in_min) * (out_max - out_min) // (in_max - in_min) + out_min

def start_following(client):

        img = sensor.snapshot()
        for tag in img.find_apriltags(): # defaults to TAG36H11
            img.draw_rectangle(tag.rect(), color = (255, 0, 0))
            img.draw_cross(,, color = (0, 255, 0))
            x_offset = int(tag.x_translation() * 100)
            map_range(x_offset, -1000, 1000, -372, 372)
            y_offset = int(tag.y_translation() * 100)
            y_offset = 150
            map_range(y_offset, -1000, 1000, -372, 372)
            z_offset = int(tag.z_translation() * 100)
            map_range(z_offset, -1000, 1000, -372, 372)
            if z_offset < -450:
                guidance = (x_offset, y_offset)            
                print("x=%s&y=%s" % guidance)
                print(bytes("GET /jsData.html?x=%s&y=%s HTTP/1.1\r\nHost: %s\r\n\r\n"%(x_offset,y_offset,addr[0]),'utf8'))
                client.send(bytes("GET /jsData.html?x=%s&y=%s HTTP/1.1\r\nHost: %s\r\n\r\n"%(x_offset,y_offset,addr[0]),'utf8')) #THIS IS CAUGHT as [Errno 22] EINVAL

while (True):
    except OSError as e:
        print("socket error: ", e)

code cut/paste was weird so attaching (1.6 KB)

The socket might be closed after the first send/recv for some reason, try it a few times outside the function ? Or try removing the try/except or comment the send line, and see if it raises a different error.

Yep - thanks (facepalm) - socket’s closing (even says so in the resp)… when i stream frames i don’t have to do anything special to keep it open but now that i’m doing HTTP i guess that’s a new trick to learn. THANK YOU

Thanks - connect and close wrapping each API call did the trick - xoxo