Ejemplo n.º 1
0
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            deferral = taskInstance.GetDeferral();

            hubUser           = $"{cm.hubAddress}/{cm.hubName}";
            hubTopicPublish   = $"devices/{cm.hubName}/messages/events/";
            hubTopicSubscribe = $"devices/{cm.hubName}/messages/devicebound/#";

            this.hat = await FEZHAT.CreateAsync();

            // https://m2mqtt.wordpress.com/m2mqtt_doc/
            client = new MqttClient(cm.hubAddress, 8883, true, MqttSslProtocols.TLSv1_2);
            client.MqttMsgPublishReceived += Client_MqttMsgPublishReceived;
            client.Subscribe(new string[] { hubTopicSubscribe }, new byte[] { 0 });

            var result = Task.Run(async() => {
                while (true)
                {
                    if (!client.IsConnected)
                    {
                        client.Connect(cm.hubName, hubUser, cm.hubPass);
                    }
                    if (client.IsConnected)
                    {
                        client.Publish(hubTopicPublish, temperature.ToJson(hat.GetTemperature()));
                        client.Publish(hubTopicPublish, light.ToJson(hat.GetLightLevel()));
                    }
                    await Task.Delay(30000); // don't leave this running for too long at this rate as you'll quickly consume your free daily Iot Hub Message limit
                }
            });
        }
        public async Task InitializeHat()
        {
            FezHat = await FEZHAT.CreateAsync();

            FezHat.D2.TurnOff();
            FezHat.D3.TurnOff();
        }
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            #region Expand to view variable initialisation
            deferral = taskInstance.GetDeferral();
            hat      = await FEZHAT.CreateAsync();

            deviceClient = ConnectionString.StartsWith("Connection") ? null : DeviceClient.CreateFromConnectionString(ConnectionString);
            telemetry    = new Telemetry("Sydney", Publish, 10);
            Command_Processing();
            #endregion

            #region Code snippets lab2 and lab3 to go between the #region and #endregion tags


            while (true)
            {
                hat.D2.Color = FEZHAT.Color.Red;
                await Task.Delay(500);

                hat.D2.Color = FEZHAT.Color.Green;
                await Task.Delay(500);

                hat.D2.Color = FEZHAT.Color.Blue;
                await Task.Delay(500);
            }

            #endregion
        }
Ejemplo n.º 4
0
        private async void setup()
        {
            try
            {
                //0.IoTHub client
                m_clt = DeviceClient.CreateFromConnectionString(TKConnectionString, TransportType.Http1);
                Task.Run(() => ReceiveDataFromAzure()); //Loop.

                //1. Fez Hat
                m_hat = await GHIElectronics.UWP.Shields.FEZHAT.CreateAsync();

                //2. Timest
                m_t          = new DispatcherTimer();
                m_t.Interval = TimeSpan.FromMilliseconds(5000);
                m_t.Tick    += M_t_Tick;

                //Can enable UI
                txtAll.IsEnabled = tgSend.IsEnabled = true;
                tgSend.IsOn      = true;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
                txtState.Text = ex.ToString();
            }
        }
Ejemplo n.º 5
0
 private async void MainPage_Loaded(object sender, RoutedEventArgs e)
 {
     _hat = await FEZHAT.CreateAsync();
     _timer = new DispatcherTimer();
     _timer.Interval = TimeSpan.FromMilliseconds(1000);
     _timer.Tick += _timer_Tick;
     _timer.Start();
 }
Ejemplo n.º 6
0
        public void Init()
        {
            var task = FEZHAT.CreateAsync();

            task.Wait();

            _instance = task.Result;
        }
Ejemplo n.º 7
0
        //private bool _complete;

        public UltrasonicDistanceSensor(FEZHAT hat)
        {
            _stopwatch        = new Stopwatch();
            _ticks_per_second = Stopwatch.Frequency;
            _tick_resolution  = (long)(RESOLUTION_cm * _ticks_per_second / SPEED_OF_SOUND_cmps * 2.0);
            _hat = hat;
            _hat.WriteDigital(FEZHAT.DigitalPin.DIO26, false); // clear trigger
        }
