public void Run(IBackgroundTaskInstance taskInstance) { // // TODO: Insert code to perform background work // // If you start any asynchronous methods here, prevent the task // from closing prematurely by using BackgroundTaskDeferral as // described in http://aka.ms/backgroundtaskdeferral // _deferral = taskInstance.GetDeferral(); // set the security for fms, and hook into fms ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; fmserver = GetFMSInstance(); // get the sensors lightSensor = DeviceFactory.Build.LightSensor(Pin.AnalogPin0); soundSensor = DeviceFactory.Build.SoundSensor(Pin.AnalogPin2); leds = DeviceFactory.Build.BuildLedBar(Pin.DigitalPin8); leds.Initialize(GrovePi.Sensors.Orientation.GreenToRed); // start the timer ThreadPoolTimer timer = ThreadPoolTimer.CreatePeriodicTimer(Timer_Tick, TimeSpan.FromSeconds(1)); }
private void InitGrovePi() { System.Diagnostics.Debug.WriteLine(DeviceFactory.Build.GrovePi().GetFirmwareVersion()); GroveRotary = DeviceFactory.Build.RotaryAngleSensor(Pin.AnalogPin0); GroveSound = DeviceFactory.Build.SoundSensor(Pin.AnalogPin1); GroveLight = DeviceFactory.Build.LightSensor(Pin.AnalogPin2); GroveRelay = DeviceFactory.Build.Relay(Pin.DigitalPin2); GroveTempHumi = DeviceFactory.Build.DHTTemperatureAndHumiditySensor(Pin.DigitalPin3, DHTModel.Dht11); GroveRanger = DeviceFactory.Build.UltraSonicSensor(Pin.DigitalPin4); GroveLedBar = DeviceFactory.Build.BuildLedBar(Pin.DigitalPin5); GroveBuzzer = DeviceFactory.Build.Buzzer(Pin.DigitalPin6); GroveButton = DeviceFactory.Build.ButtonSensor(Pin.DigitalPin7); GroveLCD = DeviceFactory.Build.RgbLcdDisplay(); GroveLedBar.Initialize(GrovePi.Sensors.Orientation.GreenToRed); GroveLCD.SetBacklightRgb(255, 50, 255); DeviceFactory.Build.GrovePi().PinMode(Pin.DigitalPin2, PinMode.Output); Delay.Milliseconds(10); DeviceFactory.Build.GrovePi().Flush(); DeviceFactory.Build.GrovePi().PinMode(Pin.DigitalPin6, PinMode.Output); Delay.Milliseconds(10); DeviceFactory.Build.GrovePi().Flush(); }
void MainPageLoaded(object sender, RoutedEventArgs e) { GroveLedBar.Initialize(GrovePi.Sensors.Orientation.GreenToRed); SoundList = new List <DataPoint>(); for (int i = 0; i < DataPointCnt; i++) { SoundList.Add(new DataPoint() { Time = i + 1, Value = 0 }); } IAsyncAction asyncAction = Windows.System.Threading.ThreadPool.RunAsync(async(workItem) => { int sound = 0; while (true) { try { sound = GroveSound.SensorValue(); setLedBar(ConvertSoundToBarLevel(sound)); System.Diagnostics.Debug.WriteLine("Sound: " + sound.ToString()); } catch (Exception ex) { // If you want to see the exceptions uncomment the following: System.Diagnostics.Debug.WriteLine(ex.ToString()); } await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { ShiftLeft(SoundList, sound); List <DataPoint> lst = new List <DataPoint>(SoundList); (LineChart.Series[0] as LineSeries).ItemsSource = lst; }); } }); }
private void InitGrovePi() { Grove_LedBar.Initialize(GrovePi.Sensors.Orientation.GreenToRed); }
private async void Timer_Tick(ThreadPoolTimer timer) { // record the start time DateTime start = DateTime.Now; // update the display leds.Initialize(GrovePi.Sensors.Orientation.GreenToRed); leds.SetLevel((byte)0); int lightValue = 0; string lightError = string.Empty; // read the light try { lightValue = lightSensor.SensorValue(); // int between 0 (dark) and 1023 (bright) } catch (Exception ex) { lightError = ex.Message; } int soundValue = 0; string soundError = String.Empty; try { soundValue = soundSensor.SensorValue(); } catch (Exception ex) { soundError = ex.Message; } // check if token is still valid // get a new token every 12 minutes - no real need to since we'll usually keep the connection alive if (token == null || token == string.Empty) { token = await fmserver.Authenticate(); tokenRecieved = DateTime.Now; } else if (DateTime.Now > tokenRecieved.AddMinutes(12)) { int logoutResponse = await fmserver.Logout(); token = string.Empty; fmserver = GetFMSInstance(); token = await fmserver.Authenticate(); tokenRecieved = DateTime.Now; } if (token != string.Empty) { // get some data from the RPI itself var processorName = Raspberry.Board.Current.ProcessorName; var rpiModel = Raspberry.Board.Current.Model.ToString(); // write it to FMS var request = fmserver.NewRecordRequest(); if (processorName != null) { request.AddField("rpiProcessor", processorName); } if (rpiModel != null) { request.AddField("rpiModel", rpiModel); } request.AddField("when_start", start.ToString()); request.AddField("sound", soundValue.ToString()); request.AddField("sound_error", soundError); request.AddField("light", lightValue.ToString()); request.AddField("light_error", lightError); request.AddField("when", DateTime.Now.ToString()); // add a script call to calculate the time diff to the previous record request.AddScript(ScriptTypes.after, "calculate_gap_grove"); var response = await request.Execute(); if (fmserver.lastErrorCode != 0) { leds.SetLevel((byte)2); } else { leds.SetLevel((byte)10); } Thread.Sleep(TimeSpan.FromMilliseconds(250)); // no longer logging out after each call // await fmserver.Logout(); // token = string.empty; } // clear the display again leds.SetLevel((byte)1); Thread.Sleep(TimeSpan.FromMilliseconds(250)); }
private void InitGrovePi() { System.Diagnostics.Debug.WriteLine(DeviceFactory.Build.GrovePi().GetFirmwareVersion()); GroveRotary = DeviceFactory.Build.RotaryAngleSensor(Pin.AnalogPin0); GroveSound = DeviceFactory.Build.SoundSensor(Pin.AnalogPin1); GroveLight = DeviceFactory.Build.LightSensor(Pin.AnalogPin2); GroveRelay = DeviceFactory.Build.Relay(Pin.DigitalPin2); GroveTempHumi = DeviceFactory.Build.DHTTemperatureAndHumiditySensor(Pin.DigitalPin3, DHTModel.Dht11); GroveRanger = DeviceFactory.Build.UltraSonicSensor(Pin.DigitalPin4); GroveLedBar = DeviceFactory.Build.BuildLedBar(Pin.DigitalPin5); GroveBuzzer = DeviceFactory.Build.Buzzer(Pin.DigitalPin6); GroveButton = DeviceFactory.Build.ButtonSensor(Pin.DigitalPin7); GroveLCD = DeviceFactory.Build.RgbLcdDisplay(); GroveLedBar.Initialize(GrovePi.Sensors.Orientation.GreenToRed); GroveLCD.SetBacklightRgb(255, 50, 255); DeviceFactory.Build.GrovePi().PinMode(Pin.DigitalPin2, PinMode.Output); Delay.Milliseconds(10); DeviceFactory.Build.GrovePi().Flush(); DeviceFactory.Build.GrovePi().PinMode(Pin.DigitalPin6, PinMode.Output); Delay.Milliseconds(10); DeviceFactory.Build.GrovePi().Flush(); }