Page 1 of 2

Domoticz and LightwaveRF with wifi Link

Posted: Sunday 04 August 2013 13:16
by techbod
Whilst waiting for a RFXCOM transceiver to arrive I've been wondering if I can get Domoticz to work with a Lightwave RF Wifi Link.

The Wifi Link receives a UDP datagram which it converts to a suitable 433MHz signal and turns On/Off the LightwaveRF device.

So on my Pi I've added lua-socket via apt-get and have a very simple lua script in the domiticz/scripts/lua directory:

require 'socket'
udp = socket.udp()
socket.udp():sendto('004,!R1D1F1|Device 1 on.', '192.168.1.100', 9760)
udp:close()

This works fine from the command line, the Wifi Link receives the datagram and the Lightwave switch turns on. Sending '004,!R1D1F0|' etc turns it off.

So any pointers on where I might be able to go from here and do something useful with this and integrate it into Domoticz properly? New to both Domoticz and Lua, so might have to keep it simple to start with...

Thanks.

Re: Domoticz and LightwaveRF with wifi Link

Posted: Tuesday 06 August 2013 22:42
by simonrg
I have been wondering how to integrate the LightwaveRF with Domoticz and was thinking that I would need to get somebody to write some C code, but what you have done means that writing a Lua package one could do it with the event system and the Dummy hardware object almost as well.

I am new to Lua, so have got stuck at how to get the Sockets library for Domoticz on a Raspberry Pi, so if someone knows how to do that that would be most helpful.

So to integrate your code into Domoticz create a script called script_device_wifi.lua in the Domoticz lua code directory with this code:

Code: Select all

commandArray = {}
if (devicechanged['WiFi'] == 'On') then
        commandArray['WiFi']='On'
        print('Switched LightwaveRF On')
        require 'socket'
        udp = socket.udp()
        socket.udp():sendto('004,!R1D1F1|Device 1 on.', '192.168.1.100', 9760)
        udp:close()
        for i, v in pairs(otherdevices) do print(i, v) end
elseif (devicechanged['WiFi'] == 'Off') then
        commandArray['WiFi']='Off'
        print('Switch LightwaveRF Off')
        require 'socket'
        udp = socket.udp()
        socket.udp():sendto('004,!R1D1F1|Device 1 off.', '192.168.1.100', 9760)
        udp:close()
end
return commandArray
I was thinking this was the perfect use for the Dummy hardware, but I can't seem to get that to work as I expected, unfortunately this probably means you can't test this until your RFXCOM arrives - :( .

All you need to do then is create an On/Off switch in the switches section, I have just created an On/Off switch called WiFi and assigned it parameters under RFXCOM X10 which don't matter, hence this should really be Dummy hardware.

Now when I have an On/Off switch called WiFi which calls the Lua script whenever pressed this should turn a real LightwaveRF switch on and off, sadly I have currently loaned my WiFi hub to somebody else as I was so happy with Domoticz, but I may get it back to play when I learn how to load packages into Lua.

Hope this makes sense, all the best, Simon

Re: Domoticz and LightwaveRF with wifi Link

Posted: Tuesday 06 August 2013 23:45
by simonrg
DUMMY hardware does work, you just need to close and restart your browser before it becomes visible in the Manual Light/Switch set up screen.

I still haven't worked out how to load a Lua package but I have corrected my Lua code, so it won't action every second forever.

Thinking I had to return something in CommandArray I was setting the switch to the state it was already in, this counts as a change, so the script was then called again, changing the setting again and hence being called again etc.

So better code below:

Code: Select all

commandArray = {}
if (devicechanged['WiFi'] == 'On') then
        print('Switched LightwaveRF On')
        require 'socket'
        udp = socket.udp()
        socket.udp():sendto('004,!R1D1F1|Device 1 on.', '192.168.1.100', 9760)
        udp:close()
        for i, v in pairs(otherdevices) do print(i, v) end
elseif (devicechanged['WiFi'] == 'Off') then
        print('Switch LightwaveRF Off')
        require 'socket'
        udp = socket.udp()
        socket.udp():sendto('004,!R1D1F1|Device 1 off.', '192.168.1.100', 9760)
        udp:close()
end
return commandArray
So add Dummy hardware, create a WiFi switch and then check the log to see that when the WiFi switch is turned on and off, then the log records these.

Re: Domoticz and LightwaveRF with wifi Link

Posted: Wednesday 07 August 2013 10:05
by techbod
Thanks for the code and the reply, that explains it all very nicely.

The RFXCOM has arrived, and as expected works with LightwaveRF and Domoticz straight out of the box.

However, since I've started looking at the wifi link I'd like to see if that can be made to work as well - just for the sake of it :)

