Introduction
We are going to create an automation project, using Raspberry Pi. In my previous articles, I have used the same concept of home automation and implemented it by using Arduino. You can find the link here.
Now, we will see the same concept with the help of Raspberry Pi. Here, in this article, we will create a Universal Windows Application in which the controls are done with the help of buttons. After this, we shall try the speech controls. Now, let us go on with this one.
Requirements
- Raspberry Pi of any version.
- LED.
- DC motors.
- Male to female jumper wires.
Designing
Create new Universal Windows Application from Visual Studio. Here, in this project, we are going to connect LED, a fan, a door and control them. Hence, we will need a total of six buttons in which three will be for turning on and three will be for turning off. You may place them as you wish. Afterwards, change the content in them as light on, light off, fan on, fan off, door open and door close to recognize the use of them. The designed page will somewhat look, as shown below.
Adding reference file
I think, you all are familiar with the reference files. Here, in the Universal Windows platform, you need to add the reference file Windows IOT extensions for UWP. This extension will help you to run your program in your Raspberry Pi.
Writing the code
Since Raspberry Pi is a microprocessor, the pins in it area little more complicated than the pins of Arduino. It has two types of pins namely GPIO and SPIO. Here, GPIO are general purpose input output, whereas SPIO are special purpose input output. Now, for our activity, we are going to use GPIO pins. To know more about this pin configuration, have a look at the image given below.
Now, for implementing the concept, I am going to use GPIO pins 6,13,19,26 and a ground pin. In MainPage.XAML, write the code snippet , which I have given below. Here, you might have to change your Namespace according to the name, which you have given to your Application.
- using Windows.UI.Xaml;
- using Windows.UI.Xaml.Controls;
- using Windows.Devices.Gpio;
- using System.Threading.Tasks;
-
- namespace Home_Automation {
- public sealed partial class MainPage: Page {
- GpioPin light, fan, door, door1, alaram;
- private void Button_Click_2(object sender, RoutedEventArgs e) {
- fan.Write(GpioPinValue.High);
- }
- private void Button_Click_3(object sender, RoutedEventArgs e) {
- fan.Write(GpioPinValue.Low);
- }
- private void Button_Click_4(object sender, RoutedEventArgs e) {
- door.Write(GpioPinValue.Low);
- door1.Write(GpioPinValue.High);
- Task.Delay(3500).Wait();
- door1.Write(GpioPinValue.Low);
- }
- private void Button_Click_6(object sender, RoutedEventArgs e) {
- alaram.Write(GpioPinValue.High);
- door.Write(GpioPinValue.High);
- door1.Write(GpioPinValue.Low);
- Task.Delay(3500).Wait();
- light.Write(GpioPinValue.Low);
- fan.Write(GpioPinValue.Low);
- door.Write(GpioPinValue.Low);
- }
- private void Button_Click_7(object sender, RoutedEventArgs e) {
- alaram.Write(GpioPinValue.Low);
- }
- private void Button_Click_5(object sender, RoutedEventArgs e) {
- door.Write(GpioPinValue.High);
- door1.Write(GpioPinValue.Low);
- Task.Delay(3500).Wait();
- door.Write(GpioPinValue.Low);
- }
- private void Button_Click_1(object sender, RoutedEventArgs e) {
- light.Write(GpioPinValue.Low);
- }
- private void Button_Click(object sender, RoutedEventArgs e) {
- light.Write(GpioPinValue.High);
- }
- public MainPage() {
- this.InitializeComponent();
- Loaded += MainPage_Load;
- }
- private void MainPage_Load(object sender, RoutedEventArgs e) {
- var controller = GpioController.GetDefault();
- door = controller.OpenPin(19);
- door1 = controller.OpenPin(26);
- alaram = controller.OpenPin(5);
- fan = controller.OpenPin(13);
- light = controller.OpenPin(6);
- door.SetDriveMode(GpioPinDriveMode.Output);
- door1.SetDriveMode(GpioPinDriveMode.Output);
- alaram.SetDriveMode(GpioPinDriveMode.Output);
- fan.SetDriveMode(GpioPinDriveMode.Output);
- light.SetDriveMode(GpioPinDriveMode.Output);
- }
- }
- }
Connecting the pins
Connect the ground pin to the breadboard, using the jumper wires. Subsequently connect pin 6 for using LED, pin 13 for using the fan and pin 19 and 26 both for using the door, since DC motor of the door must rotate in the dual directions. Connect your devices according to their pins and ground but for the door, connect both the positive and negative wires to GPIO pins. Only then will the door functionality work properly. Remember that you are creating only a prototype and not a real model. If you want to create a real model, you might have to use Relay, which can help you to control the real time appliances of your home. Have a look over the image to understand the connections.
Deploying the program
In Raspberry Pi, you will be deploying the program in the device itself. Hence, you should configure your device to Visual Studio and connect it with Raspberry. To do it, choose the remote machine option to run your solution and also use ARM option for the deployment process. I have added some extra functionality called alarm in the code snippet. You can either connect it or leave it as it is. The code will work normally.
Finally, when you make proper connections, you can see the output which is running in your Raspberry Pi. To see the output, you must connect your Pi to a monitor or some other device. You can even use Windows IOT remote client app for this, which I will explain in the next article. Hence, when you click on the buttons, the corresponding action will take place properly. Thank you.