Scripting

Description

Queries re using the xap-plugboard component to script xAP action events.

Forums

I'm looking for some help with Lua , I'm using SciTE for windows to debug my code....

I'm running the following string.match on the HAH to parse some serial data, the code section is a follows

local stnid, outtmp, hum, wndavg, wndgst, wnddir, rain, intmp, baro = data:match("(%d+) (%d+%.%d) (%d+) (%d+%.%d+) (%d+%.%d+) (%w+) (%d+%.%d) (%d+%.%d) (%d+%.%d)")

print (data)

print (stnid, outtmp, hum, wndavg, wndgst, wnddir, rain, intmp, baro )

running the code with SciTE I get:

2686 8.8 63 3.06 6.12 SW 4793.3 25.9 984.1
2686    8.8    63    3.06    6.12    SW    4793.3    25.9    984.1

running xap-plugboard in debug on the HAH I get:

2686 8.9 61 3.40 6.12 NE 4793.3 25.9 984.1
nil     nil     nil     nil     nil     nil     nil     nil     nil

Just don't understand what I'm missing, any help would be appreciated.

Forums

Hi All,

Im scripting an Applet to use with my 20x4 LCD, all is working well so far except i appear to have a timer issue

the script will happily run for 5 - 10 minutes or so and then appears to just stop - occasionally the HAH becomes unresponsive - memory leak??

im not stopping or the deleting the timer at anypoint as yet, its scripted in a way that i want the timer to continuly run... perhaps this is the issue

module(...,package.seeall)
require("xap")

info = {version="1.0", description="20x4LCDInfo"}

lcdcols = 20 -- Specify for LCD Row length
fill = " " -- Char used to fill blank LCD colums
Line1 = "Waiting for Data - 1"..string.rep(fill, lcdcols - string.len("Waiting for Data - 1")) -- Sets Line Var to contain basic text until overwritten with Catch routines
Line2 = "Waiting for Data - 2"..string.rep(fill, lcdcols - string.len("Waiting for Data - 2"))
Line3 = "Waiting for Data - 3"..string.rep(fill, lcdcols - string.len("Waiting for Data - 3"))
Line4 = "Waiting for Data - 4"..string.rep(fill, lcdcols - string.len("Waiting for Data - 4"))

Unit1 = " oC" -- Customisable Units (note the space prefix)
Unit2 = " oC"
Unit3 = " Watts"
 
function init() -- Sets up 3 filters
 Filter1 = xap.Filter()
 Filter1:add("xap-header", "source", "dbzoo.livebox.Controller:1wire.1")
 Filter1:callback(catch1wire1) -- A temperature change in the filter will trigger the Catch1wire1 routine
 Filter2 = xap.Filter()
 Filter2:add("xap-header", "source", "dbzoo.livebox.Controller:1wire.2")
 Filter2:callback(catch1wire2)
 Filter3 = xap.Filter()
 Filter3:add("xap-header", "source", "dbzoo.livebox.CurrentCost:ch.*")
 Filter3:callback(catchCurrentCost)
 send2lcd("Waiting for Data") -- Test LCD at applet start
 xap.Timer(update, 10):start() -- Starts update routine timer with a 10 second interval
end

function catch1wire1()
 Value1 = xap.getValue("input.state","displaytext") -- Sets Value1 as 1Wire data
 Line1 = Value1..Unit1..string.rep(fill, (lcdcols - string.len(Value1) - string.len(Unit1))) -- Sets Line1 Var with 1Wire data (Value1) and appends Unit1, fills remaining Lcd colums with spaces
 end

function catch1wire2()
 Value2 = xap.getValue("input.state","displaytext")
 Line2 = Value2..Unit2..string.rep(fill, (lcdcols - string.len(Value2) - string.len(Unit2)))
 end

function catchCurrentCost()
 Value3 = xap.getValue("input.state","text")
 Line3 = Value3..Unit3..string.rep(fill, (lcdcols - string.len(Value3) - string.len(unit3)))
 end

function update(self) -- Timer routine
 Line4 = "        "..os.date("%H:%M") -- Sets Line 4 as basic clock
 lcdstring = Line1..Line2..Line3..Line4 -- Conbines all LCD Lines1-4 into one string ready to send to the LCD every 10 seconds
 send2lcd(lcdstring) -- Sends to LCD
 end

function send2lcd(lcdstr) -- LCD Routine
 xap.sendShort(string.format([[xap-header
{
target=dbzoo.livebox.Controller:lcd
class=xAPBSC.cmd
}
output.state.1
{
id=*
state=on
text=%s
}]], lcdstr))
end

 

Forums

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()
Forums

Hi

Before I try and force a firmware update can anyone point me to the reason for this log when I try to run xap-plugboard?

