As mentioned I wrote a script that is connecting to a weather API and printing out the result.
Everything works fine when I run it line by line through the interpreter (ESPlorer "Send to ESP- and run "line by line""), but when I execute it via dofile() it cannot connect to the site and fails.
I am confused and hope some of you would find the mistake I am ignoring.
Here is the code:
data= ""
s= net.createConnection(net.TCP, 0)
s:on("receive", function(so, da) data= da end)
s:connect(80, "api.openweathermap.org")
s:send("GET /data/2.5/weather?q=berlin,de&appid=9a3719c191ce0e1e70673f892013647e&units=metric HTTP/1.1\r\nHost: www.api.openweathermap.org\r\n\r\n")
for x in string.gmatch(data, "([^\n]+)") do
if string.find(x, '"coord"') ~= nil then
for k,v in pairs(cjson.decode(x)) do
if k == "main" or k == "weather" then
print("++++++"..k.."++++++")
if type(v) == "table" then
for kz, vz in pairs(v) do
if kz == 1 or kz == 2 then
for kd,vd in pairs(vz) do
print(kd,vd)
end
else print(kz,vz) end
end end end
end end end
s:close()