]> vault307.fbx.one Git - garage_door_sensor.git/commitdiff
add websocket host/client test code
authorjimmy <jimipunk88@gmail.com>
Mon, 8 Jul 2024 22:14:26 +0000 (17:14 -0500)
committerjimmy <jimipunk88@gmail.com>
Mon, 8 Jul 2024 22:14:26 +0000 (17:14 -0500)
SSID and Password filled with dummy info

picoWwebsocket.py [new file with mode: 0644]
testClient.py [new file with mode: 0644]

diff --git a/picoWwebsocket.py b/picoWwebsocket.py
new file mode 100644 (file)
index 0000000..906f53e
--- /dev/null
@@ -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 (file)
index 0000000..52e2314
--- /dev/null
@@ -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)