Weather API

This question is mostly directed at @austinmccoy .

Back in September of 2014 you posted a program under the title “Retrieving Weather Info at any Longitude or Latitude”.
Do you recall where that API is drawing the data from? More of a curiosity than anything else.

I’ve gained a greater understanding of Lua and Codea just from this post.
Thank you in advance.

@Scotty Heres some code I modified in the original post. You can take this code and modify it any way you want. This displays info when the program starts and every 5 minutes on the 5,10,15,20 minute intervals.

function setup()
    checkWeather=true
    location.enable()
    APIKey = "7687f8ec79fcbe92cdd91a9589262a12"
    request()  -- request weather at startup
end

function draw()
    checkTime()
end

function checkTime()
    td=os.date("*t")
    if td.min%5 == 0 then  -- check every 5 minutes
        if checkWeather then
            checkWeather=false
            request()
        end
    else
        checkWeather=true
    end
end

function request()
    coord = string.format("%f,%f",location.latitude,location.longitude)
    http.request("https://api.forecast.io/forecast/"..APIKey.."/"..coord,success)
end

function success(Data)
    Data=string.gsub(Data,",","\
")
    Data=string.gsub(Data,"{","\
{\
")
    Data=string.gsub(Data,"}","\
}\
")
    print(string.sub(Data,1,600))   -- print the first 600 characters
    print("Data size  "..#Data)
end

Here’s an updated version. Select a city, or key the latitude, longitude. When the program starts, it uses your latitude, longitude.

function setup()
    location.enable()
    parameter.text("latitude")
    parameter.text("longitude")
    parameter.action("Use Keyed Data",req)
    parameter.action("New York",newyork)
    parameter.action("London",london)
    parameter.action("Sydney",sydney)
    parameter.action("",sydney)
    font("Courier")
    textMode(CORNER)
    fill(255)
    APIKey = "7687f8ec79fcbe92cdd91a9589262a12"
    req()
end

function req()
    if latitude=="" then
        latitude=location.latitude
        longitude=location.longitude
    end
    request()
    showKeyboard()
    hideKeyboard()
end

function newyork()
    latitude=41
    longitude=-74
    request()
end

function london()
    latitude=51
    longitude=0
    request()
end

function sydney()
    latitude=-33
    longitude=150
    request()
end

function draw()
    background(0)
    if lat~=nil then
        text("Latitude:     "..lat,WIDTH/2,HEIGHT-50)
        text("Longitude:    "..lon,WIDTH/2,HEIGHT-75)
        text("Time Zone:    "..tz,WIDTH/2,HEIGHT-100)
        text("Time:         "..tm,WIDTH/2,HEIGHT-125)
        text("Summary:      "..sm,WIDTH/2,HEIGHT-150)
        text("Temperature:  "..tp,WIDTH/2,HEIGHT-175)
        text("Dew Point:    "..dp,WIDTH/2,HEIGHT-200)
        text("Humidity:     "..hm,WIDTH/2,HEIGHT-225)
        text("Pressure:     "..pr,WIDTH/2,HEIGHT-250)
        text("Wind Speed:   "..ws,WIDTH/2,HEIGHT-275)
        text("Wind Gust:    "..wg,WIDTH/2,HEIGHT-300)
        text("Wind Bearing: "..wb,WIDTH/2,HEIGHT-325)
        text("UV Index:     "..uv,WIDTH/2,HEIGHT-350)
        text("Visibility:   "..vs,WIDTH/2,HEIGHT-375)
    end
end

function request()
    if latitude~="" then
        coord = string.format("%f,%f",latitude,longitude)
        http.request("https://api.forecast.io/forecast/"..APIKey.."/"..coord,success)
    end
end

function success(Data)
    output.clear()
    Data=string.gsub(Data,",","\
")
    Data=string.gsub(Data,"{","\
{\
")
    Data=string.gsub(Data,"}","\
}\
")
    print(Data)
    lat=string.match(Data,'latitude":(%g+)')
    lon=string.match(Data,'longitude":(%g+)')
    tz=string.match(Data,'timezone":(%g+)')
    tm=string.match(Data,'time":(%g+)')
    sm=string.match(Data,'summary":(%g+)')
    tp=string.match(Data,'temperature":(%g+)')
    dp=string.match(Data,'dewPoint":(%g+)')
    hm=(string.match(Data,'humidity":(%g+)')*100)
    pr=string.match(Data,'pressure":(%g+)')
    ws=string.match(Data,'windSpeed":(%g+)')
    wg=string.match(Data,'windGust":(%g+)')
    wb=string.match(Data,'windBearing":(%g+)')
    uv=string.match(Data,'uvIndex":(%g+)')
    vs=string.match(Data,'visibility":(%g+)')
end

Thanks @dave1707
I spent a great deal of time messing around with your code modification as well. I always found string.find and string.gsub to be somewhat mysterious. Now have a better understanding of those functions in addition to working with tables and classes.

I learn something new everyday. Life is good.

@Scotty Did you see the latest code. I think I posted that after you looked at the first one.

Nice, Dave, thanks!

Here’s another update. Change the city variable (city=my location) to the name of your city. Add whatever other cities you want to the table (name,latitude,longitude). Don’t forget to add the - sign where needed.

viewer.mode=STANDARD

function setup()
    location.enable()
    city="my location"
    tab={
    {city,location.latitude,location.longitude},    -- my location
    {"London",51,0},
    {"Sydney",-33,150},
    {"New York",41,-74},
    {"Seattle",47,-122},
    {"Moscow",56,30},
    {"Beijing",40,116},
    {"North Pole",90,0},
    {"South Pole",-90,0},
        }

    show=false
    fOffset=1
    if WIDTH>HEIGHT then
        shx,shy=WIDTH-200,HEIGHT-75
    else
        shx,shy=WIDTH/4,HEIGHT/2+50
    end        

    parameter.text("latitude")
    parameter.text("longitude")
    parameter.action("Use Keyed Data",req)
    font("Courier")

    fill(255)
    APIKey = "7687f8ec79fcbe92cdd91a9589262a12"
    req()
end

function req()
    if latitude=="" then
        latitude=location.latitude
        longitude=location.longitude
    else
        city="Unknown name"
    end
    request()
    showKeyboard()
    hideKeyboard()
end

function draw()
    background(0)
    textMode(CORNER)
    if lat~=nil then
        text(city,100,HEIGHT-50)
        text("Latitude:     "..lat,10,HEIGHT-100)
        text("Longitude:    "..lon,10,HEIGHT-125)
        text("Time Zone:    "..tz,10,HEIGHT-150)
        text("Time:         "..tm,10,HEIGHT-175)
        text("Summary:      "..sm,10,HEIGHT-200)
        text("Temperature:  "..tp,10,HEIGHT-225)
        text("Humidity:     "..hm,10,HEIGHT-250)
        text("Dew Point:    "..dp,10,HEIGHT-275)
        text("Pressure:     "..pr,10,HEIGHT-300)
        text("Wind Speed:   "..ws,10,HEIGHT-325)
        text("Wind Gust:    "..wg,10,HEIGHT-350)
        text("Wind Bearing: "..wb,10,HEIGHT-375)
        text("UV Index:     "..uv,10,HEIGHT-400)
        text("Visibility:   "..vs,10,HEIGHT-425)
    end
    showSelection()
end

function touched(t)
    if t.state==BEGAN then
        if t.x>shx-75 and t.x<shx+75 then
            for z=1,#tab do
                if t.y>shy-z*40-15 and t.y<shy-z*40+15 then
                    fOffset=z
                    latitude=tab[fOffset][2]
                    longitude=tab[fOffset][3]
                    city=tab[fOffset][1]
                    request()
                end
            end
        end
    end
end

function request()
    if latitude~="" then
        coord = string.format("%f,%f",latitude,longitude)
        http.request("https://api.forecast.io/forecast/"..APIKey.."/"..coord,success)
    end
end

function success(Data)
    output.clear()
    Data=string.gsub(Data,",","\
")
    Data=string.gsub(Data,"{","\
{\
")
    Data=string.gsub(Data,"}","\
}\
")
    print(Data)
    lat=string.match(Data,'latitude":(%g+)')
    lon=string.match(Data,'longitude":(%g+)')
    tz=string.match(Data,'timezone":(%g+)')
    tm=string.match(Data,'time":(%g+)')
    temp = os.date("*t", tm)
    tm=string.format("%02d:%02d",temp.hour,temp.min).." last update"
    sm=string.match(Data,'summary":(%g+)')
    tp=string.match(Data,'temperature":(%g+)').." deg F"
    dp=string.match(Data,'dewPoint":(%g+)').." deg F"
    hm=(string.match(Data,'humidity":(%g+)')*100).." %"
    pr=string.match(Data,'pressure":(%g+)')
    pr=string.format("%4d mb  %.2f in",pr//1,pr/33.86)
    ws=string.match(Data,'windSpeed":(%g+)').." mph"
    wg=string.match(Data,'windGust":(%g+)').." mph"
    wb=string.match(Data,'windBearing":(%g+)').." deg"
    uv=string.match(Data,'uvIndex":(%g+)')
    vs=string.match(Data,'visibility":(%g+)').." miles"
end

function showSelection()
    pushStyle()
    rectMode(CENTER)
    textMode(CENTER)
    stroke(0, 237, 255, 255)
    strokeWidth(3)
    fill(255, 254, 0, 200)
    rect(shx,shy,150,40)
    fill(255, 0, 0, 255)
    text("Location",shx,shy)
    for z=1,#tab do
        fill(47, 207, 122, 109)
        rect(shx,shy-z*40,150,40)
        fill(255)
        text(tab[z][1],shx,shy-z*40)
    end
    popStyle()
end

@dave1707 I just ran your latest version. I like the way you’re able to select from a list of locations.
Thanks for sharing.