Jump to content
  • 0

[TUTORIAL] ZeroBrane Usage (Lua Coding)


Question

Latest version of Lualibs = lualibs v1.0.1.zip

 

Changelog for v1.0.1 (thank to @petergebruers

- fix HC user authentication (was: user:password in URL, is now: basic authentication).
- fix chunked responses (was: use only chunk 1, is now: concatenate chunks). Fixes "getDevicesId".
- add error checking and display in the HTTP part, to get sensible error messages.

 

Most of the information came from the french forum which can be found here all credits go to Steven!

 

ZeroBrane is an IDE for development in Lua code. It offers you auto completion and partial support to use the actual fibaro: option in your code too. To get started grab the latest version from https://studio.zerobrane.com/download (1.5.0 as of writing) grab your favorite flavor. It supports Mac, Windows and Linux.

 

To use the Lua Library you have to install it, grab the lua libs (top of page) for fibaro. You need to install these files in your c:\<yourlocation>\ZeroBraneStudio\lualibs (for Mac this would be /Applications/ZeroBraneStudio.app/Contents/ZeroBraneStudio/lualibs) after that a restart of the app is a good thing to do.

Use this always in your code when you start testing your setup. For example:

require("FibaroSceneAPI")
hc2_user = "youraccount"
hc2_pwd = "yourpassword"
hc2_ip = "192.168.x.x"

local data = fibaro:getGlobal("somevariable")
print(data)

Do keep in mind this is your userid/password on the box itself, do NOT use a fibaro ID since that is only the login to your remote.

 

To change the Colors to be the same as on Fibaro (if you feel really cozy with those colors) edit your user settings at: Edit -> Preferences -> Settings: User

styles = loadfile('cfg/tomorrow.lua')('TomorrowNight')
stylesoutshell = styles -- apply the same scheme to Output/Console windows
styles.auxwindow = styles.text -- apply text colors to auxiliary windows
styles.calltip = styles.text -- apply text colors to tooltips
 
local colors = {
  orange = {255,158,89},
  braun = {217,191,140},
  darkgray = {38,38,38},
  blue = {89,158,255},
  green = {188,210,121},
  red = {179,94,77},
  white = {255,255,255},
  gray = {102,102,102}
}
 
styles.comment = {fg = colors.gray}
styles.operator = {fg = colors.white}
styles.number = {fg = colors.red}
styles.stringtxt  = {fg = colors.green}
styles.keywords0  = {fg = colors.blue}
styles.text = {fg = colors.braun, bg = colors.darkgray}
styles.keywords3 = {fg = colors.orange}
styles.keywords4 = {fg = colors.orange}
styles.keywords5 = {fg = colors.orange}
styles.keywords6 = {fg = colors.orange}
styles.keywords7 = {fg = colors.orange}

And if you want to have auto completion you have to add fibaro.lua to your ZeroBraneStudio/api/lua folder.

Please note that this file is the english file and has some few additions which i didn't see or where incorrect. Looks like:

autocompletion.png

 

To activate it, please make sure you add it. Go to Edit -> Preferences -> Settings: System

Add the following string to it:

api = {luadeb = {'fibaro'}}

The fibaro:calculateDistance doesn't work and the sourcetrigger needs to be set manually. See below snippet for testing:

__fibaroSceneSourceTrigger = {type="global", name="Pushbullet"}
-- __fibaroSceneSourceTrigger = {type="global", name="Pushbullet"}
-- __fibaroSceneSourceTrigger = {type="property", deviceID=6, propertyName="value"}
-- __fibaroSceneSourceTrigger = {type="autostart"}
-- __fibaroSceneSourceTrigger = {type="other"}
 
print(fibaro.getSourceTrigger().type) -- global
print(fibaro.getSourceTrigger().name) -- Pushbullet
 
__fibaroSceneSourceTrigger = {type="autostart"}
print(fibaro.getSourceTrigger().type) -- autostart

FAQ:

Q: I get the following error message: lualibs/json/decode.lua:74: bad argument #1 to 'match' (string expected, got nil)

A: It cannot reach Fibaro system correctly. Please make sure you have the right userid/pwd and correct ip number.

 

Q: I don't see any completion on my fibaro: 

A: Make sure you added the completion Edit -> Preferences -> Settings: System (lualib)

 

Q: Where do i get all my values from?

A: You can make use of the excellent tool from @Krikroff found in this thread to grab all the values.

 

Q: Where is this data coming from?

A: Fibaro has an api interface, you can go there via http://192.16.x.x/docs where you can try it out. 

 

Q: I click on play but it does not keep running?

A: Make sure you click the double green arrow. The other one stops untill you press forward. (F6 = Go, F5 = Waits on next itteration)

 

Q: I want to change the width of the tabs

A: Go to Edit -> Preferences -> Settings: User and add "editor.tabwidth = 3"

 

Q: Disable wordwrap for long lines

A: Go to Edit -> Preferences -> Settings: User and add "editor.usewrap = false"

 

Q: How to i debug line by line?

A: See below (credits go to Fred the Phew for below screenshots & explanation)

 

Open Zerobranstudio and display the following windows if you have not already done so

01.png

 

insert the following code in a new blank window Zerobranstudio (we will revise multiplication ;)

local loop = true local i = 1 local j = 1

while loop == true do
   i = i + 1
   j = j + 2 print ( i .. "x" .. j .. "=" .. i * j ) end

To activate dynamic tag tracking, simply select the name of the variable you want to track, then right click on it to bring up a context menu.

You must then select the option Add watch expression as in the image below. In our case, it must be done for the 2 variables i & j.

 

07.png

 

To switch to debug mode, press the F5 key once, the window should look like this.

The green cursor is positioned on the first instruction, and the Watch window displays the values of i and j (to nil at the moment which is logical)

 

02.png

 

Now press the F10 key to execute the program step by step, until the first result of the multiplication is displayed, as shown below.

The result of the multiplication is consistent with the values of i and j.

 

03.png

 

To test now the modification on the fly of a variable, we will re-run the loop a second time by pressing the F10 key several times but without executing the print instruction, as in the image below. The value of the 2 variables has been modified by programming.

 

04.png

 

To modify dynamically the value of j, click on the Remote console tab, type the line i = 20 then press the Enter key to validate the command, as in the image below. We can then see that the value of ja has been modified in the Watch window.

 

05.png

 

It is now enough to click on the tab Output (suspended), and to press the key F10 to resume the execution of the program.

It is now that the value of j set by hand has been dynamically taken into account by the program.

 

06.png

 

Happy Coding! \o/

(feel free to make remarks so i can make it better)

lualibs.zip

Edited by riemers
Change to lualibs 1.0.1
  • Thanks 2
Link to post
Share on other sites

Recommended Posts

  • 0
On 1/27/2017 at 4:16 PM, riemers said:

Most of the information came from the french forum which can be found here all credits go to Steven!

 

ZeroBrane is an IDE for development in Lua code. It offers you auto completion and partial support to use the actual fibaro: option in your code too. To get started grab the latest version from https://studio.zerobrane.com/download (1.5.0 as of writing) grab your favorite flavor. It supports Mac, Windows and Linux.

 

To use the Lua Library you have to install it, grab the llualibs.zip for fibaro. You need to install these files in your c:\<yourlocation>\ZeroBraneStudio\lualibs (for Mac this would be /Applications/ZeroBraneStudio.app/Contents/ZeroBraneStudio/lualibs) after that a restart of the app is a good thing to do.

Use this always in your code when you start testing your setup. For example:

require("FibaroSceneAPI")
hc2_user = "youraccount"
hc2_pwd = "yourpassword"
hc2_ip = "192.168.x.x"

local data = fibaro:getGlobal("somevariable")
print(data)

Do keep in mind this is your userid/password on the box itself, do NOT use a fibaro ID since that is only the login to your remote.

 

To change the Colors to be the same as on Fibaro (if you feel really cozy with those colors) edit your user settings at: Edit -> Preferences -> Settings: User

styles = loadfile('cfg/tomorrow.lua')('TomorrowNight')
stylesoutshell = styles -- apply the same scheme to Output/Console windows
styles.auxwindow = styles.text -- apply text colors to auxiliary windows
styles.calltip = styles.text -- apply text colors to tooltips
 
local colors = {
  orange = {255,158,89},
  braun = {217,191,140},
  darkgray = {38,38,38},
  blue = {89,158,255},
  green = {188,210,121},
  red = {179,94,77},
  white = {255,255,255},
  gray = {102,102,102}
}
 
styles.comment = {fg = colors.gray}
styles.operator = {fg = colors.white}
styles.number = {fg = colors.red}
styles.stringtxt  = {fg = colors.green}
styles.keywords0  = {fg = colors.blue}
styles.text = {fg = colors.braun, bg = colors.darkgray}
styles.keywords3 = {fg = colors.orange}
styles.keywords4 = {fg = colors.orange}
styles.keywords5 = {fg = colors.orange}
styles.keywords6 = {fg = colors.orange}
styles.keywords7 = {fg = colors.orange}

And if you want to have auto completion you have to add fibaro.lua to your ZeroBraneStudio/api/lua folder.

Please note that this file is the english file and has some few additions which i didn't see or where incorrect. Looks like:

autocompletion.png

 

To activate it, please make sure you add it. Go to Edit -> Preferences -> Settings: System

Add the following string to it:

api = {luadeb = {'fibaro'}}

The fibaro:calculateDistance doesn't work and the sourcetrigger needs to be set manually. See below snippet for testing:

__fibaroSceneSourceTrigger = {type="global", name="Pushbullet"}
-- __fibaroSceneSourceTrigger = {type="global", name="Pushbullet"}
-- __fibaroSceneSourceTrigger = {type="property", deviceID=6, propertyName="value"}
-- __fibaroSceneSourceTrigger = {type="autostart"}
-- __fibaroSceneSourceTrigger = {type="other"}
 
print(fibaro.getSourceTrigger().type) -- global
print(fibaro.getSourceTrigger().name) -- Pushbullet
 
__fibaroSceneSourceTrigger = {type="autostart"}
print(fibaro.getSourceTrigger().type) -- autostart

FAQ:

Q: I get the following error message: lualibs/json/decode.lua:74: bad argument #1 to 'match' (string expected, got nil)

A: It cannot reach Fibaro system correctly. Please make sure you have the right userid/pwd and correct ip number.

 

Q: I don't see any completion on my fibaro: 

A: Make sure you added the completion Edit -> Preferences -> Settings: System (lualib)

 

Q: Where do i get all my values from?

A: You can make use of the excellent tool from @Krikroff found in this thread to grab all the values.

 

Q: Where is this data coming from?

A: Fibaro has an api interface, you can go there via http://192.16.x.x/docs where you can try it out. 

 

Q: I click on play but it does not keep running?

A: Make sure you click the double green arrow. The other one stops untill you press forward. (F6 = Go, F5 = Waits on next itteration)

 

Q: I want to change the width of the tabs

A: Go to Edit -> Preferences -> Settings: User and add "editor.tabwidth = 3"

 

Q: Disable wordwrap for long lines

A: Go to Edit -> Preferences -> Settings: User and add "editor.usewrap = false"

 

Q: How to i debug line by line?

A: See below (credits go to Fred the Phew for below screenshots & explanation)

 

Open Zerobranstudio and display the following windows if you have not already done so

01.png

 

insert the following code in a new blank window Zerobranstudio (we will revise multiplication ;)

local loop = true local i = 1 local j = 1

while loop == true do
   i = i + 1
   j = j + 2 print ( i .. "x" .. j .. "=" .. i * j ) end

To activate dynamic tag tracking, simply select the name of the variable you want to track, then right click on it to bring up a context menu.

You must then select the option Add watch expression as in the image below. In our case, it must be done for the 2 variables i & j.

 

07.png

 

To switch to debug mode, press the F5 key once, the window should look like this.

The green cursor is positioned on the first instruction, and the Watch window displays the values of i and j (to nil at the moment which is logical)

 

02.png

 

Now press the F10 key to execute the program step by step, until the first result of the multiplication is displayed, as shown below.

The result of the multiplication is consistent with the values of i and j.

 

03.png

 

To test now the modification on the fly of a variable, we will re-run the loop a second time by pressing the F10 key several times but without executing the print instruction, as in the image below. The value of the 2 variables has been modified by programming.

 

04.png

 

To modify dynamically the value of j, click on the Remote console tab, type the line i = 20 then press the Enter key to validate the command, as in the image below. We can then see that the value of ja has been modified in the Watch window.

 

05.png

 

It is now enough to click on the tab Output (suspended), and to press the key F10 to resume the execution of the program.

It is now that the value of j set by hand has been dynamically taken into account by the program.

 

06.png

 

Happy Coding! \o/

(feel free to make remarks so i can make it better)

 

Hi @riemers

 

This is fantastic and I think it should be made more obvious if possible

Would you consider prefacing it as a TUTORIAL in the heading and bouncing it so people understand what a valuable nugget this is

 

Thanks for sharing

-frank

 

 

Link to post
Share on other sites
  • 0

Added Tutorial to the topic :) dunno what you mean by bouncing it.. presume just a post (like this one?) :P

 

