private void ControlFan()
        {
            switch (_fanMode)
            {
            case "Auto" when _gasValue > _fanThreshold:
                _relay.ChangeState(SensorStatus.On);
                break;

            case "Auto":
            {
                if (_gasValue < _fanThreshold - 16)
                {
                    _relay.ChangeState(SensorStatus.Off);
                }

                break;
            }

            case "On":
                _relay.ChangeState(SensorStatus.On);
                break;

            case "Off":
                _relay.ChangeState(SensorStatus.Off);
                break;
            }
        }
Exemplo n.º 2
0
        public void Run(IBackgroundTaskInstance taskInstance)
        {
            // Connect the Relay to Digital Pin 5
            // Initialize the relay on Digital Pin 5 (D5)
            IRelay relay = DeviceFactory.Build.Relay(Pin.DigitalPin5);

            // Loop endlessly
            while (true)
            {
                try
                {
                    // Turn the Relay On.
                    relay.ChangeState(SensorStatus.On);                     // Turn the relay on.
                    System.Diagnostics.Debug.WriteLine("Button is On.");    // Write something to debug.
                    Task.Delay(1000).Wait();                                // Delay 1 second

                    // Turn the Relay Off.
                    relay.ChangeState(SensorStatus.Off);                    // Turn the relay off.
                    System.Diagnostics.Debug.WriteLine("Button is Off.");   // Write this fact to debug.
                    Task.Delay(1000).Wait();                                // Delay 1 second
                }
                catch (Exception ex)
                {
                    // NOTE: There are frequent exceptions of the following:
                    // WinRT information: Unexpected number of bytes was transferred. Expected: '. Actual: '.
                    // This appears to be caused by the rapid frequency of writes to the GPIO
                    // These are being swallowed here/

                    // If you want to see the exceptions uncomment the following:
                    // System.Diagnostics.Debug.WriteLine(ex.ToString());
                }
            }
        }
Exemplo n.º 3
0
 public void ControlRelay(Boolean onoff)
 {
     if (onoff)
     {
         relay.ChangeState(SensorStatus.On);
     }
     else
     {
         relay.ChangeState(SensorStatus.Off);
     }
 }
 private void TimerCallBack(object state)
 {
     if (RelayOnOff == 0)
     {
         RelayOnOff = 1;
         GroveRelay.ChangeState(SensorStatus.On);
     }
     else
     {
         RelayOnOff = 0;
         GroveRelay.ChangeState(SensorStatus.Off);
     }
 }
        private void IotHubCommand_CommandReceived(object sender, CommandEventArgs <string> e)
        {
            switch (e.Item.ToUpper())
            {
            case "ON":
                relay.ChangeState(SensorStatus.On);
                break;

            case "OFF":
                relay.ChangeState(SensorStatus.Off);
                break;

            default:
                break;
            }
        }
Exemplo n.º 6
0
        // Handle the direct method call
        private async Task <MethodResponse> DoAction(MethodRequest methodRequest, object userContext)
        {
            var data   = Encoding.UTF8.GetString(methodRequest.Data);
            var action = JsonConvert.DeserializeObject <DeviceAction>(data);

            // Check the payload is a single integer value
            if (action != null)
            {
                /*
                 * Console.ForegroundColor = ConsoleColor.Green;
                 * Console.WriteLine("Telemetry interval set to {0} seconds", data);
                 * Console.ResetColor();
                 */
                switch (action.ActionName)
                {
                case "WaterIn":
                {
                    var res = bool.Parse(action.Params[0]);
                    WaterInRelay.ChangeState(res ? SensorStatus.On : SensorStatus.Off);
                    display.SetText("WATER IN :" + res);
                }
                break;

                case "WaterOut":
                {
                    var res = bool.Parse(action.Params[0]);
                    WaterOutRelay.ChangeState(res ? SensorStatus.On : SensorStatus.Off);
                    display.SetText("WATER OUT :" + res);
                }
                break;
                }
                // Acknowlege the direct method call with a 200 success message
                string result = "{\"result\":\"Executed direct method: " + methodRequest.Name + "\"}";
                return(new MethodResponse(Encoding.UTF8.GetBytes(result), 200));
            }
            else
            {
                // Acknowlege the direct method call with a 400 error message
                string result = "{\"result\":\"Invalid parameter\"}";
                return(new MethodResponse(Encoding.UTF8.GetBytes(result), 400));
            }
        }
        private void InitWorker()
        {
            IAsyncAction asyncAction = Windows.System.Threading.ThreadPool.RunAsync(
                async(workItem) =>
            {
                int work_index = 0;

                int distance        = 0, sound = 0, light = 0, rotary = 0;
                SensorStatus button = SensorStatus.Off;
                SensorStatus buzzer = SensorStatus.Off;

                while (true)
                {
                    if (work_index < 5)
                    {
                        rotary = GroveRotary.SensorValue();
                        button = GroveButton.CurrentState;

                        //
                        RotaryValue = rotary;
                        int level   = RotaryValue / 100;
                        if (level > 10)
                        {
                            level = 10;
                        }
                        GroveLedBar.SetLevel((byte)level);

                        buzzer = GroveBuzzer.CurrentState;

                        if (RotaryValue > 1000)
                        {
                            if (buzzer != SensorStatus.On)
                            {
                                GroveBuzzer.ChangeState(SensorStatus.On);
                            }
                        }
                        else
                        {
                            if (buzzer != SensorStatus.Off)
                            {
                                GroveBuzzer.ChangeState(SensorStatus.Off);
                            }
                        }

                        if (button == SensorStatus.On)
                        {
                            RelayOnOff = 1;
                            GroveRelay.ChangeState(SensorStatus.On);
                        }
                        else
                        {
                            RelayOnOff = 0;
                            GroveRelay.ChangeState(SensorStatus.Off);
                        }

                        work_index++;
                    }
                    else
                    {
                        // Read temp & humidity
                        GroveTempHumi.Measure();
                        LastTemp = GroveTempHumi.TemperatureInCelsius;
                        LastHumi = GroveTempHumi.Humidity;

                        distance = GroveRanger.MeasureInCentimeters();
                        //System.Diagnostics.Debug.WriteLine(distance);

                        sound = GroveSound.SensorValue();
                        //System.Diagnostics.Debug.WriteLine(sound);

                        light = GroveLight.SensorValue();
                        //System.Diagnostics.Debug.WriteLine(light);

                        if (distance > 0 && distance < 100)
                        {
                            ShiftLeft(DistanceList, distance);
                        }

                        ShiftLeft(SoundList, sound);
                        ShiftLeft(LightList, light);

                        work_index = 0;
                    }

                    await Dispatcher.RunAsync(
                        CoreDispatcherPriority.High,
                        () =>
                    {
                        if (work_index == 0)
                        {
                            UpdataUISlow();
                        }
                        else
                        {
                            UpdateUIFast();
                        }
                    });

                    //Delay.Milliseconds(100);
                }
            }
                );
        }