ionman
ionman

Reputation: 51

ESP8266 String max size 247 Bytes?

I program an ESP8266 NodeMCU with Lua script. As I was debugging the problem that strings just were cut off at the beginning and extended further. I send from ESP8266 to an Android Phone.

I looked more in testing the esp via UART Interface, following problem: the maximum String size when I declare a string container is 247 characters. After I exceed the 247th there is an error:

stdin:1: unexpected symbol near '='

The String is obviously too long but I need to send at least 2048 bytes per String for maximum efficiency. Is it possible to extend the input limit of a string variable?

(I build a 2048 bytes Packet and 86 bytes Overhead for the HTTP Get Response) The TCP Tx Buffer of ESP8266 is 2920 bytes.

str_resp0 = "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=UTF-8\r\n";
str_resp1 = "Connection: close\r\n\r\n";

send_buf = "";

uart.on("data", "$",
  function(data)
    t = {send_buf,data}
    send_buf = table.concat(t);

    if data=="quit$" then
      uart.on("data") -- quit the function
    end
end, 0)

srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
  conn:on("receive",function(conn,payload)
    --print(payload)
    conn:send(str_resp0)
    conn:send(str_resp1)
    conn:send(send_buf)
    send_buf = "";
  end)
  conn:on("sent",function(conn) conn:close() 
  end)
end)

Upvotes: 0

Views: 6006

Answers (2)

ionman
ionman

Reputation: 51

The Problem is as it seems unavoidable. Referring to the espressif forums:

"the delay added by the lower level code is 20ms it is documented!"

so the event frame could'nt get handled faster than this. The only way to tweak this might be buffering the data in microcontroller and sending it at once every 20ms or installing the so called "FreeRTOS SDK for ESP8266", whose transfer speed is only limited by uart speed.

Upvotes: 0

Marcel Stör
Marcel Stör

Reputation: 23535

stdin:1: unexpected symbol near '='

sounds very much like a problem with your "IDE" (ESPlorer?).

Furthermore, you should send long payloads in batches. The SDK limits the packet size to some 1500 bytes which is the standard Ethernet MTU.

http://nodemcu.readthedocs.io/en/latest/en/modules/net/#netsocketsend has some explanations and a nice example.

srv = net.createServer(net.TCP)
srv:listen(80, function(conn)
  conn:on("receive", function(sck, req)
    local response = {}

    -- if you're sending back HTML over HTTP you'll want something like this instead
    -- local response = {"HTTP/1.0 200 OK\r\nServer: NodeMCU on ESP8266\r\nContent-Type: text/html\r\n\r\n"}

    response[#response + 1] = "lots of data"
    response[#response + 1] = "even more data"
    response[#response + 1] = "e.g. content read from a file"

     -- sends and removes the first element from the 'response' table
    local function send(sk)
      if #response > 0
        then sk:send(table.remove(response, 1))
      else
        sk:close()
        response = nil
      end
    end

    -- triggers the send() function again once the first chunk of data was sent
    sck:on("sent", send)

    send(sck)
  end)
end)

Update 2016-07-18

Results of some experimenting in ESPlorer. The test uses a single initialized variable:

buffer = "A very long string with more than 250 chars..."
  • Hitting 'Send to ESP' (sending char-by-char over UART) will fail with an error similar to the one reported here.
  • Saving this into a file works fine.
    • Hit 'Save', will save the line to a file on your file system e.g. paul.lua
    • Hit 'Save to ESP', will send paul.lua to the device.
    • If it's not done automatically as part of 'Save to ESP' you can send dofile("paul.lua") to the device. This will make the variable buffer available in the global space.
    • Sending print(buffer) will print the entire string to the terminal window.

Upvotes: 1

Related Questions