Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By draco
#15200 In Print.cpp, println() for a String is indeed defined as two distinct print calls. So that's why it takes twice as long to use println() as it does print().
Code: Select allsize_t ICACHE_FLASH_ATTR Print::println(const String &s)
{
  size_t n = print(s);
  n += println();
  return n;
}


In turn, print() is defined as a call to write():
Code: Select allsize_t ICACHE_FLASH_ATTR Print::print(const String &s)
{
  return write(s.c_str(), s.length());
}


In ClientContext.h, write() for a WiFiClient object actually calls tcp_write().

I found this:
Code: Select all#define TCP_TMR_INTERVAL       250  /* The TCP timer interval in milliseconds. */

But that doesn't explain how we get to 125 ms. I will keep looking but that's all I have found so far.
User avatar
By Chris--A
#15565 I have added a pull request which has shown to remove a large amount of latency from WifiClient.

https://github.com/esp8266/Arduino/pull/111/files

Discussed here: viewtopic.php?f=28&t=2672

Also, the benchmarks need to consider things like printing a float. .println( 123.4567f, 4 ); This function prints the int part, then the decimal, then each of the numbers making up the fraction part are printed one character at a time, lastly a \r\n is printed. My example here would require 7 calls to .write() which is also 7 calls to tcp_write().

As you guys have seen, its better to concatenate a string before printing. If you would like to use the formatting features of the Print library, my output buffering class will help.

You can specify a buffer size and it will do everything for you. If you are finished with the buffer before it goes out of scope (like using a global instead of local variable), you can simply call flush() to push the remaining contents. It works with any Arduino library that inherits Print.

Code: Select all/***
    Written by Christopher Andrews
    MIT licence.
***/

template< unsigned length >
  class BufferedPrint : public Print{
    public:
      using Print::write;
      BufferedPrint( Print &output ) : output( output ), cursor( 0x00 ) {}
      ~BufferedPrint() { flush(); }
         
      void flush(){
          if( cursor ) output.write( buffer, cursor );
          cursor = 0x00;
        }
       
      size_t write( const byte *buffer, size_t size ){
        size_t count = size;
        while( count-- ) write( *buffer++ );
        return size;
      }
     
      size_t write( byte data ){
          if( cursor == length ) flush();
          buffer[ cursor++ ] = data;
          return 0x01;
        }
    protected:
      Print &output;
      byte buffer[ length ];
      uint16_t cursor;
};

//============================================================

BufferedPrint< 150 > buffer( Serial );

void setup(){
  Serial.begin( 9600 );
}

void loop(){
  buffer.println( "hi, this is a test block of text, you should see it appear in chunks!" );
  delay( 500 );
}
User avatar
By ReinholdHiller
#17176
draco wrote:I initially tried to store my data in "PROGMEM" before realizing that the ESP8266 does not make a distinction between different types of memory, like the smaller platforms that Arduino has targetted, so you'll see some of the code reference PROGMEM but it isn't really necessary. All the data is actually just sitting in regular RAM. I was concerned about that, but a minimal sketch tells me I have 38k of heap available, so having 10-15k of HTML/image in memory isn't that concerning after all.


But it adds up. Every little "Serial.println("debug: blah");" eats another piece of RAM. And how to know when Stack is full? Then you will get random crashes without any chance to debug it...

I think there is need of a solution like F() or PROGMEM...
User avatar
By rvbcrs
#17334 Hi, I have the problem that I try to send a nice looking html page with _currentClient.print(response); but html is always getting capped at 2,876 bytes. Is there a way to send the whole page in chuncks? or another way to send the whole page? Anyone who can provide me with an example?

Thanks!