A place users can post their projects. If you have a small project and would like your own dedicated place to post and have others chat about it then this is your spot.

User avatar
By Mikejstb
#18867 A comment from Stefan on Squix's blog has the clue - we should use "api.openweather.org" instead of the IP address. This is confirmed on the openweathermap web site.
Only problem is now I get the dreaded " not enough memory" error on trying to compile.
I've eliminated all local variables and all print() calls but still can't get it small enough to compile.

Any ideas?
User avatar
By Mikejstb
#18875 snipping out a few more things got it to compile, but it still isn't getting any data.

I changed this line
conn:connect(80,"95.85.58.189")

to this
conn:connect(80,"api.openweathermap.org")

but still no joy :(
-
User avatar
By kubi
#18887 @zoomx: I use the ESP-01, no problem.

@Mikejstb: I think you must retrieve the IP of 'api.openweathermap.org' first with a dns request. It should work like in init.lua for the google address. The conn:connect doesn't accept URLs.
User avatar
By Mikejstb
#18891 OK - still no joy.

Here's my code - minus the apiid.

Code: Select all-- your offset to UTC
--local UTC_OFFSET = -6
-- Enter your city, check openweathermap.org
local CITY = "5429032"
-- Get an APP ID on openweathermap.org
local APPID = "xxxxxxxxx"
-- Update interval in minutes
--local INTERVAL = 10

function init_i2c_display()
     -- SDA and SCL can be assigned freely to available GPIOs
--     sda = 5 -- GPIO14
--     scl = 6 -- GPIO12
--     sla = 0x3c
     i2c.setup(0, 5, 6, i2c.SLOW)
     disp = u8g.ssd1306_128x64_i2c(0x3c)
end

function prepare()
     disp:setFont(u8g.font_6x10)
     disp:setFontRefHeightExtendedText()
     disp:setDefaultForegroundColor()
     disp:setFontPosTop()
end

function updateWeather()
--    print("Updating weather")
    local conn=net.createConnection(net.TCP, 0)
    conn:on("receive", function(conn, payload)
        local lastUpdate = string.sub(payload,string.find(payload,"Date: ")+23,string.find(payload,"Date: ")+31)
        local hour = string.sub(lastUpdate, 0, 2) - 6
        lastUpdate = hour..string.sub(lastUpdate, 3, 9)
        local payload = string.match(payload, "{.*}")
        print(payload)
       
        weather = nil
        weather = cjson.decode(payload)
        local icon = weather.weather[1].icon
        icon = string.gsub(icon, "n", "d")
        file.open(icon..".MONO", "r")
        xbm_data = file.read()
        file.close()
       
        payload = nil
        conn:close()
        conn = nil
        drawWeather(xbm_data, weather, lastUpdate)
    end )
   
   conn:connect(80,ip)
--   conn:send("GET /data/2.5/weather?q=5429032&units=metric&APPID=xxxxxxx"
   conn:send("GET /data/2.5/weather?q="..CITY.."&units=metric&APPID="..APPID
       .." 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")
    conn = nil

end
init_i2c_display()
prepare()
sk=net.createConnection(net.TCP, 0)
--sk:dns("www.google.com",dnsIsWorking)
sk:dns("api.openweathermap.org",function(conn,ip) print("OW = "..ip) end)
  sk = nil
 
updateWeather()

function drawWeather(xbmData, weather, lastUpdate)
        disp:firstPage()
        repeat
                if weather ~= nil then
                    disp:drawXBM( 0, 0, 60, 60, xbm_data )
                    disp:setScale2x2()
                    disp:drawStr(35,5, math.floor((weather.main.temp * 2)+32).." f")
                    disp:drawStr(35,15, weather.main.humidity.."%")
                    disp:undoScale()
                    disp:drawStr(70,52, lastUpdate)
                end
        until disp:nextPage() == false
end


tmr.alarm(1, 10 * 60000, 1, function()
   ip = wifi.sta.getip()
   if ip=="0.0.0.0" or ip==nil then
--      print("connecting to AP...")
   else
--      print("Loading weather...")
      updateWeather()
   end
end )



the IP of api.openweathermap.org prints out as this
OW = 162.243.58.21
so I guess that's working.
Maybe I messed something else up stripping things out to get the compile to work?
Is there anyway to see if the connection to openweather is OK, and see if any data is coming back?
I'm totally ignorant(so far) on this net.tcp stuff