LEGO Smart Switch

Introduction

 

In this demo, we will showcase QikEasy’s Virtual Wireless Sensor feature by using it to create a LEGO smart switch. As with any smart switch, it will be voice controllable and we have chosen to use Google Home to complete this build as we have shown the integration with Amazon Alexa in the Alexa Control page, if you haven’t seen it before, you can visit it here. We will also be needing a number of free online services to complete this task. Let’s begin by taking a look at the command flow as well as the hardware and software components required:

 

  1. Google Home for receiving the voice command.
  2. IFTTT (If This Part) as the triggering point from Google.
  3. IFTTT (Than This Part) for starting a online web call.
  4. Google Script for managing the web call and to send two time delayed web calls to the home router.
  5. Home router for port forwarding the web call to the QikEasy Adapter within the home netowrk.
  6. QikEasy Adapter for receiving the online command and translating the command for the LEGO LPF2 Hub.
  7. LEGO Build to press the ON or OFF button on the wall switch.

 

We will also be showing how to use the  Android MacroDroid App to control the LEGO switch via local network. Let’s get started.

 

 

 

 

Architectural Overview

 

Here’s the flow chart to show the relevant components and how data flows between them. In the sections below, we will be describing how to build up the functioning Lego Switch Robot step by step from the bottom up.

 

Required Materials

 

To follow this tutorial, you must prepare these before starting the project:

 

    • Your LEGO Spike Prime or Robot Inventor system.

 

    • An Android phone ( If you want to use the Macrodroid App to control the LEGO switch via the local network ).

 

    • An Android or iPhone to setup the connection between Google Home and IFTTT.

 

    • Your QikEasy Adapter must be setup to run in Virtual Wireless Sensor mode.  See instructions on how to set that up here.

 

    • A Google Home Smart Speaker, we are using a Google Home Mini and you may choose other models.

 

    • A computer to set up Google Script.

 

    • A google account for  setting  up  Google  Home,  Google  Script.

 

    • An IFTTT account.

 

    • You must also know the external IP address of your internet setup.

 

    • A longer screw (18mm) for mounting the Lego Switch Robot onto the base switch. The screws that come with the switch plate is too short and it would not work.

 

 

Your smartphone, the computer and the QikEasy Adapter should all be connected to the same Wifi network.

Building your LEGO Robot

 

Please refer to the YouTube video below for detailed instructions on how to build the Lego Switch Robot and to install it on the wall switch.  There is risk on working around electricity. The example here is only to demonstration the functionality of the QikEasy Virtual Sensor.

 

DISCLAIMER:  You should be only proceed with the installation of the LEGO smart switch on the wall if you are comfortable working with electricity. If you are not the home owner or under the age of majority, you must consult the home owner before proceeding. The tutorial here is for demonstration purpose only and we do not in any way recommend or promote the use of LEGO robot as a light switch. We will not be responsible for any injury or damage.

Program running on LEGO Hub

 

Before proceeding with the LEGO Word Block program, we have assumed that you have put your QikEasy Adapter into virtual sensor mode, if you haven’t done so, you can review the setup procedure here. The LEGO Work Block program will interpret the force received from the virtual adapter and control the lever to press on the underlying switch base on the force received.

 

The Word Block Program

 

There are 3 actions the program will preform.

 

  1. Rotate the lever bar to press the upper switch button of the rocker switch; then return to home position if the force received is greater than 50.
  2. Rotate the level bar to press the lower switch button of the rocker switch; then return to home position the force received is less than 50.
  3. Stay in the home position if the force received is exactly 50.

 

The program also accepts the left and right button to press the switch ON or OFF once when pressed.

 

Please also note that the force required to press the switch as well as the height of the switch various across installations. You may need to adjust the rotation angle and the force for your motor in the program. Our setting may not work on your switch. Please also note that the medium motor may not be powerful enough to control your switch. When we added a rubber band on the lever bar to reduce the wear and tear between the 2 hard plastic of the switch and the LEGO bar; we found that the medium motor installed is no longer powerful enough to press the switch. We have to remove the rubber band for the switch to work.

 

The logic for our Word Block program is very simple:

    • When the Right button is pressed, rotate the motor to 30 degree then back to 0 degree. This is to press the ON button of the light switch.
    • When the Left button is pressed, rotate the motor to 328 degree (-32) then back to 0 degree. This is the press the OFF button of the switch.
    • In the main loop; first go to the middle position ( 0 degree ).
    • If the force received is exactly 5 Newton; then wait for 0.3 second and then check again
    • If the force received is larger the 5 Newton, simulate Right button pressed (ON). Wait for 0.5 second.
    • If the force received is smaller the 5 Newton, simulate Left button pressed (OFF). Wait for 0.5 second.
    • Later we will see that the Google script will set the force to bigger or smaller than 5N; delay a bit; and then revert back to a force setting of 5 Newton.
      • Therefore the loop polling time is set to 0.3 to ensure a fast response and to make sure can be triggered within the 1 second interval.
      • The delay time of 0.5 second after the lever movement is to reduce the possibility of pressing the button more than 1 time.

 

 

 

 

 

Testing the Robot using a URL:

 

The following 3 commands are used to test the Word Block Program. Please note that the “YourVirtualAdapterIPAddress” is the local IP address of the QikEasy Virtual Sensor.

 

“http://YourVirtualAdapterLocalIPAddress/set?t=f&f=60” — This command set the force setting of the virtual sensor to 6 Newton; upon receiving this value, the program will control the motor to press to press the light switch button to the ON position approximately every second.

 

