forked from letscontrolit/ESPEasyPluginPlayground
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_P176_CDM7160.ino
248 lines (217 loc) · 7.54 KB
/
_P176_CDM7160.ino
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
//#######################################################################################################
//######################## Plugin 176 CDM7160 I2C CO2 Sensor ############################################
//#######################################################################################################
// development version
// by: V0JT4
#define PLUGIN_176
#define PLUGIN_ID_176 176
#define PLUGIN_NAME_176 "Gases - CO2 CDM7160"
#define PLUGIN_VALUENAME1_176 "CO2"
boolean Plugin_176_init = false;
#define CDM7160_ADDR 0x69 // default address
#define CDM7160_ADDR_0 0x68 // CAD0 tied to GND
#define CDM7160_REG_RESET 0x00
#define CDM7160_REG_CTL 0x01
#define CDM7160_REG_STATUS 0x02
#define CDM7160_REG_DATA 0x03
#define CDM7160_REG_HIT 0x0A
#define CDM7160_REG_FUNC 0x0F
#define CDM7160_FLAG_REST 0x01
#define CDM7160_FLAG_DOWN 0x00
#define CDM7160_FLAG_CONT 0x06
#define CDM7160_FLAG_BUSY 0x80
#define CDM7160_FLAG_HPAE 0x04
#define CDM7160_FLAG_PWME 0x01
byte plugin_176_i2caddr;
uint16_t co2;
boolean plugin_176_begin()
{
Wire.begin();
return(true);
}
// Reads an 8 bit value from a register over I2C, no repeated start
uint8_t I2C_read8_ST_reg(uint8_t i2caddr, byte reg) {
uint8_t value;
Wire.beginTransmission(i2caddr);
Wire.write((uint8_t)reg);
Wire.endTransmission();
Wire.requestFrom(i2caddr, (byte)1);
value = Wire.read();
return value;
}
// Reads a 16 bit value starting at a given register over I2C, no repeated start
uint16_t I2C_read16_LE_ST_reg(uint8_t i2caddr, byte reg) {
uint16_t value(0);
Wire.beginTransmission(i2caddr);
Wire.write((uint8_t)reg);
Wire.endTransmission();
Wire.requestFrom(i2caddr, (byte)2);
value = (Wire.read() << 8) | Wire.read();
return (value >> 8) | (value << 8);
}
boolean plugin_176_setPowerDown(void)
// Set power down mode CDM7160 to enable correct settings modification
// Returns true (1) if successful, false (0) if there was an I2C error
{
// Write 0x06 to control register
return(I2C_write8_reg(plugin_176_i2caddr, CDM7160_REG_CTL, CDM7160_FLAG_DOWN));
}
boolean plugin_176_setContinuous(void)
// Set continuous operation mode CDM7160 to start measurements
// Returns true (1) if successful, false (0) if there was an I2C error
{
// Write 0x00 to control register
return(I2C_write8_reg(plugin_176_i2caddr, CDM7160_REG_CTL, CDM7160_FLAG_CONT));
}
boolean plugin_176_setReset(void)
// Reset CDM7160
// Returns true (1) if successful, false (0) if there was an I2C error
{
// Write 0x01 to reset register
return(I2C_write8_reg(plugin_176_i2caddr, CDM7160_REG_RESET, CDM7160_FLAG_REST));
}
boolean plugin_176_setAltitude(unsigned char alt)
// Set altitude compensation on CDM7160
// Returns true (1) if successful, false (0) if there was an I2C error
{
// Write altitude and enable compensation
I2C_write8_reg(plugin_176_i2caddr, CDM7160_REG_HIT, alt);
return(I2C_write8_reg(plugin_176_i2caddr, CDM7160_REG_FUNC, (CDM7160_FLAG_HPAE | CDM7160_FLAG_PWME)));
}
boolean plugin_176_clearAltitude(void)
// Disable altitude compensation on CDM7160
// Returns true (1) if successful, false (0) if there was an I2C error
{
// Clear altitude and disable compensation
I2C_write8_reg(plugin_176_i2caddr, CDM7160_REG_HIT,0);
return(I2C_write8_reg(plugin_176_i2caddr, CDM7160_REG_FUNC, CDM7160_FLAG_PWME));
}
uint8_t plugin_176_getStatus()
// Retrieve CO2 data in ppm
// Returns true (1) if successful, false (0) if there was an I2C error
// (Also see getError() below)
{
// Get content of status register
return I2C_read8_ST_reg(plugin_176_i2caddr, CDM7160_REG_STATUS);
}
uint16_t plugin_176_getCO2()
// Retrieve CO2 data in ppm
// Returns true (1) if successful, false (0) if there was an I2C error
// (Also see getError() below)
{
// Get co2 ppm data out of result registers
return I2C_read16_LE_ST_reg(plugin_176_i2caddr, CDM7160_REG_DATA);
}
boolean Plugin_176(byte function, struct EventStruct *event, String& string)
{
boolean success = false;
switch (function)
{
case PLUGIN_DEVICE_ADD:
{
Device[++deviceCount].Number = PLUGIN_ID_176;
Device[deviceCount].Type = DEVICE_TYPE_I2C;
Device[deviceCount].VType = SENSOR_TYPE_SINGLE;
Device[deviceCount].Ports = 0;
Device[deviceCount].PullUpOption = false;
Device[deviceCount].InverseLogicOption = false;
Device[deviceCount].FormulaOption = true;
Device[deviceCount].ValueCount = 1;
Device[deviceCount].SendDataOption = true;
Device[deviceCount].TimerOption = true;
Device[deviceCount].GlobalSyncOption = true;
break;
}
case PLUGIN_GET_DEVICENAME:
{
string = F(PLUGIN_NAME_176);
break;
}
case PLUGIN_GET_DEVICEVALUENAMES:
{
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0], PSTR(PLUGIN_VALUENAME1_176));
break;
}
case PLUGIN_WEBFORM_LOAD:
{
byte choice1 = Settings.TaskDevicePluginConfig[event->TaskIndex][0];
int optionValues1[2];
optionValues1[0] = CDM7160_ADDR;
optionValues1[1] = CDM7160_ADDR_0;
addFormSelectorI2C(F("plugin_176_cdm7160_i2c"), 2, optionValues1, choice1);
addFormNumericBox(F("Altitude"), F("plugin_176_cdm7160_elev"), Settings.TaskDevicePluginConfig[event->TaskIndex][1]);
addUnit(F("m"));
success = true;
break;
}
case PLUGIN_WEBFORM_SAVE:
{
Settings.TaskDevicePluginConfig[event->TaskIndex][0] = getFormItemInt(F("plugin_176_cdm7160_i2c"));
Settings.TaskDevicePluginConfig[event->TaskIndex][1] = getFormItemInt(F("plugin_176_cdm7160_elev"));
success = true;
break;
}
case PLUGIN_INIT:
{
plugin_176_i2caddr = Settings.TaskDevicePluginConfig[event->TaskIndex][0];
plugin_176_begin();
plugin_176_setPowerDown(); // disabled because there were issues setting alt in continuous mode
unsigned char elev = Settings.TaskDevicePluginConfig[event->TaskIndex][1] / 10;
// delay required to store config byte to EEPROM, device pulls SCL low
delay(100);
if (elev)
{
plugin_176_setAltitude(elev);
} else
{
plugin_176_clearAltitude();
}
delay(100);
plugin_176_setContinuous();
success = true;
break;
}
case PLUGIN_ONCE_A_SECOND:
{
plugin_176_i2caddr = Settings.TaskDevicePluginConfig[event->TaskIndex][0];
plugin_176_begin();
uint8_t status;
status = plugin_176_getStatus();
if (!(status & CDM7160_FLAG_BUSY))
{
co2 = plugin_176_getCO2();
}
break;
}
case PLUGIN_READ:
{
UserVar[event->BaseVarIndex] = co2;
if (co2 > 10000)
{
addLog(LOG_LEVEL_INFO,F("CDM7160: Sensor saturated! > 10000 ppm"));
}
success = true;
String log = F("CDM7160: Address: 0x");
log += String(plugin_176_i2caddr,HEX);
log += F(": CO2 ppm: ");
log += UserVar[event->BaseVarIndex];
log += F(", alt: ");
log += I2C_read8_ST_reg(plugin_176_i2caddr, CDM7160_REG_HIT);
log += F(", comp: ");
log += I2C_read8_ST_reg(plugin_176_i2caddr, CDM7160_REG_FUNC);
addLog(LOG_LEVEL_INFO,log);
break;
}
case PLUGIN_WRITE:
{
if (string.equalsIgnoreCase(F("CDMRST")))
{
success = true;
addLog(LOG_LEVEL_INFO, F("CDM7160: reset"));
plugin_176_setReset();
}
break;
}
}
return success;
}