50 #define MQ_SENSOR_ANALOG_PIN (0) //define which analog input channel you are going to use
51 #define RL_VALUE (5) //define the load resistance on the board, in kilo ohms
52 #define RO_CLEAN_AIR_FACTOR (9.83) //RO_CLEAR_AIR_FACTOR=(Sensor resistance in clean air)/RO,
55 #define CALIBARAION_SAMPLE_TIMES (50) //define how many samples you are going to take in the calibration phase
56 #define CALIBRATION_SAMPLE_INTERVAL (500) //define the time interval(in milliseconds) between each samples in the
58 #define READ_SAMPLE_INTERVAL (50) //define how many samples you are going to take in normal operation
59 #define READ_SAMPLE_TIMES (5) //define the time interval(in milliseconds) between each samples in
66 uint32_t SLEEP_TIME = 30000;
71 float LPGCurve[3] = {2.3,0.21,-0.47};
75 float COCurve[3] = {2.3,0.72,-0.34};
79 float SmokeCurve[3] = {2.3,0.53,-0.44};
90 MQ_SENSOR_ANALOG_PIN);
99 present(CHILD_ID_MQ, S_AIR_QUALITY);
104 uint16_t valMQ = MQGetGasPercentage(MQRead(MQ_SENSOR_ANALOG_PIN)/Ro,GAS_CO);
107 Serial.print(
"LPG:");
108 Serial.print(MQGetGasPercentage(MQRead(MQ_SENSOR_ANALOG_PIN)/Ro,GAS_LPG) );
109 Serial.print(
"ppm" );
112 Serial.print(MQGetGasPercentage(MQRead(MQ_SENSOR_ANALOG_PIN)/Ro,GAS_CO) );
113 Serial.print(
"ppm" );
115 Serial.print(
"SMOKE:");
116 Serial.print(MQGetGasPercentage(MQRead(MQ_SENSOR_ANALOG_PIN)/Ro,GAS_SMOKE) );
117 Serial.print(
"ppm" );
120 if (valMQ != lastMQ) {
121 send(msg.
set((int16_t)ceil(valMQ)));
122 lastMQ = ceil(valMQ);
135 float MQResistanceCalculation(
int raw_adc)
137 return ( ((
float)RL_VALUE*(1023-raw_adc)/raw_adc));
148 float MQCalibration(
int mq_pin)
153 for (i=0; i<CALIBARAION_SAMPLE_TIMES; i++) {
154 inVal += MQResistanceCalculation(analogRead(mq_pin));
155 delay(CALIBRATION_SAMPLE_INTERVAL);
157 inVal = inVal/CALIBARAION_SAMPLE_TIMES;
159 inVal = inVal/RO_CLEAN_AIR_FACTOR;
172 float MQRead(
int mq_pin)
177 for (i=0; i<READ_SAMPLE_TIMES; i++) {
178 rs += MQResistanceCalculation(analogRead(mq_pin));
179 delay(READ_SAMPLE_INTERVAL);
182 rs = rs/READ_SAMPLE_TIMES;
194 int MQGetGasPercentage(
float rs_ro_ratio,
int gas_id)
196 if ( gas_id == GAS_LPG ) {
197 return MQGetPercentage(rs_ro_ratio,LPGCurve);
198 }
else if ( gas_id == GAS_CO ) {
199 return MQGetPercentage(rs_ro_ratio,COCurve);
200 }
else if ( gas_id == GAS_SMOKE ) {
201 return MQGetPercentage(rs_ro_ratio,SmokeCurve);
216 int MQGetPercentage(
float rs_ro_ratio,
float *pcurve)
218 return (pow(10,( ((log(rs_ro_ratio)-pcurve[1])/pcurve[2]) + pcurve[0])));