Post your best Lua script examples here

User avatar
By Travis
#42068 Here is where my files are:
https://github.com/dieseltravis/esp8266-projects/tree/master/wifi-car

The problem is, whenever I connect to the the webserver with a browser, the ESP8266 never returns the whole file, it only makes it about 60 or so lines down. If I print to console I can see the whole file but something isn't working right. Take a look at the code I'm using to push the file here:
https://github.com/dieseltravis/esp8266-projects/blob/master/wifi-car/main.lua#L75-L100

Code: Select allfunction sendfile(conn, filename)
  if file.open(filename, "r") then
    --repeat
    --    --local line=file.read(128)
    --    local line=file.readline()
    --    if line then conn:send(line)end
    --  tmr.wdclr()
    --until not line
    --file.close()

    while true do
      line = file.readline()
      if (line == nil) then break end
      --print(string.sub(line, 1, -2))
      conn:send(line)
      tmr.wdclr()
      collectgarbage()
    end

    --conn:send(file.read())

    file.close()

  end

end


(you can see my various attempts in the commented out code)

My index.html file is here:
https://github.com/dieseltravis/esp8266-projects/blob/master/wifi-car/index.html

Anyone have any thoughts on what I might be doing wrong?
User avatar
By Travis
#42084 After a bit of googling it looks like I can bind to the "sent" event, and send the file in chunks like so:
https://github.com/dieseltravis/esp8266-projects/blob/master/wifi-car/main.lua#L132-L148

Code: Select allsock:on("sent", function (conn)
  if filename and file.open(filename, "r") then

    file.seek("set", index)
    local chunk=file.read(CHUNK_SIZE)
    file.close()

    if chunk then
      index = index + string.len(chunk)
      conn:send(chunk)
    else
      conn:close();
    end

    collectgarbage()
  end
end)


Not sure why a loop can't be used. ¯\_(ツ)_/¯

(Also note, after committing my code it may mess up the line numbers that I linked to in my previous post.)