lua: /usr/share/lua/5.1/pl/config.lua:80: attempt to index local 'key' (a nil value)
stack traceback:
/usr/share/lua/5.1/pl/config.lua:80: in function 'process_name'
/usr/share/lua/5.1/pl/config.lua:110: in function 'read'
/etc_ro_fs/plugboard/plugboard.lua:52: in main chunk
[C]: ?


It was fine until I rebooted :)
Forums

I have an issue with my current cost feed to Cosm, this had been ticking away nicely for some months with glitches associated with my network dropping sorted by a scheduled reboot, however after starting to setup the HAHnode 2 days ago I have somehow corrupted the feed.

 

It works normally most of the time but occationally seems to parse the data from the current cost meter wrong, picking up either part of the 'watt' data or the wrong section alltogether (see example below) which puts the PvOutput data of the scale !

 

Any help grately appreciated.

 

Xfx example;

xap-header

{

v=12

hop=1

uid=FF00DC01

class=xAPBSC.event

source=dbzoo.livebox.CurrentCost:ch.1

}

input.state

{

state=on

text=CC128-v1.29

displaytext=0 Watts

}

or

xap-header

{

v=12

hop=1

uid=FF00DC01

class=xAPBSC.event

source=dbzoo.livebox.CurrentCost:ch.1

}

input.state

{

state=on

text=>00077

displaytext=0 Watts

or it could read single figure wattage (i.e. drops 2 digits as my basline reading is >200 watts)

Forums
Hi I can successfully use Explorer/Firefox to interrogate HAH parameters via the webserver applet ie http://***********:8080/dbzoo.livebox.Controller:1wire.1/input.state/displaytext I'm messing with Java and Android and have issued an httpGET request for same URL and keep getting "client protocol exception" Probably something obvious but I'm out of my depth. Any help? John
Forums

Hi All

Is there a quick/tidy way of storing and recovering a small set of data/variables to/from a file?  I think I've done it 'old school' by creating a string from the variables, saving the string, recovering the string then parseing the string to extract the variables.... but is there a better way...??

Say I have variables x1, x2, x3 and x4 (all numbers)... is there something like..

    local file = io.open("/etc/plugboard/Variables","w+")
    file:write(x1, x2, x3, x4)

    then


    file:read(x1, x2, x3, x4)

Any help greatly appreciated.....ta!

EJ

Forums

Hi,

I'm writing a script to communicate with a serial device.

Using data=somedata in the XAP serial schema works as expected and I see "somedata" received on the serial port but I need to send hex/binary data. 

The XAP protocol suggests that ! in place of = would allow hexadecimal data to be sent so I've tried using data!0AB123F for example but this appears to get removed from the XAP message (or isn't displayed by the XFX message viewer) and no serial data is generated.

Is hex serial data not supported or am I missing something? 

Paul

Forums

I have 2 plugboard scripts that run "sometimes" below is a top of my system so the plugboard call is there,

CPU:   1% usr   1% sys   0% nic  96% idle   0% io   0% irq   0% sirq
Load average: 0.24 0.21 0.09 1/23 168
  PID  PPID USER     STAT   VSZ %MEM %CPU COMMAND
  154     1 root     S     4176  30%   1% lua /etc_ro_fs/plugboard/plugboard.lua
  166   112 root     S     1740  12%   1% dropbear -p 22
  168   167 root     R     1788  13%   1% top
  140     1 root     S     1452  10%   0% /usr/bin/iServer -i br0
  138     1 root     S     1080   8%   0% /usr/bin/xap-livebox -s /dev/ttyS0 -i br0
  134     1 root     S     1028   7%   0% /usr/bin/xap-hub -i br0
  144     1 root     S     3976  28%   0% /usr/bin/kloned
  150     1 root     S     2620  19%   0% /usr/bin/xap-currentcost -s /dev/ttyUSB0 -i br0
  167   166 root     S     1812  13%   0% -ash
    1     0 root     S     1796  13%   0% init
   99     1 root     S     1792  13%   0% udhcpc -T 10 -i br0
  112     1 root     S     1668  12%   0% dropbear -p 22
  164   140 root     S     1452  10%   0% /usr/bin/iServer -i br0
  137     1 root     S     1080   8%   0% /usr/bin/xap-pachube -i br0
  147     1 root     S     1044   7%   0% /usr/bin/xap-serial -i br0
    7     1 root     SW       0   0%   0% [mtdblockd]
    3     0 root     SWN      0   0%   0% [ksoftirqd_CPU0]
    8     1 root     SW       0   0%   0% [khubd]
    5     0 root     SW       0   0%   0% [bdflush]
    2     1 root     SW       0   0%   0% [keventd]
#
so to me looks like they should run?

 

how do i debug this?