Ejemplo n.º 8
0
        private async Task SetupHatAsync()
        {
            this.hat = await FEZHAT.CreateAsync();

            this.telemetryTimer          = new DispatcherTimer();
            this.telemetryTimer.Interval = TimeSpan.FromMilliseconds(2000);
            this.telemetryTimer.Tick    += this.TelemetryTimer_Tick;
            this.telemetryTimer.Start();
        }
Ejemplo n.º 9
0
        private async void InitializeLight()
        {
            this.hat = await FEZHAT.CreateAsync();

            this.timer          = new DispatcherTimer();
            this.timer.Interval = TimeSpan.FromMilliseconds(300);
            this.timer.Tick    += Timer_Tick;
            this.timer.Start();
        }
Ejemplo n.º 10
0
        private async void SetupHat()
        {
            this.hat = await FEZHAT.CreateAsync();
            
            this.timer = new DispatcherTimer();

            this.timer.Interval = TimeSpan.FromMilliseconds(500);
            this.timer.Tick += this.Timer_Tick;

            this.timer.Start();
        }
        private async void Setup()
        {
            hat = await FEZHAT.CreateAsync();

            hat.S1.SetLimits(500, 2400, 0, 180);
            hat.S2.SetLimits(500, 2400, 0, 180);

            timer          = new DispatcherTimer();
            timer.Interval = TimeSpan.FromMilliseconds(100);
            timer.Tick    += OnTick;
        }
Ejemplo n.º 12
0
        private async void SetupHat()
        {
            this.hat = await FEZHAT.CreateAsync();

            this.timer = new DispatcherTimer();

            this.timer.Interval = TimeSpan.FromMilliseconds(500);
            this.timer.Tick    += this.Timer_Tick;

            this.timer.Start();
        }
Ejemplo n.º 13
0
        private async void SetupHat()
        {
            _hat = await FEZHAT.CreateAsync();

            _timer = new DispatcherTimer
            {
                Interval = TimeSpan.FromMilliseconds(400)
            };
            _timer.Tick += Timer_Tick;
            _timer.Start();
        }
Ejemplo n.º 14
0
        private async void Start()
        {
            redBrush   = new SolidColorBrush(Colors.Red);
            blueBrush  = new SolidColorBrush(Colors.Blue);
            blackBrush = new SolidColorBrush(Colors.Black);
            hat        = await FEZHAT.CreateAsync();

            timer          = new DispatcherTimer();
            timer.Interval = new TimeSpan(0, 0, 0, 0, 500);
            timer.Tick    += Timer_Tick;
            timer.Start();
        }
		private async void Setup()
		{
			hat = await FEZHAT.CreateAsync();

			hat.S1.SetLimits(500, 2400, 0, 180);
			hat.S2.SetLimits(500, 2400, 0, 180);

			timer = new DispatcherTimer();
			timer.Interval = TimeSpan.FromMilliseconds(100);
			timer.Tick += OnTick;
			
		}
Ejemplo n.º 16
0
        private async void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            this.hat = await FEZHAT.CreateAsync();

            var timer = new DispatcherTimer();

            timer.Interval = TimeSpan.FromSeconds(0.25);
            timer.Tick    += Timer_Tick;
            timer.Start();

            this.timer = timer;
        }
Ejemplo n.º 17
0
        private static async void SetupHat()
        {
            // Initialize Fez Hat
            logger.Info("Initializing FEZ HAT");
            hat = await FEZHAT.CreateAsync();

            // Initialize Timer
            timer          = new Timer(2000); // (TimerCallback, null, 0, 500);
            timer.Elapsed += Timer_Elapsed;
            timer.Enabled  = true;

            Program.logger.Info("FEZ HAT Initialized");
        }
Ejemplo n.º 18
0
        private async Task SetupHatAsync()
        {
            this.hat = await FEZHAT.CreateAsync();
            
            this.telemetryTimer = new DispatcherTimer();
            this.telemetryTimer.Interval = TimeSpan.FromMilliseconds(2000);
            this.telemetryTimer.Tick += this.TelemetryTimer_Tick;
            this.telemetryTimer.Start();

            this.commandsTimer = new DispatcherTimer();
            this.commandsTimer.Interval = TimeSpan.FromSeconds(60);
            this.commandsTimer.Tick += this.CommandsTimer_Tick;
            this.commandsTimer.Start();
        }
