示例#1
0
        public static async Task SendSenseHatDataToCloudAsync(SenseHatData data)
        {
            var deviceClient  = DeviceClient.CreateFromConnectionString(DeviceConnectionString, TransportType.Http1);
            var messageInJson = JsonConvert.SerializeObject(data);
            var message       = new Message(Encoding.UTF8.GetBytes(messageInJson));

            await deviceClient.SendEventAsync(message);
        }
示例#2
0
        private async void Timer_Tick(object sender, object e)
        {
            senseHat.Sensors.HumiditySensor.Update();
            senseHat.Sensors.PressureSensor.Update();

            //gather data
            SenseHatData data = new SenseHatData();

            data.Temperature = senseHat.Sensors.Temperature;
            data.Humidity    = senseHat.Sensors.Humidity;
            data.Pressure    = senseHat.Sensors.Pressure;

            //send them to the cloud
            await AzureIoTHub.SendSenseHatDataToCloudAsync(data);

            //notify UI
            TempText.Text     = data.Temperature.ToString();
            TempHumidity.Text = data.Humidity.ToString();
            TempPressure.Text = data.Pressure.ToString();
        }
示例#3
0
        private async void SendDeviceToCloudMessagesAsync()
        {
            Microsoft.Devices.Tpm.TpmDevice myDevice = new Microsoft.Devices.Tpm.TpmDevice(0); // Use logical device 0 on the TPM by default
            string hubUri   = myDevice.GetHostName();
            string deviceId = myDevice.GetDeviceId();
            string sasToken = myDevice.GetSASToken();

            double minTemperature = 20;
            double minHumidity    = 60;
            Random rand           = new Random();

            while (true)
            {
                try
                {
                    //deviceClient = DeviceClient.CreateFromConnectionString(DeviceConnectionString, TransportType.Mqtt);
                    var deviceClientsend = DeviceClient.CreateFromConnectionString(DeviceConnectionString, TransportType.Http1);

                    double currentTemperature = minTemperature + rand.NextDouble() * 15;
                    double currentHumidity    = minHumidity + rand.NextDouble() * 20;

                    //get a reference to SenseHat
                    senseHat = await SenseHatFactory.GetSenseHat();

                    senseHat.Sensors.ImuSensor.Update();
                    senseHat.Sensors.HumiditySensor.Update();
                    senseHat.Sensors.PressureSensor.Update();



                    //gather data
                    SenseHatData data = new SenseHatData();
                    data.Temperature = senseHat.Sensors.Temperature;
                    data.Humidity    = senseHat.Sensors.Humidity;
                    data.Pressure    = senseHat.Sensors.Pressure;

                    //notify UI
                    TempText.Text     = data.Temperature.ToString();
                    TempHumidity.Text = data.Humidity.ToString();
                    TempPressure.Text = data.Pressure.ToString();

                    if (senseHat.Sensors.Acceleration.HasValue)
                    {
                        Acceleration al = new Acceleration();
                        al.X = senseHat.Sensors.Acceleration.Value.X;
                        al.Y = senseHat.Sensors.Acceleration.Value.Y;
                        al.Z = senseHat.Sensors.Acceleration.Value.Z;

                        TempAcceleration.Text = " X= " + al.X.ToString() + " Y= " + al.Y.ToString() + " Z= " + al.Z.ToString();

                        data.Acceleration = al;
                    }
                    if (senseHat.Sensors.Gyro.HasValue)
                    {
                        Gyro gy = new Gyro();
                        gy.X = senseHat.Sensors.Gyro.Value.X;
                        gy.Y = senseHat.Sensors.Gyro.Value.Y;
                        gy.Z = senseHat.Sensors.Gyro.Value.Z;

                        TempGyro.Text = " X= " + gy.X.ToString() + " Y= " + gy.Y.ToString() + " Z= " + gy.Z.ToString();

                        data.Gyro = gy;
                    }
                    if (senseHat.Sensors.Pose.HasValue)
                    {
                        Pose po = new Pose();
                        po.X = senseHat.Sensors.Pose.Value.X;
                        po.Y = senseHat.Sensors.Pose.Value.Y;
                        po.Z = senseHat.Sensors.Pose.Value.Z;

                        TempPose.Text = " X= " + po.X.ToString() + " Y= " + po.Y.ToString() + " Z= " + po.Z.ToString();

                        data.po = po;
                    }
                    if (senseHat.Sensors.MagneticField.HasValue)
                    {
                        MagneticField mf = new MagneticField();
                        mf.X = senseHat.Sensors.MagneticField.Value.X;
                        mf.Y = senseHat.Sensors.MagneticField.Value.Y;
                        mf.Z = senseHat.Sensors.MagneticField.Value.Z;

                        TempMagneticField.Text = " X= " + mf.X.ToString() + " Y= " + mf.Y.ToString() + " Z= " + mf.Z.ToString();

                        data.mf = mf;
                    }


                    data.deviceID = deviceId;

                    //send them to the cloud
                    //await AzureIoTHub.SendSenseHatDataToCloudAsync(data);



                    var    messageString = JsonConvert.SerializeObject(data);
                    string levelValue;

                    //if (rand.NextDouble() > 0.7)
                    //{
                    //    messageString = "This is a critical message";
                    //    levelValue = "critical";
                    //}
                    //else
                    //{
                    //    levelValue = "normal";
                    //}
                    levelValue = "normal";
                    var message = new Message(Encoding.ASCII.GetBytes(messageString));
                    message.Properties.Add("level", levelValue);

                    if (deviceClientsend == null)
                    {
                        deviceClientsend = DeviceClient.CreateFromConnectionString(DeviceConnectionString, TransportType.Mqtt);
                    }

                    await deviceClientsend.SendEventAsync(message);

                    //Console.WriteLine("{0} > Sent message: {1}", DateTime.Now, messageString);

                    //string _scrollText = "Hello Pi 3";

                    ////now update the Sensor HAT UI
                    //// Create the font from the image.
                    //MultiColorFont font = MultiColorFont.LoadFromImage(
                    //    new Uri("ms-appx:///Assets/ColorFont.png"),
                    //    " ABCDEFGHIJKLMNOPQRSTUVWXYZÅÄÖÉÜabcdefghijklmnopqrstuvwxyzåäöéü0123456789.,?!\"#$%&-+*:;/\\<>()'`=",
                    //    Color.FromArgb(0xFF, 0xFF, 0x00, 0xFF)).Result;

                    //// Get the characters to scroll.
                    //IEnumerable<MultiColorCharacter> characters = font.GetChars(_scrollText);

                    //// Choose a background color (or draw your own more complex background!)
                    //Color backgroundColor = Color.FromArgb(0xFF, 0x00, 0x20, 0x00);

                    //// Create the character renderer.
                    //var characterRenderer = new MultiColorCharacterRenderer();

                    //// Create the text scroller.
                    //var textScroller = new TextScroller<MultiColorCharacter>(
                    //    senseHat.Display,
                    //    characterRenderer,
                    //    characters);

                    //// Clear the display.
                    //senseHat.Display.Fill(backgroundColor);

                    //// Draw the scroll text.
                    //textScroller.Render();

                    //// Update the physical display.
                    //senseHat.Display.Update();

                    connectionstatus.Text = "Connected " + DateTime.Now.ToString();
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("Error in sample SendDeviceToCloudMessagesAsync: {0}", ex.Message);
                }
                await Task.Delay(30000);
            }
        }