0

Web サイトを介してローリング シャッターを制御する 2relay モジュールを実装する lua ファイルを作成しました。
私はled_webserverの例に従い、そこからコードを拡張しました。function を使用して完全な html ページを送信する
ために非常に多くの方法を試しました が、うまくいかないようです。client:send()


これは私が自分で開発したページで、PC 上で動作しますが、簡単に送信する方法が見つかりませんでした。
すべての html コードを含むローカル変数 (たとえばtest ) を宣言し、それを `client:send(test) のパラメーターとして配置します。
誰かがそうする別の方法を知っていますか?

4

5 に答える 5

2

eps8266 の TCP スタックは、IP パケット間のストリーミングをサポートしていません。各送信は最大 1 つの完全な IP パケットにすることができますが、それよりも長く、soc:on("sent",callback). 詳細については、nodeMCU Unofficial FAQを参照してください。

于 2015-09-06T22:15:26.607 に答える
1

他の投稿者は多くの有益な情報を提供してくれましたが、非公式の FAQ でさえも、この問題に対する適切な具体的な解決策がまだ提供されていないと思います。

大きな静的ファイルを送信するには、フラッシュから読み込み、コールバックを使用してチャンクで送信できます。他の方もおっしゃっていますが、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

これに対する簡単な解決策を見つけました。テキストを含む大きなファイルを送信する必要があり、次のコードを使用しました。

    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 に答える
0

1 回の呼び出しで数百バイトを超える送信を行うと、多くの場合失敗します。また、同じ lua チャンク内で送信する複数の連続した呼び出しがキューに入れられ、チャンクが戻った後に非同期で実行されることにも注意してください。これにより、非現実的な量のメモリが消費されます。

そのため、実行時の値が連結された複雑なページを作成することは問題があります。より合理的なアプローチは、ESP8266 がデータを JSON として返すだけで、PC から派手なページを提供し、ESP にクエリを実行してデータをページに折りたたむための埋め込みスクリプトを使用することです。

于 2015-08-09T16:07:35.457 に答える