LIFX Developer Zone

Connecting and controlling light via raspberry pi without internet?

Hi, I would like to control a LIFX lamp with the raspberry pi. The raspberry pi interacts as a wifi access point without internet. So how can I control the lamp with the pi? I tried to connect to lamp via the LIFX app, but this did not work (probably because of the cloud). Do you have any idea how to solve the problem?

If the Raspberry Pi acts as an AP and your lights and your phone are both connected to that AP, then most of the functionality of the app should work. The only things that generally require the cloud are scenes and schedules.

However, the app does try to login to the cloud and if you don’t have Internet access, that won’t work. You may have to disconnect your lights from the cloud (there’s an option in each bulb’s settings screen).

Alternatively, you could look at full programmatic control via something like Photons running on the Pi itself.

This sounds great. But I do not know how can I connect the bulb to the network without the app. Do you have any idea?

Hello,

It should work if you tell the app about your rPi’s hotspot as if it were any other network.

If it’s getting to the preparing device screen and isn’t going past that, it’s entirely possible the light is already on the other network and if you cancel from that and make sure you’re phone is also on the rPi AP then it should be controllable in the app

I got it in the network using the app. Afterwards I connected to the wifi network (hotspot) with my desktop PC and ran the application advanced ip scanner to check if the bulb is in the network of the pi. It is listed with an ip address and a mac address. Then I tried to use putty with lifxlan and a hello_world script. The result is: 0 devices detected. The bulb has the ip 192.168.1.109 and the hotspot has the ip 192.168.1.1, so it is in the same network. Any idea why lifxlan does not list up the bulb?

I also tried photons, but got this error:

12:06:56 ERROR photons_transport.transports.socket Socket got an error address=(‘255.255.255.255’, 56700) error=[Errno 101] Network is unreachableserial=None
Any idea how to fix it?

That looks like your rPi AP doesn’t allow broadcast packets.

There’s no way around that in the app but in Photons you have discovery options that’ll let you hard code an ip address.

I’m unfamiliar with making an rPi provide an AP, so I’m unsure how to make it allow broadcast UDP.

If after you hard code the IP of your device and it doesn’t work, then potentially something else is wrong. But also then I’m unsure.

It works well with the hardcoded discovery. I would like to use a python script now to simply execute some command line commands. I am a little bit struggling with how to set the environment variable programmatically in python. Maybe you can help me? I only can set a variable which is not a tuple (consisting of Mac Address and IP). The rest works really well. Or would you recommend that I call bash script using python? Does photons work also outside of a virtual enviroenment?

ok, being able to control the light is good, so something is blocking broadcast. But hard coding should be fine :slight_smile:

You can also put the configuration in a configuration file

by default it looks for a lifx.yml in the directory you are running your command from. Or you can override by setting a LIFX_CONFIG environment variable.

with the HARDCODED_DISCOVERY environment variable, you’ll need to wrap that in single quotes.

i.e.

HARDCODED_DISCOVERY='{"d073d5001337": "192.168.1.130"}' python my_script.py

But yaml will be easier,

So if you’re directory looks like

scripts/
    lifx.yml
    my_script.py

And you’re in the scripts folder, you can run python my_script.py and it’ll find lifx.yml and use that.

Then lifx.yml would have

---

discovery_options:
  hardcoded_discovery:
    d073d5001337: 192.168.1.130

The virtualenv is optional, you can install photons globally. But the virtualenv means you are isolating photons from the rest of your system.

if you’re in that scripts folder, you can do

$ python3 -m venv .photons
$ source .photons/bin/activate
$ pip install lifx-photons-core
$ python my_script.py

The documentation explains how to write scripts but don’t worry if I explain that part of photons poorly in the docs :slight_smile:

For example, if you want a script to print the labels of your devices

from photons_app.actions import an_action

from photons_messages import DeviceMessages


@an_action(needs_target=True, special_reference=True)
async def display_label(collector, target, reference, **kwargs):
    async for pkt in target.send(DeviceMessages.GetLabel(), reference):
        print(f"{pkt.serial}: {pkt.label}")

if __name__ == "__main__":
    __import__("photons_core").run('lan:display_label {@:1:}')

What do you want your script can do and I can help you write one and explain the different parts.

If you do want to use existing cli commands, then

scripts/
    lifx.yml
    run_commands.sh
    .photons

And run_commands.sh would have

#!/bin/bash

source .photons/bin/activate

lifx lan:attr _ GetLabel

And you would run

$ chmod +x run_commands.sh
$ ./run_commands.sh

The first command means you can execute the script like it was a program and as long as you have that #!/bin/bash at the top, your terminal will know to run it with bash

There’s a lot in this post so don’t be afraid to ask questions about any of it!

Thank you so much. It works fine now. I used the config file. I would recommend to set static ips in the access point. One remaining question:
Is there a list of existing commmand line commands in the documentation (e.g., parameter for the set_color etc.)? Thanks in advance.

Nice :slight_smile:

You’ll want this page https://photons.delfick.com/commandline/common_commands.html

lifx help will list what commands are available, but unfortunately it doesn’t really say much about how to use them. One day (in far future at this point) I’ll fix this.

Great, is it also possible to set the color as RGB values or just as yellow, blue, red, white, green?

yeap, it should support everything the http api supports - https://api.developer.lifx.com/docs/colors