So wifi switch created, your modified script in the correct lua directory the following appears in the log:

Error: Wed Aug 7 07:53:15 2013 /usr/local/domoticz/scripts/lua/script_device_wifi.lua:5: module 'socket' not found:
no field package.preload['socket']
no file '/usr/local/share/lua/5.2/socket.lua'
no file '/usr/local/share/lua/5.2/socket/init.lua'
no file '/usr/local/lib/lua/5.2/socket.lua'
no file '/usr/local/lib/lua/5.2/socket/init.lua'
no file './socket.lua'
no file '/usr/share/lua/5.1/socket.lua'
no file '/usr/local/lib/lua/5.2/socket.so'
no file '/usr/local/lib/lua/5.2/loadall.so'
no file './socket.so'
--

My original script still works fine from the Pi command line
'lua -v' says the Pi is running version 5.1.5
apt-get has installed lua_socket_2.0.2-8_armhf which looks like it supports lua 5.1

I think your modified script will work fine with the wifi link, I'll just have to do some digging around lua socket versions and possible path issues. Or have I got stuck at the same place - with sockets library for Domoticz?

Re: Domoticz and LightwaveRF with wifi Link

Posted: Wednesday 07 August 2013 22:55
by simonrg
You are stuck at the same place as I am.

I think the problem is that Domoticz has Lua 5.2 compiled into it, whereas the available Lua for Debian Wheezy on the RaspberryPi is 5.1.5 and trying to upgrade to 5.2 fails as various dependencies are not available. After replying to your post I did some fishing and replied to another post, suggeting Domoticz should revert to 5.1.5 Lua, as 5.2 is not properly supported on RaspberryPI. http://www.domoticz.com/forum/viewtopic ... ages#p1042

Great if you can find another solution though.

Re: Domoticz and LightwaveRF with wifi Link

Posted: Thursday 08 August 2013 0:14
by techbod
Its probably a bit messy but what about;

Code: Select all

commandArray = {}
if (devicechanged['WiFi'] == 'On') then
        print('Switched LightwaveRF On')
        os.execute("nc -u -w1 192.168.1.100 9760 < /usr/local/path/to/file/on")
        for i, v in pairs(otherdevices) do print(i, v) end
elseif (devicechanged['WiFi'] == 'Off') then
        print('Switch LightwaveRF Off')
       os.execute("nc -u -w1 192.168.1.100 9760 < /usr/local/path/to/file/off")
end
return commandArray
on file might contain; "004,!R1D1F1|Device 1 on ."
off file might contain; "004,!R1D1F0|Device 1 off ."

Something like that. So in other words you are just asking Lua to make a system call at the right time and get Netcat to actually send the datagram.

Perhaps its a bit crude, but as an idea it seems to work - I can receive and decode the udp packet via wireshark when toggling the wifi switch via the web gui. There is also a small cli program called 'sendip' which could be used in the same way.

As for doing it through Lua socket - it seems that LuaSoucket 2.1rc1 might be 5.1 and 5.2 compatible. I'm not sure if that will help or not, I haven't yet found the source code yet to try.

Re: Domoticz and LightwaveRF with wifi Link

Posted: Friday 09 August 2013 22:51
by simonrg
Seems like a good work around to avoid the package issue.

My only thought is that you would end up with a lot of files - OnLight1, OffLight1, OnLight2, OffLight2, Dim20LIght1 etc.. If you only want to control a few devices then this may still be the easiest way to do it.

