Windows IoT Core reading value returned System.Threading.Tasks.Task`1[System.Single] instead of float

302 views Asked by At

I am following Chris Pietschmann's Weather Station 3.0 tutorial for Raspberry Pi using the BME280 sensor at: https://www.hackster.io/23021/weather-station-v-3-0-b8b8bc.

In the MainPage he calls ReadTemperture (or any of the other registers of the sensor) to write out the returned values.

I get System.Threading.Tasks.Task`1[System.Single] in the Debug.Writeline() for all of the values... Temp, humidity, pressure and altitude.

I added a writeline inside the method and I get the correct value so I'm reading from the sensor... I just can't return it back to the mainpage and read it.

Looks like I'm missing something in the async side?

Here's the MainPage:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

namespace IoT_BME280_Temp
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        DispatcherTimer _timer;


        const float seaLevelPressure = 1026.00f;  //was 1022.00
        BME280Sensor _bme280 = new BME280Sensor();

        public MainPage()

        {
            this.InitializeComponent();
        }

        protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);


            await _bme280.Initialize();

            _timer = new DispatcherTimer();
            _timer.Interval = TimeSpan.FromSeconds(5);
            _timer.Tick += _timer_Tick;

            _timer.Start();

        }

        private void _timer_Tick(object sender, object e)
        {
            try
            {
                var temp     = _bme280.ReadTemperature();
                Debug.WriteLine("Temp: {0} deg C", temp);  // Results in: Temp: System.Threading.Tasks.Task`1[System.Single] deg C
                var humidity = _bme280.ReadHumidity();
                var pressure = _bme280.ReadPressure();
                var altitude = _bme280.ReadAltitude(seaLevelPressure);
            } 
        catch
            {
                Debug.WriteLine("Cannot read values from sensor...");
        }
        }
    }
}

Here is ReadTemprature() from the BME280Sensor class:

public async Task<float> ReadTemperature()
    {
        //Make sure the I2C device is initialized
        if (!init) await Begin();

        //Read the MSB, LSB and bits 7:4 (XLSB) of the temperature from the BME280 registers
        byte tmsb = ReadByte((byte)eRegisters.BME280_REGISTER_TEMPDATA_MSB);
        byte tlsb = ReadByte((byte)eRegisters.BME280_REGISTER_TEMPDATA_LSB);
        byte txlsb = ReadByte((byte)eRegisters.BME280_REGISTER_TEMPDATA_XLSB); // bits 7:4

        //Combine the values into a 32-bit integer
        Int32 t = (tmsb << 12) + (tlsb << 4) + (txlsb >> 4);

        //Convert the raw value to the temperature in degC
        double temp = BME280_compensate_T_double(t);
        Debug.WriteLine("Temp: {0} deg C", temp);  //  This results in the correct temperature value...

        //Return the temperature as a float value
        return (float)temp;
    }

Thanks in advance!

1

There are 1 answers

2
Abion47 On BEST ANSWER

You're getting a Task because that's what ReadTemperature returns when you call it synchronously. To get the result of the task rather than the task itself, you need to call the method using await and change _timer_Tick to be async as well:

private async void _timer_Tick(object sender, object e)
{
    try
    {
        var temp     = await _bme280.ReadTemperature();
        Debug.WriteLine("Temp: {0} deg C", temp);
        var humidity = _bme280.ReadHumidity();
        var pressure = _bme280.ReadPressure();
        var altitude = _bme280.ReadAltitude(seaLevelPressure);
    } 
    catch
    {
        Debug.WriteLine("Cannot read values from sensor...");
    }
}