mirror of
https://github.com/openmv/openmv.git
synced 2025-11-04 14:49:50 +08:00
Tried to emulate Arduino's 11 folders... I'd perfer to have all the shield scripts in one folder... but, that might not make sense. I don't really want one script per folder however. So, I might merge some more stuff in the future. I have a grand idea here that will become evident as I work though the examples. Anyway, the current structure is not final. It will be in flux for a little while. As for Git History, folder history is the best we're going to get. Git and GitHub don't seem to deal with moves too well.
71 lines
1.6 KiB
Python
71 lines
1.6 KiB
Python
'''
|
|
Simple echo server
|
|
'''
|
|
import wlan
|
|
import socket
|
|
import select
|
|
import led, time
|
|
|
|
SSID='' # Network SSID
|
|
KEY='' # Network key
|
|
HOST = '' # Use first available interface
|
|
PORT = 8000 # Arbitrary non-privileged port
|
|
|
|
led.off(led.RED)
|
|
led.off(led.BLUE)
|
|
led.on(led.GREEN)
|
|
|
|
# Init wlan module and connect to network
|
|
wlan.init()
|
|
wlan.connect(SSID, sec=wlan.WPA2, key=KEY)
|
|
led.off(led.GREEN)
|
|
|
|
# Wait for connection to be established
|
|
while (True):
|
|
led.toggle(led.BLUE)
|
|
time.sleep(250)
|
|
led.toggle(led.BLUE)
|
|
time.sleep(250)
|
|
if wlan.connected():
|
|
led.on(led.BLUE)
|
|
break;
|
|
|
|
# We should have a valid IP now via DHCP
|
|
wlan.ifconfig()
|
|
|
|
# Create server socket
|
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)
|
|
|
|
# Set socket in blocking mode
|
|
s.setblocking(True)
|
|
|
|
# Bind and listen
|
|
s.bind((HOST, PORT))
|
|
s.listen(5)
|
|
|
|
while(True):
|
|
print ('Waiting for connections..')
|
|
client, addr = s.accept()
|
|
print ('Connected to ' + addr[0] + ':' + str(addr[1]))
|
|
|
|
# Set client socket non-blocking
|
|
client.setblocking(False)
|
|
|
|
while (True):
|
|
rfds, wfds, xfds = select.select([client], [], [client], 1.0)
|
|
if xfds:
|
|
print("socket exception")
|
|
break
|
|
elif rfds:
|
|
buf = client.recv(1024)
|
|
if len(buf) == 0: # peer has shutdown
|
|
print("socket closed")
|
|
client.close()
|
|
break
|
|
print ("recv:"+str(buf))
|
|
client.send(buf)
|
|
elif wfds:
|
|
print ("wfds")
|
|
else:
|
|
print ("timeout")
|