Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
Wen-Liang Lin
Published © GPL3+

Ethernet to PWM speaker part 1

Use TCP to send sound commands to the device to generate PWM to sound to the speaker.

BeginnerWork in progress2 hours118
Ethernet to PWM speaker part 1

Things used in this project

Hardware components

W5100S-EVB-Pico
WIZnet W5100S-EVB-Pico
×1
Adafruit Mono 2.5W Class D Audio Amplifier - PAM8302
×1

Software apps and online services

WIZnet RP2040-HAT-CircuitPython-1.0.0
Thonny

Story

Read more

Schematics

W5100S-EVB-Pico_SCH_V110

https://github.com/Wiznet/Hardware-Files-of-WIZnet/blob/master/02_iEthernet/W5100S/W5100S-EVB-Pico/W5100S-EVB-Pico_V110/Schematic/W5100S-EVB-Pico_SCH_V110.PDF

Code

TCP_PWM_Audio.py

MicroPython
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!")

Credits

Wen-Liang Lin
29 projects • 34 followers
Hi, I am momososo
Contact

Comments

Please log in or sign up to comment.