Things used in this project

Hardware components:
R8326274 01
Raspberry Pi 2 Model B
×1
12002 04
Breadboard (generic)
×1
TIP 120 Transistor
×1
Resistor 1K
×1
1N4004 Diode
×1
Batteries
×1
Software apps and online services:
http://winwireiot.azurewebsites.net
Online web service to capture usage details and store it to a database.
W9gt7hzo
Microsoft Azure
Hand tools and fabrication machines:
Solenid valve

Custom parts and enclosures

Diagram
WinWire IoT Project-HLD-0.1.docx

Schematics

Circuit
Circuit

Code

Main Page codeC#
The code that handles events from a push button and turns the tap on/off
using System;
using System.Net.Http;
using Windows.Devices.Gpio;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Media;

namespace WaterTracker
{
    public sealed partial class MainPage : Page
    {
        private const int VALVE_PIN = 5;
        private const int BUTTON_PIN = 6;
        private GpioPin pin;
        private GpioPin buttonPin;
        private GpioPinValue pinValue;
        private DispatcherTimer timer;
        private SolidColorBrush redBrush = new SolidColorBrush(Windows.UI.Colors.Red);
        private SolidColorBrush grayBrush = new SolidColorBrush(Windows.UI.Colors.LightGray);

        public MainPage()
        {
            InitializeComponent();

            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromMilliseconds(5000);
            timer.Tick += Timer_Tick;
            InitGPIO();
            if (pin != null)
            {
                timer.Start();
            }
        }

        private void InitGPIO()
        {
            var gpio = GpioController.GetDefault();

            // Show an error if there is no GPIO controller
            if (gpio == null)
            {
                pin = null;
                GpioStatus.Text = "There is no GPIO controller on this device.";
                return;
            }

            pin = gpio.OpenPin(VALVE_PIN);
            buttonPin = gpio.OpenPin(BUTTON_PIN);
            if (buttonPin.IsDriveModeSupported(GpioPinDriveMode.InputPullUp))
                buttonPin.SetDriveMode(GpioPinDriveMode.InputPullUp);
            else
                buttonPin.SetDriveMode(GpioPinDriveMode.Input);

            // Set a debounce timeout to filter out switch bounce noise from a button press
           buttonPin.DebounceTimeout = TimeSpan.FromMilliseconds(50);

            buttonPin.ValueChanged += ButtonPin_ValueChanged;
            pinValue = GpioPinValue.High;
            pin.Write(pinValue);
            pin.SetDriveMode(GpioPinDriveMode.Output);

            GpioStatus.Text = "GPIO pin initialized correctly.";

        }
        private bool ButtonClicked = false;
        private async void ButtonPin_ValueChanged(GpioPin sender, GpioPinValueChangedEventArgs args)
        {
            // HttpClient http = new HttpClient();
            //http.BaseAddress = 
            //var result = await http.GetAsync(new Uri("http://winwireiot.azurewebsites.net/api/valve/get"));
            //var response = result.Content.ReadAsStringAsync().Result;
            if (args.Edge == GpioPinEdge.FallingEdge)
            {

                ButtonClicked = true;
                HttpClient http = new HttpClient();
                if (pinValue == GpioPinValue.High)
                {
                    pinValue = GpioPinValue.Low;
                    pin.Write(pinValue);
                    
                    StringContent queryString = new StringContent("1");
                    HttpResponseMessage response = http.PostAsync(new Uri("http://winwireiot.azurewebsites.net/api/valve/post"),queryString).Result;
                    response.EnsureSuccessStatusCode();
                    string responseBody = await response.Content.ReadAsStringAsync();
                    http.Dispose();
                    ButtonClicked = false;

                }
                else

                {
                    pinValue = GpioPinValue.High;
                    pin.Write(pinValue);
                    StringContent queryString = new StringContent("0");
                    HttpResponseMessage response = http.PostAsync(new Uri("http://winwireiot.azurewebsites.net/api/valve/post"), queryString).Result;
                    // LED.Fill = redBrush;
                    response.EnsureSuccessStatusCode();
                    string responseBody = await response.Content.ReadAsStringAsync();
                    http.Dispose();

                    ButtonClicked = false;
                    //LED.Fill = grayBrush;
                }
            }
        }
        


        private async void Timer_Tick(object sender, object e)
        {
            if (!ButtonClicked)
            {
                HttpClient http = new HttpClient();
                //http.BaseAddress = 
                var result = await http.GetAsync(new Uri("http://winwireiot.azurewebsites.net/api/valve/get"));
                var response = result.Content.ReadAsStringAsync().Result;
                if (response.Equals("1"))
                {
                    pinValue = GpioPinValue.Low;
                    pin.Write(pinValue);
                    LED.Fill = redBrush;
                }
                else
                {
                    pinValue = GpioPinValue.High;
                    pin.Write(pinValue);
                    LED.Fill = grayBrush;
                }
            }
        }
    }
}

Credits

Vineetarorablueshirt
Vineet Arora
1 project • 8 followers
WinWire.com
Contact
2ba8442
Noor Basha
1 project • 4 followers
Contact
C8880447e518a71b72ddc68dfc4e2c0e
Amit Dubay
1 project • 4 followers
Contact

Replications

Did you replicate this project? Share it!

I made one

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

Sign up / LoginProjectsPlatformsTopicsContestsLiveAppsBetaBlog