Link to post
Share on other sites
  • 0
6 minutes ago, riemers said:

Added Tutorial to the topic :) dunno what you mean by bouncing it.. presume just a post (like this one?) :P

 

 yep.. @riemers...

thanks :-)

 

Link to post
Share on other sites
  • 0
On 2017-01-27 at 5:16 PM, riemers said:

FAQ:

Q: I get the following error message: lualibs/json/decode.lua:74: bad argument #1 to 'match' (string expected, got nil)

A: It cannot reach Fibaro system correctly. Please make sure you have the right userid/pwd and correct ip number.

 

 

Hi. This is where my quick test of this setup failed when I use my admin account (the one for which the user is now an email adress thanks to the new wizard thing). The admin account has a 28 character password.

 

It works, however, using an ordinary account (my simple test account has only 13 character password), and no other settings changed.

 

bug somewhere?

Link to post
Share on other sites
  • 0

I think those email addresses just don't work in this setup. I used my user account for it too. Not the admin account.

Link to post
Share on other sites
  • 0

This is a long shot from a ZeroBrane user, one who doesn't use lualibs. I haven't found the time to download the lib. :-( But I have been thinking... the user and possibly the password needs URL encoding. Edit: Actually, the code needs a more fundamental fix, and urlencode won't work. I've fixed the login now and I send the fix to the OP.

Edited by petergebruers
Link to post
Share on other sites
  • 0

Hi,

I was also wrestling with the email id. urlencode didn't work for me, so I panicked and patched url.lua in lualibs and the function _M.parse(url, default) from

authority = string.gsub(authority,"^([^@]*)@",

to

authority = string.gsub(authority,"^([^@]*@[^@]*)@",

which now requires the id to be an email with a '@' char in it. Not very elegant :-/ ..but it works for me...

However, I still have issues with calls to fibaro:getDevicesId that seems to have problem encoding the url.

Anyone found a real fix?


 

Link to post
Share on other sites
  • 0
24 minutes ago, jgab said:

Hi,

I was also wrestling with the email id. urlencode didn't work for me, (...)

 

Thank you for trying. I'll install lualibs this evening and I'll see what I can do.

Link to post
Share on other sites
  • 0

I've checked the code. I've found and solved these issues: authentication and getDevicesId crash. The first one: the library GET request uses an old form of authentication, as you found out it is sensitive to "@" and other characters. It's also deprecated, so I've replaced that part. Then the getDevicesId: it is a command that may return large sets of data, it depends on your filter criteria. For a large set, some data was not handed to json decode, so that part crashed with error "invalid data". Unfortunately, I cannot share the fixes yet, because I also need to change & test PUT and POST code, then try everything. I'll try to do that tomorrow.

Link to post
Share on other sites
  • 0

I've got an update, but I need a volunteer to test it before I post can share it.

 

- fix HC user authentication (was: user:password in URL, is now: basic authentication).
- fix chunked responses (was: use only chunk 1, is now: concatenate chunks). Fixes "getDevicesId".
- add error checking and display in the HTTP part, to get sensible error messages.

Edited by petergebruers
Forgot to mention fixes
Link to post
Share on other sites
  • 0

You can always add it with a "use at own risks" then everyone can test ;) if it works ok, i'll add it to the first post.

 

The only thing i really miss is using .net options to do http calls. Because then you can truly use complete scripts and most use that to get information (pretty much every script i made uses that) or i was stupid and missed something, wouldn't be the first time :)

Link to post
Share on other sites
  • 0
1 minute ago, riemers said:

You can always add it with a "use at own risks" then everyone can test ;) if it works ok, i'll add it to the first post.

 

The only thing i really miss is using .net options to do http calls. Because then you can truly use complete scripts and most use that to get information (pretty much every script i made uses that) or i was stupid and missed something, wouldn't be the first time :)

 

Thanks! I could publish the code, but I prefer a volunteer first. Just one tester. I always include a bug, so I need someone to find it for me. ;-) And also someone to cure my stage fright.

 

