0

我创建了一个 lua 文件,它实现了一个 2relay 模块来控制通过网站的卷帘快门。
我遵循了led_webserver示例,并从那里扩展了我的代码。
我尝试了很多方法来发送带有该功能的完整 html 页面client:send()
但它似乎不起作用。


这是我自己开发的一个页面,它可以在电脑上运行,但我找不到简单的方法来发送它。
我声明了一个包含所有 html 代码的局部变量(例如test),然后我把它作为参数放在 `client:send(test).
有人知道另一种方法吗?

4

5 回答 5

2

eps8266 上的 TCP 堆栈不支持跨 IP 数据包的流式传输。每次发送最多可以是一个完整的 IP 数据包,但比这更长,并且您已经使用soc:on("sent",callback). 有关此问题的更多讨论,请参阅nodeMCU 非官方常见问题解答。

于 2015-09-06T22:15:26.607 回答
1

其他发帖者已经提供了很多很好的信息,但我认为目前还没有针对这个问题给出好的具体解决方案,即使在非官方的常见问题解答中也是如此。

要发送大型静态文件,您可以从闪存加载它们并使用回调将它们分块发送。正如其他人所提到的,一个电话可以发送多少是有限制的。并且单个回调中的多个发送调用可能无法按您的预期处理,并且可能占用太多内存。这是一个以块的形式加载和发送文件的简短示例:

local idx = 0 --keep track of where we are in the file
local fname = "index.html"

function nextChunk(c) --open file, read a chunk, and send it!
      file.open(fname)
      file.seek("set", idx)
      local str = file.read(500)
      if not str then return end --no more to send. 
      c:send(str)
      idx = idx + 500
      file.close() --close the file to let other callbacks use the filesystem
end
client:on("sent", nextChunk) --every time we send a chunk, start the next one!
nextChunk(client) --send the first chunk. 

或者,使用协程和计时器,我们可以使其更灵活:

local ready = true --represents whether we are ready to send again
client:on("sent", function() ready=true end) --we are ready after sending the previous chunk is finished.

local function sendFile(fname)
    local idx=0 --keep track of where we are in the file
    while true do
        file.open(fname)
        file.seek("set", idx)
        local str = file.read(500)
        if not str then return end --no more to send. 
        c:send(str)
        ready=false --we have sent something. we are no longer ready.
        idx = idx + 500
        file.close() --close the file to let other callbacks use the filesystem
        coroutine.yield() --give up control to the caller
    end
end

local sendThread = coroutine.create(sendFile)
tmr.alarm(0, 100, 1, function() --make a repeating alarm that will send the file chunk-by-chunk
    if ready then coroutine.resume(sendThread, "index.html") end
    if coroutine.status(sendThread) == "dead" then tmr.stop(0) end --stop when we are done
end)
于 2016-01-30T03:55:16.067 回答
0

You can use the following:

    buf = "<h1> ESP8266 Web Server</h1>";
    buf = buf.."<h2>Subtitle</h2>";
    client:send(buf);
于 2015-07-11T03:56:09.687 回答
0

在一次调用中发送超过几百个字节通常会失败。还要意识到,在同一个 lua 块中发送的多个连续调用会在块返回后排队并异步执行——这会消耗不切实际的内存量。

因此,使用串联的运行时值构建任何复杂的页面都是有问题的。更合理的方法是 ESP8266 仅将数据作为 JSON 返回,并从 PC 提供精美的页面,并使用嵌入式脚本来查询 ESP 并将数据折叠到页面中。

于 2015-08-09T16:07:35.457 回答
0

我为此找到了一个简单的解决方案。我不得不发送一个包含文本的大文件,并为此使用了以下代码:

    file.open("example.txt","r") 
    for counter=1, numboflinesinfile-1 do
    client:send(file.readline() .. "<br>");  
    end           
    file.close()

counter 只是一个计数变量 numberoflinesinfile 是您要发送的行数。

不是一个干净的解决方案,可能违反了所有程序规则,但可以作为一种魅力。

于 2015-09-27T16:03:31.823 回答