-
Notifications
You must be signed in to change notification settings - Fork 585
/
DhtSensor.cs
81 lines (70 loc) · 3.01 KB
/
DhtSensor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnitsNet;
namespace Iot.Device.Arduino
{
/// <summary>
/// This class supports the DhtFirmata extensions to read DHT sensors over the firmata protocol.
/// </summary>
public class DhtSensor : ExtendedCommandHandler
{
/// <summary>
/// Initializes a new instance of the DhtSensor extended command handler.
/// This handler requires pins with DHT support. This is typically the case for all GPIO pins if the
/// firmata client has the DhtFirmata extension loaded.
/// </summary>
public DhtSensor()
: base(SupportedMode.Dht)
{
}
/// <summary>
/// Special function to read DHT sensor, if supported
/// </summary>
/// <param name="pinNumber">Pin Number</param>
/// <param name="dhtType">Type of DHT Sensor: 11 = DHT11, 22 = DHT22, etc.</param>
/// <param name="temperature">Temperature</param>
/// <param name="humidity">Relative humidity</param>
/// <returns>True on success, false otherwise</returns>
public bool TryReadDht(int pinNumber, int dhtType, out Temperature temperature, out RelativeHumidity humidity)
{
IReadOnlyList<SupportedPinConfiguration> pinConfiguration = Board.SupportedPinConfigurations;
if (!pinConfiguration[pinNumber].PinModes.Contains(SupportedMode.Dht))
{
temperature = default;
humidity = default;
return false;
}
return TryReadDhtInternal(pinNumber, dhtType, out temperature, out humidity);
}
private bool TryReadDhtInternal(int pinNumber, int dhtType, out Temperature temperature, out RelativeHumidity humidity)
{
temperature = default;
humidity = default;
FirmataCommandSequence dhtCommandSequence = new();
dhtCommandSequence.WriteByte((byte)FirmataSysexCommand.DHT_SENSOR_DATA_REQUEST);
dhtCommandSequence.WriteByte((byte)dhtType);
dhtCommandSequence.WriteByte((byte)pinNumber);
dhtCommandSequence.WriteByte((byte)FirmataCommand.END_SYSEX);
byte[] reply = SendCommandAndWait(dhtCommandSequence);
// Command, pin number and 2x2 bytes data (+ END_SYSEX byte)
if (reply.Length < 7)
{
return false;
}
if (reply[0] != (byte)FirmataSysexCommand.DHT_SENSOR_DATA_REQUEST && reply[1] != 0)
{
return false;
}
int t = reply[3] | reply[4] << 7;
int h = reply[5] | reply[6] << 7;
temperature = Temperature.FromDegreesCelsius(t / 10.0);
humidity = RelativeHumidity.FromPercent(h / 10.0);
return true;
}
}
}