I'm not sure how much effort it would take to emulate the http behaviour of the net.HTTPClient() but I do know from previous experiments that lua/Zerobrane http is quite different. More than I year ago, I tried to find out if Fibaro was using some well known library, but several attempts led to nothing. :-( I have currently no plans to do that again.

 

What I (kind of) do to work around this: I separate the data access from the processing. When I test offline (Zerobrane) I inject test data into the processing part. The data accessing part is fairly static: call an url, massage the answer, check status & error... it's the processing part that's in flux.

 

 

Link to post
Share on other sites
  • 0

I do the same, just take the output, place it as a piece of text in a variable and play with it. I am sure someone could just download the libs from fibaro , but then you need to have access to the system itself. I really don't feel like opening up the box, changing the recovery image with a different /etc/shadow and restoring to just get that single file ;p So i would say, not impossible but certainly not simple. (for now, i'll keep it as it is)

 

p.s. i dont mind testing, but i am not always at home, so it could take a couple of days.

Edited by riemers
Link to post
Share on other sites
  • 0
30 minutes ago, riemers said:

(...) I am sure someone could just download the libs from fibaro (...) p.s. i dont mind testing, but i am not always at home, so it could take a couple of days.

 

Possible, but not guaranteed to work... If you assume net.HTTPClient is a lua file or set of files, like the json lib in the zip file of this project. But it could be compiled from c code too and I doubt that you'd find the source on the device... If someone knows the answer, well, I'd like to know it too. ;-) I'm certainly not "rooting" my device. The deal is: if you want support, don't root. And I want support.

 

