Hardware components | ||||||
![]() |
| × | 1 | |||
| × | 1 | ||||
Software apps and online services | ||||||
| ||||||
|
I want to complete a ethernet speaker project, but it seems to be very big, so I divide it into several parts to complete. This is part 1.
1. Download and install Thonny https://thonny.org/
2. Follow https://github.com/Wiznet/RP2040-HAT-CircuitPython install circuitPython and setup W5100S-EVB-Pico
3. Run examples/Network/W5x00_Echo_Demo_TCP.py test your network
4. generate different PWM tone follow
https://docs.circuitpython.org/en/latest/shared-bindings/audiopwmio/index.html
5. connect W5100S-EVB-Pico and class D amp and speaker.
6. run python app and use TCP client tool connect to your W5100S-EVB-Pico.
I use windows cmd connect to TCP server
7. key in 1~7 test the Ethernet to PWM speaker.
import busio
import digitalio
import audiocore
import audiopwmio
import board
import array
import time
import math
dac = audiopwmio.PWMAudioOut(board.GP3)
# Generate one period of Do sine wav.
length = 8000 // 523
sine_wave = array.array("H", [0] * length)
for i in range(length):
sine_wave[i] = int(math.sin(math.pi * 2 * i // length) * (2 ** 15) + 2 ** 15)
Do_wave = audiocore.RawSample(sine_wave)
# Generate one period of Re sine wav.
length = 8000 // 587
sine_wave = array.array("H", [0] * length)
for i in range(length):
sine_wave[i] = int(math.sin(math.pi * 2 * i // length) * (2 ** 15) + 2 ** 15)
Re_wave = audiocore.RawSample(sine_wave)
# Generate one period of Mi sine wav.
length = 8000 // 659
sine_wave = array.array("H", [0] * length)
for i in range(length):
sine_wave[i] = int(math.sin(math.pi * 2 * i // length) * (2 ** 15) + 2 ** 15)
Mi_wave = audiocore.RawSample(sine_wave)
# Generate one period of Fa sine wav.
length = 8000 // 698
sine_wave = array.array("H", [0] * length)
for i in range(length):
sine_wave[i] = int(math.sin(math.pi * 2 * i // length) * (2 ** 15) + 2 ** 15)
Fa_wave = audiocore.RawSample(sine_wave)
# Generate one period of So sine wav.
length = 8000 // 784
sine_wave = array.array("H", [0] * length)
for i in range(length):
sine_wave[i] = int(math.sin(math.pi * 2 * i // length) * (2 ** 15) + 2 ** 15)
So_wave = audiocore.RawSample(sine_wave)
# Generate one period of La sine wav.
length = 8000 // 880
sine_wave = array.array("H", [0] * length)
for i in range(length):
sine_wave[i] = int(math.sin(math.pi * 2 * i // length) * (2 ** 15) + 2 ** 15)
La_wave = audiocore.RawSample(sine_wave)
# Generate one period of Ti sine wav.
length = 8000 // 988
sine_wave = array.array("H", [0] * length)
for i in range(length):
sine_wave[i] = int(math.sin(math.pi * 2 * i // length) * (2 ** 15) + 2 ** 15)
Ti_wave = audiocore.RawSample(sine_wave)
# Generate one period of Do6 sine wav.
length = 8000 // 1047
sine_wave = array.array("H", [0] * length)
for i in range(length):
sine_wave[i] = int(math.sin(math.pi * 2 * i // length) * (2 ** 15) + 2 ** 15)
Do6_wave = audiocore.RawSample(sine_wave)
import adafruit_requests as requests
from adafruit_wiznet5k.adafruit_wiznet5k import *
from adafruit_wsgi.wsgi_app import WSGIApp
import adafruit_wiznet5k.adafruit_wiznet5k_wsgiserver as server
import adafruit_wiznet5k.adafruit_wiznet5k_socket as socket
##SPI0
SPI0_SCK = board.GP18
SPI0_TX = board.GP19
SPI0_RX = board.GP16
SPI0_CSn = board.GP17
##reset
W5x00_RSTn = board.GP20
print("Wiznet5k Server setting")
# Setup your network configuration below
# random MAC, later should change this value on your vendor ID
MY_MAC = (0x00, 0x01, 0x02, 0x03, 0x04, 0x05)
# see DHCP initialize below
IP_ADDRESS = (192, 168, 137, 200)
SUBNET_MASK = (255, 255, 255, 0)
GATEWAY_ADDRESS = (192, 168, 137, 1)
#DNS_SERVER = (8, 8, 8, 8)
DNS_SERVER = (192, 168, 137, 1)
led = digitalio.DigitalInOut(board.GP25)
led.direction = digitalio.Direction.OUTPUT
ethernetRst = digitalio.DigitalInOut(W5x00_RSTn)
ethernetRst.direction = digitalio.Direction.OUTPUT
# For Adafruit Ethernet FeatherWing
cs = digitalio.DigitalInOut(SPI0_CSn)
# cs = digitalio.DigitalInOut(board.D5)
spi_bus = busio.SPI(SPI0_SCK, MOSI=SPI0_TX, MISO=SPI0_RX)
# Reset W5500 first
ethernetRst.value = False
time.sleep(1)
ethernetRst.value = True
# Initialize ethernet interface without DHCP
eth = WIZNET5K(spi_bus, cs, is_dhcp=False, mac=MY_MAC, debug=False)
# Initialize ethernet interface with DHCP
#eth = WIZNET5K(spi_bus, cs, is_dhcp=True, mac=MY_MAC, debug=False)
# Set network configuration
eth.ifconfig = (IP_ADDRESS, SUBNET_MASK, GATEWAY_ADDRESS, DNS_SERVER)
print("Chip Version:", eth.chip)
print("MAC Address:", [hex(i) for i in eth.mac_address])
print("My IP address is:", eth.pretty_ip(eth.ip_address))
# Initialize a requests object with a socket and ethernet interface
requests.set_socket(socket, eth)
# Initialize a socket for our server
socket.set_interface(eth)
server = socket.socket() # Allocate socket for the server
server_ip = None # IP address of server
#server_ip = IP_ADDRESS # IP address of server
server_port = 50007 # Port to listen on
server.bind((server_ip, server_port)) # Bind to IP and Port
server.listen() # Begin listening for incoming clients
conn, addr = server.accept()
print("socket connected")
while True:
led.value = not led.value
time.sleep(1)
if conn:
data = conn.recv()
if data:
if data == b'1':
print("1")
dac.play(Do_wave, loop=True)
time.sleep(0.1)
dac.stop()
elif data == b'2':
print("2")
dac.play(Re_wave, loop=True)
time.sleep(0.1)
dac.stop()
elif data == b'3':
print("3")
dac.play(Mi_wave, loop=True)
time.sleep(0.1)
dac.stop()
elif data == b'4':
print("4")
dac.play(Fa_wave, loop=True)
time.sleep(0.1)
dac.stop()
elif data == b'5':
print("5")
dac.play(So_wave, loop=True)
time.sleep(0.1)
dac.stop()
elif data == b'6':
print("6")
dac.play(La_wave, loop=True)
time.sleep(0.1)
dac.stop()
elif data == b'7':
print("7")
dac.play(Ti_wave, loop=True)
time.sleep(0.1)
dac.stop()
elif data == b'8':
print("8")
dac.play(Do6_wave, loop=True)
time.sleep(0.1)
dac.stop()
else:
print(data)
else :
conn, addr = server.accept()
print("socket connected")
print("Done!")
Comments
Please log in or sign up to comment.