EPS8266 と nodeMCU を使用して時間を取得し、I2C 経由で RTC を設定したいと考えています。
これは私のスクリプトです:
-- file print.lua
local file = assert(loadfile("httpget.lua"))
file() --get Date and Time from google
print("Print follows:") --this should be executed after "file()"
print(date)
これはファイルですhttpget.lua
:
-- file httpget.lua
print('httpget.lua started')
conn=net.createConnection(net.TCP, 0)
-- show the retrieved web page
conn:on("receive", function(conn, payload)
date = string.sub(payload,string.find(payload,"Date: ")
+6,string.find(payload,"Date: ")+35)
conn:close()
end)
conn:on("connection", function(conn, payload)
print('\nConnected')
conn:send("HEAD / HTTP/1.1\r\n"
.."Host: google.com\r\n"
.."Accept: */*\r\n"
.."User-Agent: Mozilla/4.0 (compatible; esp8266 Lua;)"
.."\r\n\r\n")
end)
-- when disconnected, let it be known
conn:on("disconnection", function(conn, payload)
print("Disconnected\r\n"..date)
end)
conn:connect(80,'google.com')
conn = nil
結果は次のとおりです。
> dofile("print.lua")
httpget.lua started
Print follows: -- this should be at the end
nil -- date==nil because httpget.lua not executed
>
Connected
Disconnected
Sun, 26 Apr 2015 10:30:03 GMT
scipt を (リセットせずに) 再度実行すると、以前の実行から日付が取得されます。「httpget.lua」を実行して、それに続くスクリプトで「日付」を取得するにはどうすればよいですか?
Lua 5.1.4 を搭載した NodeMCU 0.9.6 ビルド 20150406 で ESP8266 を使用しています。 https://github.com/nodemcu/nodemcu-firmware/wiki/nodemcu_api_en#index
ESPlorer v2.0を使用してUSB経由でsriptをESP8266にロードします。conn.net... コマンドは NodeMCU ファームウェアの一部です (リンクを参照)。このスクリプトは、EPS8288 および NodeMCU ファームウェアでのみ実行できます。私の問題は次のとおりです。conn:net ルーチンを適切に終了し、次のプログラム部分にデータを返す方法が見つかりません。