Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Retrieving Weather Info at any Longitude or Latitude

edited September 2014 in General Posts: 145

I've been using a weather API lately to get the weather. It gives a summary, humidity, temperature, precipitation probability and much more! I've already parsed few things, you can continue to parse more if you choose. If anyone was interested they can check out my code:
EDIT: Many new things added and if you get an error then no weather information is available at that location

-- Weather

-- Use this function to perform your initial setup
function setup()

    parameter.text("APIKey","")
    parameter.integer("Longitude",-180,180)
    parameter.integer("Latitude",-90,90)

    --I registered an API Key that is free to use
    APIKey = "7687f8ec79fcbe92cdd91a9589262a12"


    parameter.action("Request Weather",request)


end

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

-- This function gets called once every frame
function success(Data)

    --Parse the Weather Info

    --Find Summary
    a=string.find(Data,"summary")
    b=string.find(Data,",",a)

    Summary = string.sub(Data,a+10,b-2)
    Summary = "Summary: "..Summary

    --Find Precipitation Intensity
    a=string.find(Data,"precipIntensity")
    b=string.find(Data,",",a)

    PrecipitationIntensity = string.sub(Data,a+17,b-1)
    PrecipitationIntensity = "Precipitation Intensity: "..PrecipitationIntensity

    --Find Precipitation Probability
    a=string.find(Data,"precipProbability")
    b=string.find(Data,",",a)

    PrecipitationProbability = string.sub(Data,a+19,b-1)
    PrecipitationProbability = "Precipitation Probability: "..PrecipitationProbability


    --Temperature
    a=string.find(Data,"temperature")
    b=string.find(Data,",",a)

    Temperature = string.sub(Data,a+13,b-1)
    Temperature = "Temperature: "..Temperature


        --Dew Point
    a=string.find(Data,"dewPoint")
    b=string.find(Data,",",a)

    Dew = string.sub(Data,a+10,b-1)
    Dew = "Dew Point: "..Dew


        --Humidity
    a=string.find(Data,"humidity")
    b=string.find(Data,",",a)

    Hum = string.sub(Data,a+10,b-1)
    Hum = "Humidity: "..Hum

          --Wind Speed
    a=string.find(Data,"windSpeed")
    b=string.find(Data,",",a)

    wispeed = string.sub(Data,a+11,b-1)
    wispeed = "Wind Speed: "..wispeed


          --Apparent Temp
    a=string.find(Data,"apparentTemperature")
    b=string.find(Data,",",a)

    apt = string.sub(Data,a+21,b-1)
    apt = "Feels Like: "..apt

        --Cloud Cover
    a=string.find(Data,"cloudCover")
    b=string.find(Data,",",a)

    cc = string.sub(Data,a+12,b-1)
    cc = "Cloud Cover: "..cc

        --Pressure
    a=string.find(Data,"pressure")
    b=string.find(Data,",",a)

    ap = string.sub(Data,a+10,b-1)
    ap = "Air Pressure: "..ap


        --Wind Bearing
    a=string.find(Data,"windBearing")
    b=string.find(Data,",",a)

    wb = string.sub(Data,a+13,b-1)
    wb = "Wind Bearing: "..wb

        -- Visibility
    a=string.find(Data,"visibility")
    b=string.find(Data,",",a)

    v = string.sub(Data,a+12,b-1)
    v = "Visibility: "..v

        --Ozone
    a=string.find(Data,"ozone")
    b=string.find(Data,",",a)

    o = string.sub(Data,a+11,b-2)
    o = "Ozone: "..o


    print(Summary)
    print(PrecipitationIntensity)
    print(PrecipitationProbability)
    print(Temperature)
    print(Hum)
    print(Dew)
    print(wispeed)
    print(apt)
    print(cc)
    print(ap)
    print(v)
    print(o)
    print(wb)
    ----------
    ---------------You can Continue to Find Variables In XML using this tempelate



    --Print Data for Searching for Variables
   -- print(Data)
end
function draw()
    -- This sets a dark background color 
    background(40, 40, 50)

    -- This sets the line thickness
    strokeWidth(5)

    -- Do your drawing here

