Example #1
0
        public void Run(IBackgroundTaskInstance taskInstance)
        {
            // Connect the Ultrasonic Sensor to digital port 4
            IUltrasonicRangerSensor sensor = DeviceFactory.Build.UltraSonicSensor(Pin.DigitalPin4);

            // Loop endlessly
            while (true)
            {
                Task.Delay(100).Wait(); //Delay 0.1 second
                try
                {
                    // Check the value of the Ultrasonic Sensor
                    string sensorvalue = sensor.MeasureInCentimeters().ToString();
                    System.Diagnostics.Debug.WriteLine("Ultrasonic reads " + sensorvalue);
                }
                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());
                }
            }
        }
Example #2
0
 private async void OnTick(object sender, object e)
 {
     try
     {
         if (IsConnected)
         {
             var item = new  { WaterDistance = distance.MeasureInCentimeters(), LocalTime = DateTime.Now };
             SendDeviceToCloudMessagesAsync(item);
             display.SetText($"water:{distance.MeasureInCentimeters()} cm");
         }
         else
         {
             Setup();
         }
     }
     catch
     {
         IsConnected = false;
     }
 }
Example #3
0
        private async void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            sendingPhoto = false;
            panicLed     = DeviceFactory.Build.Led(Pin.DigitalPin2);
            infoLed      = DeviceFactory.Build.Led(Pin.DigitalPin4);
            ranger       = DeviceFactory.Build.UltraSonicSensor(Pin.DigitalPin3);
            screen       = DeviceFactory.Build.RgbLcdDisplay();
            screen.SetText("setting up...");
            screen.SetBacklightRgb(0, 0, 200);
            // init mode -> Both Led's are on
            panicLed.ChangeState(SensorStatus.On);
            infoLed.ChangeState(SensorStatus.On);


            // init camera
            camera = new UsbCamera();
            var initWorked = await camera.InitializeAsync();

            // Something went wrong
            if (!initWorked || ranger.MeasureInCentimeters() == -1)
            {
                infoLed.ChangeState(SensorStatus.Off);
                screen.SetText("Camera or Sensor not connected!");
                screen.SetBacklightRgb(200, 0, 0);
                blink(panicLed);
                return;
            }

            // init photobackend

            Microsoft.WindowsAzure.Storage.Auth.StorageCredentials credentials = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(accountName, accountKey);
            //credentials.UpdateSASToken("?sv=2015-04-05&ss=b&srt=sco&sp=rwlac&se=2016-11-20T04:05:54Z&st=2016-11-12T20:05:54Z&spr=https,http&sig=B0zDabRXoO7LfWy5iACsn0sHOnWzvmmrDv8fAqITPgI%3D");
            CloudStorageAccount acc    = new CloudStorageAccount(credentials, true);
            CloudBlobClient     client = acc.CreateCloudBlobClient();

            container = client.GetContainerReference("picture-storage");

            previewElement.Source = camera.MediaCaptureInstance;

            await camera.StartCameraPreview();

            // init finished - turn off panic Led
            infoLed.ChangeState(SensorStatus.Off);
            panicLed.ChangeState(SensorStatus.Off);
            screen.SetText("");
            screen.SetBacklightRgb(0, 0, 0);

            DispatcherTimer mainThread = new DispatcherTimer();

            mainThread.Interval = TimeSpan.FromSeconds(0.5);
            mainThread.Tick    += run;
            mainThread.Start();
        }
        private async Task Distance()
        {
            int distanceRead = 400;

            distanceRead = sensor.MeasureInCentimeters();// best to find a async method

            Debug.WriteLine(" distance=" + distanceRead);
            if (distanceRead < 400 && distanceRead > 0)
            {
                distance = distanceRead;
            }
        }
Example #5
0
        private int getDistance()
        {
            sm.WaitOne();
            int distanceRead = DistSens.MeasureInCentimeters();

            sm.Release();
            if (distanceRead < 400 && distanceRead > 0)
            {
                distance = distanceRead;
            }
            return(distance);
        }
