Example #1
0
        private async void DeviceToCloudMessage()
        {
            await _sensor.Initialize();

            float temperature = 0.00f;
            float humidity    = 0.00f;

            while (true)
            {
                temperature = await _sensor.ReadTemperature();

                humidity = await _sensor.ReadHumidity();

                var sensorData = new
                {
                    date = String.Format("{0}, {1}, {2}",
                                         DateTime.Now.ToLocalTime().TimeOfDay.Hours,
                                         DateTime.Now.ToLocalTime().TimeOfDay.Minutes,
                                         DateTime.Now.ToLocalTime().TimeOfDay.Seconds),
                    temperature = Math.Round(temperature, 2),
                    humidity    = Math.Round(humidity, 2)
                };
                var messageString = JsonConvert.SerializeObject(sensorData);
                var message       = new Message(byteArray: Encoding.ASCII.GetBytes(messageString));
                await deviceClient.SendEventAsync(message);

                Debug.WriteLine("{0} > Sending message: {1}", DateTime.Now, messageString);
                Task.Delay(10000).Wait(); // 11hours running not to exceed the limit of 8000 messages
            }
        }
Example #2
0
        private async void DeviceToCloudMessage()
        {
            await _sensor.Initialize();

            float temperature = 0.00f;
            float humidity    = 0.00f;
            int   mId         = 0;

            while (true)
            {
                temperature = await _sensor.ReadTemperature();

                humidity = await _sensor.ReadHumidity();

                var sensorData = new
                {
                    messageId   = mId,
                    deviceId    = "Real Raspberry Device",
                    temperature = Math.Round(temperature, 5),
                    humidity    = Math.Round(humidity, 5)
                };
                var messageString = JsonConvert.SerializeObject(sensorData);
                var message       = new Message(Encoding.ASCII.GetBytes(messageString));
                await deviceClient.SendEventAsync(message);

                Debug.WriteLine("{0} > Sending message: {1}", DateTime.Now, messageString);
                mId++;
                Task.Delay(5000).Wait();
            }
        }
        private async void InitializeDevices()
        {
            var gpio = GpioController.GetDefault();

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

            // set up interrupt pin for proximity sensor
            interruptPin = gpio.OpenPin(intPinNumber);

            // pull up interrupt pin as sensor will pull down to notify
            interruptPin.SetDriveMode(GpioPinDriveMode.InputPullUp);

            // initialize BME280 Sensor + Intitialize VNCL4010
            await Task.WhenAll(bme280Sensor.Initialize(), vncl4010Sensor.Initialize());

            // listen to interrupt pin changes
            interruptPin.ValueChanged += OnInterrupt;

            GpioStatus.Text = "Connecting to IoT Hub...";

            await IoTHub.ConnectToIoTHub();

            GpioStatus.Text = "";

            // start measuring temperature and proximity
            measureTimer.Start();
        }
Example #4
0
        private async void DeviceToCloudMessage()
        {
            await _sensor.Initialize();

            float temperature = 0.00f;
            float humidity    = 0.00f;

            while (true)
            {
                temperature = await _sensor.ReadTemperature();

                humidity = await _sensor.ReadHumidity();

                var telemetryDataPoint = new
                {
                    date = String.Format("{0}, {1}, {2}",
                                         DateTime.Now.ToLocalTime().TimeOfDay.Hours,
                                         DateTime.Now.ToLocalTime().TimeOfDay.Minutes,
                                         DateTime.Now.ToLocalTime().TimeOfDay.Seconds),
                    temp  = Math.Round(temperature, 2),
                    humid = Math.Round(humidity, 2)
                };
                var messageString = JsonConvert.SerializeObject(telemetryDataPoint);
                var message       = new Message(Encoding.ASCII.GetBytes(messageString));

                await deviceClient.SendEventAsync(message);

                Debug.WriteLine("{0} > Sending message: {1}", DateTime.Now, messageString);

                Task.Delay(5000).Wait();
            }
        }
        private async void InitializeDevices()
        {
            #region Old
            //var gpio = GpioController.GetDefault();
            //// show an error if there is no GPIO controller
            //if (gpio is null)
            //{
            //    //interruptPin = null;
            //    GpioStatus.Text = "There is no GPIO controller on this device.";
            //    return;
            //}
            // set up interrupt pin for proximity sensor
            //interruptPin = gpio.OpenPin(intPinNumber);

            //// pull up interrupt pin as sensor will pull down to notify
            //interruptPin.SetDriveMode(GpioPinDriveMode.InputPullUp);

            // initialize BME280 Sensor + Intitialize VNCL4010
            //await Task.WhenAll(bme280Sensor.Initialize(), vncl4010Sensor.Initialize()); //hhe
            #endregion Old

            await Task.WhenAll(bme280Sensor.Initialize());

            GpioStatus.Text = "Connecting to WebAPI...";

            //await IoTHub.ConnectToIoTHub();  //hhe
            //RunAsync().GetAwaiter().GetResult();
            await webApiClient.PostDevice();

            GpioStatus.Text = "";

            // start measuring temperature and proximity
            measureTimer.Start();
        }
