Introduction: Controlling a Livolo RF Light Switch Using a Raspberry Pi 3 B in Python

About: Currently living in San Francisco pursuing the nobel path of programming. Bringing analytics to small businesses, so they can make sure the hand wrapped organic cookies you order are the best they can be. How …

Controlling RF switches with a Raspberry Pi is a mainstay in home automation. I used this wonderful instructable to set up RF sockets in my room. However, controlling my Livolo light switch was more challenging. I spent a few days searching deep into google trying to find a simple solution. Since I couldn't find one, I made one. Enjoy :)!

Step 1: Wire Up a 433mhz RF Transmitter to Raspberry Pi

In my code we use GPIO 24, so either wire it to 24 or change the code to 22.

Step 2: Get the Python Code

https://github.com/midorineko/rpi_automation/blob/...

This is the livolo transmission code I use for home automation. It features an 'off' function which is all livolo lights off and one other frequency 'on' which is actually a lightswitch toggle.

However, in the code comments I provide many other possible frequencies, which can all be saved as variables and used if you need.

In case I change the file name or location the contents are bellow.

import time
import sys import RPi.GPIO as GPIO

off = '1242424352424342424242424242425342524342' on = '124242435242434242424242424242425243424242'

if sys.argv[1:] == 'off': NUM_ATTEMPTS = 1000 else: NUM_ATTEMPTS = 200

TRANSMIT_PIN = 24

def transmit_code(code): '''Transmit a chosen code string using the GPIO transmitter''' GPIO.setmode(GPIO.BCM) GPIO.setup(TRANSMIT_PIN, GPIO.OUT) for t in range(NUM_ATTEMPTS): for i in code: if i == '1': GPIO.output(TRANSMIT_PIN, 1) time.sleep(.00055); GPIO.output(TRANSMIT_PIN, 0) elif i == '2': GPIO.output(TRANSMIT_PIN, 0) time.sleep(.00011); GPIO.output(TRANSMIT_PIN, 1) elif i == '3': GPIO.output(TRANSMIT_PIN, 0) time.sleep(.000303); GPIO.output(TRANSMIT_PIN, 1) elif i == '4': GPIO.output(TRANSMIT_PIN, 1) time.sleep(.00011); GPIO.output(TRANSMIT_PIN, 0) elif i == '5': GPIO.output(TRANSMIT_PIN, 1) time.sleep(.00029); GPIO.output(TRANSMIT_PIN, 0) else: continue GPIO.output(TRANSMIT_PIN, 0) GPIO.cleanup()

if __name__ == '__main__': for argument in sys.argv[1:]: exec('transmit_code(' + str(argument) + ')')

# How to use:: I am putting this here, because it seems few people have figured out livolo switches. # Hold down livolo light switch for 5 seconds and wait for a beep. # Run ```python livolo.py on``` # Livolo light switch should beep again, signalling it is paired # Now running ```python livolo.py on``` will toggle the switch. # ```python livolo.py off``` is global to all switches and should work out of box.

# The following lines are possible RF codes which can be learned by the switch. # Just make them strings and save them to variables similarly to 'on' or 'off'. # I only have 1 switch so multiple RF freqs werent necessary. # 1, 2, 4, 2, 4, 2, 4, 3, 5, 2, 4, 2, 4, 3, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2 # 1, 2, 4, 2, 4, 2, 4, 3, 5, 2, 4, 2, 4, 3, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 5, 3, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2 # 1, 2, 4, 2, 4, 2, 4, 3, 5, 2, 4, 2, 4, 3, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 5, 3, 5, 3, 4, 2, 4, 2, 4, 2 # 1, 2, 4, 2, 4, 2, 4, 3, 5, 2, 4, 2, 4, 3, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 5, 3, 4, 2, 4, 2, 4, 2 # 1, 2, 4, 2, 4, 2, 4, 3, 5, 2, 4, 2, 4, 3, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 5, 2, 4, 3, 4, 2, 4, 2, 4, 2, 4, 2 # 1, 2, 4, 2, 4, 2, 4, 3, 5, 2, 4, 2, 4, 3, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 5, 3, 4, 2, 4, 2, 4, 2, 4, 2 # 1, 2, 4, 2, 4, 2, 4, 3, 5, 2, 4, 2, 4, 3, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 5, 3, 4, 2, 5, 3, 4, 2, 4, 2 # 1, 2, 4, 2, 4, 2, 4, 3, 5, 2, 4, 2, 4, 3, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 5, 3, 4, 2, 4, 2 # 1, 2, 4, 2, 4, 2, 4, 3, 5, 2, 4, 2, 4, 3, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 5, 2, 4, 2, 4, 3, 4, 2, 4, 2, 4, 2 # 1, 2, 4, 2, 4, 2, 4, 3, 5, 2, 4, 2, 4, 3, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 5, 2, 4, 3, 4, 2, 4, 2, 4, 2

Step 3: Teaching the Switch

Right now with the python script you should be able to safely turn off any and all livolo lights by running.

```python livolo.py off```

To get the switch toggle working we need to teach the switch one of our RF codes, in this case 'on'.

1) Hold the livolo gang down for ~5 seconds and you will hear a beep.

2) Enter ```python livolo.py on```

3) Hear a second beep

4) If you hear a beep, run ```python livolo.py on``` to toggle your switch on/off. Else repeat steps 1-3.

Step 4: Toggling the Livolo Switch

Depending on the power of your transmitter and the distance from your livolo switch you may have to raise or lower the number of attempts in the code.

Livolo unfortunately does not support a single ON function only a TOGGLE function and all OFF function.

Warning: Double clicks on lights are bound to happen since we are sending a code 200 times to a toggle switch. However, Livolo actual remote uses a similar crappy pattern.