How can I get temperature and pressure readings into variables or arrays

We have initiated and have contact with the device but when we try to get values into our variables we have not got it correct.
Any idea how to do it in a simple way?
I include some sample from our code:

--------------------------------------

   public MainPage()
        {
            InitializeComponent();
            button_Update.IsEnabled = false;
            button_Stop.IsEnabled = false;
            textMessage.Text = "Waiting to start";
        }
 
        private void button_Start_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            MyWearStart();
        }
 
        private void button_Update_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            MyWearUpdate();
            button_Stop.IsEnabled = true;
        }
        private void GetMydata(float value)
        {
        }
 
        private async void MyWearUpdate()
        {
            string pressure = "1501";
            string temp = "30C";
            textMessage.Text = "Updating";
 
            try
            {
                var tempResult = await termometer.Sensors[0].AddRouteAsync((source => source.Stream(data => GetMydata(data.Value<float>()) )));
 
 
                var pressureResult = await barometer.Pressure.AddRouteAsync(source => source.Stream(data => data.Value<float>()));
                //               var result = await barometer.Pressure.AddRouteAsync
                //           source.Stream(data => Console.WriteLine("Pressure (Pa) = " + data.Value<float>())));

               pressure = " <====  some pressure";
               temp = "<==== some temperature";

            }
            catch (Exception e)
            {
                textMessage.Text = "Error";
            }

       }
  

Comments

  • edited November 2017
    You need to tell the sensors to start sending you data once the routes have been setup.
    For the barometer, it is straightforward:

    barometer.Pressure.Start();
    barometer.Start();

    For the temperature sensor, you'll need to schedule a Read task then start the newly created Task, see the documentation on Timers:
  • Hi 
    I have put in this for the barometer when starting as suggested:
                        barometer = metawear.GetModule<IBarometerBosch>();
                        barometer.Pressure.Start();
                        barometer.Start();

    and where we want to read the values:

           public async Task<IScheduledTask> ScheduleRead(IForcedDataProducer producer)
            {
                return await metawear.ScheduleAsync(300000, false, () => producer.Read());
            }

            private async void MyWearUpdate()
            {
                    GetMydata(data.Value<float>()) )));
                    var temp2 = await ScheduleRead(termometer.Sensors[1]);
                    var pressureResult = await barometer.Pressure.AddRouteAsync(source => source.Stream(data => data.Value<float>()));


    Is it correct implemented?
    When we run and call the ScheduleRead we get an exception with a timeout error message.

    And where do we assign the readings to the variables? 
    Unfortunately I am not used to handling async data so I might need some more specific guiding where to get the specific temp and pressure values then you are used to :-) 

    Thanks
    Björn 
  • You'll have to post the full code for me to tell if everything is properly setup.

    The received sensor data is handled in the "Stream" component. Your current code doesn't appear to be doing anything with the data.
  • HI
    I include code regarding setting up the device and reading is in the earlier posts. Initiating is in the methods MyWearStart(); and reading in MyWearUpdate().

    We would also appreciate if you could give us a simple example on how to get the values from the stream into the variables in the code in in MyWearUpdate() at the where you see:

                   var pressure = " <====  some pressure";
                   var temp = "<==== some temperature";
    ----------------------
           private async void MyWearStart(int loopCnt)
            {
                ulong deviceMac = (ulong)Convert.ToUInt64(MacAdr,16);
                await MacAddrToIMetaWearBoard(deviceMac);
                    metawear = Application.GetMetaWearBoard(device);
                    try
                    {
                        await metawear.InitializeAsync();
                        termometer = metawear.GetModule<ITemperature>();
                        termometer.FindSensors(SensorType.BoschEnv)[0].Read();
                        barometer = metawear.GetModule<IBarometerBosch>();
                        barometer.Pressure.Start();
                        barometer.Start();
            }


  • I would prefer to see the code in its entirety and current state.  For example, the definition of "MyWearStart" that you have in your most recent post does not match the one in your original post   Also, I can't really comment on the code unless I know how it's being used.  I can make educated guesses based on the function names but it would be better to have you explain the code flow.

    In general, your code should be broken down into a "setup", "run", and "clean up" phase.  The setup phase is where you call InitializeAsync, setup your routes, and schedule your tasks, you start the sensors ad tasks in the "run" phase, and finally, you stop the sensors and clean up the board in the "clean up" phase.  

    Based on the snippets you have posted, you don't have a "clean up" phase and you are running the sensors before you have even told the SDK what to do with the received data.
  • Ok, since I cant post the complete code in a post here where can I send it, or should I chunk it up in different post?

    Only the call for MyWearStart was posted in the original part, the content of the method was only posted in the last call, dont know where you can have seen that.

    I do have a Setup, run and cleanup, in my code it is called   MyWearStart();   MyWearUpdate()  ( and MyWearClose()  but this module has not been posted since the problem occur before it is called ).

    But really, what we want is a simple code snippet fetching the readings from a stream and saving it to a variable. 

    We can implement that and see if it works for us, if not we can get back to you.

    Don't make it more complicated then it is.
  • Post the code on PasteBin.

    Just because those functions have "start", "update", and "close" does not mean those functions do the 3 tasks as defined in my previous post respectively.  I suggest following this tutorial for a simple data streaming app.

    First, make sure the app can print the data into the debug console, then modify the Stream component's lambda function with whatever code you want applied to each data sample.
This discussion has been closed.