I tidier way would be to write a Lua routine that writes the file just before the netcat call to which you specify parameters for each light and then calls netcat to send the contents of the file. This routine could then be in a library making each event only a few lines of code:

Code: Select all

-- pseudo code - conceptual
require('LWRFLibrary')
commandArray = {}
if (devicechanged['LWRF1']) then
          LwrfControl(devicechanged['LWRF1'])
end
return commandArray
The two other alternatives are to find somebody to add the LightwaveRF Hub as a proper hardware device with the necessary C code or if it were possiblet to add Lua code for a hardware item. In either case the parameters could be stored in the device attributes which are set up with the gui.

Re: Domoticz and LightwaveRF with wifi Link

Posted: Friday 31 January 2014 12:42
by adamjseed
Has anyone managed to get the socket lib working yet?

I have tried to add the lib's locally but the docs http://w3.impa.br/~diego/software/luaso ... ation.html show a directory structure which is nothing like the downloads. The error I currently get is: /usr/bin/lua: ./socket.lua:13: module 'socket.core' not found:

This is because the scoket.core / core.lua / core.so isnt in any downloads as far as I can see!

Re: Domoticz and LightwaveRF with wifi Link

Posted: Friday 31 January 2014 16:09
by Mathiasw
Forgive me if this is a bad idea, as I have basically little knowledge and have only been using the system for a month...

Couldn't you offload the socket operation to another (command line driven program) and call THAT with os.execute()? For example, I use os.execute("curl ...") to send http requests to zwave.me for things that are not supported in domoticz.

And if curl itself doesn't support the sort of socket operation you want to run, just write script that does, perhaps in python or something else?

Re: Domoticz and LightwaveRF with wifi Link

Posted: Monday 10 February 2014 13:03
by SweetPants
adamjseed wrote:Has anyone managed to get the socket lib working yet?
yes, I got it compiled succesfully on RPI, finally.
1) download from https://github.com/diegonehab/luasocket luasocket-master.zip
2) unzip luasocket-master.zip
3) sudo apt-get install liblua5.2-dev (if not already installed)
4) cd luasocket-master
5) make PLAT=linux
6) cd src

edit makefile (must be a cleaner solution to do the next step) and change:

# LUAV: 5.1 5.2
# lua version to build against
change LUAV?=5.1 to LUAV?=5.2

7) cd ..
8) sudo make install

Test with:

http = require('socket.http')
base_url = "http://127.0.0.1:8080/"

local r,e = http.request( base_url .. "json.htm?type=devices&used=true")
if (e == 200) then
print (r)
end

This should do the trick, Harry

Re: Domoticz and LightwaveRF with wifi Link

Posted: Sunday 23 February 2014 14:43
by Mavy
sadly this wont work that easy, the built in lua does not support dynamic libraries.
@Mathiasw: Yes curl does allow me to do what i would like but it has one major down side. It spawns as a child of domoticz. So when you stop domoticz while a curl thread is running your domoticz will hang until you kill those threads.

IMO: I think its a shame the devs built such a nice json api for us but no way to use it from the lua scripts.

Re: Domoticz and LightwaveRF with wifi Link

Posted: Tuesday 25 February 2014 13:06
by SweetPants
Modify luaconf.h in domoticz/lua/src and add

#define LUA_USE_LINUX

Re-compile and you have dynamic library support on Raspberry

Harry

Re: Domoticz and LightwaveRF with wifi Link

Posted: Tuesday 08 July 2014 3:45
by sdh
Is there any plan to support Lua Sockets in domoticz by default?

Re: Domoticz and LightwaveRF with wifi Link

Posted: Friday 03 April 2015 16:54
by bomboclat
Hi any idea tou use it under windows machine ?
i try many luasocket but the system crash because Domoticz use lua 5.2 and all luasocket that i find is for lua 5.1.
Is it possible to downgrade lua to 5,1 under domoticz ?

Thaks
Michele

Re: Domoticz and LightwaveRF with wifi Link

Posted: Sunday 13 March 2016 15:02
by simonfr
I stumbled across this thread while trying to work out how I could use my LightwaveRF WiFi Link with Domoticz. I took the approach of using a Python script to send the necessary commands to the Link over UDP and thought I'd share my code here.