Example #6
0
        private bool userInRange()
        {
            bool isInRange = false;

            for (int i = 0; i < 1; i++)
            {
                if (ranger.MeasureInCentimeters() <= userRange)
                {
                    isInRange = true;
                }
            }
            return(isInRange);
        }
        private string ultrosonic()
        {
            string sensorvalue = "";

            try
            {
                // Check the value of the Ultrasonic Sensor
                sensorvalue = ultrasonic.MeasureInCentimeters().ToString();
                System.Diagnostics.Debug.WriteLine("Ultrasonic value " + sensorvalue);
            }
            catch (Exception ex)
            {
                // If you want to see the exceptions uncomment the following:
                System.Diagnostics.Debug.WriteLine(ex.ToString());
            }

            return(sensorvalue);
        }
Example #8
0
        private void TimerCallBack1(object state)
        {
            try
            {
                Bobdataset data = new Bobdataset();

                data.Run_id = DateTime.Now.ToString("yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss.fffffffK");

                data.Steer_angl = RotaryEncoder.Read3();
                Delay.Milliseconds(15);

                data.Dist_left = GroveRangerLeft.MeasureInCentimeters();
                Delay.Milliseconds(15);

                data.Dist_right = GroveRangerRight.MeasureInCentimeters();
                Delay.Milliseconds(15);

                output1      = gyro.GetAcceleration();
                data.Accel_x = output1[0];
                data.Accel_y = output1[1];
                data.Accel_z = output1[2];
                Delay.Milliseconds(15);

                output2     = gyro.GetRotation();
                data.Gyro_x = output2[0];
                data.Gyro_y = output2[1];
                data.Gyro_z = output2[2];

                Insert_Into_DB(data);

                counterino++;

                var UItask = this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                {
                    if (counterino % 5 == 0)
                    {
                        COUNTERBOX.Text = counterino.ToString();
                    }
                });
            }
            catch (Exception)
            {
            }
        }
        private void rpiRun()
        {
            try
            {
                var tmp = GroveRanger.MeasureInCentimeters();
                SGroveUltrasonicSensor = "Distace: " + tmp.ToString() + "cm";
            }
            catch (Exception /*ex*/)
            {
            }

            var UItask = this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
            {
                GroveUltrasonicUI.Text = SGroveUltrasonicSensor;
                //Temperature.Text = temperature;

                /*
                 * O2PPM.Text = O2;
                 * Moisture.Text = moisture;
                 */
            });
        }
