Code: Select all
from machine import Pin, SPI
import utime
import socket
hspi = SPI(1, baudrate=2000000) #2M is good
url = 'http://mp3channels.webradio.antenne.de/chillout'
def writeData(buf): #sends data to VS1053
waitDREQ()
XDCS.off()
hspi.write(buf)
XDCS.on()
def http_get(url):
_, _, host, path = url.split('/', 3)
addr = socket.getaddrinfo(host, 80)[0][-1]
s = socket.socket()
s.connect(addr)
s.send(bytes('GET /%s HTTP/1.1\r\nHost: %s\r\n\r\n' % (path, host), 'utf8'))
while True:
data = s.recv(32)
if data:
writeData(data)
else:
break
s.close()
Then I decided to use Asyncio (of which I have limited understanding) with the plan of using two buffer (say size of 128 bytes each), while sending first buffer to VS1053, asynchronous socket fills the second buffer (I am not sure if this is a good approach though).
But in my first attempt with Asyncio, even 32kb stream does not sound ok (laggy). Here is the code:
Code: Select all
import socket, utime
import uasyncio as asyncio
import vs1053 as vs
url = 'http://mp3channels.webradio.antenne.de/chillout'
def writeData(buf): #sends data to VS1053
waitDREQ()
XDCS.off()
hspi.write(buf)
XDCS.on()
async def get_stream_data(url):
_, _, host, path = url.split('/', 3)
print(host,path)
reader, writer = yield from asyncio.open_connection(host, 80)
print(reader, writer)
print("================")
query = bytes('GET /%s HTTP/1.1\r\nHost: %s\r\n\r\n' % (path, host), 'utf8')
print(query)
yield from writer.awrite(query)
while True:
line = yield from reader.read(32)
if line:
writeData(line)
loop = asyncio.get_event_loop()
loop.create_task(get_stream_data(url))
loop.run_forever()
I am stuck in the middle of the project now.