Weather applet. Help required

2 replies [Last post]
garrydwilms
Offline
United Kingdom
Joined: 31 Mar 2011

Brett,

i have successfully updated to 307.1 thanks.

i want to learn to parse an xml file and so was trying your weather applet out but keep getting the following error.

 

weatherForecastApplet.lua:57: bad argument #3 to 'format' (string expected, got nil)
stack traceback:
[C]: in function 'format'
/etc/plugboard/weatherForecastApplet.lua:57: in function 'getWeatherBody'

After diggin around a bit google weather api appears to be deprecated so I thought this may be the issue. I therefore rewrote the app to get data from my weather2.com

Script attached below for reference, however this gives the same error.

I just cannot fathom the fault, does anything stand out to you?

 

thanks 

garry

 

ps script inserted in this post as I cant attach via my ipad. Sorry!

 

 

--[[
        Fetch weather from Weather2.com and provide a forecast message
--]]
require("xap")
require("pl.xml")
local http = require("socket.http")
info={
   version="1.0", description="Weather forecast"
}
local location="wa5"
local apikey="my api key"
--function math.round(number, decimals)
--    decimals = decimals or 0
--    return tonumber(("%."..decimals.."f"):format(number))
--end
--local tocelcius = function(f) return math.round((f-32)*5/9,1) end
local current={}
local forecast={}
function lookupWeather()
    local xmlstring = http.request("http://www.myweather2.com/developer/forecast.ashx?uac="..apikey.."&output=xml&query="..location)
 
print(xmlstring)
   if(xmlstring == nil) then return end
    local d = xml.parse(xmlstring)
    current = d:match [[
<weather>
  <curren_weather>
<temp>
'$temp'
</temp>
<wind>
<speed>
'$speed'
</speed>
</wind>
  </curren_weather>
</weather>
]]
    forecast = d:match [[
<weather>
   {{<forecast>
<date>
'$day'
</date>
<day_max_temp>
'$high'
</day_max_temp>
<night_min_temp>
'$low'
</night_min_temp>
   </forecast>}}
</weather>
]]
    -- Damn google returns units in degrees F
    -- Other countries like Spain, Germany etc are in C?!
--   for _,v in ipairs(forecast) do
--         v.high = tocelcius(v.high)
--         v.low = tocelcius(v.low)
--    end
end
function getWeatherBody()
    local msg = string.format([[
current
{
location=%s
temp=%s
wind=%s
}
]], location, current.temp, current.speed)
    for i,v in ipairs(forecast) do
        msg = msg ..string.format([[
forecast.%d
{
low=%d
high=%d
day=%s
}
]], i, v.low, v.high, v.day)
    end
    return msg
end
function sendWeather()
        xap.sendShort([[
xap-header
{
class=weather.forecast
source=dbzoo.livebox.Plugboard:Weather
}
]] .. getWeatherBody())
end
function init()
   local expireImmediately = true
   -- lookup new weather forecast every hour.
   xap.Timer(lookupWeather, 60*60):start(expireImmediately)
   -- report every 2 minutes.
   xap.Timer(sendWeather, 2*60):start()
end
xap.init("dbzoo.lua.example","FF00DD00")
init()
xap.process()

brett
Offline
Providence, United States
Joined: 9 Jan 2010
Gary,You are right. Between

Gary,

You are right. Between me writing and testing the weather Applet and getting it published Google in their wisdom have put in checks to prevent people from using it !   *sigh*

I'll look into this alternate source you have in your code and get another sample working based on what you've started.  Sorry about this we aren't winning are we?

Brett

AttachmentSize
weather2com.lua 1.7 KB
garrydwilms
Offline
United Kingdom
Joined: 31 Mar 2011
Thanks!

Thanks Brett, works a treat. Obvious now it's in front of me!

 

Garry. 

Hardware Info