Vineet AroraNoor BashaAmit Dubay
Published © LGPL

Water Usage Tracker

Track and control water usage in your house by each outlet and appliance.

IntermediateShowcase (no instructions)6,772
Water Usage Tracker

Things used in this project

Hardware components

Raspberry Pi 2 Model B
Raspberry Pi 2 Model B
×1
Breadboard (generic)
Breadboard (generic)
×1
TIP 120 Transistor
×1
Resistor 1K
×1
1N4004 Diode
×1
Batteries
×1

Software apps and online services

Microsoft Azure
Microsoft Azure
Online web service to capture usage details and store it to a database.
Microsoft Azure
Microsoft Azure

Hand tools and fabrication machines

Solenid valve

Story

Read more

Custom parts and enclosures

Diagram

Schematics

Circuit

Code

Main Page code

C#
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

Vineet Arora

Vineet Arora

1 project • 8 followers
WinWire.com
Noor Basha

Noor Basha

1 project • 4 followers
Amit Dubay

Amit Dubay

1 project • 4 followers

Comments