What kind of cat loves raspberry?
…actually I know one, who is a huge fan of Raspberry Pi…it’s CHOCOLATE from the ConfigCat team 🙂
- Raspberry Pi 2/3 with:
- ConfigCat python SDK
- (‘GPIO Zero’ integration to any hardware) - optional
- ConfigCat subscription (free plan is enough :))
Step 0. – Prepare the device to run your code
I use my board with Raspbian OS but you can use any distribution. I prefer it because it (usually :)) contains all necessary components for my pet projects. Read more about Raspberry Pi OS installation here.
Install ConfigCat SDK with pip:
sudo pip3 install configcat-client
install gpiozero libs
By default the Raspbian desktop image contains the GPIO Zero package. On a different OS, you’ll have to use PIP:
sudo pip3 install gpiozero
Step 1. – Set up your ConfigCat project
Log in to ConfigCat
- Create a feature flag
- Copy your API key from the
connect your applicationstab.
Step 2. – Code
Create a file (e.g. remoteswitch.py) with your favourite editor (I prefer nano) and implement the following code:
from gpiozero import OutputDevice import time import configcatclient # set GPIO#3 port (you can select any GPIO output port) relay = OutputDevice(3) # initialize ConfigCat client to get your setting value client = configcatclient.create_client_with_manual_poll('YOUR_API_KEY') while True: # get the latest value client.force_refresh() # save into 'my_remoteswitch' variable my_remoteswitch = client.get_value('demoswitch', False) # for debug purpose only print(my_remoteswitch) # depend on the value the condition turns on/off the output if my_remoteswitch == True: relay.on() else: relay.off() time.sleep(5) pause()
In line 9 you should add your own APIKEY. This solution uses the ConfigCat SDK’s in manual polling mode. (Read more about polling modes here).
Step 3. – Test
Run your code with the following:
Depending on the value of setting the program prints the actual value of your switch on the ConfigCat Management Console and turns on/off the OutputDevice as well.
Step 4. – Demo
For the demonstration I created a simple circuit.
The components I have used:
- LED diode to show the switch status
- one resistor to limit the current (500Ω)
- two wires to connect to GPIO#3 port and a ground
- Raspberry Pi to serve power to the LED (you won’t need external power supply)
Step 5. – Further development possibilities
- Set up a program as a daemon (always run, after reboot)
- Apply this solution with any wireless power switch on your home