-- Get weather from openweathermap.org -- Modified by John Longworth - June 2017 city ="2639310" -- Put your own city code here apid = "74563a891225dce1a5796d1faddc5e41" -- Put you own API code here conn = nil conn=net.createConnection(net.TCP, 0) conn:on("connection", function(conn, payload) print("\n Connected to openweathermap.org") conn:send("GET /data/2.5/weather?id="..city.."&APPID="..apid.."&units=metric" .." HTTP/1.1\r\n" .."Host: api.openweathermap.org\r\n" .."Connection: close\r\n" .."Accept: */*\r\n" .."User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n" .."\r\n") end) conn:on("receive", function(conn, payload) payload = string.match(payload, "{.*}") -- Select json part of payload --print(payload) if payload ~= nil then forecast = sjson.decode(payload) -- Decode json string print_weather() else print("Connection failed") end end) function print_weather() print("Weather for") print(forecast.name.." "..forecast.sys.country) print("Long "..forecast.coord.lon.." Lat "..forecast.coord.lat) print("ID "..forecast.id) print("Temperature "..forecast.main.temp.." °C") print("Max Temp "..forecast.main.temp_max.." °C") print("Min Temp "..forecast.main.temp_min.." °C") print("Pressure "..forecast.main.pressure.." hPa") print("Humidity "..forecast.main.humidity.."%") print("Visibility "..forecast.visibility.." m") print("id "..forecast.weather[1].id) print("Main "..forecast.weather[1].main) print("Description "..forecast.weather[1].description) print("Windspeed "..forecast.wind.speed) print("Wind dir "..forecast.wind.deg) st = rtctime.epoch2cal(forecast.sys.sunrise) print("Sunrise "..st.hour.."."..st.min) st = rtctime.epoch2cal(forecast.sys.sunset) print("Sunset "..st.hour.."."..st.min) st = rtctime.epoch2cal(forecast.dt) print("Date "..st.hour.."."..st.min,st.day.."/"..st.mon.."/"..st.year) end conn:connect(80,'api.openweathermap.org')