FHEM Forum

FHEM => English Corner => Thema gestartet von: franc am 17 August 2014, 10:39:44

Titel: LIFX Light Bulbs
Beitrag von: franc am 17 August 2014, 10:39:44

Hi,

I have written a module for LIFX Light Bulbs, you can find it here:-

https://github.com/franc-carter/fhem-lifx.git

It doesn't expose all of the functionality of the Bulbs, but I have found the subset complete enough to be useful

cheers
Titel: Antw:LIFX Light Bulbs
Beitrag von: andrejs am 25 August 2014, 15:43:05
Nice work! I really want to test it because I have one LIFX bulb installed. My question is for testing I need to copy all 3 pm modules (LIFX.pm, LIFXBulb.pm, LIFXBrigde.pm) in FHEM directory and add  "define" sentence in cfg file?

Cheers

Andrej
Titel: Antw:LIFX Light Bulbs
Beitrag von: franc am 30 August 2014, 02:07:50

Hi,

I'm afraid there isn't a nice clean install at the moment, so there is several steps - I'll go through them below.

1. Get the Perl LIFX module from https://github.com/franc-carter/perl-lifx.git
2. Copy the 'Device' directory in your Perl Library path somewhere (probably O/S, version etc dependent)
3. Run ./examples/example1.pl  <LABEL>  where <LABEL> is the name you have assigned the Bulb
4. This should turn the Bulb on and off and shows that the Perl module is working

Once this works the FHEM module can be installed


1. Get the FHEm module from https://github.com/franc-carter/fhem-lifx.git
2. Copy 30_LIFXBridge.pm  and 31_LIFXBulb.pm to the FHEM modules directory
3. Add the following to your FHEM config

     define <BRIDGE_NAME> LIFXBridge
     define <BULB_NAME>    LIFXBulb <BULB_IDENTIFIER>

     where <BRIDGE_NAME> and <BULB_NAME> are whatever you like
     and <BULB_IDENTIFIER> is either the Bulb MAC address or Bulb Label

You should then be able to use the Bulb in the rest of the FHEM rules.

Let me know how you go






Titel: Antw:LIFX Light Bulbs
Beitrag von: andrejs am 30 August 2014, 22:40:34
Hi Franc
Thanks for the instructions. Unfortunately I stucked at running example1.pl. I copied the Perl LIFX Module at /usr/bin/perl (I am using Ubuntu 14.04) and after that run in terminal ./examples/example1.pl d0:73:d5:01:45:ba. No error messages have been displayed but the script hang in the loop and I was forced to end it with ctrl C. I checked the mac address of the bulb and it is correct. I looked at the pl script and I think the problem is  this part:

my $bulb = undef;
while(!defined($bulb)) {
    my $msg = $lifx->next_message(1);
    if (defined($mac)) {
        $bulb = $lifx->get_bulb_by_mac($mac);
    } else {
        $bulb = $lifx->get_bulb_by_label($ARGV[0]);
    }
}

Maybe just one additional information I upgraded a few days ago the firmware of lifx bulb.

Andrej
Titel: Antw:LIFX Light Bulbs
Beitrag von: franc am 31 August 2014, 15:09:15

Hi,

Can you replace that section of code with the code below and tell me what it prints. I have also upgraded to the new firmware so that should be fine.

my $bulb = undef;
while(!defined($bulb)) {
    my $msg = $lifx->next_message(1);
print "$msg\n";
    if (defined($mac)) {
        $bulb = $lifx->get_bulb_by_mac($mac);
print "$bulb\n";
    } else {
        $bulb = $lifx->get_bulb_by_label($ARGV[0]);
    }
}

cheers
Titel: Antw:LIFX Light Bulbs
Beitrag von: andrejs am 31 August 2014, 17:33:46
Hi Franc,

I replaced the section of the code and run again. No error messages have been displayed just the empty lines are displayed. I was forced again to stop the script with ctrl C.

Andrej
Titel: Antw:LIFX Light Bulbs
Beitrag von: franc am 01 September 2014, 00:05:01

Ok, let's try looking at a more detailed log of what is happening. Can you run

script -a lifx.log -c 'PERLDB_OPTS="NonStop=1 AutoTrace=1 frame=2" perl -dS ./examples/example1.pl d0:73:d5:01:45:ba'

and then end me the lifx.log, hopefully I'll be able to work out what is happening from that

cheers
Titel: Antw:LIFX Light Bulbs
Beitrag von: franc am 01 September 2014, 08:38:14
I just had a thought - are you running a 32bit version of Ubuntu ? what's the output of uname -a