Example #10
0
        public void Run(IBackgroundTaskInstance taskInstance)
        {
            // LCD - This screen is I2C
            IRgbLcdDisplay LCD = DeviceFactory.Build.RgbLcdDisplay();

            LCD.SetBacklightRgb(255, 255, 255);
            LCD.SetText("Hello world!"); // Not sure what colour this will show up in

            // LEDs
            ILed red  = DeviceFactory.Build.Led(Pin.DigitalPin2);
            ILed blue = DeviceFactory.Build.Led(Pin.DigitalPin3);

            // Ultrasonic
            IUltrasonicRangerSensor Ultrasonic = DeviceFactory.Build.UltraSonicSensor(Pin.DigitalPin4);

            // Temperature and Humidity
            // TODO: Double check Sensor model number. Assumed DHT11 from the GrovePi+ Starter Kit
            IDHTTemperatureAndHumiditySensor tempHumidity = DeviceFactory.Build.DHTTemperatureAndHumiditySensor(Pin.DigitalPin5, DHTModel.Dht11);

            // Sound sensor
            ISoundSensor Sound = DeviceFactory.Build.SoundSensor(Pin.AnalogPin0);

            // LDR
            IRotaryAngleSensor LDR = DeviceFactory.Build.RotaryAngleSensor(Pin.AnalogPin1);

            while (true)
            {
                Task.Delay(100).Wait();

                try
                {
                    // Ultrasonic sensor
                    int distance = Ultrasonic.MeasureInCentimeters();
                    Debug.WriteLine("Distance: " + distance.ToString());
                    // TODO - tune to distance of door
                    //if(distance < 50)

                    // LDR
                    int lightLevel = LDR.SensorValue();
                    Debug.WriteLine("Light Level: " + lightLevel.ToString());

                    // LEDs
                    red.ChangeState(SensorStatus.On);
                    blue.ChangeState(SensorStatus.On);

                    // Temperature Humidity
                    tempHumidity.Measure();
                    double temp_degC = tempHumidity.TemperatureInCelsius;
                    double humidity  = tempHumidity.Humidity;
                    Debug.WriteLine("Temperature: " + temp_degC + "\tHumidity: " + humidity);

                    // Sound sensor
                    int soundLevel = Sound.SensorValue();
                    Debug.WriteLine("Sound Level: " + soundLevel);

                    // TODO: Send data to Azure
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }
        }
Example #11
0
        private void OnTick(object sender, object e)
        {
            try
            {
                // Check the value of the button.

                string buttonon = button.CurrentState.ToString();
                // bool buttonison = buttonon.Equals("On", StringComparison.OrdinalIgnoreCase);
                System.Diagnostics.Debug.WriteLine("Button is " + buttonon);
            }
            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());
            }

            try
            {
                System.Diagnostics.Debug.WriteLine("Brightness: " + brightness.ToString());

                // Check the brightness, if it's going to overflow, reset it.
                if (brightness > 250)
                {
                    brightness = 0;
                }

                // Increase the brightness by 5 points.
                brightness = brightness + 5;
                // Write the values to the three LEDs.
                // USA!  Red, White, and Blue!
                Led1.AnalogWrite(Convert.ToByte(brightness));
            }

            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());
            }
            try
            {
                // Check the value of the button.

                string buttonon   = button.CurrentState.ToString();
                bool   buttonison = buttonon.Equals("On", StringComparison.OrdinalIgnoreCase);

                // Check the state of the buzzer.  This is just to output to debug!
                SensorStatus status   = buzzer.CurrentState;
                bool         buzzeron = status.ToString().Equals("On", StringComparison.OrdinalIgnoreCase);

                // Print out Diagnostics.
                System.Diagnostics.Debug.WriteLine("Button is " + buttonon);
                System.Diagnostics.Debug.WriteLine("Buzzer is " + status.ToString());

                // If the Button is on . . . .
                if (buttonison)
                {
                    buzzer.ChangeState(GrovePi.Sensors.SensorStatus.On);
                }
                else
                {
                    buzzer.ChangeState(GrovePi.Sensors.SensorStatus.Off);
                }
                try
                {
                    // Check the value of the button, turn it into a string.
                    string sensorvalue = light1.SensorValue().ToString();
                    System.Diagnostics.Debug.WriteLine("light is " + sensorvalue);
                }
                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());
                }
            }
            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());
            }
            //Task.Delay(100).Wait();     // Delay 0.1 second
            // We need to make sure we delay here.  If we don't, we won't be able to read
            // the LCD Screen.
            try
            {
                // First, output to the LCD Display.
                display.SetText("Light:" + light1.SensorValue()).SetBacklightRgb(255, 50, 255);
                // Then output to the debug window.
                System.Diagnostics.Debug.WriteLine("Hello from Dexter Industries!");
            }
            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());
            }
            try
            {
                // Check the value of the Ultrasonic Sensor
                string sensorvalue = distance1.MeasureInCentimeters().ToString();
                System.Diagnostics.Debug.WriteLine("Ultrasonic reads " + sensorvalue);
            }
            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());
            }
        }
        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);
                }
            }
                );
        }
Example #13
0
 public int GetRange()
 {
     return(_ultrasonicRanger.MeasureInCentimeters());
 }
 //Read values of Ultrasonic Sensors
 public void ReadDistanceSensors()
 {
     _distAValue = _distSensorA.MeasureInCentimeters();
     _distBValue = _distSensorB.MeasureInCentimeters();
 }