I have named the script myLWRF.py

Code: Select all

import socket
import time
import sys
import logging

logging.basicConfig(filename='myLWRF.log', level=logging.INFO, format='%(asctime)s : %(message)s')

def lw_action(deviceID, action):
  UDP_IP = '192.168.1.95'     # IP of your Wifi Link
  UDP_PORT = 9760
  INET_ADDR = (UDP_IP,UDP_PORT)

  cmdON  = "F1"
  cmdOFF = "F0"

  if action == "on":
    cmd = cmdON
  elif action == "off":
    cmd = cmdOFF
  else:
    cmd = "unknown_command"
    logging.info('Unrecognized command. Arguments passed : {0} - {1}'.format(deviceID, action))

  if cmd != "unknown_command":
    myDevices = []
    myDevices.append(deviceID)

    # Ignore the following - had previously used an array to look through and switch on/off all devices
    # myDevices.append("R1D1") # Lounge : TV LEDs
    # myDevices.append("R2D1") # Hall : Stair lights
    # myDevices.append("R2D2") # Hall : test switch
    # myDevices.append("R3D1") # Family Room : Mirror LEDs
    # myDevices.append("R4D1") # Master Bedroom : Bookshelf LEDs

    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

    for device in myDevices:
      command = "000,!" + device + cmd + "|"

      sock.sendto(command, INET_ADDR)
      time.sleep(2)

  return

# End of function - start of main code block

# Read arguments passed 
deviceID = str(sys.argv[1])
action = str(sys.argv[2])

# Log arguments passed
logging.info('Arguments {0} - {1}'.format(deviceID, action))

# Call function to switch on/off device
lw_action(deviceID, action)
Save the script to :

Code: Select all

/home/pi/domoticz/scripts/myLWRF.py
The ownership of the script must be set to the pi user, and the attributes changed to executable :

Code: Select all

sudo chown pi.pi /home/pi/domoticz/scripts/myLWRF.py
chmod +x /home/pi/domoticz/scripts/myLWRF.py
To trigger the code you should set up a Hardware item of type "Dummy". Then create a Virtual Sensor for the item of type "Switch". Once you've done that, go in to the "Switches" dashboard and edit the switch you've just created. The on/off actions will need to be set as follows :

Code: Select all

script:///home/pi/domoticz/scripts/myLWRF.py R2D2 on
script:///home/pi/domoticz/scripts/myLWRF.py R2D2 off
To work out what your device ID is you'll need to use the LightwaveRF app to set up devices and allocate to rooms. After you've created all your devices and rooms the switches will be set up as follows :

R1D1 = First room, first device
R1D2 = First room, second device
R2D1 = Second room, first device
R2D2 = Second room, second device
etc, etc

Hopefully this will be of help to someone :)

Simon

Re: Domoticz and LightwaveRF with wifi Link

Posted: Wednesday 21 December 2016 19:53
by jonohunt
simonfr wrote:I stumbled across this thread while trying to work out how I could use my LightwaveRF WiFi Link with Domoticz. I took the approach of using a Python script to send the necessary commands to the Link over UDP and thought I'd share my code here.

I have named the script myLWRF.py

Code: Select all

import socket
import time
import sys
import logging

logging.basicConfig(filename='myLWRF.log', level=logging.INFO, format='%(asctime)s : %(message)s')

def lw_action(deviceID, action):
  UDP_IP = '192.168.1.95'     # IP of your Wifi Link
  UDP_PORT = 9760
  INET_ADDR = (UDP_IP,UDP_PORT)

  cmdON  = "F1"
  cmdOFF = "F0"

  if action == "on":
    cmd = cmdON
  elif action == "off":
    cmd = cmdOFF
  else:
    cmd = "unknown_command"
    logging.info('Unrecognized command. Arguments passed : {0} - {1}'.format(deviceID, action))

  if cmd != "unknown_command":
    myDevices = []
    myDevices.append(deviceID)

    # Ignore the following - had previously used an array to look through and switch on/off all devices
    # myDevices.append("R1D1") # Lounge : TV LEDs
    # myDevices.append("R2D1") # Hall : Stair lights
    # myDevices.append("R2D2") # Hall : test switch
    # myDevices.append("R3D1") # Family Room : Mirror LEDs
    # myDevices.append("R4D1") # Master Bedroom : Bookshelf LEDs

    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

    for device in myDevices:
      command = "000,!" + device + cmd + "|"

      sock.sendto(command, INET_ADDR)
      time.sleep(2)

  return

