-- Get weather from openweathermap.org and display in a browser -- John Longworth - June 2017 -- Needs sjson and rtctime modules city ="2639310" -- Put your own city code here apid = "74563a891345dce1a5796d1faddc5e41" -- Put you own API code here wifi.setmode(wifi.STATIONAP) station_cfg={} station_cfg.ssid="your SSID" -- Put you own here station_cfg.pwd="password" -- Put you own here station_cfg.save=true wifi.sta.config(station_cfg) cfg={ip="192.168.0.50",netmask="255.255.255.0",gateway="192.168.0.1"} wifi.sta.setip(cfg) ip = wifi.sta.getip() if ip ~= nil then print("\n Weather Server started.\n") print("Key this IP address "..ip.." into a browser") end 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("Icon "..forecast.weather[1].icon) 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 srv=net.createServer(net.TCP,0) srv:listen(80,function(server) server:on("receive",function(server,payload) server:send("NodeMCU Weather Page" .."

Weather from NodeMCU & openweathermap.org

" .."

ESP8266 Chip ID "..node.chipid().."

" .."
" ..forecast.weather[1].main.."

" .."

Max Temp "..forecast.main.temp_max.."°C
" .."Min Temp "..forecast.main.temp_min.."°C

" .."") end) server:on("sent",function(srv) srv:close() end) end) conn:connect(80,'api.openweathermap.org')