ESP8266 Internet Radio unreliable WiFi data

stevestrong
Posts: 4
Joined: Sun Feb 11, 2024 8:57 pm

ESP8266 Internet Radio unreliable WiFi data

Postby stevestrong » Sun Feb 11, 2024 9:24 pm

Hello,

I used a very simple WiFi client to connect to an internet radio stream on a ESP8266 Node V3 board.

I have a big problems with the data reception reliability:
- if I try to receive the data as quick as possible (main loop basically containing if client connected and if client available then read all data and store in a ring buffer) then I read very within 200 ms about 20kB of data, which, however, does not seem to be ok.

I see that is not ok because I detect the meta interval info from the header and I parse the meta info on each interval from the stream.
In this error case the meta section in the received stream occurs in 200 ms interval, and not in 1..1.5 seconds interval as usual. And after receiving 3...5 times this meta info it losses the sync within the stream (means that the chunk meta number after counting the interval has nonsense values (larger than 100). So I have the feeling that the read data from the client is old data repeated.

I found out that if I insert some delay in the reception part (where I read out the data from client) then the thing works, meaning that meta info comes regularly in the specified intervals and losses sync only when the ring buffer overrun (read out from the buffer is slower than the writing to the buffer).

To fix this thing I finally ended up with implementing a dynamic delay on the WIFi client read-out side dependent on the ring buffer fill status. And it works like a charm, nice continuous sound, no buffer overflow, and no missed metadata within 1 hour test period.

So my question would be: is this kind of behavior known? I mean no one had any problems with such kind of setup before?

I appreciate any reply / hint / advice / comment.

stevestrong
Posts: 4
Joined: Sun Feb 11, 2024 8:57 pm

Re: ESP8266 Internet Radio unreliable WiFi data

Postby stevestrong » Mon Feb 12, 2024 8:44 am

Just for those interested:

I have searched more about the techniques how to best consume streams, and I figured out that the solution is to read from internet client only if the decoder (VS1003 in my case) needs data.
So now I fill up the buffer, start streaming, and read from client as long as there is space in buffer.
This way the buffer can be always be filled to max and I read from client only if space is available (due to decoder read out).

User avatar
Inq720
Posts: 37
Joined: Fri Dec 22, 2023 1:36 pm
Location: North Carolina, USA
Contact:

Re: ESP8266 Internet Radio unreliable WiFi data

Postby Inq720 » Mon Feb 12, 2024 12:09 pm

Without seeing your code, I can't be sure... but you can't just continuously read in a loop and not return control to the processor. Assuming you are using the Arduino IDE coding style, you have to return control from the loop() in a timely manner. This is well documented, so yes, it is known. The 8266 is a single threaded processor, but you have to let the underlying WiFi code process the TCP/IP packets so that you can retrieve them. On some implementations, the delay() function (under the covers) actually returns control to the processor to let it do its work.

rin67630
Posts: 141
Joined: Sun Mar 11, 2018 5:13 pm

Re: ESP8266 Internet Radio unreliable WiFi data

Postby rin67630 » Tue Feb 13, 2024 8:45 am

stevestrong wrote:
Mon Feb 12, 2024 8:44 am
I have searched more about the techniques how to best consume streams...
Everybody has obsolete smartphones in their drawers. They are perfect to listen to radio streams.
No need to tease an ESP8266 for that.

stevestrong
Posts: 4
Joined: Sun Feb 11, 2024 8:57 pm

Re: ESP8266 Internet Radio unreliable WiFi data

Postby stevestrong » Tue Feb 13, 2024 1:57 pm

Inq720 wrote:
Mon Feb 12, 2024 12:09 pm
Without seeing your code, I can't be sure... but you can't just continuously read in a loop and not return control to the processor. Assuming you are using the Arduino IDE coding style, you have to return control from the loop() in a timely manner. This is well documented, so yes, it is known. The 8266 is a single threaded processor, but you have to let the underlying WiFi code process the TCP/IP packets so that you can retrieve them. On some implementations, the delay() function (under the covers) actually returns control to the processor to let it do its work.
I am aware of inserting

Code: Select all

yield();
in time critical places, so that is not a problem, I do not get any WDT exceptions.
The Arduino loop is fragmented and has at least two sections, none of them is blocking for more than needed.
One of the section is to read data from wifi client and store it into the ring buffer.

The error case happens when I neglect the buffer state and just read out data from Wifi client continuously as long as data available. Each time I read out about ~1500 byes.
Of course I continue with other sections of the loop when this is done.
But when I do not slow down this read out rate, then the received data is somehow broken, as I described above. This is the main point.

Can happen that the server will send me this data when I consume it too quickly?

stevestrong
Posts: 4
Joined: Sun Feb 11, 2024 8:57 pm

Re: ESP8266 Internet Radio unreliable WiFi data

Postby stevestrong » Tue Feb 13, 2024 2:03 pm

rin67630 wrote:
Tue Feb 13, 2024 8:45 am
Everybody has obsolete smartphones in their drawers. They are perfect to listen to radio streams.
No need to tease an ESP8266 for that.
I am doing this not because there are no other (better) ways to accomplish that, but because I want to do it ;) .
The main reason is that I have a portable broken Internet Radio device, and want to repair it by replacing the main controller. :mrgreen:

Who is online

Users browsing this forum: No registered users and 24 guests