Remarks

Htu31d
Status Status badge: working
Source code GitHub
Datasheet(s) GitHub
NuGet package NuGet Gallery for Meadow.Foundation.Sensors.Atmospheric.Htux1d

Code Example

Htu31d? sensor;

public override Task Initialize()
{
    Resolver.Log.Info("Initializing...");

    sensor = new Htu31d(Device.CreateI2cBus());

    var consumer = Htu31d.CreateObserver(
        handler: result =>
        {
            Resolver.Log.Info($"Observer: Temp changed by threshold; new temp: {result.New.Temperature?.Celsius:N2}C, old: {result.Old?.Temperature?.Celsius:N2}C");
        },
        filter: result =>
        {
            if (result.Old?.Temperature is { } oldTemp &&
                result.Old?.Humidity is { } oldHumidity &&
                result.New.Temperature is { } newTemp &&
                result.New.Humidity is { } newHumidity)
            {
                return ((newTemp - oldTemp).Abs().Celsius > 0.5 &&
                        (newHumidity - oldHumidity).Percent > 0.05);
            }
            return false;
        }
    );
    sensor.Subscribe(consumer);

    sensor.Updated += (sender, result) =>
    {
        Resolver.Log.Info($"  Temperature: {result.New.Temperature?.Celsius:F1}C");
        Resolver.Log.Info($"  Relative Humidity: {result.New.Humidity?.Percent:F1}%");
    };

    return Task.CompletedTask;
}

public override async Task Run()
{
    if (sensor == null) { return; }

    var result = await sensor.Read();
    Resolver.Log.Info("Initial Readings:");
    Resolver.Log.Info($"  Temperature: {result.Temperature?.Celsius:F1}C");
    Resolver.Log.Info($"  Relative Humidity: {result.Humidity:F1}%");

    sensor.StartUpdating(TimeSpan.FromSeconds(1));
}

Sample project(s) available on GitHub

