505浏览
查看: 505|回复: 3

[K10教程] K10远程扩音器(对讲机)

[复制链接]
最近用ESP做大模型终端很流行,我们有个小群一些人也一直在摸索。
在实现大模型终端之前,准备先做对讲机。
今天这个教程就算半个对讲机吧。大家加个按钮控制,改成双向的应该就可以了。
声明:本教程代码由多个人使用多个大模型生成修改整合。

硬件:esp32一块,K10一块,麦克风模块INMP441一块,导线若干
编程环境:thonny,语言:micropython
固件:ESP32和ESP32S3标准固件(K10没用DF官方固件)

esp32接线图:

K10远程扩音器(对讲机)图1K10远程扩音器(对讲机)图2

ESP32作为发射端,K10作为接收端(还不会用K10的麦克风)
发送端代码:
  1. from machine import I2S, Pin
  2. import network
  3. import socket
  4. import time
  5. # Network Configuration
  6. SSID = 'waoo2111280'
  7. PASSWORD = 'waoo2111280'
  8. SERVER_IP = '192.168.22.198'
  9. SERVER_PORT = 12345
  10. # I2S Configuration
  11. SCK_PIN = 23  # Serial clock output
  12. WS_PIN = 22   # Word clock output
  13. SD_PIN = 21   # Serial data output
  14. # Audio Configuration
  15. SAMPLE_RATE = 16000
  16. SAMPLE_BITS = 16     # Changed to 16-bit for better compatibility
  17. CHANNELS = 1         # Mono channel
  18. BUFFER_LENGTH = 2048 # Increased buffer size for better performance
  19. def connect_wifi():
  20.     wlan = network.WLAN(network.STA_IF)
  21.     wlan.active(True)
  22.     if not wlan.isconnected():
  23.         print('Connecting to WiFi...')
  24.         wlan.connect(SSID, PASSWORD)
  25.         while not wlan.isconnected():
  26.             time.sleep(1)
  27.     print('WiFi Connected')
  28.     print('IP Address:', wlan.ifconfig()[0])
  29. def init_audio():
  30.     audio_in = I2S(
  31.         0,                     
  32.         sck=Pin(SCK_PIN),
  33.         ws=Pin(WS_PIN),
  34.         sd=Pin(SD_PIN),
  35.         mode=I2S.RX,           
  36.         bits=SAMPLE_BITS,      
  37.         format=I2S.MONO,       # Changed to mono for simplicity
  38.         rate=SAMPLE_RATE,      
  39.         ibuf=BUFFER_LENGTH * 2 # Double buffer size
  40.     )
  41.     return audio_in
  42. def main():
  43.     connect_wifi()
  44.     audio = init_audio()
  45.     sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  46.     audio_buf = bytearray(BUFFER_LENGTH)
  47.    
  48.     print('Starting audio capture and transmission...')
  49.    
  50.     try:
  51.         while True:
  52.             # Read audio data
  53.             num_read = audio.readinto(audio_buf)
  54.             if num_read > 0:
  55.                 # Send data in smaller chunks to prevent packet loss
  56.                 chunk_size = 1024
  57.                 for i in range(0, num_read, chunk_size):
  58.                     chunk = audio_buf[i:min(i + chunk_size, num_read)]
  59.                     sock.sendto(chunk, (SERVER_IP, SERVER_PORT))
  60.             time.sleep_ms(1)
  61.                
  62.     except Exception as e:
  63.         print('Error:', e)
  64.     finally:
  65.         audio.deinit()
  66.         sock.close()
  67. if __name__ == '__main__':
  68.     main()
复制代码
接收端代码:wifi_manager.py
  1. """WiFi connection management"""
  2. import network
  3. import time
  4. def connect_wifi(ssid, password):
  5.     """Connect to WiFi network"""
  6.     wlan = network.WLAN(network.STA_IF)
  7.     wlan.active(True)
  8.    
  9.     if not wlan.isconnected():
  10.         print('Connecting to WiFi...')
  11.         wlan.connect(ssid, password)
  12.         while not wlan.isconnected():
  13.             time.sleep(1)
  14.    
  15.     print('Connected to WiFi')
  16.     print('IP:', wlan.ifconfig()[0])
  17.     return wlan
