Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By fgomes
#24688 Hi, I was trying to get a webradio stream using the WiFiClient as an example, and after some tweaking I am able to start receiveing the stream but it stops in a few seconds, any idea why? The 'simplified' code:

Code: Select all  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }
 
  // We now create a URI for the request
  String url = streamId;
 
  Serial.print("Requesting URL: ");
  Serial.println(url);
 
  // This will send the request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n\r\n");

  Serial.println(client.status());
  Serial.println(client.available());

  while(client.connected()){   
    n = client.peek();
    if(n>0) {
      if (n>1024)
        n = 1024;
      client.read(buf, n);
      buf[n] = 0;
      Serial.println((char*)buf);
    } else {
      Serial.print(".");
      delay(1000);
    }
  }
  Serial.println();
  Serial.println("closing connection");


Using the following server:

Code: Select allconst char* host = "icecast.omroep.nl";
const char* streamId   = "/3fm-bb-mp3";


This is the result I get:

Code: Select allConnecting to xpto-net
......
WiFi connected
IP address:
192.168.3.249
connecting to icecast.omroep.nl
Requesting URL: /3fm-bb-mp3
4
0
.HTTP/1.0 200 OK
Server: ics2kss.omroep.nl
Date: Fri, 31 Jul 2015 00:15
:53 GMT
Content-Type: audio/mpeg
Cache-Control: no-cache

Expires: Mo
n, 26 Jul 1997 05:00:00 GMT
Pragma: no-cache
icy-br:192
ice-audio-info: bitrate=192
icy-br:192
icy-genre:
Pop
icy-name:3FM Serious Radio
icy-pub:0
icy-url:http://www.3fm.nl

Ì[@¡7Ë
Ú(Eìí
ÉÇÊšh»n�øÔ²
Oþ*÷M&8Üu×èó”úË”ÝÿþçiŸHpNɨ 
¦C:Še!­îß|å¨Ñ�
µ½mÇÉlêMVµÞé·,³I›æ-gù�HrC¼±þ5lÛãÅöß’§2•|¬Ù€
Š8»Ùz�?‰L0
¯8±ÚH³Üù\y#Ô�«srÝ0€6&©[‰’è<Ÿˆ†¦êàS¾ÓíºvŠ2bÿû´`@ŽF)_OZcr?ǪG¥\Z±}8miíÁ©³só« +èaõZ=$U´‡ïïÁÞ�K±׬ÿÏ~m{e,&¨œyý^r¾ ¹óˆj
KH}OËÙè\ûFÕï¬û¶YÕŽ°Ì_­� 
¥ xâA7U˜eºˆH
”Y¦ÞÕÜÓMåÍï´
ÿŒcýæƒ|R÷Î}3�W¶ÃÜ-OZVÙž[<ÔG0ó½G«Æ¶FúZùs¹ïu
F%&y˜†ÌÌ”ŒTG©(qdD‹.A’9Á
ºžé$Ü@(ÓK‚$µ± HÉz¬°êW
‰hÁCPº&˜¢0‘VÂ_öqÄ”^)l,íôaê·8ŠõFW!«–÷ˆi²>ÉaÑ�€ÿû´`A fí_ΓY{p=':j b\Z½�DláíÁ›h`ö   pì’k÷}ÈërT¹AM·¶1ÇX€«Š¬`x§~Îß¼IŒ•‰¸¹’sðœoŽCAÀÿ:ß!Š=Ày{¿‡�{îŸÍ$yØØãæô�
÷sŒó2Þ'–kü<
yxù¾žDí'L@
^€0l¨pîCºÐ!Ƶ>ÿ×vf×Ûh|¨·)“.˜}ú~¥qÛ“’ˆÉŽ®br€ËtË
S)oCN³ôý8UH¥V%ƒxªN… �Ñpºì›Ì;g0fÄqÔÍH$¤
Ñd0„Ø.Ëq::¡œ¨JèÒ~žQ]�[[(¤m’hÌÑ«   ¬¿9ÃNÑ$i9V4VWŠÌ6e5/Hù„ÇÇ9mP¼ff«Æ©€
«3v†œñ†¼J�ƒMRøœ1nT°¸Þ/êf:2öŠ
kÿû´`6f `OZ{p@:m\•�>miíÀÞiLS   qBŽÛß·U
?ŽE$6(fº‚�ãÇq÷ÜHLnr³(kµNâ9½oñqMVÖqYry–1Ô‘Bصë«æ5í½í3Yß?ƒk­ÁË›ŒÖœÂÜUl+½‘µìhóYôõÊ}Já¼O
ì
gö„
Ê—ð¼X¢˜�€
s²½]6«°P�
çÑi8f¦ç=4�38ŽÙ¯•ÿû´`7€ž`Ok
4Ál ’[š'
\$È…ÔI6#)zÝëZ
îó…gÜ   kR¦w–:(µ¤(}û¼ý•Mn륌IoKeïâçŽÅ!ùe6}¾íJ"±oüÓ‰Ü_µ°Æ!—Qe+íËT¹oõ•éd}[Æ
H8m��ˆ_âES¬xª‚À–ÅQ¿ è%oˆÀ¡+Ÿ7ÉÕ}KÆ�8)àp›¤2ê2å”ãD É}Bÿû´`$ŽL`Qk
Æ*šâ 
ÙîfÌ“�Ÿ’ûöÀ�O$”HŠB‹›gˆ
€
................................................................................


So after receiving some stream mp3 data I stop receiving data, (no bytes available) no matter how long I wait, but the connection is kept (client.connected() keep returning true). In this example I'm printing the bytes just for testing purposes, I've also made other tests where I just periodically printed the number of bytes received and the problem was the same.

This stream works well with other clients (for example in the browser). It is also one of the streams used in the ESP8266 WebRadio example code using the SDK:

https://github.com/espressif/esp8266_mp ... ser_main.c

Thanks in advance!

Fernando
User avatar
By lasse.lukkari
#24689 http://arduino.esp8266.com/versions/1.6.5-947-g39819f0/doc/reference.html says:

Remember that there is a lot of code that needs to run on the chip besides the sketch when WiFi is connected. WiFi and TCP/IP libraries get a chance to handle any pending events each time the loop() function completes, OR when delay is called. If you have a loop somewhere in your sketch that takes a lot of time (>50ms) without calling delay, you might consider adding a call to delay function to keep the WiFi stack running smoothly.

There is also a yield() function which is equivalent to delay(0). The delayMicroseconds function, on the other hand, does not yield to other tasks, so using it for delays more than 20 milliseconds is not recommended.


Could this help?
User avatar
By fgomes
#24696 Hi lasse.lukkari

Thanks for your reply, you might be right! I'll test it later today, I was (maybe wrongly) assuming that the WiFiClient methods will call the yield function. Nevertheless, when the sketch doesn't receive any data it calls delay(1000), so it will indirectly calls yield, but it might be too late because this only happens if it doesn't 'receive' any new data, and this might be caused by not calling yield before :-)

I'll try it and post here the result!

Best regards

Fernando