Getting started on the Plugboard

10 replies [Last post]
BodgeIT
Offline
London, United Kingdom
Joined: 10 Jun 2010

I've been frying my brains trying to get started on the Lua scripts to bring some intelligence to my HAH.

I just don't seem to be able to get anything going.

  • I have enabled the plugboard in the xap-livebox.ini.
  • I have added scipt(s) to the /etc/plugboard folder.
  • I am assuming that if I run the script it should have some effect.
  • I have tested the RF socket from the WebGui.

My first script is a somewhat confused attempt to turn on an RF socket if the temp get's above 33c.

-- Script to turn fan 1 on via RF when temp hits 33
-- Rev 0.1
--
function init()
  register_source('dbzoo.livebox.Controller:1wire.1', "gettemp")
--  register_source('dbzoo.livebox.Controller:rf.2', "getstate")
end

-- This is the xap header for turning RF on function cmd(action)
  xap_send(string.format("xap-header\
{\
target=dbzoo.livebox.Controller:rf.2\
class=xAPBSC.cmd\
}\
output.state.1\
{\
id=*\
state=%s\
}", action))
end

-- Get state of RF.2
-- function getstate()
--    state = xapmsg_getvalue("output.state", "state")
-- end

-- Get valid info from sensor
function gettemp()
  if xAPMsg.class == "xAPBSC.event" then
    if xapmsg_getvalue("input.state", "text") > 33 then
--       state == "off" then
         cmd("on")
    end
  end
end

I know there maybe be a couple of fumbles in this script but at the moment I just want to see the RF switch activate.

I run the script by typing 'lua temp_high.lua' from the console.  I'm expecting the RF switch to activate when the temp is above 33(which it is)? Nothing happens!  Grr!

Please provide clip round ear, condescending tut and a handy answer to put me out of my misery!

Thanks

Gary

derek
Offline
Glasgow, United Kingdom
Joined: 26 Oct 2009
Learning the Plugboard

Hi Gary,

Well it's great to see somebody giving this a try. The best way to get up to speed with this stuff is to give it a try, get it wrong and then learn by fixing it. Brett is the scripting guru (I'm still learning), he will correct anything that I've got wrong here. Good that you have the Plugboard enabled in the .ini file.

I've never invoked a script directly from the command line. The whole idea of the Plugboard is that it watches for xAP messages on the bus and automatically calls a script (or scripts) when a message of interest is found. The 'watching for messages' bit is the complex part, but Plugboard (written in C) does all this for you - your script is like the 'event handler'. Plugboard also sets up some important variables that your script can access e.g. xAPMsg.class - if you invoke the script manually, these won't be set properly.

In the init() function you bind the Plugboard 'watcher' to a function inside your script. Note that you can 'register' an interest in as many messages as you like with multiple 'register_source/_target/_class' calls. In fact you could put all your logic into a single, large, script. However, in practice, it's better organised to have lots of small scripts.

So, rather then invoking your script by running this yourself from the command line, start by writing a script that will be called when an xAP message, that you can easily generate, is found. e.g. you could use the UI to send 'RF 1 on' xAP messages. Then in your handler code, put in the code to turn on another relay. This will get you going. Then you can tweak the code to watch instead for the 1-Wire temperature event.

As for your actual code...

The main thing that I don't understand in your script is your declaration of the 'cmd' function. The first line of this seems to be commented out? 

Hope this helps. Do let me know how you get on.

BodgeIT
Offline
London, United Kingdom
Joined: 10 Jun 2010
Thanks Derek, that first line

Thanks Derek, it's this bit that is the most exciting at this point.  The Arduino/ZigBee stuff will come later.  It provides the opportunity to build in some real intelligence(limited by my own I guess, so might not be creating HAL just yet).

The commented first line is just a typo in the post, it actually looks like this:

--  Comments...blah

function cmd(action)

 

I notice in the examples and the wiki notes, the "s and 's seem to be interspersed willy nilly, which of these should be used or doesn't it matter?  Would it be worth going through them all and standardising them?

I'm going to start creating an idiots guide as I fumble my way through, save you guys some time...

 

Is there a way to "view" the plugboard?

brett
Offline
Providence, United States
Joined: 9 Jan 2010
What do you mean view the

