From: jimmy Date: Mon, 8 Jul 2024 22:14:26 +0000 (-0500) Subject: add websocket host/client test code X-Git-Url: https://vault307.fbx.one/gitweb/garage_door_sensor.git/commitdiff_plain/eb6b65e3f69cdd18bad359c85013fa3980c644b7?ds=sidebyside add websocket host/client test code SSID and Password filled with dummy info --- diff --git a/picoWwebsocket.py b/picoWwebsocket.py new file mode 100644 index 0000000..906f53e --- /dev/null +++ b/picoWwebsocket.py @@ -0,0 +1,51 @@ +import network, time, socket, gc + +ssid='SSID' +password='PASSWORD' + +def connect(): + wlan=network.WLAN(network.STA_IF) + wlan.active(True) + wlan.connect(ssid,password) + while wlan.isconnected() == False: + print('Waiting for connection...') + time.sleep(1) + ip=wlan.ifconfig()[0] + print(f'Connected on {ip}') + return ip + +def open_socket(ip): + address=(ip,80) + connection=socket.socket() + connection.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + connection.bind(address) + connection.listen(1) + return connection + +opEN='0000' +closED='0' + +def serve(connection): + state=opEN + gc.collect() + + while True: + client=connection.accept()[0] + request=client.recv(1024) + request=str(request) + print(request) + try: + request=request.split()[1] + except IndexError: + pass + msg=state + client.send(msg) + client.close() + #gc.collect() + +try: + ip=connect() + connection=open_socket(ip) + serve(connection) +except KeyboardInterrupt: + pass diff --git a/testClient.py b/testClient.py new file mode 100644 index 0000000..52e2314 --- /dev/null +++ b/testClient.py @@ -0,0 +1,21 @@ +import network, time, socket + +ssid='SSID' +password='PASSWORD' + +def connect(): + wlan=network.WLAN(network.STA_IF) + wlan.active(True) + wlan.connect(ssid,password) + while wlan.isconnected() == False: + print('Waiting for connection...') + time.sleep(1) + ip=wlan.ifconfig()[0] + print(f'Connected on {ip}') + return ip + +connect() +address=('192.168.0.228',80) +sock=socket.socket() +sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) +sock.connect(address)