Characteristic Locus
Inheritance object ObservableBase<(Temperature? Temperature, RelativeHumidity? Humidity)> SamplingSensorBase<(Temperature? Temperature, RelativeHumidity? Humidity)> PollingSensorBase<(Temperature? Temperature, RelativeHumidity? Humidity)> ByteCommsSensorBase<(Temperature? Temperature, RelativeHumidity? Humidity)> Htux1dBase > Htu31d
Implements IObservable<IChangeResult<(Temperature? Temperature, RelativeHumidity? Humidity)>> ISamplingSensor<(Temperature? Temperature, RelativeHumidity? Humidity)> ISensor<(Temperature? Temperature, RelativeHumidity? Humidity)> IDisposable ITemperatureSensor ISamplingSensor<Temperature> ISensor<Temperature> IHumiditySensor ISamplingSensor<RelativeHumidity> ISensor<RelativeHumidity> II2cPeripheral
Inherited Members Htux1dBase.TemperatureUpdated Htux1dBase.HumidityUpdated Htux1dBase.DefaultI2cAddress Htux1dBase.DEFAULT_SPEED Htux1dBase.Temperature Htux1dBase.Humidity Htux1dBase.SerialNumber Htux1dBase.RaiseEventsAndNotify(IChangeResult<(Temperature? Temperature, RelativeHumidity? Humidity)>) ByteCommsSensorBase<(Temperature? Temperature, RelativeHumidity? Humidity)>.Init(int, int) ByteCommsSensorBase<(Temperature? Temperature, RelativeHumidity? Humidity)>.Dispose(bool) ByteCommsSensorBase<(Temperature? Temperature, RelativeHumidity? Humidity)>.Dispose() ByteCommsSensorBase<(Temperature? Temperature, RelativeHumidity? Humidity)>.BusComms ByteCommsSensorBase<(Temperature? Temperature, RelativeHumidity? Humidity)>.ReadBuffer ByteCommsSensorBase<(Temperature? Temperature, RelativeHumidity? Humidity)>.WriteBuffer PollingSensorBase<(Temperature? Temperature, RelativeHumidity? Humidity)>.StartUpdating(TimeSpan?) PollingSensorBase<(Temperature? Temperature, RelativeHumidity? Humidity)>.StopUpdating() SamplingSensorBase<(Temperature? Temperature, RelativeHumidity? Humidity)>.samplingLock SamplingSensorBase<(Temperature? Temperature, RelativeHumidity? Humidity)>.ReadSensor() SamplingSensorBase<(Temperature? Temperature, RelativeHumidity? Humidity)>.RaiseEventsAndNotify(IChangeResult<(Temperature? Temperature, RelativeHumidity? Humidity)>) SamplingSensorBase<(Temperature? Temperature, RelativeHumidity? Humidity)>.Read() SamplingSensorBase<(Temperature? Temperature, RelativeHumidity? Humidity)>.SamplingTokenSource SamplingSensorBase<(Temperature? Temperature, RelativeHumidity? Humidity)>.Conditions SamplingSensorBase<(Temperature? Temperature, RelativeHumidity? Humidity)>.IsSampling SamplingSensorBase<(Temperature? Temperature, RelativeHumidity? Humidity)>.UpdateInterval SamplingSensorBase<(Temperature? Temperature, RelativeHumidity? Humidity)>.Updated ObservableBase<(Temperature? Temperature, RelativeHumidity? Humidity)>.NotifyObservers(IChangeResult<(Temperature? Temperature, RelativeHumidity? Humidity)>) ObservableBase<(Temperature? Temperature, RelativeHumidity? Humidity)>.Subscribe(IObserver<IChangeResult<(Temperature? Temperature, RelativeHumidity? Humidity)>>) ObservableBase<(Temperature? Temperature, RelativeHumidity? Humidity)>.CreateObserver(Action<IChangeResult<(Temperature? Temperature, RelativeHumidity? Humidity)>>, Predicate<IChangeResult<(Temperature? Temperature, RelativeHumidity? Humidity)>>) ObservableBase<(Temperature? Temperature, RelativeHumidity? Humidity)>.observers object.Equals(object) object.Equals(object, object) object.GetHashCode() object.GetType() object.MemberwiseClone() object.ReferenceEquals(object, object) object.ToString()
Namespace Meadow.Foundation.Sensors.Atmospheric
Assembly Htux1d.dll

Syntax

public class Htu31d : Htux1dBase, IObservable<IChangeResult<(Temperature? Temperature, RelativeHumidity? Humidity)>>, ISamplingSensor<(Temperature? Temperature, RelativeHumidity? Humidity)>, ISensor<(Temperature? Temperature, RelativeHumidity? Humidity)>, IDisposable, ITemperatureSensor, ISamplingSensor<Temperature>, ISensor<Temperature>, IHumiditySensor, ISamplingSensor<RelativeHumidity>, ISensor<RelativeHumidity>, II2cPeripheral

Constructors

Htu31d(II2cBus, byte, TimeSpan?)

Create a new Htu31d temperature and humidity sensor.

Declaration
public Htu31d(II2cBus i2cBus, byte address = 64, TimeSpan? updateInterval = null)

Parameters

Type Name Description
II2cBus i2cBus

I2CBus (default to 100 KHz).

byte address

Sensor address (default to 0x40).

TimeSpan? updateInterval

Update interval, defaults to 1 sec if null

Remarks

Htu31d
Status Status badge: working
Source code GitHub
Datasheet(s) GitHub
NuGet package NuGet Gallery for Meadow.Foundation.Sensors.Atmospheric.Htux1d

Code Example

Htu31d? sensor;

