From 59429cdcbed8b88e80179c8701dd2ea529b12f19 Mon Sep 17 00:00:00 2001 From: Lucian Copeland Date: Wed, 17 Feb 2021 11:41:23 -0500 Subject: Move manual tests and rename --- .../jeplayer-splash-16000-16bit-mono-signed.wav | Bin 0 -> 130448 bytes .../jeplayer-splash-16000-16bit-stereo-signed.wav | Bin 0 -> 260852 bytes .../jeplayer-splash-16000-24bit-mono-signed.wav | Bin 0 -> 195650 bytes .../jeplayer-splash-16000-24bit-stereo-signed.wav | Bin 0 -> 391256 bytes .../jeplayer-splash-16000-8bit-mono-unsigned.wav | Bin 0 -> 65246 bytes .../jeplayer-splash-16000-8bit-stereo-unsigned.wav | Bin 0 -> 130448 bytes .../jeplayer-splash-44100-16bit-mono-signed.wav | Bin 0 -> 359468 bytes .../jeplayer-splash-44100-16bit-stereo-signed.wav | Bin 0 -> 718892 bytes .../jeplayer-splash-44100-24bit-mono-signed.wav | Bin 0 -> 539180 bytes .../jeplayer-splash-44100-24bit-stereo-signed.wav | Bin 0 -> 1078316 bytes .../jeplayer-splash-44100-8bit-mono-unsigned.wav | Bin 0 -> 179756 bytes .../jeplayer-splash-44100-8bit-stereo-unsigned.wav | Bin 0 -> 359468 bytes .../audiocore/jeplayer-splash-44100-stereo.mp3 | Bin 0 -> 65200 bytes .../jeplayer-splash-8000-16bit-mono-signed.wav | Bin 0 -> 65246 bytes .../jeplayer-splash-8000-16bit-stereo-signed.wav | Bin 0 -> 130448 bytes .../jeplayer-splash-8000-24bit-mono-signed.wav | Bin 0 -> 97848 bytes .../jeplayer-splash-8000-24bit-stereo-signed.wav | Bin 0 -> 195650 bytes .../jeplayer-splash-8000-8bit-mono-unsigned.wav | Bin 0 -> 32646 bytes .../jeplayer-splash-8000-8bit-stereo-unsigned.wav | Bin 0 -> 65246 bytes .../audiopwmio/single_buffer_loop.py | 65 +++++++++++++++++++++ .../audiopwmio/wavefile_pause_resume.py | 39 +++++++++++++ .../audiopwmio/wavefile_playback.py | 36 ++++++++++++ .../socketpool/client/cpy-client.py | 26 +++++++++ .../socketpool/client/host-server.py | 27 +++++++++ .../socketpool/client/readme.md | 46 +++++++++++++++ .../socketpool/datagram/ntp.py | 28 +++++++++ .../socketpool/datagram/readme.md | 19 ++++++ .../socketpool/server/cpy-server.py | 31 ++++++++++ .../socketpool/server/host-client.py | 18 ++++++ .../socketpool/server/readme.md | 43 ++++++++++++++ 30 files changed, 378 insertions(+) create mode 100644 tests/circuitpython-manual/audiocore/jeplayer-splash-16000-16bit-mono-signed.wav create mode 100644 tests/circuitpython-manual/audiocore/jeplayer-splash-16000-16bit-stereo-signed.wav create mode 100644 tests/circuitpython-manual/audiocore/jeplayer-splash-16000-24bit-mono-signed.wav create mode 100644 tests/circuitpython-manual/audiocore/jeplayer-splash-16000-24bit-stereo-signed.wav create mode 100644 tests/circuitpython-manual/audiocore/jeplayer-splash-16000-8bit-mono-unsigned.wav create mode 100644 tests/circuitpython-manual/audiocore/jeplayer-splash-16000-8bit-stereo-unsigned.wav create mode 100644 tests/circuitpython-manual/audiocore/jeplayer-splash-44100-16bit-mono-signed.wav create mode 100644 tests/circuitpython-manual/audiocore/jeplayer-splash-44100-16bit-stereo-signed.wav create mode 100644 tests/circuitpython-manual/audiocore/jeplayer-splash-44100-24bit-mono-signed.wav create mode 100644 tests/circuitpython-manual/audiocore/jeplayer-splash-44100-24bit-stereo-signed.wav create mode 100644 tests/circuitpython-manual/audiocore/jeplayer-splash-44100-8bit-mono-unsigned.wav create mode 100644 tests/circuitpython-manual/audiocore/jeplayer-splash-44100-8bit-stereo-unsigned.wav create mode 100644 tests/circuitpython-manual/audiocore/jeplayer-splash-44100-stereo.mp3 create mode 100644 tests/circuitpython-manual/audiocore/jeplayer-splash-8000-16bit-mono-signed.wav create mode 100644 tests/circuitpython-manual/audiocore/jeplayer-splash-8000-16bit-stereo-signed.wav create mode 100644 tests/circuitpython-manual/audiocore/jeplayer-splash-8000-24bit-mono-signed.wav create mode 100644 tests/circuitpython-manual/audiocore/jeplayer-splash-8000-24bit-stereo-signed.wav create mode 100644 tests/circuitpython-manual/audiocore/jeplayer-splash-8000-8bit-mono-unsigned.wav create mode 100644 tests/circuitpython-manual/audiocore/jeplayer-splash-8000-8bit-stereo-unsigned.wav create mode 100644 tests/circuitpython-manual/audiopwmio/single_buffer_loop.py create mode 100644 tests/circuitpython-manual/audiopwmio/wavefile_pause_resume.py create mode 100644 tests/circuitpython-manual/audiopwmio/wavefile_playback.py create mode 100644 tests/circuitpython-manual/socketpool/client/cpy-client.py create mode 100644 tests/circuitpython-manual/socketpool/client/host-server.py create mode 100644 tests/circuitpython-manual/socketpool/client/readme.md create mode 100644 tests/circuitpython-manual/socketpool/datagram/ntp.py create mode 100644 tests/circuitpython-manual/socketpool/datagram/readme.md create mode 100644 tests/circuitpython-manual/socketpool/server/cpy-server.py create mode 100644 tests/circuitpython-manual/socketpool/server/host-client.py create mode 100644 tests/circuitpython-manual/socketpool/server/readme.md (limited to 'tests/circuitpython-manual') diff --git a/tests/circuitpython-manual/audiocore/jeplayer-splash-16000-16bit-mono-signed.wav b/tests/circuitpython-manual/audiocore/jeplayer-splash-16000-16bit-mono-signed.wav new file mode 100644 index 000000000..cd3da395b Binary files /dev/null and b/tests/circuitpython-manual/audiocore/jeplayer-splash-16000-16bit-mono-signed.wav differ diff --git a/tests/circuitpython-manual/audiocore/jeplayer-splash-16000-16bit-stereo-signed.wav b/tests/circuitpython-manual/audiocore/jeplayer-splash-16000-16bit-stereo-signed.wav new file mode 100644 index 000000000..c1036475b Binary files /dev/null and b/tests/circuitpython-manual/audiocore/jeplayer-splash-16000-16bit-stereo-signed.wav differ diff --git a/tests/circuitpython-manual/audiocore/jeplayer-splash-16000-24bit-mono-signed.wav b/tests/circuitpython-manual/audiocore/jeplayer-splash-16000-24bit-mono-signed.wav new file mode 100644 index 000000000..6aa6afc7a Binary files /dev/null and b/tests/circuitpython-manual/audiocore/jeplayer-splash-16000-24bit-mono-signed.wav differ diff --git a/tests/circuitpython-manual/audiocore/jeplayer-splash-16000-24bit-stereo-signed.wav b/tests/circuitpython-manual/audiocore/jeplayer-splash-16000-24bit-stereo-signed.wav new file mode 100644 index 000000000..c175aaf61 Binary files /dev/null and b/tests/circuitpython-manual/audiocore/jeplayer-splash-16000-24bit-stereo-signed.wav differ diff --git a/tests/circuitpython-manual/audiocore/jeplayer-splash-16000-8bit-mono-unsigned.wav b/tests/circuitpython-manual/audiocore/jeplayer-splash-16000-8bit-mono-unsigned.wav new file mode 100644 index 000000000..ccf63b9c1 Binary files /dev/null and b/tests/circuitpython-manual/audiocore/jeplayer-splash-16000-8bit-mono-unsigned.wav differ diff --git a/tests/circuitpython-manual/audiocore/jeplayer-splash-16000-8bit-stereo-unsigned.wav b/tests/circuitpython-manual/audiocore/jeplayer-splash-16000-8bit-stereo-unsigned.wav new file mode 100644 index 000000000..63aaa5b29 Binary files /dev/null and b/tests/circuitpython-manual/audiocore/jeplayer-splash-16000-8bit-stereo-unsigned.wav differ diff --git a/tests/circuitpython-manual/audiocore/jeplayer-splash-44100-16bit-mono-signed.wav b/tests/circuitpython-manual/audiocore/jeplayer-splash-44100-16bit-mono-signed.wav new file mode 100644 index 000000000..147b14b16 Binary files /dev/null and b/tests/circuitpython-manual/audiocore/jeplayer-splash-44100-16bit-mono-signed.wav differ diff --git a/tests/circuitpython-manual/audiocore/jeplayer-splash-44100-16bit-stereo-signed.wav b/tests/circuitpython-manual/audiocore/jeplayer-splash-44100-16bit-stereo-signed.wav new file mode 100644 index 000000000..adfe84fb0 Binary files /dev/null and b/tests/circuitpython-manual/audiocore/jeplayer-splash-44100-16bit-stereo-signed.wav differ diff --git a/tests/circuitpython-manual/audiocore/jeplayer-splash-44100-24bit-mono-signed.wav b/tests/circuitpython-manual/audiocore/jeplayer-splash-44100-24bit-mono-signed.wav new file mode 100644 index 000000000..2c5337045 Binary files /dev/null and b/tests/circuitpython-manual/audiocore/jeplayer-splash-44100-24bit-mono-signed.wav differ diff --git a/tests/circuitpython-manual/audiocore/jeplayer-splash-44100-24bit-stereo-signed.wav b/tests/circuitpython-manual/audiocore/jeplayer-splash-44100-24bit-stereo-signed.wav new file mode 100644 index 000000000..a290c6c37 Binary files /dev/null and b/tests/circuitpython-manual/audiocore/jeplayer-splash-44100-24bit-stereo-signed.wav differ diff --git a/tests/circuitpython-manual/audiocore/jeplayer-splash-44100-8bit-mono-unsigned.wav b/tests/circuitpython-manual/audiocore/jeplayer-splash-44100-8bit-mono-unsigned.wav new file mode 100644 index 000000000..cd1c79280 Binary files /dev/null and b/tests/circuitpython-manual/audiocore/jeplayer-splash-44100-8bit-mono-unsigned.wav differ diff --git a/tests/circuitpython-manual/audiocore/jeplayer-splash-44100-8bit-stereo-unsigned.wav b/tests/circuitpython-manual/audiocore/jeplayer-splash-44100-8bit-stereo-unsigned.wav new file mode 100644 index 000000000..12c48e258 Binary files /dev/null and b/tests/circuitpython-manual/audiocore/jeplayer-splash-44100-8bit-stereo-unsigned.wav differ diff --git a/tests/circuitpython-manual/audiocore/jeplayer-splash-44100-stereo.mp3 b/tests/circuitpython-manual/audiocore/jeplayer-splash-44100-stereo.mp3 new file mode 100644 index 000000000..fd820e69a Binary files /dev/null and b/tests/circuitpython-manual/audiocore/jeplayer-splash-44100-stereo.mp3 differ diff --git a/tests/circuitpython-manual/audiocore/jeplayer-splash-8000-16bit-mono-signed.wav b/tests/circuitpython-manual/audiocore/jeplayer-splash-8000-16bit-mono-signed.wav new file mode 100644 index 000000000..a730c2b84 Binary files /dev/null and b/tests/circuitpython-manual/audiocore/jeplayer-splash-8000-16bit-mono-signed.wav differ diff --git a/tests/circuitpython-manual/audiocore/jeplayer-splash-8000-16bit-stereo-signed.wav b/tests/circuitpython-manual/audiocore/jeplayer-splash-8000-16bit-stereo-signed.wav new file mode 100644 index 000000000..5038ae8ae Binary files /dev/null and b/tests/circuitpython-manual/audiocore/jeplayer-splash-8000-16bit-stereo-signed.wav differ diff --git a/tests/circuitpython-manual/audiocore/jeplayer-splash-8000-24bit-mono-signed.wav b/tests/circuitpython-manual/audiocore/jeplayer-splash-8000-24bit-mono-signed.wav new file mode 100644 index 000000000..ac25cddb3 Binary files /dev/null and b/tests/circuitpython-manual/audiocore/jeplayer-splash-8000-24bit-mono-signed.wav differ diff --git a/tests/circuitpython-manual/audiocore/jeplayer-splash-8000-24bit-stereo-signed.wav b/tests/circuitpython-manual/audiocore/jeplayer-splash-8000-24bit-stereo-signed.wav new file mode 100644 index 000000000..6fec67431 Binary files /dev/null and b/tests/circuitpython-manual/audiocore/jeplayer-splash-8000-24bit-stereo-signed.wav differ diff --git a/tests/circuitpython-manual/audiocore/jeplayer-splash-8000-8bit-mono-unsigned.wav b/tests/circuitpython-manual/audiocore/jeplayer-splash-8000-8bit-mono-unsigned.wav new file mode 100644 index 000000000..d5e49b24b Binary files /dev/null and b/tests/circuitpython-manual/audiocore/jeplayer-splash-8000-8bit-mono-unsigned.wav differ diff --git a/tests/circuitpython-manual/audiocore/jeplayer-splash-8000-8bit-stereo-unsigned.wav b/tests/circuitpython-manual/audiocore/jeplayer-splash-8000-8bit-stereo-unsigned.wav new file mode 100644 index 000000000..3df7945aa Binary files /dev/null and b/tests/circuitpython-manual/audiocore/jeplayer-splash-8000-8bit-stereo-unsigned.wav differ diff --git a/tests/circuitpython-manual/audiopwmio/single_buffer_loop.py b/tests/circuitpython-manual/audiopwmio/single_buffer_loop.py new file mode 100644 index 000000000..dfac475b5 --- /dev/null +++ b/tests/circuitpython-manual/audiopwmio/single_buffer_loop.py @@ -0,0 +1,65 @@ +import audiocore +import audiopwmio +import board +import digitalio +import array +import time +import math + +trigger = digitalio.DigitalInOut(board.D4) +trigger.switch_to_output(True) + +# Generate one period of sine wav. +length = 8000 // 440 + +samples = [] +sample_names = [ +"unsigned 8 bit", +"signed 8 bit", +"unsigned 16 bit", +"signed 16 bit" +] + + +# unsigned 8 bit +u8 = array.array("B", [0] * length) +for i in range(length): + u8[i] = int(math.sin(math.pi * 2 * i / length) * (2 ** 7) + 2 ** 7) + +samples.append(audiocore.RawSample(u8, sample_rate=4000)) + +# signed 8 bit +s8 = array.array("b", [0] * length) +for i in range(length): + s8[i] = int(math.sin(math.pi * 2 * i / length) * (2 ** 7)) + +samples.append(audiocore.RawSample(s8, sample_rate=16000)) + +# unsigned 16 bit +u16 = array.array("H", [0] * length) +for i in range(length): + u16[i] = int(math.sin(math.pi * 2 * i / length) * (2 ** 15) + 2 ** 15) + +samples.append(audiocore.RawSample(u16, sample_rate=8000)) + + +# signed 16 bit +s16 = array.array("h", [0] * length) +for i in range(length): + s16[i] = int(math.sin(math.pi * 2 * i / length) * (2 ** 15)) + +samples.append(audiocore.RawSample(s16, sample_rate=8000)) + + +dac = audiopwmio.PWMAudioOut(board.D13) +for sample, name in zip(samples, sample_names): + print(name) + trigger.value = False + dac.play(sample, loop=True) + time.sleep(1) + dac.stop() + time.sleep(0.1) + trigger.value = True + print() + +print("done") diff --git a/tests/circuitpython-manual/audiopwmio/wavefile_pause_resume.py b/tests/circuitpython-manual/audiopwmio/wavefile_pause_resume.py new file mode 100644 index 000000000..27912d321 --- /dev/null +++ b/tests/circuitpython-manual/audiopwmio/wavefile_pause_resume.py @@ -0,0 +1,39 @@ +import audiocore +import audiopwmio +import board +import digitalio +import time +import math +import os + +trigger = digitalio.DigitalInOut(board.D4) +trigger.switch_to_output(True) + +sample_prefix = "jeplayer-splash" + +samples = [] +for fn in os.listdir("/"): + if fn.startswith(sample_prefix): + samples.append(fn) + +dac = audiopwmio.PWMAudioOut(left_channel=board.D12, right_channel=board.D13) +for filename in samples: + print("playing", filename) + with open(filename, "rb") as sample_file: + try: + sample = audiocore.WaveFile(sample_file) + except OSError as e: + print(e) + continue + trigger.value = False + dac.play(sample) + while dac.playing: + time.sleep(0.1) + dac.pause() + time.sleep(0.1) + dac.resume() + trigger.value = True + time.sleep(0.1) + print() + +print("done") diff --git a/tests/circuitpython-manual/audiopwmio/wavefile_playback.py b/tests/circuitpython-manual/audiopwmio/wavefile_playback.py new file mode 100644 index 000000000..0adbf2b19 --- /dev/null +++ b/tests/circuitpython-manual/audiopwmio/wavefile_playback.py @@ -0,0 +1,36 @@ +import audiocore +import audiopwmio +import board +import digitalio +import time +import math +import os + +trigger = digitalio.DigitalInOut(board.D4) +trigger.switch_to_output(True) + +sample_prefix = "jeplayer-splash" + +samples = [] +for fn in os.listdir("/"): + if fn.startswith(sample_prefix): + samples.append(fn) + +dac = audiopwmio.PWMAudioOut(left_channel=board.D12, right_channel=board.D13) +for filename in samples: + print("playing", filename) + with open(filename, "rb") as sample_file: + try: + sample = audiocore.WaveFile(sample_file) + except OSError as e: + print(e) + continue + trigger.value = False + dac.play(sample) + while dac.playing: + time.sleep(1) + trigger.value = True + time.sleep(0.1) + print() + +print("done") diff --git a/tests/circuitpython-manual/socketpool/client/cpy-client.py b/tests/circuitpython-manual/socketpool/client/cpy-client.py new file mode 100644 index 000000000..dcc742c9f --- /dev/null +++ b/tests/circuitpython-manual/socketpool/client/cpy-client.py @@ -0,0 +1,26 @@ +import wifi +import socketpool +import ssl +import time + +TIMEOUT = None +HOST = '192.168.10.179' +PORT = 5000 + +# Connect to wifi +print("Connecting to wifi") +wifi.radio.connect("mySSID", "myPASS") +pool = socketpool.SocketPool(wifi.radio) + +print("Creating Socket") +with pool.socket(pool.AF_INET, pool.SOCK_STREAM) as s: + s.settimeout(TIMEOUT) + + print("Connecting") + s.connect((HOST, PORT)) + print("Sending") + sent = s.send(b'Hello, world') + print("Receiving") + buff = bytearray(128) + numbytes = s.recv_into(buff) +print(repr(buff)) diff --git a/tests/circuitpython-manual/socketpool/client/host-server.py b/tests/circuitpython-manual/socketpool/client/host-server.py new file mode 100644 index 000000000..4a5faf2a0 --- /dev/null +++ b/tests/circuitpython-manual/socketpool/client/host-server.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 +import socket + +TIMEOUT = 10 +HOST = "192.168.10.179" +PORT = 5000 + +print("Create Socket") +with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + s.settimeout(TIMEOUT) + s.bind((HOST, PORT)) + s.listen() + print("Accepting connections") + while True: + try: + conn, addr = s.accept() + break + except BlockingIOError: + pass + + with conn: + s.settimeout(TIMEOUT) + print('Connected by', addr) + data = conn.recv(128) + print("got: " + str(data)) + conn.sendall(data) + print("sent: " + str(data)) \ No newline at end of file diff --git a/tests/circuitpython-manual/socketpool/client/readme.md b/tests/circuitpython-manual/socketpool/client/readme.md new file mode 100644 index 000000000..1ef069531 --- /dev/null +++ b/tests/circuitpython-manual/socketpool/client/readme.md @@ -0,0 +1,46 @@ +# Circuitpython as Client + +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. + +## Prerequisites + +Circuitpython V6.2.0 minimum. Neither the host or client sketch has installed module prerequisites. + +## Setup + +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! + +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. + +Start the Server on the host PC first, within this folder: + +``` +python host-server.py +``` + +Then, reload the client sketch in Circuitpython. + +## Expected Behavior + +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. + +Expected client output: + +``` +Connecting to wifi +Creating Socket +Connecting +Sending +Receiving +bytearray(b'Hello, world') +``` + +Expected Server output (IP/port values will vary): + +``` +Create Socket +Accepting connections +Connected by ('192.168.10.128', 64509) +got: b'Hello, world' +sent: b'Hello, world' +``` \ No newline at end of file diff --git a/tests/circuitpython-manual/socketpool/datagram/ntp.py b/tests/circuitpython-manual/socketpool/datagram/ntp.py new file mode 100644 index 000000000..31d922858 --- /dev/null +++ b/tests/circuitpython-manual/socketpool/datagram/ntp.py @@ -0,0 +1,28 @@ +import wifi +import socketpool +import struct +import time + +# connect to wifi +print("Connecting to Wifi") +wifi.radio.connect("mySSID", "myPASS") +pool = socketpool.SocketPool(wifi.radio) + +# make socket +print("Creating socket") +sock = pool.socket(pool.AF_INET,pool.SOCK_DGRAM) + +# Fill packet +packet = bytearray(48) +packet[0] = 0b00100011 # Not leap second, NTP version 4, Client mode +NTP_TO_UNIX_EPOCH = 2208988800 # 1970-01-01 00:00:00 + +print("Sending packet") +sock.sendto(packet, ("pool.ntp.org", 123)) + +size, address = sock.recvfrom_into(packet) +print("Received packet") + +seconds = struct.unpack_from("!I", packet, offset=len(packet) - 8)[0] +print("Address:", address) +print("Time:", time.localtime(seconds - NTP_TO_UNIX_EPOCH)) diff --git a/tests/circuitpython-manual/socketpool/datagram/readme.md b/tests/circuitpython-manual/socketpool/datagram/readme.md new file mode 100644 index 000000000..f63f0abcc --- /dev/null +++ b/tests/circuitpython-manual/socketpool/datagram/readme.md @@ -0,0 +1,19 @@ +# Circuitpython Datagrams + +This example demonstrates using UDP (datagrams) with the Socket module, accessing the time from an NTP server using `sendto` and `recvfrom_into`. + +## Prerequisites + +Circuitpython V6.2.0 minimum. + +## Expected behavior + +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. + +Expected output: +``` +Sending packet +Received packet +Address: ('82.197.188.130', 31488) +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) +``` diff --git a/tests/circuitpython-manual/socketpool/server/cpy-server.py b/tests/circuitpython-manual/socketpool/server/cpy-server.py new file mode 100644 index 000000000..d865156bc --- /dev/null +++ b/tests/circuitpython-manual/socketpool/server/cpy-server.py @@ -0,0 +1,31 @@ +import wifi +import socketpool + +TIMEOUT = None + +print("Connecting to Wifi") +wifi.radio.connect("mySSID", "myPASS") + +pool = socketpool.SocketPool(wifi.radio) + +print("Finding IP address") +print(wifi.radio.ipv4_address) +HOST = str(wifi.radio.ipv4_address) +PORT = 80 # Port to listen on + +print("Creating socket") +sock = pool.socket(pool.AF_INET,pool.SOCK_STREAM) + +sock.bind((HOST, PORT)) +sock.listen(1) +print("Accepting connections") +conn, addr = sock.accept() +with conn: + print('Connected by', addr) + buff = bytearray(128) + print("Receiving") + numbytes = conn.recvfrom_into(buff) + print(buff[:numbytes[0]]) + if numbytes: + print("Sending") + conn.send(buff[:numbytes[0]]) diff --git a/tests/circuitpython-manual/socketpool/server/host-client.py b/tests/circuitpython-manual/socketpool/server/host-client.py new file mode 100644 index 000000000..b5aacbfa8 --- /dev/null +++ b/tests/circuitpython-manual/socketpool/server/host-client.py @@ -0,0 +1,18 @@ +import socket + +HOST = '192.168.10.128' # The server's hostname or IP address +PORT = 80 # The port used by the server + +with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + s.settimeout(None) + + print("Connecting") + s.connect((HOST, PORT)) + + print("Sending") + s.send(b'Hello, world') + + print("Receiving") + data = s.recv(1024) + print('Received', repr(data)) + diff --git a/tests/circuitpython-manual/socketpool/server/readme.md b/tests/circuitpython-manual/socketpool/server/readme.md new file mode 100644 index 000000000..26118b8f1 --- /dev/null +++ b/tests/circuitpython-manual/socketpool/server/readme.md @@ -0,0 +1,43 @@ +# Circuitpython as Client + +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. + +## Prerequisites + +Circuitpython V6.2.0 minimum. Neither the host or client sketch has installed module prerequisites. + +## Setup + +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: + +``` +python host-client.py +``` + +Each sketch can have Timeout values changed. + +## Expected behavior + +The host machine will connect to the server running on the Circuitpython device, and send a "Hello, world" string which is then returned. + +Expected client output: + +``` +Connecting +Sending +Receiving +Received b'Hello, world' +``` + +Expected server output: +``` +Connecting to Wifi +Finding IP address +192.168.10.128 +Creating socket +Accepting connections +Connected by ('192.168.10.179', 33274) +Receiving +bytearray(b'Hello, world') +Sending +``` -- cgit v1.2.3