Raspberry Pi WiFi Controlled Lamp

IoT is all the craze these days, and with the Raspberry Pi, it’s easier than ever to integrate smart devices into your everyday life. Relays allow makers to connect their electronics to a Pi by controlling the flow of electricity. You set up a trigger on the Pi, and the appliance toggles on or off.

There are three components of this project: software, hardware, and a trigger. The software part is a web server running on the Raspberry Pi that can receive requests from a device on your current network to control the relay. The hardware is a relay that will cut the power of the lamp until it gets the signal from the Pi to complete the circuit. The trigger can be anything from your web browser to an app. After port forwarding your Pi, which means that you open it up to the Internet, you can trigger it from anywhere rather than just your wifi network. This allows services like Siri, Alexa, Google Home, and even IFTTT to trigger your lamp. If you don’t know, IFTTT is a popular online event trigger service, that brings several apps and services together. You can even set it up to text a number to turn on the lamp. Of course, a relay isn’t just confined to toggling lamps. It can be used for things like garage doors, or everyday kitchen appliances.

We’re going to start by wiring the Pi to the relay. The red wire is the 5v supply to the relay. Black is ground, and the white wire is GPIO pin 7. 

To connect the lamp to the relay, cut one of the two wires leading from the outlet into the lamp. Plug the two new connections into the relay. Make sure to DISCONNECT the appliance before cutting wires.

Next, we’ll configure the Raspberry Pi and install the server script. First, run these in the command line to update your Pi.

sudo apt-get update

sudo apt-get dist-upgrade

sudo apt-get install npm

These will download all of the newest updates and npm, the Javascript package manager. Next, run this to download the latest version of Node.js.

curl -sL https://deb.nodesource.com/setup_9.x | sudo -E bash -

sudo apt install nodejs

Finally, to verify that the install was complete, enter this to return the Node JS version number.

node -v

Congratulations! Node JS is successfully installed on your Pi. Now onto the packages for the web server script. First, we’ll install Forever. This enables you to run Node JS scripts as a daemon so that the program never quits. It will also start again on startup.

git clone https://github.com/themaanas/wifiLamp.git
cd wifiLamp
sudo npm install
sudo npm run start

Your server is now running! If you go to IP_ADDRESS_OF_PI:1000/toggle in a web browser, the lamp should toggle. To obtain your Pi’s IP address type ifconfig into the command line. Experiment with different triggers; there’s so many out there so pick one that best suits your needs. Now you have a fully functioning, homemade IoT desk lamp!

/ Free Portfolio Plugin for WordPress by Silicon Themes.