public override Task Initialize()
{
    Resolver.Log.Info("Initializing...");

    sensor = new Htu31d(Device.CreateI2cBus());

    var consumer = Htu31d.CreateObserver(
        handler: result =>
        {
            Resolver.Log.Info($"Observer: Temp changed by threshold; new temp: {result.New.Temperature?.Celsius:N2}C, old: {result.Old?.Temperature?.Celsius:N2}C");
        },
        filter: result =>
        {
            if (result.Old?.Temperature is { } oldTemp &&
                result.Old?.Humidity is { } oldHumidity &&
                result.New.Temperature is { } newTemp &&
                result.New.Humidity is { } newHumidity)
            {
                return ((newTemp - oldTemp).Abs().Celsius > 0.5 &&
                        (newHumidity - oldHumidity).Percent > 0.05);
            }
            return false;
        }
    );
    sensor.Subscribe(consumer);

    sensor.Updated += (sender, result) =>
    {
        Resolver.Log.Info($"  Temperature: {result.New.Temperature?.Celsius:F1}C");
        Resolver.Log.Info($"  Relative Humidity: {result.New.Humidity?.Percent:F1}%");
    };

    return Task.CompletedTask;
}

public override async Task Run()
{
    if (sensor == null) { return; }

    var result = await sensor.Read();
    Resolver.Log.Info("Initial Readings:");
    Resolver.Log.Info($"  Temperature: {result.Temperature?.Celsius:F1}C");
    Resolver.Log.Info($"  Relative Humidity: {result.Humidity:F1}%");

    sensor.StartUpdating(TimeSpan.FromSeconds(1));
}

Sample project(s) available on GitHub

Methods

Heater(bool)

Turn the heater on or off

Declaration
public void Heater(bool heaterOn)

Parameters

Type Name Description
bool heaterOn

Heater status, true = turn heater on, false = turn heater off

Remarks

Htu31d
Status Status badge: working
Source code GitHub
Datasheet(s) GitHub
NuGet package NuGet Gallery for Meadow.Foundation.Sensors.Atmospheric.Htux1d

Code Example

Htu31d? sensor;

public override Task Initialize()
{
    Resolver.Log.Info("Initializing...");

    sensor = new Htu31d(Device.CreateI2cBus());

    var consumer = Htu31d.CreateObserver(
        handler: result =>
        {
            Resolver.Log.Info($"Observer: Temp changed by threshold; new temp: {result.New.Temperature?.Celsius:N2}C, old: {result.Old?.Temperature?.Celsius:N2}C");
        },
        filter: result =>
        {
            if (result.Old?.Temperature is { } oldTemp &&
                result.Old?.Humidity is { } oldHumidity &&
                result.New.Temperature is { } newTemp &&
                result.New.Humidity is { } newHumidity)
            {
                return ((newTemp - oldTemp).Abs().Celsius > 0.5 &&
                        (newHumidity - oldHumidity).Percent > 0.05);
            }
            return false;
        }
    );
    sensor.Subscribe(consumer);

    sensor.Updated += (sender, result) =>
    {
        Resolver.Log.Info($"  Temperature: {result.New.Temperature?.Celsius:F1}C");
        Resolver.Log.Info($"  Relative Humidity: {result.New.Humidity?.Percent:F1}%");
    };

    return Task.CompletedTask;
}

public override async Task Run()
{
    if (sensor == null) { return; }

    var result = await sensor.Read();
    Resolver.Log.Info("Initial Readings:");
    Resolver.Log.Info($"  Temperature: {result.Temperature?.Celsius:F1}C");
    Resolver.Log.Info($"  Relative Humidity: {result.Humidity:F1}%");

    sensor.StartUpdating(TimeSpan.FromSeconds(1));
}

Sample project(s) available on GitHub

ReadSensor()

Read atmospheric data from sensor

Declaration
protected override Task<(Temperature? Temperature, RelativeHumidity? Humidity)> ReadSensor()

Returns

Type Description
Task<(Temperature? Temperature, RelativeHumidity? Humidity)>

Overrides

Meadow.Foundation.SamplingSensorBase<(Meadow.Units.Temperature? Temperature, Meadow.Units.RelativeHumidity? Humidity)>.ReadSensor()