Example #6
0
        public async Task InitializeAsync()
        {
            _bme280 = new BME280Sensor(BME280_I2C_ADDRESS);
            await _bme280.Initialize();

            IsInitialized = true;
        }
Example #7
0
        public async Task <Bme280Controller> Initialise()
        {
            _bme280 = new BME280Sensor();
            await _bme280.Initialize();

            return(this);
        }
        protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            _bme280 = new BME280Sensor();
            await _bme280.Initialize();

            _timer          = new DispatcherTimer();
            _timer.Interval = TimeSpan.FromSeconds(1);
            _timer.Tick    += _timer_Tick;

            _timer.Start();
        }
        protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            _bme280 = new BuildAzure.IoT.Adafruit.BME280.BME280Sensor();
            await _bme280.Initialize();

            deviceClient = DeviceClient.Create(iotHubUri, new DeviceAuthenticationWithRegistrySymmetricKey(deviceId, deviceKey), TransportType.Mqtt);

            _timer          = new DispatcherTimer();
            _timer.Interval = TimeSpan.FromSeconds(60); //Para enviar al IoTHub 4 mensajes por minuto - 240 a la hora - 5.760 al día (el límite son 8.000)
            _timer.Tick    += _timer_Tick;

            _timer.Start();
        }
Example #10
0
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            _deferral = taskInstance.GetDeferral();

            // Create a new object for our sensor class
            IBME280Sensor ibme280Sensor = new BME280Sensor();

            // Initialize the sensor
            await ibme280Sensor.Initialize();

            // Initialize them to 0.
            float temperature = 0;
            float pressure    = 0;
            float altitude    = 0;
            float humidity    = 0;
            // Create a constant for pressure at sea level.
            // This is based on your local sea level pressure (Unit: Hectopascal)
            const float seaLevelPressure = 1022.00f;

            // Read 10 samples of the data
            for (int i = 0; i < 10; i++)
            {
                temperature = await ibme280Sensor.ReadTemperature();

                //pressure = await BME280.ReadPreasure();
                //altitude = await BME280.ReadAltitude(seaLevelPressure);
                //humidity = await BME280.ReadHumidity();
                //Write the values to your debug console
                Debug.WriteLine($"Temperature: {temperature} deg C");
                //Debug.WriteLine("Humidity: " + humidity.ToString() + " %");
                //Debug.WriteLine("Pressure: " + pressure.ToString() + " Pa");
                //Debug.WriteLine("Altitude: " + altitude.ToString() + " m");
                Debug.WriteLine("");
            }

            taskInstance.Canceled += TaskInstanceOnCanceled;

            _deferral.Complete();
            //await AzureIoTHub.ReceiveCloudToDeviceMessageAsync();
        }
Example #11
0
        private async Task <bool> InitSensors()
        {
            Log.Info("Initializing sensors");
            OnStatusChanged("Initialising sensors");
            // Initialize the BMP180 Sensor
            try
            {
                OnStatusChanged("Initialising sensors..BMP180");
                bmp180 = new Bmp180Sensor();
                await bmp180.InitializeAsync();
            }
            catch (Exception ex)
            {
                Log.Info("BMP180 Init Error", ex);
                bmp180 = null;
            }

            try
            {
                OnStatusChanged("Initialising sensors..BME280");
                bme280 = new BME280Sensor();
                // Initialize BME280 Sensor
                await bme280.Initialize(0x76);

                if (bme280ForceMode)
                {
                    await bme280.SetSampling(SensorMode.MODE_FORCED,
                                             SensorSampling.SAMPLING_X1, // temperature
                                             SensorSampling.SAMPLING_X1, // pressure
                                             SensorSampling.SAMPLING_X1, // humidity
                                             SensorFilter.FILTER_OFF,
                                             StandbyDuration.STANDBY_MS_1000);
                }
            }
            catch (Exception ex)
            {
                Log.Info("BME280 Init Error", ex);
                bme280 = null;
            }

            try
            {
                OnStatusChanged("Initialising sensors..DHT22");
                GpioController controller = GpioController.GetDefault();
                dhtPin    = GpioController.GetDefault().OpenPin(DHT22_Pin, GpioSharingMode.Exclusive);
                dhtSensor = new Dht22(dhtPin, GpioPinDriveMode.InputPullUp);
            }
            catch (Exception ex)
            {
                Log.Info("DHT22 Init Error", ex);
                dhtSensor = null;
            }
            string status = "";

            if (bmp180 != null || bme280 != null || dhtSensor != null)
            {
                if (bmp180 != null)
                {
                    status += "BMP180";
                }
                if (bme280 != null)
                {
                    if (status.Length > 0)
                    {
                        status += ", ";
                    }
                    status += "BME280";
                }
                if (dhtSensor != null)
                {
                    if (status.Length > 0)
                    {
                        status += ", ";
                    }
                    status += "DHT22";
                }
                status = $"Found: {status} sensors";
            }
            else
            {
                status = "No sensor found";
            }
            OnStatusChanged(status);
            await Task.Delay(500);

            Log.Trace($"end");
            Log.Info(status);
            return(bmp180 != null || bme280 != null || dhtSensor != null);
        }