Introduction: Smartphone Controlled Ceiling Fan
Stop worrying about the haze of mystery surrounding "connected" things... I've done the work, now you get to be the hero! I started working with Arduino and Raspberry Pi about a year ago, and I'm gonna cut out all the crap that doesn't work and we're gonna get a ceiling fan and it's light functioning from a smartphone (Android, iPhone, Blackberry, Raspberry, iPad, Desktop, Laptop, Mac, Windows, Linux, or whatever has a web browser and an internet connection). ONE THING: you MUST have wireless (wi-fi) internet at your house/brothel/outhouse/hunting camp/etc.. ready? let's go!
Don't be an idiot. If you've never installed a ceiling fan or wired an outlet, then this is probably way too difficult for you!!
I have tried to make this as simple as possible. If you are reading this, you probably already know what a Raspberry Pi is - you may even own one and will find most of this lesson rather "sophomoric" (which has nothing to do with that chick you dated back in Junior College), but I wanted to write this so that an average dad with some knowledge of computers and electricity could make an "APP" for his "little princess" to control her ceiling fan with her iPhone10s.
I have also tried to inject a little humor into the tutorial. I am not politically correct, but I'm not insulting, either. Try to have some fun and over-look anything that you might find "offensive" - it's only humor!
Special Props to mattrichardson.com - without Matt and his uber-awesome website, this would never have been easy! Click on his site to see how to make the Pi do all kids of stuff!
Disclaimer: I am NOT a qualified electrician. If you injure/kill yourself with the High (or any other) Voltage or burn your house/dwelling/business/outhouse/brothel/etc. down, I am NOT responsible. You will need to wire this project according to your local wiring/building codes. I am just a guy who designs conveyor systems for a living and has a love of electronics. YOU ARE RESPONSIBLE FOR ANY ACTIONS YOU TAKE! Yes, 100-120 volts AC can kill you! You have been warned! I am not responsible for anything YOU make - even if it exactly what I made!
Step 1: What You're Gonna Need:
This is what you will need for this project (if you look around, you can find all this in one "kit" -- except for the "4-channel relay," it will have to be purchased separately)
1) Raspberry Pi 2 B+ (with AC power supply and NOOBS on a SD card) $49.95
NOOBS is a "boot program" that will install the operating system (OS) into your Pi (you want this). You can probably use any Raspberry Pi, but I wrote this for "Pi2B+" so the wiring differences will make a difference!! This is the EXACT unit I bought: http://www.amazon.com/Vilros-Raspberry-Complete-St...
1) 4-Channel Relay - $6.95
These are available from Amazon, too! Here is the exact one I bought:http://www.amazon.com/JBtek-Channel-Module-Arduino...
1) Breadboard - $8
You will need one with at least 400 "points" or 25 "lines." Bigger is NOT always better in these situations. Here is the one I bought: http://www.amazon.com/microtivity-IB401-400-point-...
1) Raspberry Pi 2 Cobbler -- $12
Make sure you buy one for a "Raspberry Pi 2" !!! The one for a PLAIN "Raspberry Pi" is smaller and will not work with "Raspberry Pi 2." Here is one: http://www.amazon.com/Assembled-Cobbler-Ribbon-Cab...
1) Female Plug end - $?
To be 100% accurate, I used and old extension cord and cut the "male" end off. This is probably not the best method, but this is what I did. You may want to do something "cleaner." If so, hit Home Depot or Lowes and get something you can plug your "power supply" into and wire into the 2 legs of your home power supply.
1) Set of Female-to-male jumpers (2.54mm):
The Relay and the "Raspberry Pi 2" have "male" outputs. The breadboard is all "female." Get ones that are at least 10CM long (look, I don't like metric either, but that is how electronic stuff is sold). I'd get some of all types, like here:http://www.amazon.com/Frentaly%C2%AE-120pcs-Comple...
1) Wi-fi Dongle $8
this is how you will connect to the internet - more directly, what you will be doing is connecting to your wireless modem. Here is what you need:http://www.amazon.com/Edimax-EW-7811Un-150Mbps-Ras...
Miscellaneous items you will need:
hot-glue gun (with glue -- for you people who need to be told not fall off ladders or put your hand on rotating saw blades - lol)
a box (I used a 10"x4"x3" cardboard box) - Your local wiring code will require an "electrical enclosure" -- Find one you like...
Step 2: Set Up Your Pi2!
From here, I'm gonna have to hope you are interested in doing MORE with your computer than checking Facebook or watching porn... If that is all you want to do with your computer you have already read too far!
You are gonna have to turn on the Raspberry PI 2. This means you will need to be able to "see" what it's writing. Since you will need a special adapter (http://www.amazon.com/LinkS-Active-Female-Adapter-... ) to use a regular VGA monitor, DO WHAT I DID and just plug it into the TV's HDMI port. This will upset the whole family if you do it during "American Idol" or "Pretty little Liars" or some other TV crap, so you may want to avoid this during "Prime Time."
You will need to plug in a keyboard and a mouse (I use a wireless combo so I have ONE "dongle" that controls both) and insert your wi-fi "dongle." Put the tiny SD-micro card into the little slot on the bottom with the "contacts" toward the board.
Now... plug in the Pi!
Assuming you were smart enough to get the SD-card with Noobs pre-installed, you will soon be looking at a screen that will ask you to install "Raspian." DON'T BE SCARED, but this is a version of Linux (exactly it is Debian - Wheezy or Jessie). It is like Windows 8 or 10, but not as confusing and IS safer and better (Linux versions cannot get viruses, so no need to worry about anti-virus software with Raspian). Your screen should look like the above pic...
In the upper left-hand corner is an "Install" icon. You will have to check (or "X") the box next to "Raspian" in the list, then click the aforementioned "install Icon."
Now, Go get a beer or do some laundry or see what you can talk the wife into or out of, because your on a 20 minute wait for the Pi to install "Raspian" - the Operating System of choice (and it is quite sweet)!!
Step 3: Testing the Operating System
This is it.. you are finally going to do some "programming!"
So put on your best Matthew Brodderick face and get ready to dial up WOPPER from War Games (the 1984 movie).
Once you've booted your Pi, you should see the above screen. We have some work to do:
First, set up Wi-fi:
1) right click the "double-screen" icon in the top-RIGHT corner.
2) Find your wi-fi and click on it.
3) Ask your wife/girlfriend/kid what the wi-fi password is.. then use it to log on.
4) you should see the Wi-Fi icon (curved lines) start to blink.
Next, Click the Icon on the top-LEFT that looks like the Earth. This will open a web browser. It is a very simplistic browser, but type in:
...and see if it takes you to Google. If so, then consider sending your resume to MicroSoft!! If not, start googling from your phone why you cannot connect...
Close the Browser.. or log onto Facebook to tell your friends you are using a $35 computer - (it doesn't play porn videos very well, but you probably just figured that out, I'm sure...)
Now that the browser is closed, we're going to open a "terminal." This is the "Black Screen Icon" on the top-LEFT. once it is open, It should read:
After the $ type this:
2 "paragraphs" of stuff will come up.. all of it having to do with your connection to your Wi-fi modem (actually the "router," but that's not what we're covering) What we are looking for is our Pi's "IP address." You are looking for a number like "10.0.0.20" or "192.168.0.8". It will NOT start with a 127 or a 255!!! It should be preceded by the phrase "inet addr:" but will NOT be the one that starts with 127!! Write that down - you will need it later! WRITE IT DOWN! It will look like 192.168.somenumber.somenumber OR 10.0.somenumber.somenumber. And it may be something other than that, but it will NOT start 127 or 255. It is NOT the "Bcast:" or the "Mask:'.
Now, at the bottom of the black screen, you will, again, see:
We need to add some "helper" apps, so type this after the $
sudo apt-get install python-pip
(YOU, may want to just "cut and paste" the code above since your typing skilz SUX!)
(this goes right after the $, don't add spaces or use the line below it or save it next to your "Debbie does Dallas" DVD, just follow the instructions!)
... and press enter. When it gets done (usually a few seconds, but could be longer depending on setup and internet speed, but it may be shorter since you have that SUPER_HI_SPEED internet connection for watching porn... just stop, ok!) it will show you the prompt again. Enter this after the $
sudo pip install flask
.. and press enter. This is not your cue to break out the liquor, but it does install an app you need to have your web browser talk to your fan! Don't worry if it tells you that "the C extension in not compiled"... that is way over your pay-grade!
one last command; Type this after the new $:
and hit enter... (Here we go.... Lord, help us... and help all the little pygmies in Africa, Amen)
Step 4: The Software (the Spooky Part)
The image above is the Raspian Configeration Screen (aka: Config).
Don't screw with ANYTHING except #2 and #9 until you have done some Google searches to see what you are screwing up! (I know you want to overclock it, but don't... you don't know a damn thing about heatsinks or thermal expansion or Thermal Dynamics, so just let it be... ok, if you HAVE to mess with it, set it to "high" and get out of there!)
"Arrow down" to #2: Change User Password. Click enter and give yourself a password.. this is a good idea, so that I can't drive by your house, hop on your Pi and start flicking the light on and off like a 3rd grader on a sugar-high...
When you are done, it will bring you back to the "Config" screen.
"Arrow down" to #9: Advanced Options and press enter.
For the love of all that is holy, Don't go mucking around in this area.. be a man and just follow instructions.. Now, "Arrow Down" to A2: Hostname. This is gonna give your pi a name.. most people use some witty "slicepi" or MyPi or PiEye or some other crap, but don't.. use your name and a number like Bill1, Dave1 or DeMarquezello1.. this way, when you get another Pi, you can name it Bill2 or Dave2 and they will be easy to identify! Hit enter to say "ok"...
Next, scroll back down to 9: Advanced Options, then arrow down to A4: SSH. Hit enter and it will take you to a screen to enable SSH. The "enabled" option will already be highlighted. Just press enter and it's done.. the next screen will say you enabled it, hit enter again an it will take you out to the "config" screen
Press TAB until "finish" is highlighted and press enter. It will ask you if you want to re-boot... YOU DO!!! Press enter!
Step 5: The Files
This is the part where I step aside and thank Matt Richardson - because I totally ripped-off his program he built to run a lamp and a coffee pot so YOU wouldn't have to disturb your hangover by getting out of bed to turn off/on the fan/light... You need to swing by his site an see where the "code" came from... Matt, I don't know how to give you the proper credit, but sending people to your website is the best way I know how:
Now: back you your Pi:
FIRST, look at the Desktop and on the top-LEFT, you will see an icon that looks like a 2-drawer file cabinet. Click on it.
This will open the "home" folder and show you all your primary "folders" (like Desktop, Downloads, documents, Music, pictures, etc.). We want to create a folder called "WebLamp." (PAY ATTENTION TO THE CAPITAL AND LOWER-CASE LETTERS - and spelling matters!)
Right-click in the white space and it will ask "Create new..." and beside it click on "Folder." A box will appear asking what you want to name it. type in:
Next, Double-click on the "WebLamp" folder and create a new folder (just like we did above) and name it:
Lets download and fix the names of the files:
I am not happy with Instructables.com for making me upload the files in a screwy manner, but we have to do it this way for whatever reason. You will notice 2 different sets of instructions... this was already the "hard part" and they made it harder by not allowing me to upload an "html" file.. so here we go:
Download the files one at a time!! let's start with weblamp.py It will not require as much "fixing."
Right click on the Icon that looks like a sheet of paper with a folded corner next to the phrase "weblamp.py". Select "download link..."
This will put the file into your Downloads folder. Click on the "filing cabinet" at the top of the screen and you will see it. The GIANT problem is that Instructables.com changed the name to something incomprehensible, so we have to change it back! Right click on it and select "Rename..."
At this point, a box will pop up and you need to delete everything and rename it:
Click ok, and the file name should be changed. Right-click on the file icon and "cut" it. Go to the folder you made called WebLamp and put this file in that folder. There should only be one other thing in there and that is the "templates" folder.
Right click on the Icon that looks like a sheet of paper with a folded corner next to the phrase "main.py". Select "download link..."
This will put the file into your Downloads folder. Click on the "filing cabinet" at the top of the screen and you will see it. The SAME GIANT problem is that Instructables.com (again) changed the name to something incomprehensible, so we have to change it to what we need! Right click on it and select "Rename..." At this point, a box will pop up and you need to delete everything and rename it:
Click ok, and the file name should be changed. Right-click it and "cut" it. Go to the FOLDER you made called "templates" that is inside the FOLDER "WebLamp" and put this file in that folder (put "main.html" into the "templates" folder). There should be NOTHING ELSE in the "templates" folder.
Here's what you should have:
When you open the WebLamp folder, you should have 2 things:
weblamp.py (a file)
When you open the "templates" folder, there should only be one file called
main.html (a file)
The hard part (programming) is over. Congrats, Mr. Snowden...
Step 6: The Hardware....
So now we're here - this is called "physical computing." When you chat with your "girlfriend" on facebook, that is "virtual." It's not "real." This is "physical." It is the opposite of "virtual." When you do this , you can SEE the actual light; you can FEEL the actual breeze from the fan - this is Physical Computing!
Now, We shall put all those parts together. Click the "menu" button on the Pi's Desktop and shutdown the Pi.
Your harware will look basically like the picture above, but you can configure this anyway you want in your box.. now a creative dude will recognize that the "cobbler," and breadboard are not really needed. You can attach the wires (with female-female jumpers) directly between the Pi and the Relay... yes, you can, but I'm not covering that...
If you don't know how a breadboard works, Sparkfun has an excellent tutorial here:
Attach the plain end of the cobbler to the Pi - there is a "tab" or "lip" and grooves on one side that goes towards the circuitry on the board. the "T-Shapped" end goes on the Breadboard.
Now, with a male-female wire, connect the VCC pin of the relay to a breadboard column under 5v+ symbol on the "cobbler." Mine is attached all the way at the bottom by the side of the box - it is the blue wire. The green wire with it on the breadboard is attached to 2 places: 1) the GRD on the relay... 2) the 5v- column on the breadboard.
Each Relay on the relay board has a corresponding "control" pin. We will take whichever of the relay blocks you use to control the fan (for me it was labelled "K1") and connect it's "control pin" (for me the pin was labelled "K1" as well, which was really logical) to the Pi's #24 pin. The light's "control pin" (for me it was "K2") will be connected to the Pi's pin #25. (You should have an open "hole" between pin #25 and #24.. this is because the Pi pins are not in a numerical order you can understand and I never bothered to care...)
The relay can be wired as "normally open" or "normally closed." Obviously, we want to use it as normally open (which means the light/fan is off because the switch is "open"). Each Relay will have 3 holes. One is common (generally the center hole on each relay block) and the other hole is either opened or closed to the center. when we put power to the relay's "control pin" it will switch positions; in other words, if the was "open" and the light was off, once we put power to it, the switch will "close" and the light will come on (or vice versa).
The relay is really just a simple switch. It just interrupts current flow on ONE leg of the light or fan circuit. HERE is a generic example: if you had two wires running to a light bulb, then you would cut one of the wires and put the relay "in" that wire, so when the relay is "open" no electricity flows... and when the relay is "closed" the electricity flows and the bulb burns!
If you wire the relay to the light and your browser says the light is off and the light is really on, then you have the wire running TO the light in the wrong hole of the relay - just switch the "non-center" to the open hole (Be sure and turn OFF the power before attempting this - remember, if you get shocked, I told you NOT to do this in the introduction!). Obviously, we want the "light relay" to control the light and the "fan relay" to control the fan.. if it doesn't, then you need to switch the wires to the other relay, Mr. Gates...
Next, find "the outlet" that you'll plug the Pi into. In the pic, I cut my black power wire down to about 6 inches long (if you do this, you need to be sure to attach the wires back together the same way: one wire will have a white stripe and the other will be solid black - DON'T mix them up. Don't do this with it plugged in!)
Now, the "female plug" has to be wired to both "legs" of the fans power supply as if you were plugging in a lamp or wiring a light fixture (consult you local building codes and wiring codes or hire an electrician). I put it on the two "legs" running to the fan.
At this point, you actually have 3 circuits all connected to the two wires coming out of the ceiling. #1 is just a circuit that makes a "plug" for the power supply. #2 is inlet power to the fan relay that goes to the fan. #3 is inlet power to the light relay that goes to the light. SO, the "inlet power" (probably the white wire, but could be some other color) will have 4 wires in the same wire nut. The "outlet power" (probably the Black wire but could be some other color) will have 3 wires: one to the "plug" in the box, one to the Fan and one to the fuse/breaker box.. If this doesn't make sense, then you don't know how to wire a ceiling fan you you need your dad's help.
Step 7: Fire It Up, Baby!
Now, we got to do the magic!
Believe it or not, you have actually set up a petite "web server" and created your own custom web page. Now, the server is not very dynamic and the web page is a far cry from something Sun Microsystems can make, but be proud of yourself!
Remember when I told you to write-down that IP address in step 2? Well, you will need it, now!!
Download a program called Putty on your regular computer/laptop. Putty will connect that computer to the "terminal" (black screen) on your Raspberry Pi.
Open the Putty program and it will be asking for a Hostname (or IP address). This is where you put in that IP address you found (this is not the IP address you would find if you went to Google and typed in "what is my IP." That is a whole different IP thing!). Make sure you are in Port 22 and the connection is SSH (you'll see what I mean when putty is open). Basically you are now "hacking" another computer (the Pi).
Click "connect" at the bottom of the Putty window. You will get the "Black Screen," but this time it will be asking: Login as:
You will answer:
Next, it will ask you for your password. You will put in the password that you created in the Config screen way back in step 3.
Now, you will get the "prompt" we saw when we were in the "terminal" the last time (pi@raspberrypi:~ $).
at this "prompt," you need to type in this:
it will give you a new "prompt" (pi@raspberrypi:~ WebLamp $)
sudo python weblamp.py
press enter and type:
and press enter again...
Now, go to your smartphone (or anything that can pull up a webpage) AND IT MUST BE CONNECT VIA THE SAME WI-FI THE Pi is connected to and type in the IP address you used to connect with Putty. There will be a webpage telling you the status of your fan and light and giving you the option to change it!
I cannot wait to find out what I left out!
Step 8: AFTERTHOUGHTS (FAQ's)
After I made this Instructable, I knew there would be some of you who wanted to to make some changes to it...
Well, let's chat about that.
If you want to use this for something else rather than than a "fan" and a "light," you need to hook it up to something else AND you will want to change the names. Changing the names is in the weblamp.py file. Right towards the top, you will see "light" and "fan." obviously you can change them to read whatever you want.. AND you can copy the code and ADD more items on different pins - as many as you have pins for on the Pi. Have fun and Check out
The above link will explain (from a tiny little "flask" job) what, where, how and why it works.
If all you want to do is change the way your "website" looks (colors, box size, background, whatever) when you go to turn on your light or fan, then that is done in the main.html file. You will need a moderate knowledge of HTML and CSS, but there are websites like:
That can teach you really quickly what you want to do... Just type something like "HTML box size" into google and you will get links to w3school to do pretty much whatever you like.
As for the possibilities, they are limitless. Remember this is a fully functional computer that is wireless. You can add a battery pack (at 5VDC) and it will run by itself in the middle of the back yard. The raspberry Pi is easily equipped with a camera that will be more than happy to send you VIDEO while it controls motors or lights or blenders or weed-eaters or whatever using "flask." Why would you need that? Well, DRONES!.. You can hook up R/C trucks and quad-copters or build your own... it's all a matter of controlling the motors (and we did that with the fan, remember)... Drones can easily be attached to cell phones to go virtually ANYPLACE in the world (as long as they have the fuel). Programs like
You could literally make one that is solar powered that could travel to the other side of the content or world - as long as you can keep an internet connection (or use a GPS card for your PI, but that is a little more tricky)... and with a camera, you could see everything it sees!
These things ARE possible and you just took a huge step towards them... Programming languages like Python and Scratch (made for kids to learn) are so powerful and so easy, we're just now breaking into "the future." This is NOT "NASA-stuff." This is blue-collar, redneck engineering... Sky-Net (from the Terminator movie) is probably coming; And we will have the power to fight back...
I will quote Nina from in her 1984 song "99 Luft ballons" (translated):
- Ninety-nine knights of the air
Ride super high-tech jet fighters
- Everyone's a super hero
- Everyone's a Captain Kirk...
Just remember this: you just became a very dangerous person... these things you CAN BUILD are 1000 times more deadly than any AR-15 or AK-47 AND 1000 times MORE HELPFUL than an army of Doctors and engineers! - your govt KNOWS this, but is completely powerless to stop it - the "genie" is already out of the bottle. The future is OURS and the power will go to the one who can build the best toys!