What do you mean view the plugboard?  The C source code is available on google projects.

If you run the xap-plugboard from the command line, you can put in "print" statement in your functions to help you debug what is going on.  Of course once you get it sorted its best to removed these as when plugboard is started by the init scripts all output is sent to /dev/null.

If running from the command line you can start it up in debug mode.  This will tell you from a C point of view then various things are being executed, it may or may not help.

# xap-plugboard -d 9

Also from the command line if any scripts are crashing you will get STACK TRACE output, even without supplying any debug (-d) option.

Don't forget you should only have 1 xap-plugboard running on the system at a time.

I'm not sure what build you are on - but build 250 made some BSC compliance changes to the 1wire events.  Otherwise the key will be "level" not "text".  Do check with xFx viewer what you are seeing on the xap bus.

function gettemp()
  if xAPMsg.class == "xAPBSC.event" then
    if xapmsg_getvalue("input.state", "text") > 33 then
--       state == "off" then
         cmd("on")
    end
  end
end

Quote types in LUA are interchangable, I should santized the wiki docs.

There is also an LUA command line interpretter on the HAH too, this is very useful for testing out bits of code.  For example:  Let say you aren't sure if the STRING comparison with a NUMBER is automatically performing a cast.  You can check this out like this:

# lua
Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> value="30"
> print(value>33)
stdin:1: attempt to compare number with string
stack traceback:
        stdin:1: in main chunk
        [C]: ?
> print(value>"33")
false
>

So now do you see why your code is breaking?

As this is a standalone interpretter the XAP functions are not available to you however all other LUA commands are.

BodgeIT
Offline
London, United Kingdom
Joined: 10 Jun 2010
I had tried the comparison

I had tried the comparison with quotes too, that didn't work either but maybe I had something else wrong at that point.

The problem I'm having now is with vi.  It keeps adding in spurious characters and messing up the contents of the file?

I imagine it's something to do with my settings in putty?  Any suggestions?

In the end I deleted the line in question and retyped it, that seemed to clear it up.

 

I think one of the other problems I had been having was that the plugboard had not started!

I hadn't noticed it in xfx until I started it manually, now after reboot it starts no problem?  Just a glitch in the matrix I imagine...

** Edit **

Not just a glitch I fear.  My plugboard is definitely not starting every time.  I can't see what is the difference between reboots but sometimes it starts(mostly when it was running when it was rebooted), other times it doesn't.

BodgeIT
Offline
London, United Kingdom
Joined: 10 Jun 2010
Think something a miss...

Basically my plugboard had not started.  It was not visible in xfx.

I typed "xap-plugboard stop" (actually thought it was running).

I got this:

# xap-plugboard

xAP Plugboard - for xAP v1.2
Copyright (C) DBzoo 2009

br0: address 172.16.10.100
br0: netmask 255.255.255.0
Autoconfig: xAP broadcasts on 172.16.10.255:3639
xAP uid=FF00D800, source=dbzoo.livebox.Plugboard
Broadcast socket port 3639 in use
Assuming a hub is active
Socket port 3639 in use
Socket port 3640 in use
Discovered port 3641
Ready

Suddenly it appeared in xfx and my scripts started to run.

Then it went mental...I could see it "turning on" my rf 2 hundreds of times a second, even though I wasn't getting that many events on 1wire.1.

I then control c out of it and it seemed to settle down.  This was already after 2 reboots, so it should have started from the xap-livebox.ini entry. 

On second check, it's clear now that the plugboard isn't running. After control c ing out and leaving it for a bit, it's now grey'd out in xfx. And my scripts are no longer running.

 

Any suggestions?

derek
Offline
Glasgow, United Kingdom
Joined: 26 Oct 2009
Back to basics

Hi Gary,

I've added a 'getting started' section to the plugboard wiki page (at the end of the page). Do try disabling your script for now & try the step by step approach to see if you get the same results as I do. Then gradually build up your script.

Also, email your script to Brett & me and we can debug it locally. If you don't specify filters carefully, you can write a script that becomes recursive - this might explain what you are seeing ... 

Cheers,
Derek

 

BodgeIT
Offline
London, United Kingdom
Joined: 10 Jun 2010
Hi Derek,Thanks, I think I

Hi Derek,

Thanks, I think I have it now.  I found a couple of things...firstly I had a script which just started the fan!