Ejemplo n.º 19
0
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            deferral = taskInstance.GetDeferral();

            hat = await FEZHAT.CreateAsync();

            hat.D2.TurnOff();
            hat.D3.TurnOff();

            telemetry = new Telemetry("Sydney", Publish, 30);

            iotHubCommand = new IoTHubCommand <string>(deviceClient, telemetry);
            iotHubCommand.CommandReceived += Commanding_CommandReceived;
        }
Ejemplo n.º 20
0
        private async Task Init()
        {
            hat = await FEZHAT.CreateAsync();

            Shades.Add(new ShadeItemViewModel(hat.MotorA, 5, "Left Window", hat.D2));
            Shades.Add(new ShadeItemViewModel(hat.MotorB, 5, "Right Window", hat.D3));

            // Main Timer - For Temp, light reading and status LEDs
            timer = new DispatcherTimer {
                Interval = TimeSpan.FromMilliseconds(100)
            };
            timer.Tick += OnTick;

            IsMonitoring = true;
        }
Ejemplo n.º 21
0
        private async void Setup()
        {
            this.hat = await FEZHAT.CreateAsync();

            this.dist = new UltrasonicDistanceSensor(hat);

            this.hat.S1.SetLimits(500, 2400, 0, 180);
            this.hat.S2.SetLimits(500, 2400, 0, 180);

            this.currentAction = RobotAction.forward;

            this.timer          = new DispatcherTimer();
            this.timer.Interval = TimeSpan.FromMilliseconds(100);
            this.timer.Tick    += this.OnTick;
            this.timer.Start();
        }
        // rather perform initialization in loaded event
        private async void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            // initialize the FEZHAT
            var fez = await FEZHAT.CreateAsync();

            // set the color of the red-green-blue LED labelled
            // D2 (diode 2) on the board to yellow
            fez.D2.Color = FEZHAT.Color.Yellow;

            while (true)
            {
                // update the runPressed Run's Text to be that of button DIO18's value
                runPressed.Text = fez.IsDIO18Pressed().ToString();
                // wait 100ms
                await Task.Delay(100);
            }
        }
Ejemplo n.º 23
0
        private async Task <bool> InitPhysicalDevice()
        {
            _fezhat = await FEZHAT.CreateAsync();

            if (_fezhat != null)
            {
                _fezhat.DIO24On = true;

                _sampleTimer.Interval = TimeSpan.FromMilliseconds(50);
                _sampleTimer.Tick    += SampleTimer_Tick;
                _sampleTimer.Start();

                return(true);
            }

            return(false);
        }
Ejemplo n.º 24
0
        private async void RunWeatherLoop()
        {
            var fez = await FEZHAT.CreateAsync();

            while (true)
            {
                var light = fez.GetLightLevel();
                runLight.Text       = light.ToString();
                runTemperature.Text = fez.GetTemperature().ToString();
                var brightness = 255 * (1 - light);
                fez.D2.Color = new FEZHAT.Color((byte)brightness, (byte)brightness, (byte)brightness);

                btnEllipse.Fill = new SolidColorBrush(fez.IsDIO18Pressed() ? Colors.Green : Colors.Red);

                await Task.Delay(100);
            }
        }
