forked from letscontrolit/ESPEasyPluginPlayground
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_P136_MQ7.ino
361 lines (339 loc) · 15.4 KB
/
_P136_MQ7.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#ifdef USES_P136
//#######################################################################################################
//#################################### Plugin 136: MQ7## #############################################
//#################################### by dony71 ########################################################
//#######################################################################################################
// based on https://github.com/R2D2-2017/R2D2-2017/wiki/MQ-7-gas-sensor
#define PLUGIN_136
#define PLUGIN_ID_136 136
#define PLUGIN_NAME_136 "Gases - MQ7 [TESTING]"
#define PLUGIN_VALUENAME1_136 "CO_ppm"
#define PLUGIN_VALUENAME2_136 "CO_Limit"
/// Analog Sensor Pin
#define SENSOR_PIN A0
#define CAL_WARMUP 1
#define GET_RZERO 2
#define DAQ_WARMUP 3
#define GET_ANALOG 4
#define GET_DIGITAL 5
byte Plugin_Voltage_Select_Pin;
byte Plugin_MQ7_CO_Limit_Pin;
boolean Plugin_136_init = false;
unsigned long previousMillis;
unsigned long currentMillis;
const long read_interval = 90000; // 90 seconds
const long heat_interval = 60000; // 60 seconds
const long flag_interval = 30000; // 30 seconds
int CurrentState;
int NextState;
int SensorVoltage;
int CO_Limit;
float sensorValue;
float sensor_volt;
float RS_air;
float RZero;
float RS_gas;
float R_ZERO;
float Ratio;
float Exponent;
float CO_PPM;
//==============================================
// PLUGIN
// =============================================
boolean Plugin_136(byte function, struct EventStruct *event, String& string)
{
boolean success = false;
switch (function)
{
case PLUGIN_DEVICE_ADD:
{
Device[++deviceCount].Number = PLUGIN_ID_136;
Device[deviceCount].Type = DEVICE_TYPE_DUAL;
Device[deviceCount].VType = SENSOR_TYPE_DUAL;
Device[deviceCount].Ports = 0;
Device[deviceCount].PullUpOption = false;
Device[deviceCount].InverseLogicOption = false;
Device[deviceCount].FormulaOption = true;
Device[deviceCount].ValueCount = 2;
Device[deviceCount].SendDataOption = true;
Device[deviceCount].TimerOption = true;
Device[deviceCount].GlobalSyncOption = true;
break;
}
case PLUGIN_GET_DEVICENAME:
{
string = F(PLUGIN_NAME_136);
break;
}
case PLUGIN_GET_DEVICEVALUENAMES:
{
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0], PSTR(PLUGIN_VALUENAME1_136));
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[1], PSTR(PLUGIN_VALUENAME2_136));
break;
}
case PLUGIN_WEBFORM_LOAD:
{
addFormSeparator(2);
addFormTextBox(F("R Zero"), F("plugin_136_RZERO"), String(Settings.TaskDevicePluginConfigFloat[event->TaskIndex][0]), 33);
addUnit(F("260.00"));
success = true;
break;
}
case PLUGIN_WEBFORM_SAVE:
{
Settings.TaskDevicePluginConfigFloat[event->TaskIndex][0] = getFormItemFloat(F("plugin_136_RZERO"));
success = true;
break;
}
case PLUGIN_INIT:
{
Plugin_136_init = true;
Plugin_MQ7_CO_Limit_Pin = Settings.TaskDevicePin1[event->TaskIndex];
pinMode(Plugin_MQ7_CO_Limit_Pin, INPUT);
Plugin_Voltage_Select_Pin = Settings.TaskDevicePin2[event->TaskIndex];
pinMode(Plugin_Voltage_Select_Pin, OUTPUT);
digitalWrite(Plugin_Voltage_Select_Pin, LOW);
// delay(100);
SensorVoltage = 1;
CO_Limit = 1;
UserVar[event->BaseVarIndex + 1] = CO_Limit;
previousMillis = 0;
CurrentState = 1;
NextState = 1;
success = true;
break;
}
case PLUGIN_READ:
{
switch (CurrentState) {
case CAL_WARMUP:
{ // Warm up Sensor for RZero Calibration
currentMillis = millis();
/* String log = F("Interval Millis ");
log += currentMillis - previousMillis;
addLog(LOG_LEVEL_INFO, log);
log = F("Current State: ");
log += CurrentState;
addLog(LOG_LEVEL_INFO, log);
log = F("Sensor Voltage: ");
log += SensorVoltage;
addLog(LOG_LEVEL_INFO, log); */
// Check high voltage to sensor >= 60s
if (currentMillis - previousMillis >= heat_interval) {
String log = F("MQ7: Sensor already warmed up for 60s, ready for getting RZero");
addLog(LOG_LEVEL_INFO, log);
/* log = F("Interval Millis ");
log += currentMillis - previousMillis;
addLog(LOG_LEVEL_INFO, log);
log = F("Current State: ");
log += CurrentState;
addLog(LOG_LEVEL_INFO, log);
log = F("Sensor Voltage: ");
log += SensorVoltage;
addLog(LOG_LEVEL_INFO, log); */
// Apply low voltage to sensor for getting RZero
digitalWrite(Plugin_Voltage_Select_Pin, HIGH);
// delay(100);
log = F("MQ7: Ready for getting RZero, set Sensor Voltage LOW");
addLog(LOG_LEVEL_INFO, log);
SensorVoltage = 0;
NextState = GET_RZERO;
previousMillis = currentMillis;
}
CurrentState = NextState;
break;
}
case GET_RZERO:
{ // Get RZero
currentMillis = millis();
/* log = F("Interval Millis ");
log += currentMillis - previousMillis;
addLog(LOG_LEVEL_INFO, log);
log = F("Current State: ");
log += CurrentState;
addLog(LOG_LEVEL_INFO, log);
log = F("Sensor Voltage: ");
log += SensorVoltage;
addLog(LOG_LEVEL_INFO, log); */
// To get RZero then check low voltage to sensor >= 90s
if (currentMillis - previousMillis >= read_interval) {
sensorValue = analogRead(SENSOR_PIN);
// delay(20);
String log = F("MQ7: Read Analog Sensor for RZero");
addLog(LOG_LEVEL_INFO, log);
sensor_volt = sensorValue/1024*3.3;
RS_air = (3.3-sensor_volt)/sensor_volt;
//RZero = RS_air/(26+(1/3));
RZero = RS_air/0.26; // Assume sensor minimum range 10ppm => Rs/R0=0.2559
//UserVar[event->BaseVarIndex + 2] = (float)RZero;
log = F("MQ7: RZero: ");
log += RZero;
addLog(LOG_LEVEL_INFO, log);
/* log = F("Interval Millis ");
log += currentMillis - previousMillis;
addLog(LOG_LEVEL_INFO, log);
log = F("Current State: ");
log += CurrentState;
addLog(LOG_LEVEL_INFO, log);
log = F("Sensor Voltage: ");
log += SensorVoltage;
addLog(LOG_LEVEL_INFO, log); */
// Apply back high voltage to sensor
digitalWrite(Plugin_Voltage_Select_Pin, LOW);
// delay(100);
log = F("MQ7: Heat Sensor for getting concentration, set Sensor Voltage HIGH");
addLog(LOG_LEVEL_INFO, log);
SensorVoltage = 1;
NextState = DAQ_WARMUP;
previousMillis = currentMillis;
}
CurrentState = NextState;
break;
}
case DAQ_WARMUP:
{ // Warm up Sensor for getting concentration
currentMillis = millis();
/* String log = F("Interval Millis ");
log += currentMillis - previousMillis;
addLog(LOG_LEVEL_INFO, log);
log = F("Current State: ");
log += CurrentState;
addLog(LOG_LEVEL_INFO, log);
log = F("Sensor Voltage: ");
log += SensorVoltage;
addLog(LOG_LEVEL_INFO, log); */
// Check high voltage to sensor >= 60s
if (currentMillis - previousMillis >= heat_interval) {
String log = F("MQ7: Sensor already warmed up for 60s, ready for getting concentration");
addLog(LOG_LEVEL_INFO, log);
/* log = F("Interval Millis ");
log += currentMillis - previousMillis;
addLog(LOG_LEVEL_INFO, log);
log = F("Current State: ");
log += CurrentState;
addLog(LOG_LEVEL_INFO, log);
log = F("Sensor Voltage: ");
log += SensorVoltage;
addLog(LOG_LEVEL_INFO, log); */
// Apply low voltage to sensor to get CO concentration
digitalWrite(Plugin_Voltage_Select_Pin, HIGH);
// delay(100);
log = F("MQ7: Ready for getting concentration, set Sensor Voltage LOW");
addLog(LOG_LEVEL_INFO, log);
SensorVoltage = 0;
NextState = GET_ANALOG;
previousMillis = currentMillis;
}
CurrentState = NextState;
break;
}
case GET_ANALOG:
{ // Get concentration
currentMillis = millis();
/* log = F("Interval Millis ");
log += currentMillis - previousMillis;
addLog(LOG_LEVEL_INFO, log);
log = F("Current State: ");
log += CurrentState;
addLog(LOG_LEVEL_INFO, log);
log = F("Sensor Voltage: ");
log += SensorVoltage;
addLog(LOG_LEVEL_INFO, log); */
// To get CO concentration then check low voltage to sensor >= 90s
if (currentMillis - previousMillis >= read_interval) {
sensorValue = analogRead(SENSOR_PIN);
// delay(20);
String log = F("MQ7: Read Analog Sensor for CO concentration");
addLog(LOG_LEVEL_INFO, log);
sensor_volt = sensorValue/1024*3.3;
RS_gas = (3.3-sensor_volt)/sensor_volt;
R_ZERO = Settings.TaskDevicePluginConfigFloat[event->TaskIndex][0];
Ratio = RS_gas/R_ZERO; //Replace R_ZERO with the value found using the calibration code
//CO_PPM = 100 * pow(log10(40)/log10(0.09), Ratio); //Formula for co 2 concentration
Exponent = -(log10(1000)-log10(10))*log10(Ratio)/(0.53*2)+log10(0.02)+(0.53*3); //Formula for CO concentration from CO/PPM sensitivity charateristic graph in MQ-7 datasheet
CO_PPM = pow(10, Exponent);
UserVar[event->BaseVarIndex] = (float)CO_PPM;
log = F("MQ7: CO: ");
log += CO_PPM;
addLog(LOG_LEVEL_INFO, log);
/* log = F("Interval Millis ");
log += currentMillis - previousMillis;
addLog(LOG_LEVEL_INFO, log);
log = F("Current State: ");
log += CurrentState;
addLog(LOG_LEVEL_INFO, log);
log = F("Sensor Voltage: ");
log += SensorVoltage;
addLog(LOG_LEVEL_INFO, log); */
// Apply back high voltage to sensor
digitalWrite(Plugin_Voltage_Select_Pin, LOW);
// delay(100);
log = F("MQ7: Heat Sensor for getting RZero, set Sensor Voltage HIGH");
addLog(LOG_LEVEL_INFO, log);
SensorVoltage = 1;
NextState = GET_DIGITAL;
previousMillis = currentMillis;
}
CurrentState = NextState;
break;
}
case GET_DIGITAL:
{ // Get CO limit flag
currentMillis = millis();
/* String log = F("Interval Millis ");
log += currentMillis - previousMillis;
addLog(LOG_LEVEL_INFO, log);
log = F("Current State: ");
log += CurrentState;
addLog(LOG_LEVEL_INFO, log);
log = F("Sensor Voltage: ");
log += SensorVoltage;
addLog(LOG_LEVEL_INFO, log); */
// To get CO limit flag then check high voltage to sensor >= 30s
if (currentMillis - previousMillis >= flag_interval) {
CO_Limit = digitalRead(Plugin_MQ7_CO_Limit_Pin);
// delay(20);
String log = F("MQ7: Read Digital Sensor for CO limit");
addLog(LOG_LEVEL_INFO, log);
UserVar[event->BaseVarIndex + 1] = CO_Limit;
log = F("MQ7: CO_Limit: ");
log += CO_Limit;
addLog(LOG_LEVEL_INFO, log);
/* log = F("Interval Millis ");
log += currentMillis - previousMillis;
addLog(LOG_LEVEL_INFO, log);
log = F("Current State: ");
log += CurrentState;
addLog(LOG_LEVEL_INFO, log);
log = F("Sensor Voltage: ");
log += SensorVoltage;
addLog(LOG_LEVEL_INFO, log); */
NextState = CAL_WARMUP;
previousMillis = currentMillis;
}
CurrentState = NextState;
break;
}
/*default:
{
CurrentState = CAL_WARMUP;
NextState = CAL_WARMUP;
break;
}*/
}
/*
String log = F("Current State: ");
log += CurrentState;
addLog(LOG_LEVEL_INFO, log);
log = F("Sensor Voltage: ");
log += SensorVoltage;
addLog(LOG_LEVEL_INFO, log);
*/
success = true;
break;
}
}
return success;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
#endif // USES_P136