I know, that was a bit daft, but I thought about creating lots of different functions etc and then calling them from other files...lesson learned.

Also in my temp_high script, I was passing the 'on' value to the cmd function without quotes.  This seemed to create a panic in the plugboard.  It said something about "was expecting a string and got 'nil'".   It wasn't until I started the plugboard manually with debug set at 5, that I spotted it.

I think what I was asking for earlier..."can you view the plugboard?" the debug mode kind of provides that.  Would be nice for the laymen amongst us to have that feature in the GUI.  Maybe in futer you could have an 'Advanced' tab which might provide extra info like logs etc.

For now.. things work.  When I turn off the RF in Webgui, as soon as the 1wire registers an event msg, and it's value is above 33, it turns the rf back on.  The other script I'm running(a mod of your bind_relay script for rf) turns on a second rf in my loft.  Perfect!

I'll keep my eye on the plugboard starting thingy.  Might be nothing( a bit suspicious because the plugboard had never shown up in top or xfx even though I hadn't written any scripts when I had set it to start and rebooted the system.  At this point I'm just happy things are happening as they should...ish.

Next stop SMS.

Also, any plans to implement an email interpretter/handler/sender?

p.s.  Just read the getting started section...excellent.  Would have helped enourmously...I now realise the big thing that was missing was starting the plugboard.  Also xfx is godsend!

derek
Offline
Glasgow, United Kingdom
Joined: 26 Oct 2009
Stability rules

Good. Sounds like things are a bit more sorted.

Yes, the plugboard does need lots more support by way of tutorials and tools. Early days for the 'getting started' wiki page. If there are any things that you would want added to this, do let me know. Your perseverance has paid off & others might benefit from knowing what you found to be 'difficult' along the way.

The reason the Lua reported "was expecting a string and got 'nil'" is because in Lua, you create a new variable just by using it. Lua initialises all variables to a special value called 'nil'. So when you passed the parameter 'on' (without the quotes), Lua created a variable called 'on', assigned it the value 'nil' and passed it as the parameter to your function.

Email should be possible directly from a Lua script - Brett would know the best way to do this. If we ask nicely, he might knock out an example :-)

BodgeIT
Offline
London, United Kingdom
Joined: 10 Jun 2010
Hi Derek,Should the plugboard

Hi Derek,

Should the plugboard have "paniced"? because of this error?  Does this need a look at?  I suspect this was crashing my plugboard and was the reason it to appear 'not started'.

I'm aware you guys have a lot on already.  Now I have proven cause and effect for myself and know what I should be looking for, I'll go off and funmble around for myself a little bit.

I think a script repostitory would be usefull.  I'm going to make sure I comment all the scripts I create.  Should be handy for others following.  Once I get my phone connected(cable arrives tomorrow), I'll be happy enough for a bit.  I'll be ordering my Arduino soon, but won't be expecting to produce anything with it other than flashy lights etc, for a while anyway...what's really exciting is just how flexible this all is.  Imagination seems to be the only limit!

Thanks

Gary

derek
Offline
Glasgow, United Kingdom
Joined: 26 Oct 2009
Don't panic, don't panic!

Well, the difficulty with the embedded Lua is that it's relatively easy to mess up. We must always remember that we don't have much RAM or CPU grunt on the HAH platform. Everything must be "squeezed in". The rewards are that we don't suffer the Windoze tendancy for hangups and random blue screens (and we only burn 6 Watts of power).

This is fine for the tightly controlled C code that Brett engineers so nicely. When we 'open up the door' via Lua, it's a double edged sword. With great power comes great responsibility.

I don't think that there is much that we can really do to sandbox the Lua interpreter and prevent it from causing a panic if a script is just plain wrong. At the end of the day, if somebody can't hack the Lua, they might just want to stick to what the HAH does 'out of the box'. You've bitten this bullet & are now 'Lua aware'.

Script repository, a good "howto get started" and timely online support is IMHO the best way to go. I've had plugboard scripts running smoothly for extended periods (weeks). Even then, I schedule a periodic automatic reboot of the HAH - just to keep things 'clean'.

Thanks for all the observations. Do send any content (scripts, writeup, whatever) that you would be happy to have published.

Cheers,
Derek.

Hardware Info