Web based BEVRLink relay control using Raspberry Pi and Bottle.py

Web based BEVRLink relay control using Raspberry Pi and Bottle.py

For this we have connected a Raspberry Pi 5 to the BEVRLink Raspberry Pi 5 8 Channel Relay 12V. With this relay module board you need the BEVRLink Power Supply 12V 2A. As addional boards BEVRLink 8 Channel Relay V1 EXPANSION and BEVRLink 4 Channel Relay and 4 manual test buttons 5V were connected.

This guide works for BEVRLink Raspberry Pi 5 4 Channel Relay, just omit the relay 5-8 and the status LED from the code. With this relay module you can either power the Raspberry Pi directly or back feed it from a connected BEVRLink relay module through the Expansion Connector.

Optional for a safe setup we recommend to use case for both the manager and relay boards: BEVRLink Cases



Before we dive into the exciting world of home automation, let's make sure you have everything you need:

  • Raspberry Pi (with compatible OS installed)
  • BEVRLink Raspberry Pi 5 8 Channel Relay HAT
  • BEVRLink Relay boards
  • Address jumpers if a lot of extra boards are connected
  • BEVRLink Power supply
  • Example code <-Download

Step 1: Setting Up Raspberry Pi

Let's begin by setting up your Raspberry Pi. This will serve as the heart of your home automation system. Follow these steps:

  1. Install a compatible operating system (e.g., Ubuntu) on your Raspberry pi.
  2. Connect to your Wi-Fi network or Ethernet to enable remote access.
  3. Enable SSH for secure remote communication.
  4. Note down your Raspberry Pi's IP address.

Step 2: Wiring Relay Boards

Now, let's connect the relay boards to your Raspberry Pi. The relays will act as switches to control your IoT devices. Here's how to do it:

  1. Beginning with addressing each additional relay board
  2. Assemble the boards and manager board together
  3. Connect the loads to be controlled
  4. Connect the power supply to the relay board.
  5. Access the RPI over SSH

Step 3: Preparing the Python Script

Time to add some intelligence to your setup! We'll prepare a Python script to control the relay boards.

  1. Unzip the webcontrol code from the URL link in the beginning
  2. Install bottle.py
    sudo pip install bottle
  3. Run the script with
    sudo python3 webcontrol.py
  4. Navigate to the URL shown in the terminal to control your relays
Disclaimer: This demonstration is for educational purposes only. 
The code is developed with the help from chatGPT and is a
work in progress and might contain bugs. Feel free to use
the code for your own projects as is without warranties.
Always follow proper safety guidelines when working with
electronics and electrical components.

API Reference

For advanced users and developers, here's an API reference to interact with your home automation system programmatically:

GET http://{host_name}:8080/control_relay/<address>/<channel:int>/<state>

  • Description: Controls an I2C relay channel by setting its state (on/off).

  • URL Format:

    • http://{host_name}:8080/control_relay/<address>/<channel:int>/<state>
  • Parameters:

    • address (string or int): The I2C address of the relay expander (e.g., "0x20" or 32).
    • channel (int): The channel number to control (e.g., 1, 2, 3).
    • state (string): The desired state for the channel ("on" or "off").
  • Response:

    • 200 OK with a message indicating the new state of the channel.
  • Example Request:

  • GET http://{host_name}:8080/control_relay/0x20/1/on
  • Example Response:

  • Set channel 1 on relay expander at address 32 to state on

This updated API reference provides the URL format http://{host_name}:8080 for the control_relay function, allowing users to understand how to control relay channels through the web interface using the appropriate URL structure.

GET http://{host_name}:8080/list_relays

  • Description: Lists available I2C relay expanders and their configuration.

  • URL Format:

    • http://{host_name}:8080/list_relays
  • Parameters:

    • None.
  • Response:

    • 200 OK with a JSON object containing relay information.
  • Example Request:

  • GET http://{host_name}:8080/list_relays

  • Example Response:

  • { "relays": [ { "address": 32, "status": "configured", "channels": [ { "channel": 1, "status": "off" }, { "channel": 2, "status": "on" } ] }, { "address": 48, "status": "configured", "channels": [ { "channel": 1, "status": "on" }, { "channel": 2, "status": "off" } ] } ] }

This API reference provides the URL format http://{host_name}:8080 for the list_relays function, allowing users to retrieve information about available I2C relay expanders and their channel configuration through the web interface.