As the title says... Chat on...

User avatar
By d.s
#47109 0
down vote
favorite


As mentioned I wrote a script wich is connecting to an 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 so on fails. I am confused and hope some of you would find the mistake I am ignoring.

Code: Select alldata= ""
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()
User avatar
By dnc40085
#47180 Dev 0.9.6 is outdated and had a lot of problems. You should update to the most recent version of NodeMCU (1.5.1), the dev branch is the most current.
The simplest solution for getting the latest firmware is running a build of the dev branch on the cloud builder service, which emails you a link to the image when the build is complete.
You can find other options for building the firmware HERE.

As for the issue you are having, the example in readme.md of the master branch is incorrect. If you check out the example for net.socket:send() in the NodeMCU documentation it should help you remedy your problem.