“http://YourVirtualAdapterLocalIPAddress/set?t=f&f=40” — This command set the force setting of the virtual sensor to 4 Newton; upon receiving this value, the program will control the motor to press to press the light switch button to the OFF position approximately every second.

 

“http://YourVirtualAdapterLocalIPAddress/set?t=f&f=50” — This command set the force setting of the virtual sensor to 5 Newton; upon receiving this value, the program will return the lever to the centre position and wait for the next force command.

 

 

Tuning the Robot:

 

There are 3 parameters you can tune:

 

  1. On position of 30 degree. Depends on the physical set-up of your light switch, this may be larger or smaller.
  2. Off position of 328 degree. Depends on the physical set-up of your light switch, this may be larger or smaller.
  3. Wait time of 0.5 second. If you found that your switch is press more than once with Google home, you may increase this slightly to correct the issue.

 

 

Making the Word Block program visible to the outside world:

 

You will have to consult your router menu on how to port forward an external port to the http port of the QikEasy virtual wireless adapter (port 80).  Please note that in our example; our external IP address is 154.20.239.19 and the external port we have chosen to forward to the virtual sensor is port 5566. Please note that some Internet Service Provider (ISP) may block your inward ports and the example here will not work for you. You will have to consult your ISP to see if they have blocked the ports for security concerns.

The Google App Script

 

The Google Apps Script is responsible for creating a single URL that will do the following 2 commands in sequences. First, the push button signal and Second the return to home position signal after some time. You will need a google account to complete this step. The following video above shows the setup process. The code is also capture below for reference. Please note that the code will send the force of 6N; then wait for 0.3 second and final send the force of 5N upon receiving the “ON” command. In addition, it will send the force of 4N; then wait for 0.3 second and final send the force of 5N upon receiving the “OFF” command. This step is completed on a computer.

 

function doGet(e) {
  // Parse the query parameters from the URL
  var params = e.parameter;
  var result = "OK";

  // Make a HTTP request to the specified endpoint
  if (params && params.set === "ON") {
    let resp = UrlFetchApp.fetch("http://154.20.239.19:5566/set?t=f&f=60");
  }
  else if (params && params.set === "OFF") {
    let resp = UrlFetchApp.fetch("http://154.20.239.19:5566/set?t=f&f=40");
  }
  else {
    result = "ERR";
  }

  if ( result === "OK") {
    // Delay by 1 second
    Utilities.sleep(500);
  
    // Return the switch to idling state
    let resp = UrlFetchApp.fetch("http://154.20.239.19:5566/set?t=f&f=50");
  }
    // Return the data as plain text output
  return ContentService
    .createTextOutput(result)
.setMimeType(ContentService.MimeType.TEXT);
}

 

 

Please note that you will have to replace the IP address “154.20.239.19:5566” with your own external IP address and port. We are not covering the port forwarding details here as it is router specific; you will have to consult your routers user manual. You may also need to check with your ISP to see if the inbound ports are blocked.

 

 

Testing The Google Apps Script

 

As shown on the video below, you can test the Google Apps Script by appending the strings,”?set=ON” and “?set=OFF”, to the Web App URL copied from the deployment step.

 

 

IFTTT Configuration

 

This is the triggering point from the Google Voice Speaker.  IFTTT is the hook between the Google Voice Speaker and the Google Apps Script. On the “IF THIS” part, it tells the Google Voice Speaker the command to listen for; in our case, we are using 2 separate command to turn ON and OFF the LEGO Smart Switch — “Activate Lego Switch ON” and “Activate Lego Switch OFF”. These 2 voice command will then trigger the 2 Google Apps Scripts that we just set up above. Please note that the IFTTT only set up the voice command that Google Speaker should be listening for; there is actual one more step for connecting IFTTT to the Google account that should listen for the voice command.

 

The combined command that you need to put in the “THAN THAT” part of IFTTT is obtained from the Google Apps Script setup. Make sure you remember to add the “?set=ON” or “?set=OFF”.  Please see the video below for configuration detail on how to set up the IFTTT on Android smart phone.

 

 

 

Setting Up the Connection between Google Home and IFTTT

 

Now we have prepared all the pieces for the Lego Smart Switch, the final step is give the Google Voice Speaker something to “Works With”.  We are doing this on a Android phone as it is easier to navigate; we start by going into the settings of Google Home and adding IFTTT as something that “Works with Google”. Check out the video below for details.

 

 

Setting up MacroDroid for Quick Local Access

 

If you want quicker response time to your Lego Smart Switch, you can use an Android applications that is similar to the iPhone’s “Shortcuts”. The Android Application “MacroDroid” can issue command into the local network sending force setting to the QikEasy virtual sensors. The following video will show you how that is done.

 

 

 

 

 

Challenge Extensions

 

These are some of our suggestions for extending this project:

 

  • Updating the marco in MarcoDroid to work outside of your local network
  • Modify the switch to work with toggle type of switch
  • Modifying the Lego Word Block program to use a different type of virtual sensor. Be sure to update the Google Apps Script and the MacroDroid Actions to work with the new virtual sensor type

Conclusion

 

QikEasy Adapter’s Virtual Wireless Sensor provides boundless integration opportunities with all sorts of data sources available over the network.  This project presents only one of its possible use.  You may visit our Virtual Wireless Sensor documentation page for more fun and interesting ideas.