51 #define MQ_SENSOR_ANALOG_PIN (0) //define which analog input channel you are going to use
52 #define RL_VALUE (5) //define the load resistance on the board, in kilo ohms
53 #define RO_CLEAN_AIR_FACTOR (9.83) //RO_CLEAR_AIR_FACTOR=(Sensor resistance in clean air)/RO,
56 #define CALIBARAION_SAMPLE_TIMES (50) //define how many samples you are going to take in the calibration phase
57 #define CALIBRATION_SAMPLE_INTERVAL (500) //define the time interval(in milliseconds) between each samples in the
59 #define READ_SAMPLE_INTERVAL (50) //define how many samples you are going to take in normal operation
60 #define READ_SAMPLE_TIMES (5) //define the time interval(in milliseconds) between each samples in
67 uint32_t SLEEP_TIME = 30000;
72 float LPGCurve[3] = {2.3,0.21,-0.47};
76 float COCurve[3] = {2.3,0.72,-0.34};
80 float SmokeCurve[3] = {2.3,0.53,-0.44};
91 MQ_SENSOR_ANALOG_PIN);
100 present(CHILD_ID_MQ, S_AIR_QUALITY);
105 uint16_t valMQ = MQGetGasPercentage(MQRead(MQ_SENSOR_ANALOG_PIN)/Ro,GAS_CO);
108 Serial.print(
"LPG:");
109 Serial.print(MQGetGasPercentage(MQRead(MQ_SENSOR_ANALOG_PIN)/Ro,GAS_LPG) );
110 Serial.print(
"ppm" );
113 Serial.print(MQGetGasPercentage(MQRead(MQ_SENSOR_ANALOG_PIN)/Ro,GAS_CO) );
114 Serial.print(
"ppm" );
116 Serial.print(
"SMOKE:");
117 Serial.print(MQGetGasPercentage(MQRead(MQ_SENSOR_ANALOG_PIN)/Ro,GAS_SMOKE) );
118 Serial.print(
"ppm" );
121 if (valMQ != lastMQ) {
122 send(msg.
set((int16_t)ceil(valMQ)));
123 lastMQ = ceil(valMQ);
136 float MQResistanceCalculation(
int raw_adc)
138 return ( ((
float)RL_VALUE*(1023-raw_adc)/raw_adc));
149 float MQCalibration(
int mq_pin)
154 for (i=0; i<CALIBARAION_SAMPLE_TIMES; i++) {
155 inVal += MQResistanceCalculation(analogRead(mq_pin));
156 delay(CALIBRATION_SAMPLE_INTERVAL);
158 inVal = inVal/CALIBARAION_SAMPLE_TIMES;
160 inVal = inVal/RO_CLEAN_AIR_FACTOR;
173 float MQRead(
int mq_pin)
178 for (i=0; i<READ_SAMPLE_TIMES; i++) {
179 rs += MQResistanceCalculation(analogRead(mq_pin));
180 delay(READ_SAMPLE_INTERVAL);
183 rs = rs/READ_SAMPLE_TIMES;
195 int MQGetGasPercentage(
float rs_ro_ratio,
int gas_id)
197 if ( gas_id == GAS_LPG ) {
198 return MQGetPercentage(rs_ro_ratio,LPGCurve);
199 }
else if ( gas_id == GAS_CO ) {
200 return MQGetPercentage(rs_ro_ratio,COCurve);
201 }
else if ( gas_id == GAS_SMOKE ) {
202 return MQGetPercentage(rs_ro_ratio,SmokeCurve);
217 int MQGetPercentage(
float rs_ro_ratio,
float *pcurve)
219 return (pow(10,( ((log(rs_ro_ratio)-pcurve[1])/pcurve[2]) + pcurve[0])));