end




Comments

  • dave1707dave1707 Mod
    Posts: 9,233

    @austinmccoy Nice program. Lot of room for adding more display information.

  • Thanks!

  • dave1707dave1707 Mod
    Posts: 9,233

    @austinmccoy I changed your program a little to make it easy to read all the data that's returned from the request. You can easily see what needs to be searched for to display other information.


    -- Weather function setup() parameter.text("APIKey","") parameter.integer("Longitude",-180,180,-82) parameter.integer("Latitude",-90,90,41) APIKey = "7687f8ec79fcbe92cdd91a9589262a12" parameter.action("Request Weather",request) end function request() coord = string.format("%f,%f",Latitude,Longitude) http.request("https://api.forecast.io/forecast/"..APIKey.."/"..coord,success) end function success(Data) Data=string.gsub(Data,",","\n") Data=string.gsub(Data,"{","\n{\n") Data=string.gsub(Data,"}","\n}\n") print(Data) end
  • Posts: 2,047

    Here is a modified version of Dave's version which uses your current location by default:

    -- Weather
    
    function setup()
        location.enable()
        parameter.text("APIKey","")
        parameter.integer("Longitude",-180,180,location.longitude)
        parameter.integer("Latitude",-90,90,location.longitude)
        APIKey = "7687f8ec79fcbe92cdd91a9589262a12"
        parameter.action("Request Weather",request)
    end
    
    function request()
        coord = string.format("%f,%f",Latitude,Longitude)
        http.request("https://api.forecast.io/forecast/"..APIKey.."/"..coord,success)
    end
    
    function success(Data)
        Data=string.gsub(Data,",","\n")
        Data=string.gsub(Data,"{","\n{\n")
        Data=string.gsub(Data,"}","\n}\n")
        print(Data)
    end
    
  • dave1707dave1707 Mod
    Posts: 9,233

    @JakAttak Nice going. I totally forgot about the location functions.

  • Nice improvements! It looks a lot better now.

  • Is there any way to request automatic?

    I'm doing a game that changes the background using the weather

  • Jmv38Jmv38 Mod
    Posts: 3,297

    tween.delay could be doing it.

  • dave1707dave1707 Mod
    Posts: 9,233

    @erickyamato You can set up a loop that checks the time of day and every 15, 30, or 60 minutes it checks the weather.

  • dave1707dave1707 Mod
    edited September 2014 Posts: 9,233

    @erickyamato Here's an example that checks the weather every 5 minutes. Just change the 5 in td.min%5 to how often you want to check. For instance, 15 for every 15 minutes, 30 for every 30 minutes, 60 for every hour.


    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,",","\n") Data=string.gsub(Data,"{","\n{\n") Data=string.gsub(Data,"}","\n}\n") print(string.sub(Data,1,400)) -- print the first 400 characters end
  • @dave1707 is there any way to start requesting the weather wihout pressing the button?

  • dave1707dave1707 Mod
    Posts: 9,233

    @erickyamato Pressing what button. The above code will check the weather every 5 minutes as long as the code is running. There's nothing to press.

  • @dave1707 "Request Weather"

    Can I use anything like parameter.boolean?

  • dave1707dave1707 Mod
    Posts: 9,233

    @erickyamato I'm sorry, I forgot to remove parameter.action. It's not needed, so I removed it from the above code. The way the code above is set up, it will check the weather when the minutes is at 5, 10, 15, 20, etc. So you might have to wait for the first check.

  • Ok!

    Thank you @dave1707 !

  • dave1707dave1707 Mod
    Posts: 9,233

    @erickyamato I added request() in setup() above, so it will check the weather as soon as you start the program, then it will check at the 5 minute marks.

  • Thank you @dave1707 ! I just need to request the "summary", to change the background...

    ^:)^

  • dave1707dave1707 Mod
    Posts: 9,233

    @erickyamato I wasn't sure what you wanted from the request. That's why I was only printing the first 400 characters.

  • @dave1707 , just "summary" (fog, partily cloudy)... This stuffs

Sign In or Register to comment.