Build your own Clapper like switch [Homeyduino English version]

Build your own Clapper like switch [Homeyduino English version]

Who remembers this handy invention from the 80s-90s? The ‘Clapper’ switch. A switch that could turn devices on or off simply by clapping.

In the meantime surpassed by modern day technology, but at the time a futuristic piece of home automation. If you are not familiar with this, be sure to watch the legendary commercial:

https://youtube.com/watch?v=Ny8-G8EoWOw%3Ffeature%3Doembed

As a project today we will be making a modern version of the ‘Clapper’ this time, because although it is an old concept, there are many modern applications for it. Certainly when you link it to Homey via Homeyduino , you can not only use it to turn devices on or off, but also use it to start or stop complete smart home ‘flows’.

This project will run on both the Homey Pro as well as on the Homey Bridge.

Supplies: on Amazon or Aliexpress:

If you prefer to buy on Aliexpress, you can use these links:

Connection diagram  

Follow the diagram below to connect the sensors to your board:

SensorWemos D1 Mini pin
OUTD2
VCC5V
GNDGND
 
Homeyduino Clapper Switch connection diagram Fritzing |  Huisvandaag.nl
Connection diagram “Clapper Switch”

Arduino IDE

Install Arduino IDE, this is the software needed to put the code on your Wemos D1 Mini board. First we need to install the Homeyduino Library for this to work.

Open a new window, on the top bar click Sketch > Use Library > Manage Libraries.

Arduino IDE, first install the Homeyduino library |  Huisvandaag.nl
Arduino IDE, first install the Homeyduino library

In the screen that opens, search for Homeyduino and then click install.

Look up: ESP8266 and install this as well.

Wemos D1 Mini board

You probably need to install this board before you can use it, so you can do this as follows:

Go to: Tools > Board > Board Management.

Install board Arduino |  Huisvandaag.nl

In the search window you then search for Wemos D1 mini and install it.

Install board Arduino |  Huisvandaag.nl

Homeyduino Code

When you have followed the steps above, open a new Arduino IDE screen and replace the text with the code below. Make sure you enter the correct network data here (see red text)

If you plan to place multiple Sound sensors in your house, it is important that you give them all a unique name, so that Homey recognizes them as different sensors.


// Code for Homeyduino made by Smart Home Blog https://huisvanvandaag.nl. 
// Take a look at my site for more Homeyduino en other Smart Home projects.
// Original code: https://en.code-bude.net/2014/12/08/how-to-build-a-clap-switch-using-arduino/

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <Homey.h>

int soundSensor = D2;
int claps = 0;
long detectionSpanInitial = 0;
long detectionSpan = 0;
boolean ClapState = false;

void wifi() {
  if (WiFi.status() != WL_CONNECTED) {
    WiFi.begin("<SSID>", "<PASSWORD>");
    uint8_t timeout = 30;
    while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
      if (timeout<1) break;
    }
    if (WiFi.status() == WL_CONNECTED) {
      //Print IP address
      Serial.print("Connected to WiFi! (");
      Serial.print(WiFi.localIP());
      Serial.println(")");
    }
  }
}
 
void setup() {
 Serial.begin(9600); 
  Homey.begin("Sound sensor"); // pick a name for your sensor
  Homey.setClass("sensor");
  pinMode(soundSensor, INPUT);
 }
 
void loop() {
   wifi();
  Homey.loop();
  bool clap_on = !digitalRead(soundSensor); 
  bool clap_off = !digitalRead(soundSensor); 
 
  int sensorState = digitalRead(soundSensor);
 
  if (sensorState == 0)
  {
    if (claps == 0)
    {
      detectionSpanInitial = detectionSpan = millis();
      claps++;
    }
    else if (claps > 0 && millis()-detectionSpan >= 50)
    {
      detectionSpan = millis();
      claps++;
    }
  }
 
  if (millis()-detectionSpanInitial >= 400)
  {
    if (claps == 2)
    {
      if (!ClapState)
        {
          ClapState = true;
        Serial.println("Light on");
        Homey.trigger("clap_on",true);
        }
        else if (ClapState) 
        {
          ClapState = false;
        Serial.println("Light off");
        Homey.trigger("clap_off",false);
        }
    }
    claps = 0;
  }
}

Check if correct board is selected

Before we write the code to the board, we have to check if we have selected the right board in Arduino IDE, otherwise you won’t get it to work. In our case that is the “LOLIN(Wemos) D1, R2 and Mini”

When this is all correct, we click on the arrow in the icon bar (second icon from the left) > Upload.

Choosing the right board in Arduino |  Huisvandaag.nl
Selecting the right board in Arduino

Testing the code

Now that you have successfully uploaded the code, we will immediately test it. We do this by opening the Serial Monitor in Arduino IDE, click on Tools and then Serial Monitor or use the keyboard shortcut CTRL+SHIFT+M.

A screen will then open on which you can read the values ​​from the sensor. When you clap twice in a row this should be readable on the Serial Monitor. Also experiment with one or three claps, so you will see that the code is correct and will detect little or no false ‘claps’.

Serial Monitor Clapper Switch |  Huisvandaag.nl
You can test whether the sensor is working properly via the Serial Monitor. It should show: light on or light off after you clap.

Linking it to Homey 

You can now link the sensor to Homey via the Homey app.

Link Homeyduino sensor to Homey |  Huisvandaag.nl
Click on ‘Devices’
Link Homeyduino sensor to Homey |  Huisvandaag.nl
Click on ‘Homeyduino’
Link Homeyduino sensor to Homey |  Huisvandaag.nl
Click on ‘Arduino’
Link Homeyduino sensor to Homey |  Huisvandaag.nl
Then click on ‘Install’
Link Homeyduino sensor to Homey |  Huisvandaag.nl
Choose the right sensor, in our case ‘Sound Sensor’

Homey example flow

Homeyduino Clapper Switch Flow Setting |  Huisvandaag.nl
Select as ‘Trigger’ “Boolean”
Homeyduino Clapper Switch Flow Setting |  Huisvandaag.nl
And then enter ‘clap_on’ or ‘clap_off’ here. The first time the sensor detects clap, it switches the ‘clap_on’ and the second time ‘clap_off’.
Homeyduino Clapper Switch Flow |  Huisvandaag.nl
The ‘clap_off’ ‘Trigger’ allows you to set an action that you want to perform when the sensor detects a second clap. To do this, enter ‘clap_off’ as ‘Boolean trigger’.

Conclusion

Hopefully you’ve got your own ‘Clapper’ like switch working. If you know of any fun, crazy, funny or special applications for this, please leave a comment.

More projects coming soon! Sign up here so you don’t have to miss anything!

Mike | Huisvanvandaag

Geef een reactie

Het e-mailadres wordt niet gepubliceerd.

Aanbevolen
Dit keer wederom een nieuwe Multi sensor en wel een combinatie van een CO2-, licht-…
Translate »