# End of function - start of main code block

# Read arguments passed 
deviceID = str(sys.argv[1])
action = str(sys.argv[2])

# Log arguments passed
logging.info('Arguments {0} - {1}'.format(deviceID, action))

# Call function to switch on/off device
lw_action(deviceID, action)
Save the script to :

Code: Select all

/home/pi/domoticz/scripts/myLWRF.py
The ownership of the script must be set to the pi user, and the attributes changed to executable :

Code: Select all

sudo chown pi.pi /home/pi/domoticz/scripts/myLWRF.py
chmod +x /home/pi/domoticz/scripts/myLWRF.py
To trigger the code you should set up a Hardware item of type "Dummy". Then create a Virtual Sensor for the item of type "Switch". Once you've done that, go in to the "Switches" dashboard and edit the switch you've just created. The on/off actions will need to be set as follows :

Code: Select all

script:///home/pi/domoticz/scripts/myLWRF.py R2D2 on
script:///home/pi/domoticz/scripts/myLWRF.py R2D2 off
To work out what your device ID is you'll need to use the LightwaveRF app to set up devices and allocate to rooms. After you've created all your devices and rooms the switches will be set up as follows :

R1D1 = First room, first device
R1D2 = First room, second device
R2D1 = Second room, first device
R2D2 = Second room, second device
etc, etc

Hopefully this will be of help to someone :)

Simon
I've just started using Domoticz, and came to the forum looking how to use my LightwaveRF Link and lights with it.

I've set this up and it works perfectly. Thanks Simon!

Re: Domoticz and LightwaveRF with wifi Link

Posted: Thursday 22 December 2016 1:41
by simonfr
Glad someone has been able to put it to use :-) I now use a RFXTRX433E in favour of the LightwaveRF Link, but that's just to help cut down on running costs by minimising the number of hubs I have to run.

Re: Domoticz and LightwaveRF with wifi Link

Posted: Friday 23 December 2016 11:35
by jonohunt
Yeah, I might end up getting some other than the LightwaveRF Link, but for now it'll do fine :)

Re: Domoticz and LightwaveRF with wifi Link

Posted: Thursday 29 December 2016 21:22
by jonohunt
So I'm now using this all the time. I don't suppose you (or anyone else) knows to dim the lights with a script?

I'm not bothered about setting all different levels of brightness, just one setting. E.g. just setting it to 50% brightness or something.

Re: Domoticz and LightwaveRF with wifi Link

Posted: Monday 09 January 2017 21:19
by jonohunt
simonfr wrote:I stumbled across this thread while trying to work out how I could use my LightwaveRF WiFi Link with Domoticz. I took the approach of using a Python script to send the necessary commands to the Link over UDP and thought I'd share my code here.
I've started looking at creating a kind of 'scene' where I could run the script to dim a couple of LightwaveRF lights (dimmer switches) to set their brightness levels. If I could get it working I could set one light to 15% and the other to 50%.

Could the code of your script be modified in a similar way to below:

cmdON = "F1"
cmdOFF = "F0"
cmdDIM15 = "FdP5" # Dim to 15%
cmdDIM50 = "FdP16" # Dim to 50%

if action == "on":
cmd = cmdON
elif action == "off":
cmd = cmdOFF
elif action == "dim15":
cmd = cmdDIM15
elif action == "dim50":
cmd = cmdDIM50


So that you could create a dummy switches in Domoticz along the lines of these:

script:///home/pi/domoticz/scripts/myLightwaveRF.py R2D1 dim50

script:///home/pi/domoticz/scripts/myLightwaveRF.py R2D2 dim15


This doesn't work (as I don't know python), but I'm wondering if something like this could be made to work with the correct code?