MoistureSensor sensor;
public override Task Initialize()
{
Resolver.Log.Info("Initialize...");
sensor = new MoistureSensor(Device.Pins.A01);
var consumer = MoistureSensor.CreateObserver(
handler: result => Resolver.Log.Info($"Observer filter satisfied: {result.New.Millivolts:N2}mV, old: {result.Old?.Millivolts:N2}mV"),
filter: result =>
{
if (result.Old is { } old)
{
return (result.New - old).Abs().Millivolts > 500;
}
return false;
});
sensor.Subscribe(consumer);
sensor.Updated += (sender, result) =>
{
Resolver.Log.Info($"Voltage Changed, new: {result.New.Millivolts:N2}mV, old: {result.Old?.Millivolts:N2}mV");
};
return Task.CompletedTask;
}
public override async Task Run()
{
var result = await sensor.Read();
Resolver.Log.Info($"Initial read: {result.Millivolts:N2}mV");
sensor.StartUpdating(TimeSpan.FromMilliseconds(1000));
}