So I tested your script using the following host side script:
Code: Select all
import sys, socket
HOST = '' # Symbolic name, meaning all available interfaces
PORT = 1880 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
print ('Socket created')
#Bind socket to local host and port
try:
s.bind((HOST, PORT))
except socket.error as msg:
print ('Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1])
sys.exit()
print ('Socket bind complete')
#Start listening on socket
s.listen(10)
#now keep talking with the client
while 1:
try:
#wait to accept a connection - blocking call
print("Waiting for connections...")
client, addr = s.accept()
client.settimeout(5.0)
print('Connected to ' + addr[0] + ':' + str(addr[1]))
data = client.recv(1024)
client.send("Hello from server!")
client.close()
except socket.error as msg:
print ('Connection failed. Error: ' + str(msg))
client.close()
s.close()
I noticed that only the first say() fails, then it runs normally, adding a delay after connect to network seems to make the first say() work, not sure why, will look into it further but for now you could just remove the first say(), also made small changes to the script :
Code: Select all
import sensor, time, image,pyb, sys
import network, usocket
# AP info
SSID='' # Network SSID
KEY='' # Network key
wlan = network.WINC()
wlan.connect(SSID, key=KEY, security=wlan.WPA_PSK)
print(wlan.ifconfig())
def say():
try:
client = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM)
client.connect(('192.168.0.101', 1880))
client.settimeout(2.0)
client.send("GET /camera?say=Hello HTTP/1.0\r\n\r\n")
reply = client.recv(256).decode("utf-8")
client.close()
print(reply)
except Exception as e:
print("Error : " + str(e))
def blink():
led = pyb.LED(2) # Use green LED.
led.on()
time.sleep(150)
led.off()
time.sleep(100)
led.on()
time.sleep(150)
led.off()
# Reset sensor
sensor.reset()
# Sensor settings
sensor.set_contrast(1)
sensor.set_gainceiling(16)
# HQVGA and GRAYSCALE are the best for face tracking.
sensor.set_framesize(sensor.HQVGA)
sensor.set_pixformat(sensor.GRAYSCALE)
# Flip display
sensor.set_vflip(True)
# Load Haar Cascade
# By default this will use all stages, lower satges is faster but less accurate.
face_cascade = image.HaarCascade("frontalface", stages=25)
# FPS clock
clock = time.clock()
while (True):
clock.tick()
# Capture snapshot
img = sensor.snapshot()
# Find objects.
# Note: Lower scale factor scales-down the image more and detects smaller objects.
# Higher threshold results in a higher detection rate, with more false positives.
objects = img.find_features(face_cascade, threshold=0.5, scale=1.5)
if objects:
blink()
say()
# Draw objects
for r in objects:
img.draw_rectangle(r)
print(r)
Note you can use sensor.set_vflip(True) to flip upside down, and image.width/height() to get frame dimensions.