示例#1
0
        private static async void SendDeviceToCloudMessagesAsync()
        {
            Random rand = new Random();

            while (true)
            {
                double x, y, z;
                Console.WriteLine("masuk loop");
                hat.GetAcceleration(out x, out y, out z);

                var Light    = hat.GetLightLevel();
                var Temp     = hat.GetTemperature();
                var Accel    = $"({x:N2}, {y:N2}, {z:N2})";
                var Button18 = hat.IsDIO18Pressed().ToString();
                var Button22 = hat.IsDIO22Pressed().ToString();
                var Analog1  = hat.ReadAnalog(GIS.FEZHAT.AnalogPin.Ain1).ToString("N2");

                var telemetryDataPoint = new
                {
                    deviceId     = "FezHat",
                    temperature  = Temp,
                    light        = Light,
                    acceleration = Accel,
                    button18     = Button18,
                    Button22     = Button22,
                    analog       = Analog1
                };
                Console.WriteLine("data telemetri");
                var messageString = JsonConvert.SerializeObject(telemetryDataPoint);
                var message       = new Message(Encoding.ASCII.GetBytes(messageString));
                message.Properties.Add("temperatureAlert", (Temp > 30) ? "true" : "false");
                Console.WriteLine("siap kirim");
                await deviceClient.SendEventAsync(message);

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

                await Task.Delay(2000);
            }
        }
示例#2
0
        private static void TimerCallback(Object o)
        {
            double x, y, z;

            hat.GetAcceleration(out x, out y, out z);

            var LightTextBox    = hat.GetLightLevel().ToString("P2");
            var TempTextBox     = hat.GetTemperature().ToString("N2");
            var AccelTextBox    = $"({x:N2}, {y:N2}, {z:N2})";
            var Button18TextBox = hat.IsDIO18Pressed().ToString();
            var Button22TextBox = hat.IsDIO22Pressed().ToString();
            var AnalogTextBox   = hat.ReadAnalog(GIS.FEZHAT.AnalogPin.Ain1).ToString("N2");

            Console.WriteLine($"light : {LightTextBox} - Temp : {TempTextBox} - Accel : {AccelTextBox} -" +
                              $"Button18 : {Button18TextBox} - Button22 : {Button22TextBox} - Analog : {AnalogTextBox}");
            if ((i++ % 5) == 0)
            {
                var LedsTextBox = next.ToString();
                Console.WriteLine($"Led : {LedsTextBox}");
                hat.DIO24On  = next;
                hat.D2.Color = next ? GIS.FEZHAT.Color.Green : GIS.FEZHAT.Color.Black;
                hat.D3.Color = next ? GIS.FEZHAT.Color.Blue : GIS.FEZHAT.Color.Black;

                hat.WriteDigital(GIS.FEZHAT.DigitalPin.DIO16, next);
                hat.WriteDigital(GIS.FEZHAT.DigitalPin.DIO26, next);

                hat.SetPwmDutyCycle(GIS.FEZHAT.PwmPin.Pwm5, next ? 1.0 : 0.0);
                hat.SetPwmDutyCycle(GIS.FEZHAT.PwmPin.Pwm6, next ? 1.0 : 0.0);
                hat.SetPwmDutyCycle(GIS.FEZHAT.PwmPin.Pwm7, next ? 1.0 : 0.0);
                hat.SetPwmDutyCycle(GIS.FEZHAT.PwmPin.Pwm11, next ? 1.0 : 0.0);
                hat.SetPwmDutyCycle(GIS.FEZHAT.PwmPin.Pwm12, next ? 1.0 : 0.0);

                next = !next;
            }

            if (hat.IsDIO18Pressed())
            {
                hat.S1.Position += 5.0;
                hat.S2.Position += 5.0;

                if (hat.S1.Position >= 180.0)
                {
                    hat.S1.Position = 0.0;
                    hat.S2.Position = 0.0;
                }
            }

            if (hat.IsDIO22Pressed())
            {
                if (hat.MotorA.Speed == 0.0)
                {
                    hat.MotorA.Speed = 0.5;
                    hat.MotorB.Speed = -0.7;
                }
            }
            else
            {
                if (hat.MotorA.Speed != 0.0)
                {
                    hat.MotorA.Speed = 0.0;
                    hat.MotorB.Speed = 0.0;
                }
            }
        }