-
Notifications
You must be signed in to change notification settings - Fork 0
/
SensorAM2320.h
208 lines (191 loc) · 7.3 KB
/
SensorAM2320.h
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#pragma once
#include "ArduinoWorkflow.h"
namespace AW {
template <uint8_t Address = 0x5c, uint8_t PowerPin = -1, typename WireType = TWire>
class TSensorAM2320 : public TActor {
public:
TActor* Owner;
TTime Period = TTime::MilliSeconds(8000);
bool SendValues = true;
TSensor<2> Sensor;
enum ESensor {
Temperature,
Humidity
};
TSensorAM2320(TActor* owner, const String& name = "am2320")
: Owner(owner)
{
Sensor.Name = name;
Sensor.Values[ESensor::Temperature].Name = "temperature";
Sensor.Values[ESensor::Humidity].Name = "humidity";
}
protected:
static constexpr auto PowerOnDelay = TTime::MilliSeconds(1200);
WireType Wire;
bool Powered = false;
TPin<PowerPin> Power;
unsigned long Errors = 0;
void OnEvent(TEventPtr event, const TActorContext& context) override {
switch (event->EventID) {
case TEventBootstrap::EventID:
return OnBootstrap(static_cast<TEventBootstrap*>(event.Release()), context);
case TEventReceive::EventID:
return OnReceive(static_cast<TEventReceive*>(event.Release()), context);
}
}
void PowerOn() {
if (!Powered) {
Powered = true;
if (PowerPin != -1) {
Power = Powered;
}
}
}
void PowerOff() {
if (Powered) {
Powered = false;
if (PowerPin != -1) {
Power = Powered;
}
}
}
void OnBootstrap(TUniquePtr<TEventBootstrap>, const TActorContext& context) {
for (auto tries = 0; tries < 2; ++tries) {
PowerOn();
delay(PowerOnDelay.MilliSeconds());
Wire.BeginTransmission(Address);
Wire.EndTransmission();
Wire.BeginTransmission(Address);
Wire.Write(0x03);
Wire.Write(0x08);
Wire.Write(0x02);
if (Wire.EndTransmission()) {
delayMicroseconds(1600);
if (Wire.RequestFrom(Address, 0x06) == 0x06) {
uint8_t code;
uint8_t length;
uint16_t model;
uint16_t crc16;
Wire.Read(code);
Wire.Read(length);
Wire.Read(model);
Wire.Read(crc16);
if (code == 0x03 && length == 0x02) {
context.Send(this, this, new AW::TEventReceive(context.Now + Period));
context.Send(this, Owner, new AW::TEventSensorMessage(Sensor, StringStream() << "AM2320 on " << String(Address, 16)));
break;
}
}
}
PowerOff();
delay(3000);
}
}
static uint16_t CRC16(const uint8_t* ptr, uint8_t length) {
uint16_t crc = 0xFFFF;
uint8_t s = 0x00;
while (length--) {
crc ^= *ptr++;
for (s = 0; s < 8; ++s) {
if ((crc & 0x01) != 0) {
crc >>= 1;
crc ^= 0xA001;
} else crc >>= 1;
}
}
return crc;
}
template <typename T>
static uint16_t CRC16(const T& data) {
return CRC16(reinterpret_cast<const uint8_t*>(&data), sizeof(data));
}
static void bswap(uint16_t& data) {
data = (data >> 8) | (data << 8);
}
struct TData {
uint8_t Code;
uint8_t Length;
uint16_t Humidity;
uint16_t Temperature;
};
void OnReceive(AW::TUniquePtr<AW::TEventReceive> event, const AW::TActorContext& context) {
if (!Powered) {
PowerOn();
event->NotBefore = context.Now + PowerOnDelay;
context.Resend(this, event.Release());
return;
}
auto timeout = Period;
bool good = false;
//Wire.BeginTransmission(Address);
// delayMicroseconds(1600);
//Wire.EndTransmission();
for (auto tries = 0; tries < 1; ++tries) {
//delayMicroseconds(30);
Wire.BeginTransmission(Address);
Wire.EndTransmission(false);
//delayMicroseconds(800);
Wire.BeginTransmission(Address);
Wire.Write(0x03);
Wire.Write(0x00);
Wire.Write(0x04);
if (Wire.EndTransmission()) {
delayMicroseconds(3000);
if (Wire.RequestFrom(Address, 0x08) == 0x08) {
TData data;
uint16_t crc16;
Wire.Read(data);
Wire.Read(crc16);
bswap(crc16);
if (data.Code != 0x03) {
/*StringStream stream;
stream << "AM2320 wrong Code (" << data.Code << ")";
context.Send(this, Owner, new AW::TEventSensorMessage(stream));
stream.clear();
stream << data.Code << " vs " << 3;
context.Send(this, Owner, new AW::TEventSensorMessage(stream));*/
//timeout = TTime::MilliSeconds(2000);
} else if (data.Length != 4) {
/*StringStream stream;
stream << "AM2320 wrong Length";
context.Send(this, Owner, new AW::TEventSensorMessage(stream));
stream.clear();
stream << data.Length << " vs " << 4;
context.Send(this, Owner, new AW::TEventSensorMessage(stream));*/
//timeout = TTime::MilliSeconds(2000);
} else if (CRC16(data) != crc16) {
/*StringStream stream;
stream << "AM2320 wrong CRC16";
context.Send(this, Owner, new AW::TEventSensorMessage(stream));
stream.clear();
stream << String(crc16, 16) << " vs " << String(CRC16(data), 16);
context.Send(this, Owner, new AW::TEventSensorMessage(stream));*/
//timeout = TTime::MilliSeconds(2000);
} else {
bswap(data.Temperature);
bswap(data.Humidity);
Sensor.Values[ESensor::Temperature].Value = (float)data.Temperature / 10;
Sensor.Values[ESensor::Humidity].Value = (float)data.Humidity / 10;
Sensor.Updated = context.Now;
if (SendValues) {
context.Send(this, Owner, new AW::TEventSensorData(Sensor, Sensor.Values[ESensor::Temperature]));
context.Send(this, Owner, new AW::TEventSensorData(Sensor, Sensor.Values[ESensor::Humidity]));
}
good = true;
}
}
}
if (good) {
break;
}
}
if (!good) {
PowerOff();
timeout = TTime::MilliSeconds(Period - PowerOnDelay.MilliSeconds());
context.Send(this, Owner, new AW::TEventSensorMessage(Sensor, StringStream() << "error " << ++Errors));
}
event->NotBefore = context.Now + timeout;
context.Resend(this, event.Release());
}
};
}