Case study: How to build a smart relay

A relay is an electrically operated mechanical switch. Relays are used where it is necessary to control a high-power circuit by a separate low-power signal, or where several circuits must be controlled by one signal. A relay becomes smart if some kind of computing device controls it.

Here you can learn how to build a smart relay by installing Ozeki 10 on your Raspberry Pi, so you can control your smart relay from a remote location or directly with a help of a button.

What will you learn?

  • How to build your smart relay
  • How to install a Raspbian OS on your Raspberry Pi
  • How to install Ozeki 10 on your Raspberry Pi
  • How to test the components after installation

    Smart relay overview

    A smart relay is a computer controlled relay. You will learn how to build a smart relay to control your relay over the internet. It is also an effective way to test your Ozeki 10 on your Raspbian OS, which is the operating system designed for your Raspberry Pi. Ozeki 10 is capable to manage every GPIO pin of your Raspberry Pi.

    A red and green LED are wired to GPIO pins. The red LED shows if the relay is latched off and the green LED shows if the relay is latched on. These LEDs are controlled by Ozeki 10. The GPIO pins are set to output mode for each LED.

    A button is also connected to a GPIO pin, which is set to input mode, so you can control the relay without internet connection with a press of a button. Even this small function is driven through Ozeki 10, which silently performs and logs every activity from users or applications.


    Wiring a relay to your Raspberry

    Required components for wiring your smart relay

    • Raspberry Pi 1/2/3
    • Copper wires
    • 4 * 1 kΩ resistor
    • 104 ceramic capacitor (100 nF)
    • LEDs (green and red)
    • Button
    • Logic Level Converter (more details...)
    • SRD-5VDC-SL-C relay board

    wiring relay and other useful components to a raspberry pi
    Figure 1 - Wiring a relay and other useful components to a Raspberry Pi

    On figure 1 you can see an easy method to build a smart relay. The red and green LEDs and the button are wired to GPIO pins set to Active-Low state, which means the LEDs will glow if the GPIO output is LOW and the button is pressed if the input is LOW as well.

    General components that are required

    GPIO pins used in this smart relay project

    • 2: Green LED (Active-Low)
    • 3: Red LED (Active-Low)
    • 4: Button (Active-Low)
    • 17: Logic Level Converter -> SRD-5VDC-SL-C relay board

    The high state of a GPIO pin is 3.3V, but the high state of the SRD-5VDC-SL-C relay board needs to be 5V, so a Logic Level Converter is needed to convert the 0V-3.3V logic level to 0V-5V logic level.


    Installing Raspbian Stretch Lite on your Raspberry Pi

    Raspbians are Linux based operating systems booting on any Raspberry Pi. A Raspbian must be uploaded to a Micro SD card before placed into a Raspberry as shown from 'Step 1'. The Micro SD card in your Raspberry Pi has the same functionality as a Hard Drive in your computer. In the following steps you can learn how to format your Micro SD card and upload a Raspbian to it, so you can boot your Raspberry Pi with the inserted Micro SD card.

    Step 1: Insert Micro SD card into your machine and format it

    First select the proper Micro SD card (Figure 1) for your needs. You are advised to use a minimum 4 GB card for Raspbian Stretch Lite or a 8 GB for a Raspbian Stretch with Desktop.

    selecting your microsd card
    Figure 2 - Selecting your Micro SD card

    Insert your Micro SD card into your card reader (Figure 3) and plug the reader into your computer's USB slot. It will be detected as a Mass Storage Device.

    micro sd flash memory card
    Figure 3 - Micro SD Flash Memory Card Reader

    For formatting it (Figure 4), you are advised to use the SD Memory Card Formatter for Windows or for Mac.

    formating a microsd card
    Figure 4 - Formatting a Micro SD card

    Step 2: Download and Unzip a Raspbian

    Please select an operating system from the official Raspberry Pi website and download it. Here you can see a Raspbian Stretch Lite written on a 4 GB card, but for the Raspbian Stretch with Desktop a 8 GB Micro SD card is advised.

    After unzipping the downloaded file you should see the image file (Figure 5).

    image of a raspbian stretch lite
    Figure 5 - Image of a Raspbian Stretch Lite

    Step 3: Write image on your Micro SD card

    Please download an image burner software compatible with your operating system to upload the Raspbian image onto your Micro SD card (Figure 6). If you use Windows please download Win32 Disk Imager.

    wiring a raspbian pi
    Figure 6 - Writing a Raspbian Stretch Lite image onto a Micro SD card

    After the Raspbian image has been successfully written on the selected storage device, you should see an automatically generated pop-up window suggesting a full format. Please click 'Cancel' to avoid this pop-up (Figure 7).

    canceling disk format
    Figure 7 - Canceling disk format after the image has been written

    Now you should see the original pop-up window of the Win32 Disk Imager software acknowledging the image was successfully written (Figure 8). Click 'Ok' and close the Win32 Disk Imager software.

    image writing finished
    Figure 8 - Image writing has been finished

    Step 4: Enable remote command line to your Raspberry Pi

    Remote command line helps you to communicate with your Raspberry Pi through an SSH protocol over the internet. Every Raspbian boots with SSH turned Off, unless you put a file on the bootable Micro SD card, which turns On SSH. Please create an empty file with no extension. The file must be called 'SSH'. Copy the 'SSH' file to the 'boot' partition of your Micro SD card (Figure 9). You must do this before inserting the card into your Raspberry Pi, so you can access your Pi using the remote command line from an SSH client. You will see how to connect with your Pi from 'Step 6'.

    copying shh file to the boot
    Figure 9 - Copying 'SSH' file to the 'boot' partition of the Micro SD card

    Step 5: Insert Memory Card into Raspberry and Power it Up

    Now you can unplug the Memory Card Reader from your computer and insert the Micro SD card into your Raspberry Pi (Figure 10).

    inserting microsd card
    Figure 10 - Inserting Micro SD card into Raspberry Pi

    Please plug the Micro USB power source and LAN cable into your Raspberry Pi (Figure 11), so you can watch how your Raspberry Pi powers up. You will be able to connect to it from anywhere.

    inserting power source and lan cable
    Figure 11 - Inserting power source and LAN cable into Raspberry Pi

    Step 6: Find IP address of your Raspberry Pi

    You will need the IP address to connect to your Raspberry Pi over the internet with a safe SSH connection. Without IP address you wouldn't be able to find your device. SSH connection is very useful for controlling your Raspberry Pi over the remote command line. For example you will be able to find out how to install Ozeki 10 on your Pi later in this tutorial.

    Two options to find IP:

    Use your router's web-based utility:
    This option needs nothing, just you. You need to login in to your router by using your router's IP address. On your router's web-based utility you need to find the IP address of your Raspberry Pi. The last registered IP should be the address you are searching for. You can most likely find it in the routing table.

    Find it from your Raspberry Pi's terminal:
    This option requires more hardware. You need to connect your computer screen with a HDMI cable to your Raspberry Pi and connect an USB keyboard to it as well, so you can ask it's own IP address by typing 'ifconfig' in the Raspberry Pi's command line. 'ifconfig' is used for viewing or changing the configuration of network interfaces.

    Step 7: Login to your Raspberry Pi's terminal using SSH connection

    Remote command line helps you to communicate with your Raspberry Pi through an SSH protocol over the internet. Please download an SSH client to login to the Raspbian. PuTTY is advised for SSH access through Windows. Please download PuTTY from the organisation's official website. With the help of the IP address of your Raspberry Pi you have previously figured out, please create an SSH connection to port 22 (Figure 12).

    ssh connection to port 22
    Figure 12 - SSH connection to port 22 of your Raspberry Pi by using PuTTY

    Now it is time to login to the Raspbian operating system (Figure 13).
    Username: pi
    Password: raspberry

    logging into raspberry pi
    Figure 13 - Logging into your Raspberry Pi

    You can change your default password by using the 'passwd' command (Figure 14). It is suggested because anyone can access your Raspbian who finds out your IP address. Please memorize your new password or write it down on a paper.

    change raspberry pi password
    Figure 14 - Changing the password to your Raspbian


    Installing Ozeki 10 on your Raspberry Pi

    Ozeki 10 is a perfect solution to manage your Raspberry Pi by helping you to control and check the status of the Raspberry Pi it is installed on. Users and applications can communicate with your Raspberry's GPIO pins, so basicly you can manually control these pins or write applications to control them.

    Step 1: Before installing Ozeki 10 please install mono-complete

    Please download and install the mono-complete debian package which includes tools for both developers and users. You are advised to follow the installation process described on www.mono-project.com, although if needed you can follow the process below with the help of some screenshots.

    First use the 'apt-key' command (Figure 15), which is used to manage the list of keys used by apt to authenticate packages. Packages which have been authenticated using these keys will be considered trusted. For example the mono-complete package can be authenticated and considered trusted.

    setting mono-complete apt package key
    Figure 15 - Setting mono-complete apt package key

    After setting the key you need to add 'mono-official.list' to your package list (Figure 16).

    downloading mono-official package list
    Figure 16 - Downloading mono-official package list

    Update packages to prepare the installation of the most recent mono-complete version (Figure 17).

    updating mono-complete packages
    Figure 17 - Updating mono-complete packages

    Finally you should start installing mono-complete (Figure 18).

    installing mono-complete
    Figure 18 - Installing mono-complete

    The installer will ask you if you have enough storage space for mono-complete. Press 'y' and hit ENTER to continue. You can also be asked if you would like to install mono-complete packages without verification (Figure 19). As you can see the authentication key was not created in Figure 15, so the package cannot be verified, although mono-complete can be successfully installed if you press 'y'.

    installing without verification
    Figure 19 - Installing without verification

    Step 2: Installing packages for Ozeki 10

    Before installing Ozeki 10 you will be required to install some packages
    with the following commands:
    sudo apt-get install libturbojpeg0 (Figure 20)
    sudo apt-get install libportaudio0 (Figure 21)
    sudo apt-get install libspeexdsp1 (Figure 22)
    

    installing libturbojpeg
    Figure 20 - Installing 'libturbojpeg0' package

    installing libportaudio package
    Figure 21 - Installing 'libportaudio0' package

    installing libspeedxdsp1 package
    Figure 22 - Installing 'libspeexdsp1' package

    Step 3: Installing Ozeki 10

    Please download the latest Ozeki 10 debian package by using the 'wget' command (Figure 23). The download url is http://www.ozeki.hu/index.php?owpn=5657&platform=arm

    The wget command should look the following way
    wget -O [file name] '[download URL]' like
    wget -O Ozeki10.deb 'http://www.ozeki.hu/index.php?owpn=5657&platform=arm'

    'wget' will download the debian package in a file. In this example the package file is called 'Ozeki10.deb'

    downloading the latest ozeki debian package
    Figure 23 - Downloading the latest Ozeki 10 debian package

    Install the Ozeki 10 debian package with the 'sudo dpkg -i Ozeki10.deb' command, where '-i' stands for install. When the installer asks you if you would like to overwrite the user configuration, please choose yes ('Y') (Figure 24) to create an administrator. The default username for the administrator is 'admin' and the password can be anything.

    creating and administrator user config
    Figure 24 - Creating an administrator user configuration

    Please set the password for the 'admin' user and hit ENTER to continue (Figure 25). In the example below it is set to 'admin'. Later on you can use the 'admin' username with the selected password to log in to Ozeki 10.

    setting administrator password
    Figure 25 - Setting administrator password

    ozeki 10 installation finished
    Figure 26 - Ozeki 10 installation has been finished

    If successfully installed (Figure 26), you can start Ozeki 10 service with the 'service ozeki start' command (Figure 27).

    starting ozeki service
    Figure 27 - Starting Ozeki service

    After a few minutes you can use it from any webbrowser (Figure 28) by typing in your Raspberry Pi's IP address and the default port number of your Ozeki 10: 9505. If you use it from a local area network the beginning of the IP should be 192.168.???.???, while you need to figure out the ending using these methods. You can also find out the global IP address of your Raspberry by knowing the IP address of your gateway.

    ozeki login screen
    Figure 28 - Ozeki 10 login screen. You can find out the IP address using these methods


    Separately test your wired components after Ozeki 10 installation

    Please check if the wires in your circuit are properly connected to the appropiate GPIO pins of your Raspberry Pi as seen in this wiring diagram. Every pin is defaulty set as a 0V output, which means the LEDs will glow, since they are wired in active-low, and the relay will turn on as well. You can test these output pins with the easy to use test interface. In the next few steps you will read about how to navigate to this test interface and see how to use it.

    Step 1: Log in to select 'Control Panel' from 'Start' menu and add a 'Raspberry Pi' connection

    See how to navigate from the login screen (Figure 28 above) to the test interface.

    Please log in with the following details:
    username: admin (default)
    password: You have choosen it during Ozeki 10 installation.

    Please start 'Control panel' from the Start menu of Ozeki 10 (Figure 29).

    starting control panel from start menu
    Figure 29 - Starting 'Control panel' from Start menu

    On the left side panel please click on 'Connections' and tap the a blue 'Create new Connection' button to choose Raspberry Pi from the list of connections (Figure 30).

    selecting raspberry pi connection
    Figure 30 - Selecting Raspberry Pi connection

    A default connection name is already filled. If it differs from the name you wish to give to your Raspberry Pi, you can modify it. Click 'Ok' to create new Raspberry Pi connection (Figure 31).

    raspberry pi connection
    Figure 31 - Creating Raspberry Pi connection

    Now you should see the new connection appearing in the connections table. Please click the blue 'Details' button next to the connection (Figure 32). It will lead you to the configuration and test GUI of your Raspberry Pi.

    choosing raspberry pi details
    Figure 32 - Choosing Raspberry Pi details

    Step 2: Try out the relay and the LED's with the test interface

    Select 'Test' tabpage to reach the test interface (Figure 33).

    selecting the test tabpage
    Figure 33 - Selecting the 'Test' tabpage

    The test interface (Figure 34) was designed to easily control the output pins and show input pin values. In the screenshot below you can see how the relay and the two LEDs are turned off by setting the 2, 3, 17 GPIO pins HIGH. This works because these components are wired in active-low as shown in this wiring diagram.

    using the test interface
    Figure 34 - Using the test interface

    Step 3: Test the button

    To use the button on GPIO pin 4 you need to change pin to input mode by selecting the 'Configure' tabpage (Figure 35). Select GPIO4 pin. The button is wired in active-low such as the LEDs, which means if it is pressed the input should be LOW and if it is released than the input value should change to HIGH. So please set the button to input mode and active-low. You are recommended to choose 'Input Both' to react to both edges.

    In input mode, events can be generated by edges. In electronics, a rising edge is a transition in a digital signal from LOW to HIGH (0 to 1). The falling edge is a transition from HIGH to LOW (1 to 0). By setting it to both, events are generated on both rising and falling edges.

    setting gpio4 to an input which watches both rising and falling edge
    Figure 35 - Setting GPIO4 to an input which watches both rising and falling edge

    Go back to the 'Test' tabpage (Figure 36). This way you can see if the state of the button is unpressed (Figure 36) or pressed (Figure 37).

    going back to the test interface to see the button pressed
    Figure 36 - Going back to the test interface to see if the button is pressed

    In this example Ozeki 10 reacts to both edges, since the input was configured for both rising and falling edge.

    button pressed which is wired the following way
    Figure 37 - Button pressed which is wired the following way

    The current setup let's you to control the LEDs, relay and button. They are depending on eachother. Program your Ozeki Robot Controller to set them up.

    More information