MQSensorsLib/src/MQUnifiedsensor.cpp

259 lines
6.5 KiB
C++
Raw Normal View History

2019-04-30 08:52:47 -05:00
#include "MQUnifiedsensor.h"
2019-05-24 16:15:02 -05:00
MQUnifiedsensor::MQUnifiedsensor(int pin, int type) {
2019-05-23 15:17:39 -05:00
2019-04-30 08:52:47 -05:00
this->_pin = pin;
2019-05-19 13:27:00 -05:00
this->_type = type; //2,3,4,5,6,7,8,9,131,135,303,309
2019-05-23 15:17:39 -05:00
//Set _MQ variable with MQ sensor selected
if(_type == 2)
{
2019-05-25 10:46:44 -05:00
memcpy(_MQ, _MQ2, sizeof(_MQ2));
2019-05-29 21:03:46 -05:00
_ratioInCleanAir = RatioMQ2CleanAir;
2019-05-29 21:30:08 -05:00
_R0 = R0_MQ2;
2019-05-24 17:02:46 -05:00
//_MQ = MQ2;
2019-05-23 15:17:39 -05:00
}
else if(_type == 3)
{
2019-05-25 10:46:44 -05:00
memcpy(_MQ, _MQ3, sizeof(_MQ3));
2019-05-29 21:03:46 -05:00
_ratioInCleanAir = RatioMQ3CleanAir;
2019-05-29 21:30:08 -05:00
_R0 = R0_MQ3;
2019-05-24 17:02:46 -05:00
//_MQ = MQ3;
2019-05-23 15:17:39 -05:00
}
else if(_type == 4)
{
2019-05-25 10:46:44 -05:00
memcpy(_MQ, _MQ4, sizeof(_MQ4));
2019-05-29 21:03:46 -05:00
_ratioInCleanAir = RatioMQ4CleanAir;
2019-05-29 21:30:08 -05:00
_R0 = R0_MQ4;
2019-05-24 17:02:46 -05:00
//_MQ = MQ4;
2019-05-23 15:17:39 -05:00
}
else if(_type == 5)
{
2019-05-25 10:46:44 -05:00
memcpy(_MQ, _MQ5, sizeof(_MQ5));
2019-05-29 21:03:46 -05:00
_ratioInCleanAir = RatioMQ5CleanAir;
2019-05-29 21:30:08 -05:00
_R0 = R0_MQ5;
2019-05-24 17:02:46 -05:00
//_MQ = MQ5;
2019-05-23 15:17:39 -05:00
}
else if(_type == 6)
{
2019-05-25 10:46:44 -05:00
memcpy(_MQ, _MQ6, sizeof(_MQ6));
2019-05-29 21:03:46 -05:00
_ratioInCleanAir = RatioMQ6CleanAir;
2019-05-29 21:30:08 -05:00
_R0 = R0_MQ6;
2019-05-24 17:02:46 -05:00
//_MQ = MQ6;
2019-05-23 15:17:39 -05:00
}
else if(_type == 7)
{
2019-05-25 10:46:44 -05:00
memcpy(_MQ, _MQ7, sizeof(_MQ7));
2019-05-29 21:03:46 -05:00
_ratioInCleanAir = RatioMQ7CleanAir;
2019-05-29 21:30:08 -05:00
_R0 = R0_MQ7;
2019-05-24 17:02:46 -05:00
//_MQ = MQ7;
2019-05-23 15:17:39 -05:00
}
else if(_type == 8)
{
2019-05-25 10:46:44 -05:00
memcpy(_MQ, _MQ8, sizeof(_MQ8));
2019-05-29 21:03:46 -05:00
_ratioInCleanAir = RatioMQ8CleanAir;
2019-05-29 21:30:08 -05:00
_R0 = R0_MQ8;
2019-05-24 17:02:46 -05:00
//_MQ = MQ8;
2019-05-23 15:17:39 -05:00
}
else if(_type == 9)
{
2019-05-25 10:46:44 -05:00
memcpy(_MQ, _MQ9, sizeof(_MQ9));
2019-05-29 21:03:46 -05:00
_ratioInCleanAir = RatioMQ9CleanAir;
2019-05-29 21:30:08 -05:00
_R0 = R0_MQ9;
2019-05-24 17:02:46 -05:00
//_MQ = MQ9;
2019-05-23 15:17:39 -05:00
}
else if(_type == 131)
{
2019-05-25 10:46:44 -05:00
memcpy(_MQ, _MQ131, sizeof(_MQ131));
2019-05-29 21:03:46 -05:00
_ratioInCleanAir = RatioMQ131CleanAir;
2019-05-29 21:30:08 -05:00
_R0 = R0_MQ131;
2019-05-24 17:02:46 -05:00
//_MQ = MQ131;
2019-05-23 15:17:39 -05:00
}
else if(_type == 135)
{
2019-05-25 10:46:44 -05:00
memcpy(_MQ, _MQ135, sizeof(_MQ135));
2019-05-29 21:03:46 -05:00
_ratioInCleanAir = RatioMQ135CleanAir;
2019-05-29 21:30:08 -05:00
_R0 = R0_MQ135;
2019-05-24 17:02:46 -05:00
//_MQ = MQ135;
2019-05-23 15:17:39 -05:00
}
else if(_type == 303)
{
2019-05-25 10:46:44 -05:00
memcpy(_MQ, _MQ303A, sizeof(_MQ303A));
2019-05-29 21:03:46 -05:00
_ratioInCleanAir = RatioMQ303CleanAir;
2019-05-29 21:30:08 -05:00
_R0 = R0_MQ303;
2019-05-24 17:02:46 -05:00
//_MQ = MQ303A;
2019-05-23 15:17:39 -05:00
}
else if(_type == 309)
{
2019-05-25 10:46:44 -05:00
memcpy(_MQ, _MQ309A, sizeof(_MQ309A));
2019-05-29 21:03:46 -05:00
_ratioInCleanAir = RatioMQ309CleanAir;
2019-05-29 21:30:08 -05:00
_R0 = R0_MQ309;
2019-05-24 17:02:46 -05:00
//_MQ = MQ309;
2019-05-23 15:17:39 -05:00
}
2019-04-30 08:52:47 -05:00
}
2019-05-25 10:46:44 -05:00
void MQUnifiedsensor::setVoltResolution(float voltaje)
{
2019-05-29 22:13:13 -05:00
_VOLT_RESOLUTION = voltaje;
2019-05-25 10:46:44 -05:00
}
2019-05-24 17:13:54 -05:00
void MQUnifiedsensor::inicializar()
{
pinMode(_pin, INPUT);
2019-04-30 08:52:47 -05:00
}
2019-05-24 17:02:46 -05:00
int MQUnifiedsensor::readSensor(String nameLectureRequeired, bool print)
2019-04-30 08:52:47 -05:00
{
2019-05-24 17:24:37 -05:00
setSensorCharacteristics(nameLectureRequeired, print);
2019-05-24 17:02:46 -05:00
_PPM =readPPM(_m, _b);
if(print)
{
2019-05-24 17:24:37 -05:00
String nameLecture = getnameLecture();
2019-05-26 15:18:16 -05:00
Serial.println("**********************");
Serial.println("* Sensor: MQ-" + String(_type));
2019-05-29 21:31:45 -05:00
Serial.println("* m =" + String(_m) + " ,b =" + String(_b) + ", R0 = " + _R0);
Serial.println("* RS/R0 = " + String(_ratio) + " ,Voltaje leido(ADC): " + String(_sensor_volt));
2019-05-26 15:18:16 -05:00
Serial.println("* Lectura(" + nameLecture + ")=" + String(_PPM) + " PPM");
Serial.println("**********************");
2019-05-24 17:02:46 -05:00
}
return _PPM;
2019-04-30 08:52:47 -05:00
}
2019-05-24 17:13:54 -05:00
String MQUnifiedsensor::getnameLecture()
2019-05-23 16:51:24 -05:00
{
2019-05-24 22:15:53 -05:00
return _nameLectureRequeired;
2019-05-23 16:51:24 -05:00
}
2019-05-24 17:24:37 -05:00
void MQUnifiedsensor::setSensorCharacteristics(String nameLectureRequeired, bool print)
2019-04-30 08:52:47 -05:00
{
2019-05-24 17:24:37 -05:00
//Defaults index
2019-05-23 16:21:53 -05:00
if(nameLectureRequeired == "")
{
//Set default
setDefaultGas();
//Put the default into variable internally used
2019-05-24 22:16:37 -05:00
nameLectureRequeired = _nameLectureRequeired;
2019-05-23 16:21:53 -05:00
}
2019-05-24 22:09:11 -05:00
//Dinamic index search
for (int i=0; i<sizeof(_MQ); i++) {
if (nameLectureRequeired == _MQ[i]) { //modified here
2019-05-24 22:09:11 -05:00
_lecturePosInArray = i;
break;
}
}
2019-05-24 17:24:37 -05:00
//Serial debugging
2019-05-24 22:09:11 -05:00
_m = stringToDouble(_MQ[_lecturePosInArray+1]);
_b = stringToDouble(_MQ[_lecturePosInArray+2]);
2019-05-24 22:14:34 -05:00
//Save the name for future calls
2019-05-24 22:16:37 -05:00
nameLectureRequeired = _nameLectureRequeired;
2019-04-30 08:52:47 -05:00
}
2019-05-24 17:13:54 -05:00
int MQUnifiedsensor::readPPM(int m, int b) {
/**
* Returns the PPM concentration
*/
2019-05-29 21:31:45 -05:00
_sensor_volt = this->getVoltage();
2019-05-24 17:13:54 -05:00
double RS_gas; //Define variable for sensor resistance
2019-04-30 08:52:47 -05:00
2019-05-29 22:13:13 -05:00
RS_gas = ((_VOLT_RESOLUTION*_RLValue)/_sensor_volt)-_RLValue; //Get value of RS in a gas
2019-05-24 17:13:54 -05:00
2019-05-26 15:18:16 -05:00
_ratio = RS_gas / this->_R0; // Get ratio RS_gas/RS_air
2019-05-24 17:13:54 -05:00
2019-05-26 15:18:16 -05:00
double ppm_log = (log10(_ratio) - b) / m; //Get ppm value in linear scale according to the the ratio value
2019-05-24 17:13:54 -05:00
double ppm = pow(10, ppm_log); //Convert ppm value to log scale
return floor(ppm);
}
2019-05-29 22:17:25 -05:00
int MQUnifiedsensor::calibrate(boolean print) {
2019-05-29 20:58:22 -05:00
//More explained in: https://jayconsystems.com/blog/understanding-a-gas-sensor
/*
V = I x R
VRL = [VC / (RS + RL)] x RL
VRL = (VC x RL) / (RS + RL)
Así que ahora resolvemos para RS:
VRL x (RS + RL) = VC x RL
(VRL x RS) + (VRL x RL) = VC x RL
(VRL x RS) = (VC x RL) - (VRL x RL)
RS = [(VC x RL) - (VRL x RL)] / VRL
RS = [(VC x RL) / VRL] - RL
*/
2019-05-29 21:31:45 -05:00
_sensor_volt; //Define variable for sensor voltage
2019-05-29 20:58:22 -05:00
float RS_air; //Define variable for sensor resistance
float R0; //Define variable for R0
float sensorValue; //Define variable for analog readings
2019-05-29 21:31:45 -05:00
_sensor_volt = this->getVoltage(); //Convert average to voltage
2019-05-29 22:13:13 -05:00
RS_air = ((_VOLT_RESOLUTION*_RLValue)/_sensor_volt)-_RLValue; //Calculate RS in fresh air
2019-05-29 20:58:22 -05:00
R0 = RS_air/_ratioInCleanAir; //Calculate R0
2019-05-29 22:17:25 -05:00
if(print)
{
2019-05-29 22:19:43 -05:00
Serial.println("*******Calibrating*********");
2019-05-29 22:17:25 -05:00
Serial.println("_sensor_volt: " + String(_sensor_volt));
Serial.println("_VOLT_RESOLUTION: " + String(_VOLT_RESOLUTION));
Serial.println("_ratioInCleanAir: " + String(_ratioInCleanAir));
Serial.println("R0: " + String(R0));
2019-05-29 22:19:43 -05:00
Serial.println("*******Calibrating*********");
2019-05-29 22:17:25 -05:00
}
2019-05-29 20:58:22 -05:00
return R0;
2019-04-30 08:52:47 -05:00
}
double MQUnifiedsensor::getVoltage() {
double avg = 0.0;
for (int i = 0; i < retries; i ++) {
avg += analogRead(this->_pin) / retries;
delay(retry_interval);
}
2019-05-29 22:13:13 -05:00
double voltage = avg * _VOLT_RESOLUTION / (pow(2, ADC_RESOLUTION) - 1);
2019-04-30 08:52:47 -05:00
return voltage;
}
2019-05-24 17:13:54 -05:00
void MQUnifiedsensor::setR0(double R0) {
this->_R0 = R0;
2019-05-24 22:09:11 -05:00
}
void MQUnifiedsensor::setDefaultGas()
{
if(_type == 2)
{
2019-05-24 22:14:34 -05:00
_nameLectureRequeired = defaultMQ2;
2019-05-24 22:09:11 -05:00
}
else if(_type == 3)
{
2019-05-24 22:14:34 -05:00
_nameLectureRequeired = defaultMQ3;
2019-05-24 22:09:11 -05:00
}
else if(_type == 4)
{
2019-05-24 22:14:34 -05:00
_nameLectureRequeired = defaultMQ4;
2019-05-24 22:09:11 -05:00
}
else if(_type == 5)
{
2019-05-24 22:14:34 -05:00
_nameLectureRequeired = defaultMQ5;
2019-05-24 22:09:11 -05:00
}
else if(_type == 6)
{
2019-05-24 22:14:34 -05:00
_nameLectureRequeired = defaultMQ6;
2019-05-24 22:09:11 -05:00
}
else if(_type == 7)
{
2019-05-24 22:14:34 -05:00
_nameLectureRequeired = defaultMQ7;
2019-05-24 22:09:11 -05:00
}
else if(_type == 8)
{
2019-05-24 22:14:34 -05:00
_nameLectureRequeired = defaultMQ8;
2019-05-24 22:09:11 -05:00
}
else if(_type == 9)
{
2019-05-24 22:14:34 -05:00
_nameLectureRequeired = defaultMQ9;
2019-05-24 22:09:11 -05:00
}
else if(_type == 131)
{
2019-05-24 22:14:34 -05:00
_nameLectureRequeired = defaultMQ131;
2019-05-24 22:09:11 -05:00
}
else if(_type == 135)
{
2019-05-24 22:14:34 -05:00
_nameLectureRequeired = defaultMQ135;
2019-05-24 22:09:11 -05:00
}
else if(_type == 303)
{
2019-05-24 22:14:34 -05:00
_nameLectureRequeired = defaultMQ303;
2019-05-24 22:09:11 -05:00
}
else if(_type == 309)
{
2019-05-24 22:14:34 -05:00
_nameLectureRequeired = defaultMQ309;
2019-05-24 22:09:11 -05:00
}
}
2019-05-24 22:14:34 -05:00
double MQUnifiedsensor::stringToDouble(String & str)
2019-05-24 22:09:11 -05:00
{
return atof( str.c_str() );
2019-05-24 17:13:54 -05:00
}