If you’ve ever been dumbfounded by how someone controls things through an app then you’ve come to the right place. I’m going to teach you the basic concept in a really easy way through light control after which I’m sure you’ll be able to apply it to many other things. And if you are one of those people who stumbles in the dark, bumping into things while fumbling for the lights switch then I have a solution for you. How about if your lights turn on automatically at night when they detect motion and using a timer loop turn off after a while? Not only it will help save energy but save you from a lot of stubbed toes and bruised knees. Interested? Then read on!
This project describes the app control of lights through Microsoft azure, raspberry pi 2, a switching circuit (for the active low relay) and a relay. Also light control through sensors, specifically a dark and motion sensor, is also a part of this project.
Well let’s start with my favorite part, the hardware! First I’ll describe the circuits and the modules then I’ll tell you how to hook them up with your raspberry and the programming codes.
-
Hardware
THE DARK SENSOR
The schematic (Fig.1) shows a simple circuit that gives a high output (3.5-5V) when it’s dark. You’ll need an LDR (light dependent resistor), a couple of resistors (values indicated in the schematic) and an op amp LM741. Connect the components together as shown in the schematic. 5V supply and ground are connected. LDR was covered up with a pen cap so the 'dark' condition is simulated. I set the value of the variable resistor by connecting the output of this circuit to a powered up relay and adjusting the resistor till it turn the relay on but was at the turning point of on and off.
THE MOTION SENSOR
I have used the RB-Ite-116 motion sensor module (Fig.2). It has three terminals for supply (5V), ground and output (3.3-5V). I tested mine, it gave 3.5V. Incase it your gives more than 3.5 be sure to add a regulator or resistor to maintain 3.3V.
THE RELAY
I used a 4 channel, 5V sainsmart relay(Fig.3). It’s active low. So we’ll need a switching circuit so when the raspberry gives a high signal, the switching circuit gives a low to the relay.
THE SWITCHING CIRCUIT
For the switching circuit we’ll need four 2N2222 transistors, four 10K resistors, four 2.2K resistors, some pcb pin connectors and jumpers. The figures show the pcb I made. You can easily connect this circuit on a breadboard. I have also uploaded the schematic and pcb circuit of the switching circuit in proteus by the name 'activelowswitches'. 3D visualizer is also present in the software. Below link is for the proteus 8 software download.
http://www.fullprogramlarindir.com/proteus-full-professional-tam-indir.html
The link for install and activation tutorial of the software is:
www.youtube.com/watch?v=Pf7qofcmldY
-
Setting up our PC and devices
1.Now lets setup our pc .I have provided the link:
https://ms-iot.github.io/content/en-US/win10/SetupPCRPI.htm
2.Now lets set up our device. Follow the steps in the link:
https://ms-iot.github.io/content/en-US/win10/SetupRPI.htm
Done? Good!
-
Connecting Raspberry pi to laptop
Connect power supply to the raspberry and connect it to your laptop with an ethernet cable.Open network and sharing center. (Fig.4)
1.Click on your wifi/ethernet connection (like mine is WiFI Qubee).
2.Click on properties.
3.Go to sharing.
4.Allow other user to connect
Search window IoTCoreWatcher which was installed automatically when you followed the step 2 of setting up our pc and devices. Open it and it will show you the mac address as well us IP address of your raspberry pi.
Using PuTTY to connect raspberry to laptop
Download puTTY. The link is:
http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html
A shortcut will be formed on your desktop. (Fig.5)
1.Open it and enter the IP address of your raspberry pi in the host name (or IP address) dialogue box.
2.Click open.
Command prompt will open. (Fig.6) It will ask “login as”. Enter Administrator if you are logging on your raspberry pi Windows IoT Core device for the first time. It will next ask password which by default is p@ssw0rd (0 is zero). The password doesn't show while typing, its sort of a security feature.
Congratulations! Your device is now connected! (Fig.7)
Connecting raspberry pi to the internet
Connect the raspberry wifi adapter to your device.Make sure raspberry has access to an open wifi network. Now open IoTCoreWatcher and right click on the ip address and select open browser . In the browser find your way into networks .You will see all available wifi networks there connect to yours by clicking it.
-
Hardware connections
The connections are pretty simple. Supply 5V and ground to relay, motion sensor and the LDR circuit.
Pass the wire on the lamp through the relay as shown.
Do the same for the 3.3V supply from raspberry and connect the other end to pin 37.
Connect the output of the motion sensor to pin 29.
Connect the output of LDR to IN2.
Connect pin number 13 and 16 to pin 3 of the switching circuit.
Connect pin 3 of the output side of the switching circuit to IN3.
(use a breadboard to draw out vcc, ground and other common connections to avoid a mess of wires)
-
Microsoft Azure Web App
For beginners I'm providing this link to help you set up your web app. It gives and introduction to microsoft azure,how to signup and explains what a web app is and how to create it.
https://azure.microsoft.com/en-us/documentation/articles/web-sites-dotnet-get-started/
Hopefully you'll have created your web app. I created mine by the name iotpieas.
-
Let's code (not really)
I've provided codes for controlling the sensors and app control of lights.
For the sensor code
Download the code from GitHub.Open up microsoft visual studio. Select File> new project>C#project file where you saved it .
In the right you will see a column that says solutions . In the column you will see solutions 'Blinky' . Right click and select build . It might tell you that some packages are missing but it'll get them for you automatically.
Set ARM for Raspberry pi and select remote device. Another Window will open. enter the IP address of the device and click find. It will connect with your device. Run the Code with everything connected and powered up. (Fig.8)
Have Fun!
For the webapp code
Download the webapp code from GitHub. open it up in visual studio. Build it. Select lamp.client right click as set as start up project. Go to tools and select connect to Microsoft Azure subscription. Enter your credentials.A side window will appear. Click on App Service and your web app should appear. So now you are connected. Follow the same steps of ARM and remote device connection.
Run the project. Open up your azure website. It should appear like in the Figure below
With the code running you should be able to control your light.
-
Videos
Since I made this project by parts, it was tested step by step. I made videos while my (not so enthusiastic :p) friend explained whats going on.
1.Testing the motion sensor and dark sensor circuit and relay connected lamp (light control) through it. LDR is covered to simulate the 'dark' condition and when hand is passed in front of the motion sensor the relay hence the lights turn on. A timer is set in the code. The lights turn off automatically.
2.Testing the azure web app control to turn the lamp on and off.
3.Testing sensors and app control simultaneously.
4.Blooper.
Hope you enjoyed this tutorial and it provided a good learning experience for you! Please leave any questions and feedback in the comments.
Comments