Skip to content

Commit 5d15493

Browse files
authored
Merge pull request #4187 from hierophect/manual-tests
Create tests/_manual, add Socket tests
2 parents fdf9d04 + e77981f commit 5d15493

30 files changed

+237
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import wifi
2+
import socketpool
3+
import ssl
4+
import time
5+
6+
TIMEOUT = None
7+
HOST = '192.168.10.179'
8+
PORT = 5000
9+
10+
# Connect to wifi
11+
print("Connecting to wifi")
12+
wifi.radio.connect("mySSID", "myPASS")
13+
pool = socketpool.SocketPool(wifi.radio)
14+
15+
print("Creating Socket")
16+
with pool.socket(pool.AF_INET, pool.SOCK_STREAM) as s:
17+
s.settimeout(TIMEOUT)
18+
19+
print("Connecting")
20+
s.connect((HOST, PORT))
21+
print("Sending")
22+
sent = s.send(b'Hello, world')
23+
print("Receiving")
24+
buff = bytearray(128)
25+
numbytes = s.recv_into(buff)
26+
print(repr(buff))
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env python3
2+
import socket
3+
4+
TIMEOUT = 10
5+
HOST = "192.168.10.179"
6+
PORT = 5000
7+
8+
print("Create Socket")
9+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
10+
s.settimeout(TIMEOUT)
11+
s.bind((HOST, PORT))
12+
s.listen()
13+
print("Accepting connections")
14+
while True:
15+
try:
16+
conn, addr = s.accept()
17+
break
18+
except BlockingIOError:
19+
pass
20+
21+
with conn:
22+
s.settimeout(TIMEOUT)
23+
print('Connected by', addr)
24+
data = conn.recv(128)
25+
print("got: " + str(data))
26+
conn.sendall(data)
27+
print("sent: " + str(data))
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Circuitpython as Client
2+
3+
This example demonstrates the use of Socket as a client, accessing a server on a host development machine. This Circuitpython sketch uses the Connect, Send, and Recv_Into methods.
4+
5+
## Prerequisites
6+
7+
Circuitpython V6.2.0 minimum. Neither the host or client sketch has installed module prerequisites.
8+
9+
## Setup
10+
11+
Find a viable IP address for the host machine first and insert it in both sketches as HOST. On mac, this can be done by going to System Preferences/Network and checking the IP address used to connect to the local wireless network. Make sure that both devices are using the same WIFI!
12+
13+
Each sketch can have Timeout values changed. The host sketch usually needs a value above 0, or the recv() will fail. Currently, Circuitpython's Connect function is always blocking, so changing the client timeout will not cause much change in behavior.
14+
15+
Start the Server on the host PC first, within this folder:
16+
17+
```
18+
python host-server.py
19+
```
20+
21+
Then, reload the client sketch in Circuitpython.
22+
23+
## Expected Behavior
24+
25+
The example should connect to a server running on the host machine. The client will send a "Hello world" string to the server, which will return it.
26+
27+
Expected client output:
28+
29+
```
30+
Connecting to wifi
31+
Creating Socket
32+
Connecting
33+
Sending
34+
Receiving
35+
bytearray(b'Hello, world')
36+
```
37+
38+
Expected Server output (IP/port values will vary):
39+
40+
```
41+
Create Socket
42+
Accepting connections
43+
Connected by ('192.168.10.128', 64509)
44+
got: b'Hello, world'
45+
sent: b'Hello, world'
46+
```
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import wifi
2+
import socketpool
3+
import struct
4+
import time
5+
6+
# connect to wifi
7+
print("Connecting to Wifi")
8+
wifi.radio.connect("mySSID", "myPASS")
9+
pool = socketpool.SocketPool(wifi.radio)
10+
11+
# make socket
12+
print("Creating socket")
13+
sock = pool.socket(pool.AF_INET,pool.SOCK_DGRAM)
14+
15+
# Fill packet
16+
packet = bytearray(48)
17+
packet[0] = 0b00100011 # Not leap second, NTP version 4, Client mode
18+
NTP_TO_UNIX_EPOCH = 2208988800 # 1970-01-01 00:00:00
19+
20+
print("Sending packet")
21+
sock.sendto(packet, ("pool.ntp.org", 123))
22+
23+
size, address = sock.recvfrom_into(packet)
24+
print("Received packet")
25+
26+
seconds = struct.unpack_from("!I", packet, offset=len(packet) - 8)[0]
27+
print("Address:", address)
28+
print("Time:", time.localtime(seconds - NTP_TO_UNIX_EPOCH))
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Circuitpython Datagrams
2+
3+
This example demonstrates using UDP (datagrams) with the Socket module, accessing the time from an NTP server using `sendto` and `recvfrom_into`.
4+
5+
## Prerequisites
6+
7+
Circuitpython V6.2.0 minimum.
8+
9+
## Expected behavior
10+
11+
The Circuitpython device will attempt to connect to wifi, and send a request for the time to `pool.ntp.org`. It will then convert the seconds returned into a unix time struct.
12+
13+
Expected output:
14+
```
15+
Sending packet
16+
Received packet
17+
Address: ('82.197.188.130', 31488)
18+
Time: struct_time(tm_year=2021, tm_mon=2, tm_mday=11, tm_hour=22, tm_min=22, tm_sec=40, tm_wday=3, tm_yday=42, tm_isdst=-1)
19+
```
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import wifi
2+
import socketpool
3+
4+
TIMEOUT = None
5+
6+
print("Connecting to Wifi")
7+
wifi.radio.connect("mySSID", "myPASS")
8+
9+
pool = socketpool.SocketPool(wifi.radio)
10+
11+
print("Finding IP address")
12+
print(wifi.radio.ipv4_address)
13+
HOST = str(wifi.radio.ipv4_address)
14+
PORT = 80 # Port to listen on
15+
16+
print("Creating socket")
17+
sock = pool.socket(pool.AF_INET,pool.SOCK_STREAM)
18+
19+
sock.bind((HOST, PORT))
20+
sock.listen(1)
21+
print("Accepting connections")
22+
conn, addr = sock.accept()
23+
with conn:
24+
print('Connected by', addr)
25+
buff = bytearray(128)
26+
print("Receiving")
27+
numbytes = conn.recvfrom_into(buff)
28+
print(buff[:numbytes[0]])
29+
if numbytes:
30+
print("Sending")
31+
conn.send(buff[:numbytes[0]])
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import socket
2+
3+
HOST = '192.168.10.128' # The server's hostname or IP address
4+
PORT = 80 # The port used by the server
5+
6+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
7+
s.settimeout(None)
8+
9+
print("Connecting")
10+
s.connect((HOST, PORT))
11+
12+
print("Sending")
13+
s.send(b'Hello, world')
14+
15+
print("Receiving")
16+
data = s.recv(1024)
17+
print('Received', repr(data))
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Circuitpython as Client
2+
3+
This example demonstrates the use of Socket as a server, accessed by a client program on a host development machine. This Circuitpython sketch uses the Bind, Listen, Accept, and recvfrom_into calls.
4+
5+
## Prerequisites
6+
7+
Circuitpython V6.2.0 minimum. Neither the host or client sketch has installed module prerequisites.
8+
9+
## Setup
10+
11+
Make sure that both devices are using the same WIFI! The Circuitpython Server will automatically pick an IP and print it over the CDC monitor. Copy this value into the host client sketch. Start the Server on Circuitpython first, then run the client sketch in this folder:
12+
13+
```
14+
python host-client.py
15+
```
16+
17+
Each sketch can have Timeout values changed.
18+
19+
## Expected behavior
20+
21+
The host machine will connect to the server running on the Circuitpython device, and send a "Hello, world" string which is then returned.
22+
23+
Expected client output:
24+
25+
```
26+
Connecting
27+
Sending
28+
Receiving
29+
Received b'Hello, world'
30+
```
31+
32+
Expected server output:
33+
```
34+
Connecting to Wifi
35+
Finding IP address
36+
192.168.10.128
37+
Creating socket
38+
Accepting connections
39+
Connected by ('192.168.10.179', 33274)
40+
Receiving
41+
bytearray(b'Hello, world')
42+
Sending
43+
```

0 commit comments

Comments
 (0)