HOWTO: Turning on and switching off lighthouses with a simple script
Page: 1/3»
  Go to:
scaine Oct 5, 2020
So for the past couple of months, whenever I feel like playing VR, I do a trek around the room, turning on my HMD, then both lighthouses. While I'll still have to manually switch on my HMD, it turns out there's a simple script that takes care of the lighthouses, via bluetooth, it's awesome!

Now, I'm on Manjaro, but I think this will work pretty seamlessly on just about any distro, because it's all installed using pip/python, and in just two simple commands, which we'll later turn into a launcher.

The script we'll be using is this one:
https://gist.github.com/prefiks

But we'll modify it slightly according to one of the comments, so that we can create that launcher for it. One click = lighthouses turn on, click again = lighthouses turn off. Honestly, I've hardly had to use my legs this whole weekend!

Here's the steps for Manjaro, and also Mint, wherever Mint is different.

Step One
If you haven't already, install Python's PIP manager.

Manjaro - pacman -S python-pip
Mint - sudo apt-get install python3-pip libglib2.0-dev

Step Two
Open a terminal and install the dependency for this script
Manjaro - pip install bluepy
Mint - pip3 install bluepy

Step Three
Give yourself rights to run bluepy without sudo:
sudo setcap 'cap_net_raw,cap_net_admin+eip' ~/.local/lib/python3.8/site-packages/bluepy/bluepy-helper
(note that you might have a different path here, for example if you're only on Python 3.7. Just backspace and tab it out to the correct bluepy-helper executable)

Step Four
Download this script, save it somewhere safe, then execute with python:

#!/usr/bin/python3

from bluepy import btle
import sys

class DiscoLH(btle.DefaultDelegate):

    def __init__(self):
        self.devices = []
        btle.DefaultDelegate.__init__(self)

    def handleDiscovery(self, dev, isNewDev, isNewData):
        if not isNewDev:
            return

        isLH = False
        name = ""

        v = dev.getValue(btle.ScanEntry.MANUFACTURER)

        if v is not None and v[0:4] == b'\x5d\x05\x00\x02':
            print('Found LightHouse %s: address = %s' %
                  (dev.getValue(btle.ScanEntry.COMPLETE_LOCAL_NAME), dev.addr))
            self.devices.append(dev.addr)

if __name__ == '__main__':
    scanner = btle.Scanner()
    delegate = DiscoLH()
    scanner.withDelegate(delegate)
    scanner.scan(2)
    for device in delegate.devices:
        lh = btle.Peripheral()
        print("Connecting to %s" % (device))
        lh.connect(device, addrType = btle.ADDR_TYPE_RANDOM)
        if len(sys.argv) > 1 and sys.argv[1] == 'on':
            lh.writeCharacteristic(0x12, b'\x01')
        elif len(sys.argv) > 1 and sys.argv[1] == 'off':
            lh.writeCharacteristic(0x12, b'\x00')
        else:
            # toggle
            current = lh.readCharacteristic(0x12)
            if(current) == b'\x00':
                # turn on
                lh.writeCharacteristic(0x12, b'\x01')
            else:
                # turn off
                lh.writeCharacteristic(0x12, b'\x00')
        lh.disconnect()


I called mine vr.py and I ran it with

Manjaro
python vr.py

Mint
python3 vr.py

You can also force an option with
python3 vr.py on
or
python3 vr.py off
but if you run it without arguments, it's just a toggle.

That's pretty much it. I created a simple launcher, pinned it to my toolbar, and now whenever I click on it, my lighthouses toggle between powered up and powered down.

Hope this helps someone!

Last edited by scaine on 20 October 2020 at 11:03 pm UTC
Corben Oct 5, 2020
I got linked to this gist last week, which is a fork of prefiks lh.py script. But looks like the toggle option got already added in the original.

Haven't tried it myself yet, as when I have to prepare the greenscreen, I can also turn the lighthouses on. And turn them off, when I stow it away again ;)
Julius Oct 5, 2020
One would think (if it is that easy) this function could be added to the SteamVR client... /s

Does it work both with Lighthouse 1.0 and 2.0?
scaine Oct 5, 2020
Quoting: JuliusOne would think (if it is that easy) this function could be added to the SteamVR client... /s

Does it work both with Lighthouse 1.0 and 2.0?

I only have the latest lighthouses, so I can't say for certain. But the project is over a year old so... maybe? Hopefully someone can test and report back.
Julius Oct 5, 2020
Ah, I still have the Lighthouse 1.0 so I will try it later today I guess.

Edit: sorry didn't get around doing it, and are now on a longer work related travel. Maybe next month.

Last edited by Julius on 9 October 2020 at 7:17 pm UTC
scaine Oct 5, 2020
Quoting: CorbenI got linked to this gist last week, which is a fork of prefiks lh.py script. But looks like the toggle option got already added in the original.
Yep, but I like the comment-version above, because it gives you both explicit "on" and "off" options!

Quoting: CorbenHaven't tried it myself yet, as when I have to prepare the greenscreen, I can also turn the lighthouses on. And turn them off, when I stow it away again ;)

Pfff. You're not being anywhere near lazy enough.
Ehvis Oct 5, 2020
Nice! It's something I meant to look into, but never bothered with. Now I won't have to . Should hook the script up to auto detect SteamVR so it won't need manual intervention. There are a few other thing I'd want to add to it like setting some Nvidia settings and maybe forcing the sound output (switching is better these days, but it won't always switch back).
scaine Oct 5, 2020
Quoting: EhvisNice! It's something I meant to look into, but never bothered with. Now I won't have to . Should hook the script up to auto detect SteamVR so it won't need manual intervention. There are a few other thing I'd want to add to it like setting some Nvidia settings and maybe forcing the sound output (switching is better these days, but it won't always switch back).

I have no idea how you'd auto-detect SteamVR starting... but I love the idea of use a pavucontrol command or similar to set/unset the sound sink. I'll have a look into that, definitely.

I wouldn't bother much if Pulseaudio was intelligent enough to choose my previous sound card when I exit SteamVR... but oh no... it choose my Yeti Microsoft "speaker" every goddam time. Pulseaudio really sucks pretty hard, given how old it is now. Nothing is simple and I STILL have to edit a configuration file just to get it to choose my primary sound card on start up!

Switching sound outputs shouldn't be this hard.
Ehvis Oct 5, 2020
Quoting: scaineI have no idea how you'd auto-detect SteamVR starting... but I love the idea of use a pavucontrol command or similar to set/unset the sound sink. I'll have a look into that, definitely.

SteamVR starts a few processes (vrcompositor and such) that can be detected. Don't have a suggestion right now, but I'm sure there are a few options for detecting when certain processes appear and disappear so you can trigger a script.
scaine Oct 5, 2020
Actually, thinking about it, it's probably easier just to modify the Steam properties launch script to something like

python /path/to/vr.py ; %command%

That works a treat, but of course you still need to execute your vr.py again after closing SteamVR, in order to shutdown the lighthouses again. So a script that detects this stuff would still be pretty cool.

I did try
python /path/to/vr.py ; %command% ; python /path/to/vr.py
but it doesn't work, even with a wait in there, the whole line is executed immediately, because it appears that Steam is forking its games off as a separate process, without waiting for their exit codes.
Blu3ye Oct 9, 2020
Hi there!

I just came here from Twitter and tried that script.
I get no error messages but it doesn't do anything either. Whether I am running SteamVR or not.

HTC Vive on Manjaro Linux.
While you're here, please consider supporting GamingOnLinux on:

Reward Tiers: Patreon. Plain Donations: PayPal.

This ensures all of our main content remains totally free for everyone! Patreon supporters can also remove all adverts and sponsors! Supporting us helps bring good, fresh content. Without your continued support, we simply could not continue!

You can find even more ways to support us on this dedicated page any time. If you already are, thank you!
Login / Register


Or login with...
Sign in with Steam Sign in with Google
Social logins require cookies to stay logged in.