-
Notifications
You must be signed in to change notification settings - Fork 317
/
_P130_VEML6075.ino
248 lines (207 loc) · 9.07 KB
/
_P130_VEML6075.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
#ifdef USES_P130
//#######################################################################################################
//########################### Plugin 130 VEML6075 I2C UVA/UVB Sensor ###############################
//#######################################################################################################
//###################################### [email protected] ##########################################
//#######################################################################################################
#define PLUGIN_130
#define PLUGIN_ID_130 130
#define PLUGIN_NAME_130 "Environment - VEML6075 [TESTING]"
#define PLUGIN_VALUENAME1_130 "UVA"
#define PLUGIN_VALUENAME2_130 "UVB"
#define PLUGIN_VALUENAME3_130 "UVIndex"
////////////////////////////
// VEML6075 Command Codes //
////////////////////////////
#define VEML6075_UV_CONF 0x00 // command codes
#define VEML6075_UVA_DATA 0x07 // 2 bytes
#define VEML6075_UVDUMMY_DATA 0x08
#define VEML6075_UVB_DATA 0x09
#define VEML6075_UVCOMP1_DATA 0x0A
#define VEML6075_UVCOMP2_DATA 0x0B
#define VEML6075_UV_ID 0x0C // should retrn 0x26
#define ACoef 3.33
#define BCoef 2.5
#define CCoef 3.66
#define DCoef 2.75
#define UVAresponsivity 0.0011
#define UVBresponsivity 0.00125
enum IT {
IT_50 = 0, // 50 ms
IT_100 = 1, // 100 ms
IT_200 = 2, // 200 ms
IT_400 = 3, // 400 ms
IT_800 = 4 // 800 ms
};
// Specify VEML6075 Integration time
uint8_t IT = IT_100;
uint16_t UVData[5] = {0, 0, 0, 0, 0}; // UVA, Dummy, UVB, UVComp1, UVComp2
float UVAComp, UVBComp, UVIndex;
bool HD = 0;
uint8_t veml6075_i2caddr;
uint8_t Plugin_130_read8(byte reg, bool * is_ok = NULL); // Declaration
boolean Plugin_130_init[2] = {false, false};
boolean Plugin_130(byte function, struct EventStruct *event, String& string)
{
boolean success = false;
switch (function)
{
case PLUGIN_DEVICE_ADD:
{
Device[++deviceCount].Number = PLUGIN_ID_130;
Device[deviceCount].Type = DEVICE_TYPE_I2C;
Device[deviceCount].VType = Sensor_VType::SENSOR_TYPE_TRIPLE;
Device[deviceCount].Ports = 0;
Device[deviceCount].PullUpOption = false;
Device[deviceCount].InverseLogicOption = false;
Device[deviceCount].FormulaOption = true;
Device[deviceCount].ValueCount = 3;
Device[deviceCount].SendDataOption = true;
Device[deviceCount].TimerOption = true;
Device[deviceCount].GlobalSyncOption = true;
break;
}
case PLUGIN_GET_DEVICENAME:
{
string = F(PLUGIN_NAME_130);
break;
}
case PLUGIN_GET_DEVICEVALUENAMES:
{
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0], PSTR(PLUGIN_VALUENAME1_130));
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[1], PSTR(PLUGIN_VALUENAME2_130));
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[2], PSTR(PLUGIN_VALUENAME3_130));
break;
}
case PLUGIN_WEBFORM_LOAD:
{
byte choice = Settings.TaskDevicePluginConfig[event->TaskIndex][0];
int optionValues[2] = { 0x10, 0x11 };
addFormSelectorI2C(F("plugin_130_veml6075_i2c"), 2, optionValues, choice);
addFormNote(F("SDO Low=0x10, High=0x11"));
byte choiceMode2 = Settings.TaskDevicePluginConfig[event->TaskIndex][1];
String optionsMode2[5];
optionsMode2[0] = F("VEML6075_IT_50MS");
optionsMode2[1] = F("VEML6075_IT_100MS");
optionsMode2[2] = F("VEML6075_IT_200MS");
optionsMode2[3] = F("VEML6075_IT_400MS");
optionsMode2[4] = F("VEML6075_IT_800MS");
int optionValuesMode2[5];
optionValuesMode2[0] = IT_50;
optionValuesMode2[1] = IT_100;
optionValuesMode2[2] = IT_200;
optionValuesMode2[3] = IT_400;
optionValuesMode2[4] = IT_800;
addFormSelector(F("Integration Time"), F("plugin_130_veml6075_it"), 5, optionsMode2, optionValuesMode2, choiceMode2);
byte choiceMode3 = Settings.TaskDevicePluginConfig[event->TaskIndex][2];
String optionsMode3[2];
optionsMode3[0] = F("VEML6075_NORMAL_DYNAMIC");
optionsMode3[1] = F("VEML6075_HIGH_DYNAMIC");
int optionValuesMode3[2];
optionValuesMode3[0] = 0;
optionValuesMode3[1] = 1;
addFormSelector(F("Dynamic Setting"), F("plugin_130_veml6075_hd"), 2, optionsMode3, optionValuesMode3, choiceMode3);
success = true;
break;
}
case PLUGIN_WEBFORM_SAVE:
{
Settings.TaskDevicePluginConfig[event->TaskIndex][0] = getFormItemInt(F("plugin_130_veml6075_i2c"));
Settings.TaskDevicePluginConfig[event->TaskIndex][1] = getFormItemInt(F("plugin_130_veml6075_it"));
Settings.TaskDevicePluginConfig[event->TaskIndex][2] = getFormItemInt(F("plugin_130_veml6075_hd"));
Plugin_130_init[Settings.TaskDevicePluginConfig[event->TaskIndex][0]] = false;
success = true;
break;
}
case PLUGIN_READ:
{
//int idx = Settings.TaskDevicePluginConfig[event->TaskIndex][0] ;
veml6075_i2caddr = Settings.TaskDevicePluginConfig[event->TaskIndex][0];
IT = Settings.TaskDevicePluginConfig[event->TaskIndex][1]; // set Integration Time
if (!Plugin_130_init[veml6075_i2caddr])
{
Plugin_130_init[veml6075_i2caddr] = Plugin_130_init_sensor(); // Check id device is present
}
String log = F("VEML6075: veml6075_i2caddr: 0x");
log += String(veml6075_i2caddr,HEX);
addLog(LOG_LEVEL_DEBUG, log);
log = F("VEML6075: plugin(veml6075_i2caddr): ");
log += String(Plugin_130_init[veml6075_i2caddr]?"true":"false");
addLog(LOG_LEVEL_DEBUG, log);
if (Plugin_130_init[veml6075_i2caddr])
{
for (int j = 0; j < 5; j++)
{
UVData[j] = I2C_read16_LE_reg(Settings.TaskDevicePluginConfig[event->TaskIndex][0], VEML6075_UVA_DATA + j);
}
// Calculate the UV Index, valid in open air not behind glass!
UVAComp = (UVData[0] - UVData[1]) - ACoef*(UVData[3] - UVData[1]) - BCoef*(UVData[4] - UVData[1]);
UVBComp = (UVData[2] - UVData[1]) - CCoef*(UVData[3] - UVData[1]) - DCoef*(UVData[4] - UVData[1]);
UVIndex = ( (UVBComp*UVBresponsivity) + (UVAComp*UVAresponsivity) )/2.;
//float UVASensitivity = 0.93/((float) (IT + 1)); // UVA light sensitivity increases with integration time
//float UVBSensitivity = 2.10/((float) (IT + 1)); // UVB light sensitivity increases with integration time
log = F("VEML6075: IT raw: 0x");
log += String((IT + 1),HEX);
addLog(LOG_LEVEL_DEBUG, log);
UserVar[event->BaseVarIndex] = UVData[0]/pow(2,IT-1); // UVA light sensitivity increases linear with integration time
UserVar[event->BaseVarIndex + 1] = UVData[2]/pow(2,IT-1); // UVB light sensitivity increases linear with integration time
UserVar[event->BaseVarIndex + 2] = UVIndex;
log = F("VEML6075: Address: 0x");
log += String(veml6075_i2caddr,HEX);
log += F(" / Integration Time: ");
log += Settings.TaskDevicePluginConfig[event->TaskIndex][1];
log += F(" / Dynamic Mode: ");
log += Settings.TaskDevicePluginConfig[event->TaskIndex][2];
log += F(" / divisor: ");
log += String(pow(2,IT-1));
log += F(" / UVA: ");
log += UserVar[event->BaseVarIndex];
log += F(" / UVB: ");
log += UserVar[event->BaseVarIndex + 1];
log += F(" / UVIndex: ");
log += UserVar[event->BaseVarIndex + 2];
addLog(LOG_LEVEL_INFO, log);
success = true;
}
break;
}
}
return success;
}
//**************************************************************************/
// Check VEML6075 presence and initialize
//**************************************************************************/
bool Plugin_130_init_sensor() {
uint16_t deviceID = I2C_readS16_LE_reg(veml6075_i2caddr, VEML6075_UV_ID);
String log = F("VEML6075: ID: 0x");
log += String(deviceID, HEX);
log += F(" / checked Address: 0x");
log += String(veml6075_i2caddr, HEX);
log += F(" / 0x");
log += String(VEML6075_UV_ID, HEX);
addLog(LOG_LEVEL_DEBUG, log);
if (deviceID != 0x26) {
log = F("VEML6075: wrong deviceID: ");
log += String(deviceID, HEX);
addLog(LOG_LEVEL_ERROR, log);
return false;
} else {
log = F("VEML6075: found deviceID: 0x");
log += String(deviceID, HEX);
if (!I2C_write16_LE_reg(veml6075_i2caddr, VEML6075_UV_CONF, (IT << 4)|(HD << 3))) { // Bit 3 must be 0, bit 0 is 0 for run and 1 for shutdown, LS Byte
log = F("VEML6075: setup failed!!");
log += F(" / CONF: ");
log += String((uint16_t)(IT << 4)|(HD << 3), BIN);
addLog(LOG_LEVEL_ERROR, log);
return false;
} else {
log = F("VEML6075: sensor initialised");
log += F(" / CONF: ");
log += String((uint16_t)(IT << 4)|(HD << 3), BIN);
addLog(LOG_LEVEL_INFO, log);
delay(150);
return true;
}
}
}
#endif