cheers
Titel: Antw:LIFX Light Bulbs
Beitrag von: andrejs am 01 September 2014, 18:56:17
Thanks Franc for a quick reply. Please find life.log attached to this message. My Ubuntu/Linux version is:

Linux andrejs-MCP79 3.13.0-24-generic #46-Ubuntu SMP Thu Apr 10 19:08:14 UTC 2014 i686 i686 i686 GNU/Linux

Cheers

Andrej
Titel: Antw:LIFX Light Bulbs
Beitrag von: franc am 02 September 2014, 11:21:52

Ahh, i686 - that's 32 bit I'm afraid. I discovered yesterday that the modules does not work at all on 32bit machines ;-(

I've started to fix it, but it is more difficult than I first hoped.

I'm guessing it will take about a week, unless it's even messier than I expect - I'll let you know once I have it fixed

sorry
Titel: Antw:LIFX Light Bulbs
Beitrag von: andrejs am 02 September 2014, 16:32:40
It OK Franc I am prepared to wait because I am really interested to have lifx installed in FHEM.

Thanks again

Andrej
Titel: Antw:LIFX Light Bulbs
Beitrag von: franc am 04 September 2014, 14:21:29

Hi Andrej,

I've made some progress, the version of the module I have attached partially works on my raspberry pi. Could you try the example programs using this new version and let me know if it works better.

thanks
Titel: Antw:LIFX Light Bulbs
Beitrag von: andrejs am 04 September 2014, 20:06:16
Hi Franc,

I tried to run example1.pl and got the following error:

ZitatHexadecimal number > 0xffffffff non-portable at /usr/lib/perl5/Device/LIFX.pm line 343.


Andrej
Titel: Antw:LIFX Light Bulbs
Beitrag von: franc am 04 September 2014, 22:20:32

You can ignore that message for the moment, it's to do with managing the Tags on the Bulbs - I haven't fix that part yet.

Did example1 manage to turn your Bulb on/off

cheers
Titel: Antw:LIFX Light Bulbs
Beitrag von: andrejs am 05 September 2014, 21:14:03
Hi Franc,

unfortunately I was not able to switch off/on the bulb. I ran the script again script -a lifx.log -c 'PERLDB_OPTS="NonStop=1 AutoTrace=1 frame=2" perl -dS ./examples/example1.pl d0:73:d5:01:45:ba' and I attached the log file to this message.

Andrej
Titel: Antw:LIFX Light Bulbs
Beitrag von: franc am 07 September 2014, 14:51:02
I've pushed a new version to github that has more updates and works reasonably well on my raspberry pi. Can you grab the new version and try the examples/monitor.py - it should print the message types and mac addresses of the Bulbs it sees.

No need to run it with the PERLDB_OPTS, it looks like the Perl on your system and on mine are compiled with different debug options and hence I can't get much info from those logs

cheers
Titel: Antw:LIFX Light Bulbs
Beitrag von: andrejs am 07 September 2014, 16:44:26
Franc,
I copied Device directory and examples from your Github on my Ubuntu and ran monitor.pl script. The printed result was "Timeout".
I checked attached devices on my Netgear router and life bulb is on the list (see attached file). I am also able to control life bulb with iOS application.
I also created short perl script (printenv6.pl)  to switch off the life bulb. I have not got any error message but unfortunately the bulb stayed switched on.I think it should be something wrong with the protocol. For the protocol I followed the instructions https://github.com/magicmonkey/lifxjs/blob/master/Protocol.md but I am not sure if it is still valid. In my case the message was created as follows:

$size\x00\x00\x14\x00\x00\x00\x00$machex\x00\x00$machex\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00$command\x00\x00\x00\x00$payload

$size=pack("H*","26");
$command=pack("H*","15");
$payload=pack("H*","00");

script for $machex (because I have just one life bulb it is also a gateway)
my $zmac='d0:73:d5:01:45:ba';
  $zmac=~ s/://g;
  my $i=0;
  my $machex;
  my ($a,$c);
  my $n = length($zmac);
 
  while($i<$n)
    {
    $a =substr($zmac,$i,2);
    $c = pack("H*",$a);
      if ($i==0){$machex=$c;}
      else {$machex.=$c;}
      $i+=2;
    }

Could you please tell me do you use the same protocol/message to switch off the life bulb?


Andrej
Titel: Antw:LIFX Light Bulbs
Beitrag von: franc am 08 September 2014, 09:33:00

The docs at MagicMonkey are mostly correct for the new firmware, however the second $machex has been replaced with "LIFXV2".

That repo also contains a wireshark protocol interpreter for the LIFX protocol, which I found very useful. If you install Wireshark along with the protocol intepreter than you will be able to see the packets in a readable form

cheers
Titel: Antw:LIFX Light Bulbs
Beitrag von: andrejs am 09 September 2014, 00:55:38
Thank you very much Franc for your answer it was very useful because after I replaced second $machex with LIFXV2 I was able to switch off the bulb. Chancing the $payload I managed to switch on the bulb.
I ran again monitor.pl but the result was the message "Timeout" and not the mac address of the bulb. Do you know what should be wrong here?

Thanks

Andrej
Titel: Antw:LIFX Light Bulbs
Beitrag von: franc am 11 September 2014, 14:29:00

Hi,

That's mysterious, I'm sot sure what is going wrong with my code. Are you comfortable with sending me a packet dump of the communications to your Bulbs ? If so, could you run:-

   sudo tcpdump -n -s0 -i eth0 -w lifx.pcap port  56700

the run your script that works, followed by my script that does not.

Once you have done that can you send the lifx.pcap file and I will have a look at it to see what is going on

cheers




Titel: Antw:LIFX Light Bulbs
Beitrag von: andrejs am 12 September 2014, 22:56:34
Franc,
I followed your instructions and here is the file lifx.pcap.

Andrej
Titel: Antw:LIFX Light Bulbs
Beitrag von: franc am 16 September 2014, 13:08:30
Hi Andrejs,

I'm afraid that packet capture is empty - It may be that your network interface is not called eth0, could yo send the output of

  ifconfig -a

(sorry this is taking so long to debug)

cheers
Titel: Antw:LIFX Light Bulbs
Beitrag von: andrejs am 18 September 2014, 23:33:57
Franc,
the problem was because I forgot to open udp port 56700 (I opened just tcp port 56700) on firewall. I defined LIFXBridge and LIFXBulb in FHEM and it works!!! :D
I added attribute webcmd to lifxbulb rgb:rgb FF0000:rgb FFFFFF:rgb 0000FF:toggle:on:off  and I was able also to change colors! You really have done very nice module. Thank you again.
I notices just one issue. When I tried to change color (see attached part of my print screen at the top of picture) device lifxbulb was shutdown/deleted. I checked the log but nothing was there.

Andrej
Titel: Antw:LIFX Light Bulbs
Beitrag von: franc am 28 September 2014, 09:16:34

Great, I'm glad it works.

I've found (and fixed) the bug with color (thanks for finding it), if you pull the fhem-lifx from github you should see that 'color' has been changed to 'kelvin' and works properly.
Titel: Antw:LIFX Light Bulbs
Beitrag von: d.schoen am 15 August 2015, 12:22:19
I made some improvements to the module, according my needs here: https://github.com/dsgrafiniert/fhem-lifx

Feel free to contribute.
Titel: Antw:LIFX Light Bulbs
Beitrag von: fringl am 31 Januar 2016, 12:34:33
Hi,

I'm new to this forum and also fhem, which I am currently trying to get connected to my lifx bulbs. I still have an issue hopefully someone can help me with:

The perl test scripts went fine (switching light on/off), and I could define the bridge in fhem no problem. However, I cannot define the bulb correctly by using the command "define LIFX_Bulb1 LIFXBulb d0:73:d5:01:29:e9"

The problem is the ID parameter of the bulb, which looks somehow cryptic and not seem to be valid:

DEF             d0:73:d5:01:29:e9
ID               �s�)�
IODev         LIFX_Bridge
NAME          LIFX_Bulb1
NR              28
STATE        Initialized
TYPE           LIFXBulb

I have used the original (franc's) sw version.

thanks for the help.... 
Ingo
Titel: Antw:LIFX Light Bulbs
Beitrag von: franc am 08 März 2016, 09:06:18

Hi Ingo,

The ID is not necessarily a problem - the approach I used was a bit buggy/silly/naive as I wrote the module when I was new to fhem.

I assume other than it 'looking wrong' that when you try to control the light through fhem's UI it does not work - correct ?

Can you have a look in the fhem.log for LIFX related message for anything that looks odd. If nothing pops out at you grep the logs for LIFX stuff and I'll see if I can see an issue

cheers
Titel: Antw:LIFX Light Bulbs
Beitrag von: Aikawa24 am 25 November 2016, 01:48:19
Hi, i am switching the Lifx with Homebridge over fhem because fhem was my first project.

the Lifx switch flawless in fhem but in Homebridge i got some error and i can switch only once

LIFXSchlafzimmer-rgb not a number: aN
[Fri Nov 25 2016 01:42:10 GMT+0100 (CET)] [FHEM]   value converted to aN

does anyone knows what it is ?

Thanks