Remote Switch

In this project, we will learn to use Cloud4RPi’s Switch to control the relay, thereby controlling the relay’s load-LED. You can also change the load of the relay to household appliances, but with safety in mind.

Experimental Procedures

Build the circuit.


Open the code.

cd /home/pi/cloud4rpi-raspberrypi-python
sudo nano

Find the line below and fill in the correct device token.


Run the code.

sudo python3

Go to Cloud4RPi, add a new Control Panel named project2, and click Add Widget to add a Switch widget.


Once added, you can use the Switch widget to control the relay.


Code Explanation


Relay connected to GPIO18 of the T-expansion board.


Set to BCM nomenclature and set RELAY_PIN to output.

def relay_control(value=None):
    GPIO.output(RELAY_PIN, value)
    return GPIO.input(RELAY_PIN)

This function controls the relay according to the value and returns the current level of the relay.


Since the relay works at high level, the relay closes when the switch state is True and opens when the switch state is False.

variables = {
    'LED On': {
        'type': 'bool',
        'value': False,
        'bind': relay_control

By setting the value of the 'value' key, we can set the initial value of the Switch widget, then always read the value of the 'value' key and send the current level of RELAY_PIN (the value returned by the function relay_control()) to Cloud4RPi.