Remarks

Htu31d
Status Status badge: working
Source code GitHub
Datasheet(s) GitHub
NuGet package NuGet Gallery for Meadow.Foundation.Sensors.Atmospheric.Htux1d

Code Example

Htu31d? sensor;

public override Task Initialize()
{
    Resolver.Log.Info("Initializing...");

    sensor = new Htu31d(Device.CreateI2cBus());

    var consumer = Htu31d.CreateObserver(
        handler: result =>
        {
            Resolver.Log.Info($"Observer: Temp changed by threshold; new temp: {result.New.Temperature?.Celsius:N2}C, old: {result.Old?.Temperature?.Celsius:N2}C");
        },
        filter: result =>
        {
            if (result.Old?.Temperature is { } oldTemp &&
                result.Old?.Humidity is { } oldHumidity &&
                result.New.Temperature is { } newTemp &&
                result.New.Humidity is { } newHumidity)
            {
                return ((newTemp - oldTemp).Abs().Celsius > 0.5 &&
                        (newHumidity - oldHumidity).Percent > 0.05);
            }
            return false;
        }
    );
    sensor.Subscribe(consumer);

    sensor.Updated += (sender, result) =>
    {
        Resolver.Log.Info($"  Temperature: {result.New.Temperature?.Celsius:F1}C");
        Resolver.Log.Info($"  Relative Humidity: {result.New.Humidity?.Percent:F1}%");
    };

    return Task.CompletedTask;
}

public override async Task Run()
{
    if (sensor == null) { return; }

    var result = await sensor.Read();
    Resolver.Log.Info("Initial Readings:");
    Resolver.Log.Info($"  Temperature: {result.Temperature?.Celsius:F1}C");
    Resolver.Log.Info($"  Relative Humidity: {result.Humidity:F1}%");

    sensor.StartUpdating(TimeSpan.FromSeconds(1));
}

Sample project(s) available on GitHub

Reset()

Reset the sensor

Declaration
public void Reset()

Remarks

Htu31d
Status Status badge: working
Source code GitHub
Datasheet(s) GitHub
NuGet package NuGet Gallery for Meadow.Foundation.Sensors.Atmospheric.Htux1d

Code Example

Htu31d? sensor;

public override Task Initialize()
{
    Resolver.Log.Info("Initializing...");

    sensor = new Htu31d(Device.CreateI2cBus());

    var consumer = Htu31d.CreateObserver(
        handler: result =>
        {
            Resolver.Log.Info($"Observer: Temp changed by threshold; new temp: {result.New.Temperature?.Celsius:N2}C, old: {result.Old?.Temperature?.Celsius:N2}C");
        },
        filter: result =>
        {
            if (result.Old?.Temperature is { } oldTemp &&
                result.Old?.Humidity is { } oldHumidity &&
                result.New.Temperature is { } newTemp &&
                result.New.Humidity is { } newHumidity)
            {
                return ((newTemp - oldTemp).Abs().Celsius > 0.5 &&
                        (newHumidity - oldHumidity).Percent > 0.05);
            }
            return false;
        }
    );
    sensor.Subscribe(consumer);

    sensor.Updated += (sender, result) =>
    {
        Resolver.Log.Info($"  Temperature: {result.New.Temperature?.Celsius:F1}C");
        Resolver.Log.Info($"  Relative Humidity: {result.New.Humidity?.Percent:F1}%");
    };

    return Task.CompletedTask;
}

public override async Task Run()
{
    if (sensor == null) { return; }

    var result = await sensor.Read();
    Resolver.Log.Info("Initial Readings:");
    Resolver.Log.Info($"  Temperature: {result.Temperature?.Celsius:F1}C");
    Resolver.Log.Info($"  Relative Humidity: {result.Humidity:F1}%");

    sensor.StartUpdating(TimeSpan.FromSeconds(1));
}

Sample project(s) available on GitHub