private async Task SendDeviceToCloudMessageAsync(string status, BMP280SensorData BMP280SensorData, MCP3008SensorData MCP3008SensorData) { ConferenceRoomDataPoint conferenceRoomDataPoint = new ConferenceRoomDataPoint() { DeviceId = deviceName, Time = DateTime.UtcNow.ToString("o"), RoomTemp = BMP280SensorData.Temperature.ToString(), RoomPressure = BMP280SensorData.Pressure.ToString(), RoomAlt = BMP280SensorData.Altitude.ToString(), LightStatus = MCP3008SensorData.lightStatus, LightCDSValue = MCP3008SensorData.cdsReadVal.ToString(), LightCDSVoltageValue = MCP3008SensorData.cdsVoltage.ToString(), RoomStatus = status }; if (status == "Occupied") { conferenceRoomDataPoint.Color = "Red"; } else { conferenceRoomDataPoint.Color = "Green"; } var jsonString = JsonConvert.SerializeObject(conferenceRoomDataPoint); //var jsonStringInBytes = new Message(Encoding.ASCII.GetBytes(jsonString)); await AzureIoTHub.SendDeviceToCloudMessageAsync(jsonString); Debug.WriteLine("{0} > Sending message: {1}", DateTime.UtcNow, jsonString); }
public async void Run(IBackgroundTaskInstance taskInstance) { // Using BackgroundTaskDeferral // as described in http://aka.ms/backgroundtaskdeferral BackgroundTaskDeferral deferral = taskInstance.GetDeferral(); bmpsensor = new BMP280(); await bmpsensor.Initialize(); mcp3008 = new MCP3008(ReferenceVoltage); mcp3008.Initialize(); InitGPIO(); bmpSensorData = new BMP280SensorData(); adcSensorData = new MCP3008SensorData(); string roomstatus = String.Empty; while (true) { Debug.WriteLine("Reading taken at " + DateTime.UtcNow); #region Read pressure and temperature data from sensor bmpSensorData.Temperature = await bmpsensor.ReadTemperature(); bmpSensorData.Pressure = await bmpsensor.ReadPreasure(); Debug.WriteLine(bmpSensorData.Temperature + " deg C"); Debug.WriteLine(bmpSensorData.Pressure + " Pa"); Debug.WriteLine(bmpSensorData.Pressure / 100000 + " Bar"); Debug.WriteLine(bmpSensorData.Pressure / 100 + " milliBar"); #endregion #region Reading Lighting Information from Sensor adcSensorData = ReadLightStatusInRoom(); Debug.WriteLine("Light Status in Room: " + adcSensorData.lightStatus); #endregion #region Read the motion sensor // If sensor pin is high, then motion was detected if (pir.Read() == GpioPinValue.High) { // turn on the LED led.Write(GpioPinValue.Low); Debug.WriteLine("Motion detected - Room is occupied"); roomstatus = "Occupied"; } else { // turn off the LED led.Write(GpioPinValue.High); Debug.WriteLine("No motion detected - Room is vacant"); roomstatus = "Vacant"; } #endregion await SendDeviceToCloudMessageAsync(roomstatus, bmpSensorData, adcSensorData); await Task.Delay(10000); } // Once the asynchronous method(s) are done, close the deferral. // deferral.Complete(); }