Introduction: Light-Switch

In this project I will exemplify how to create a completely wireless light control system with an added bonus at the end. Follow along for the complete tutorial.

Requirements:

- Raspberry Pi (any flavour is fine, but I used the 3B+ model)

- Two 2n2222 transistors (I recommend purchasing at least five as there are high chances you will short a few by mistake)

- Male-to-Male and Male-to-Female jumper cables

- Breadboard

- (optional but recommended) Breadboard 5V power supply

- Two of each 1k and 10k resistors

- Dual-Relay module

- Two old house lamps or other lighting appliances (use led bulbs as the incandescent ones tend to burn up during testing)

- Duct-tape and a cable cutter for splicing

Step 1: Setting Up the Server and Dependencies on the Raspberry Pi

For my project I used NGiNX, although you can use whichever server you want (Apache2, etc.). The vanilla install with the package manager is just fine, no need to compile our own source or anything like that. We will also need PHP, Python3 and Composer.

- SSH into your PI and type $sudo nano /etc/dhcpcd.conf

- Configure the static IP parameters

- Reboot your PI

- Check to see if everything is working by typing {the_ip_you_chose} in the address bar of your browser (you should see the welcome page of nginx)

Step 2: Install the Web Framework and Configuration for NGiNX

We want to have a website we can access from our mobile or desktop browser and in order to rid ourselves of writing plain php/html code which isn't the scope of this tutorial, we will instead use a PHP framework called Yii2 to handle basic routing and styling for us.

- Go to the Yii2 install page and use composer to install it in /var/www/html/light-switch

- We won't see anything just yet as we still haven't put in our config file for the server

- I attached the file (default) to this step

- Use it as is with this framework or look up another one online if you prefer a different backend

- You have to put this file in /etc/nginx/sites-available/

- Reload NGiNX with $sudo systemctl reload nginx

- In case something isn't working you can run $sudo nginx -t to debug the config files

Step 3: Setting Up the Python Scripts Which Will Interact With the GPIO

As PHP doesn't like to play that well with runtime-type scripts like the GPIO API and python isn't very friendly with web apps, we will use each of them in their advantageous contexts. Attached to this step you will find the 4 necessary python scripts which interact with the pins and trigger the lights. Place the scripts wherever you want, but note their location as we will need it in the next step when we set up the php side of things.

Step 4: Finishing Up on the Php Side of Things

All we need to do is remove some of the default paths and pages from the main layout and add a few lines of code in the SiteController plus a new method called actionLightswitch(). After this, it will execute the python scripts from the previous step which will in turn command the GPIO pins.

Step 5: Creating the Circuit

Due to the nature of this specific relay module (it switches when connected to GND) we need to build the circuit using transistors. The PI will only open the transistor which will let the current flow from the relay data pins to GND thus triggering the coil and closing the circuit for the lamps. Follow the schematics in the attached pictures and you will be good to go.

For this next part, you have to be really careful as it's time to connect the relay. Unplug everything first! The relay has 3 ports per connection. We will connect our appliances cables to the middle one and the one with the open sign on it. Carefully splice the cable to expose the copper conductors and insert them in the aforementioned relay ports. The order doesn't matter as we are now working with alternative current.

Step 6: Final Testing

Check your circuit and assigned pins at least two times and then connect the PI to power and type {the_chosen_ip} in the browser's address bar. The first page will have our two switches. Click on them and watch the lights turn on/off.

Step 7: Bonus Part: Old-school Clapper Trigger

Requirements:

- any usb microphone (it doesn't have to be a high quality one)

- ALSA package (the installation method will differ depending on your distribution)

- PyAudio module

Copy the attached python script and plug in your microphone. Reboot the PI. Execute the script and follow the console prompts. Get close to the mic and clap your hands twice. You should see the claps being registered in the console output and if successful, both of the lights will turn on after 2 seconds.

Attachments