Ejemplo n.º 25
0
        /// <summary>
        /// メインページがロードされた処理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void MainPage_LoadedAsync(object sender, RoutedEventArgs e)
        {
            // ConfigのDeviceIDが未設定の場合
            if (Guid.Empty == ChobiIotConfig.DeviceId)
            {
                FixDeviceId();
                if (_deviceId == "chobiPi")
                {
                    Debug.Write("Please set devicdId or unique machine name");
                    throw new ArgumentOutOfRangeException("Please set devicdId or unique machine name");
                }
            }
            else
            {
                _deviceId = ChobiIotConfig.DeviceId.ToString();
            }
            tbDeviceId.Text = _deviceId.ToString();
            // fezHatの初期化
            _fezhat = await FEZHAT.CreateAsync();

            // LEDセンサーをOFFにする
            _fezhat.D2.TurnOff();
            _fezhat.D3.TurnOff();
            // Device認証サーバーへ接続します
            try
            {
                var result = await TryConnect();

                if (result)
                {
                    await InitializeUpload();
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

            // タイマーの初期化
            lastSensorReading     = new List <SensorReadingBuffer>();
            measureTimer          = new DispatcherTimer();
            measureTimer.Interval = TimeSpan.FromMilliseconds(measureIntervalMSec);
            measureTimer.Tick    += MeasureTimer_Tick;
            measureTimer.Start();
        }
Ejemplo n.º 26
0
        private async void SetupHat()
        {
            if (UseMockedSensors)
            {
                this.rnd = new Random();
            }
            else
            {
                this.hat = await FEZHAT.CreateAsync();
            }

            this.timer = new DispatcherTimer();

            this.timer.Interval = TimeSpan.FromMilliseconds(500);
            this.timer.Tick    += this.Timer_Tick;

            this.timer.Start();
        }
Ejemplo n.º 27
0
        private async void SetupAsync()
        {
            _hat = await FEZHAT.CreateAsync();

            _timer = new DispatcherTimer()
            {
                Interval = TimeSpan.FromMilliseconds(100)
            };

            bool on = false;

            _timer.Tick += (s, e) =>
            {
                _hat.D2.Color = on ? FEZHAT.Color.Red : FEZHAT.Color.Black;
                on            = !on;
            };

            _timer.Start();
        }
Ejemplo n.º 28
0
        public MainPage()
        {
            this.InitializeComponent();
            this.Loaded += OnLoaded;

            var ai = AnalyticsInfo.VersionInfo;

            Debug.WriteLine(string.Format("Device Family = {0}", ai.DeviceFamily));
            _isRunningOnPi = ai.DeviceFamily == "Windows.IoT";
            //_isRunningOnPi = Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.Devices.Gpio.GpioController");             BUG? Always returns true regardless of the device.
            if (_isRunningOnPi)
            {
                Task.Run(async() => _fezhat = await FEZHAT.CreateAsync());
                _buttonMonitor          = new DispatcherTimer();
                _buttonMonitor.Interval = new TimeSpan(TimeSpan.TicksPerSecond / 4);
                _buttonMonitor.Tick    += ButtonMonitorOnTick;
                _buttonMonitor.Start();
            }
        }
Ejemplo n.º 29
0
        private async void Setup()
        {
            try
            {
                _hat = await FEZHAT.CreateAsync();
            }
            catch (Exception)
            {

                ErrorBox.Text = "Could not initialize Hat";
            }

            _deviceClient = DeviceClient.CreateFromConnectionString(connectionString, TransportType.Http1);

            _timer = new DispatcherTimer();
            _timer.Interval = TimeSpan.FromMilliseconds(2000);
            _timer.Tick += this.OnTick;
            _timer.Start();
        }
Ejemplo n.º 30
0
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            var deferral = taskInstance.GetDeferral();

            var hat = await FEZHAT.CreateAsync();

            for (int i = 0; i < 100; i++)
            {
                hat.D3.Color = FEZHAT.Color.Blue;
                SillyWait();
                hat.D3.TurnOff();
                SillyWait();
                hat.D3.Color = FEZHAT.Color.Red;
                SillyWait();
                hat.D3.TurnOff();
                SillyWait();
            }
            hat.Dispose();

            deferral.Complete();
        }
 public async Task InitializeAsync()
 {
     fezHat = await FEZHAT.CreateAsync();
 }
Ejemplo n.º 32
0
 public void SetFezhat(FEZHAT fezhat)
 {
     _fezhat = fezhat;
 }
Ejemplo n.º 33
0
 private async void SetupAsync()
 {
     _hat = await FEZHAT.CreateAsync();
 }
Ejemplo n.º 34
0
 public async void CreateFezHAT()
 {
     this.hat = await FEZHAT.CreateAsync();
 }
Ejemplo n.º 35
0
 public async void Run(IBackgroundTaskInstance taskInstance)
 {
     _deferral = taskInstance.GetDeferral();
     _hat = await FEZHAT.CreateAsync();
     _timer = ThreadPoolTimer.CreatePeriodicTimer(Timer_Tick, TimeSpan.FromMilliseconds(1000));
 }
Ejemplo n.º 36
0
 public void CheckHat()
 {
     if (_hat == null)
         _hat = FEZHAT.CreateAsync().Result;
 }
Ejemplo n.º 37
0
 public async Task InitiatizeAsync()
 {
     hat = await FEZHAT.CreateAsync();
 }