复制代码
audio_output.py
  1. """Audio output handling using I2S and MAX98357A"""
  2. from machine import I2S, Pin
  3. class AudioOutput:
  4.     def __init__(self, bclk_pin, ws_pin, data_pin, sample_rate, bits):
  5.         """Initialize I2S output for MAX98357A"""
  6.         self.audio_out = I2S(
  7.             1,                          # I2S ID (0 or 1)
  8.             sck=Pin(bclk_pin),         # Bit clock
  9.             ws=Pin(ws_pin),            # Word select
  10.             sd=Pin(data_pin),          # Data
  11.             mode=I2S.TX,               # Transmit mode
  12.             bits=bits,                 # Sample size
  13.             format=I2S.MONO,           # Mono format
  14.             rate=sample_rate,          # Sample rate
  15.             ibuf=10000                 # Internal buffer size
  16.         )
  17.    
  18.     def write(self, audio_data):
  19.         """Write audio data to I2S DAC"""
  20.         self.audio_out.write(audio_data)
  21.    
  22.     def deinit(self):
  23.         """Clean up I2S resources"""
  24.         self.audio_out.deinit()
复制代码
config.py
  1. """Configuration settings for the audio receiver"""
  2. # Network settings
  3. WIFI_SSID = 'waoo2111280'
  4. WIFI_PASSWORD = 'waoo2111280'
  5. UDP_PORT = 12345
  6. '''
  7. # I2S pins for MAX98357A
  8. BCLK_PIN = 26  # Bit clock
  9. WS_PIN = 25    # Word select / Left-right clock
  10. DATA_PIN = 22  # Data pin
  11. '''
  12. # 初始化引脚定义
  13. BCLK_PIN = 0 # 串行时钟输出
  14. WS_PIN  = 38  # 字时钟
  15. DATA_PIN  = 45  # 串行数据输出
  16. # Audio settings
  17. SAMPLE_RATE = 16000
  18. BITS_PER_SAMPLE = 16
  19. BUFFER_SIZE = 1024
复制代码
main.py
  1. """Main program for ESP32 audio receiver"""
  2. import socket
  3. from config import *
  4. from wifi_manager import connect_wifi
  5. from audio_output import AudioOutput
  6. def main():
  7.     # Connect to WiFi
  8.     connect_wifi(WIFI_SSID, WIFI_PASSWORD)
  9.    
  10.     # Initialize audio output
  11.     audio = AudioOutput(
  12.         bclk_pin=BCLK_PIN,
  13.         ws_pin=WS_PIN,
  14.         data_pin=DATA_PIN,
  15.         sample_rate=SAMPLE_RATE,
  16.         bits=BITS_PER_SAMPLE
  17.     )
  18.    
  19.     # Setup UDP socket
  20.     sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  21.     sock.bind(('0.0.0.0', UDP_PORT))
  22.     print(f'Listening on port {UDP_PORT}')
  23.    
  24.     try:
  25.         while True:
  26.             try:
  27.                 # Receive audio data
  28.                 data, addr = sock.recvfrom(BUFFER_SIZE)
  29.                 # Play audio through MAX98357A
  30.                 audio.write(data)
  31.             except Exception as e:
  32.                 print('Error:', e)
  33.    
  34.     finally:
  35.         audio.deinit()
  36.         sock.close()
  37. if __name__ == '__main__':
  38.     main()
复制代码
修改wifi账号密码,把两端各自的代码上传到板子上,运行,先运行接收端,获得IP地址填入发送端代码。
K10远程扩音器(对讲机)图3
K10远程扩音器(对讲机)图4
K10远程扩音器(对讲机)图5
视频


音频.zip

2.07 MB, 下载次数: 36

easy猿  初级技师

发表于 2024-12-29 14:34:19

回复

使用道具 举报

glwz007  初级技匠

发表于 2024-12-29 15:15:00

非常好的项目,谢谢分享!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4 备案 沪公网安备31011502402448

© 2013-2025 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail