-
Notifications
You must be signed in to change notification settings - Fork 0
/
OpenAg_Si7021.cpp
206 lines (168 loc) · 4.87 KB
/
OpenAg_Si7021.cpp
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
/**************************************************************************/
/*!
@file OpenAg_Si7021.cpp
@author Limor Fried (Adafruit Industries)
@license BSD (see license.txt)
This is a library for the Adafruit Si7021 breakout board
----> https://www.Adafruit.com/products/3251
Adafruit invests time and resources providing this open source code,
please support OpenAg and open-source hardware by purchasing
products from Adafruit!
@section HISTORY
v1.0 - First release
*/
/**************************************************************************/
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include <OpenAg_Si7021.h>
/**************************************************************************/
//The i2c address cannot be changed, so no need to have it as an input parameter
OpenAg_Si7021::OpenAg_Si7021(void) {
_i2caddr = SI7021_DEFAULT_ADDRESS;
sernum_a = sernum_b = 0;
}
//change signature for OpenAg compatability
void OpenAg_Si7021::begin(){
Wire.begin();
reset();
if (readRegister8(SI7021_READRHT_REG_CMD) != 0x3A){
status_level = ERROR;
status_code = 1;
status_msg = "Unable to read register";
return;
}
readSerialNumber();
//Serial.println(sernum_a, HEX);
//Serial.println(sernum_b, HEX);
status_level = OK;
status_code = CODE_OK;
status_msg = "";
}
//throttle data flow
void OpenAg_Si7021::update(){
if (millis() - _time_of_last_reading > _min_update_interval) {
_time_of_last_reading = millis();
_send_humidity = true;
_send_temperature = true;
status_level = OK;
status_msg = "";
}
}
bool OpenAg_Si7021::get_air_temperature(std_msgs::Float32 &msg){
msg.data = readTemperature();
bool res = _send_temperature;
_send_temperature = false;
return res;
}
bool OpenAg_Si7021::get_air_humidity(std_msgs::Float32 &msg){
msg.data = readHumidity();
bool res = _send_humidity;
_send_humidity = false;
return res;
}
//Adafruit code below
float OpenAg_Si7021::readHumidity(void) {
Wire.beginTransmission(_i2caddr);
Wire.write((uint8_t)SI7021_MEASRH_NOHOLD_CMD);
Wire.endTransmission(false);
delay(25);
Wire.requestFrom(_i2caddr, 3);
uint16_t hum = Wire.read();
hum <<= 8;
hum |= Wire.read();
uint8_t chxsum = Wire.read();
float humidity = hum;
humidity *= 125;
humidity /= 65536;
humidity -= 6;
return humidity;
}
float OpenAg_Si7021::readTemperature(void) {
Wire.beginTransmission(_i2caddr);
Wire.write((uint8_t)SI7021_MEASTEMP_NOHOLD_CMD);
Wire.endTransmission(false);
delay(25);
Wire.requestFrom(_i2caddr, 3);
uint16_t temp = Wire.read();
temp <<= 8;
temp |= Wire.read();
uint8_t chxsum = Wire.read();
float temperature = temp;
temperature *= 175.72;
temperature /= 65536;
temperature -= 46.85;
return temperature;
}
void OpenAg_Si7021::reset(void) {
Wire.beginTransmission(_i2caddr);
Wire.write((uint8_t)SI7021_RESET_CMD);
Wire.endTransmission();
delay(50);
}
void OpenAg_Si7021::readSerialNumber(void) {
Wire.beginTransmission(_i2caddr);
Wire.write((uint8_t)SI7021_ID1_CMD>>8);
Wire.write((uint8_t)SI7021_ID1_CMD&0xFF);
Wire.endTransmission();
Wire.requestFrom(_i2caddr, 8);
sernum_a = Wire.read();
Wire.read();
sernum_a <<= 8;
sernum_a |= Wire.read();
Wire.read();
sernum_a <<= 8;
sernum_a |= Wire.read();
Wire.read();
sernum_a <<= 8;
sernum_a |= Wire.read();
Wire.read();
Wire.beginTransmission(_i2caddr);
Wire.write((uint8_t)SI7021_ID2_CMD>>8);
Wire.write((uint8_t)SI7021_ID2_CMD&0xFF);
Wire.endTransmission();
Wire.requestFrom(_i2caddr, 8);
sernum_b = Wire.read();
Wire.read();
sernum_b <<= 8;
sernum_b |= Wire.read();
Wire.read();
sernum_b <<= 8;
sernum_b |= Wire.read();
Wire.read();
sernum_b <<= 8;
sernum_b |= Wire.read();
Wire.read();
}
/*******************************************************************/
void OpenAg_Si7021::writeRegister8(uint8_t reg, uint8_t value) {
Wire.beginTransmission(_i2caddr);
Wire.write((uint8_t)reg);
Wire.write((uint8_t)value);
Wire.endTransmission();
//Serial.print("Wrote $"); Serial.print(reg, HEX); Serial.print(": 0x"); Serial.println(value, HEX);
}
uint8_t OpenAg_Si7021::readRegister8(uint8_t reg) {
uint8_t value;
Wire.beginTransmission(_i2caddr);
Wire.write((uint8_t)reg);
Wire.endTransmission(false);
Wire.requestFrom(_i2caddr, 1);
value = Wire.read();
//Serial.print("Read $"); Serial.print(reg, HEX); Serial.print(": 0x"); Serial.println(value, HEX);
return value;
}
uint16_t OpenAg_Si7021::readRegister16(uint8_t reg) {
uint16_t value;
Wire.beginTransmission(_i2caddr);
Wire.write((uint8_t)reg);
Wire.endTransmission();
Wire.requestFrom(_i2caddr, 2);
value = Wire.read();
value <<= 8;
value |= Wire.read();
//Serial.print("Read $"); Serial.print(reg, HEX); Serial.print(": 0x"); Serial.println(value, HEX);
return value;
}