BTW thanks for the offer to test. Since you probably have no time, let's see if someone else is in desperate need :-):-) 

Edited by petergebruers
Link to post
Share on other sites
  • 0

True, and its not lacking time, i just have a daughter & 3 months old baby, they don't actually automate the way i want it.. perhaps @AutoFrank can test.

Link to post
Share on other sites
  • 0
3 minutes ago, riemers said:

True, and its not lacking time, i just have a daughter & 3 months old baby, they don't actually automate the way i want it.. perhaps @AutoFrank can test.

 

Ah! I totally understand. Two daughters, 17 and 20 here... They seem to run a weird operating system that I don't fully understand. Recently also switched to "paid subscription model" too. ;-)

Link to post
Share on other sites
  • 0
7 minutes ago, petergebruers said:

 

Ah! I totally understand. Two daughters, 17 and 20 here... They seem to run a weird operating system that I don't fully understand. Recently also switched to "paid subscription model" too. ;-)

@petergebruers

@riemers

 

I have a trio, a little older that riemers 2 but seem to be cloud based. I haven't hit the paid subscription model yet, I hear there is no 'get out' clause with that one :-)

... happy to test something this evening if that helps...

 

Link to post
Share on other sites
  • 0

Changelog for v1.0.1 (thank to @petergebruers

- fix HC user authentication (was: user:password in URL, is now: basic authentication).
- fix chunked responses (was: use only chunk 1, is now: concatenate chunks). Fixes "getDevicesId".
- add error checking and display in the HTTP part, to get sensible error messages.

 

Lualibs is added to first post at the top. Added a version number to the zip so we don't have different versions floating around.

Link to post
Share on other sites
  • 0

@riemer This is fantastic, been using Zero for years writing LUA and have been waiting for this.

Thank you for bringing this to my attention!!

 

And to Steven who made it and to all the people contributing @petergebruers and others!

 

This is what it should have been from day 1 writing code for Homecenter,

 

Some code for using CentralSceneEvent as trigger:

__fibaroSceneSourceTrigger = {
  type = "event", 
  event = {type = "CentralSceneEvent", data = {keyAttribute = "Pressed", keyId = 1, deviceId = 216} }
}

 

 

 

Edited by speedy
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...