mirror of
https://github.com/miguel5612/MQSensorsLib.git
synced 2025-03-15 05:17:30 +03:00
Fixed string
This commit is contained in:
parent
c3068ecca2
commit
9cc3635e75
@ -25,17 +25,14 @@
|
||||
MQUnifiedsensor MQ2(pin, type);
|
||||
|
||||
void setup() {
|
||||
//Init serial port
|
||||
Serial.begin(115200);
|
||||
//init the sensor
|
||||
MQ2.inicializar();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
//Read the sensor
|
||||
int read = MQ2.readSensor();
|
||||
//Print measurements
|
||||
Serial.print("MQ2: ");
|
||||
Serial.print(read);
|
||||
Serial.println(" PPM");
|
||||
//delay 1s to next measure
|
||||
delay(1000);
|
||||
//Read the sensor and print in serial port
|
||||
int lecture = MQ2.readSensor("", true);
|
||||
delay(400);
|
||||
}
|
||||
|
@ -81,14 +81,12 @@ int MQUnifiedsensor::readSensor(String nameLectureRequeired, bool print)
|
||||
if(print)
|
||||
{
|
||||
String nameLecture = getnameLecture();
|
||||
|
||||
Serial.print("Medicion");
|
||||
Serial.print("(" + nameLecture + "): ");
|
||||
Serial.println(_PPM);
|
||||
Serial.print("Slope: ");
|
||||
Serial.print(String(_m));
|
||||
Serial.print(", B point: ");
|
||||
Serial.println(String(_b));
|
||||
Serial.println("**********************");
|
||||
Serial.println("* Sensor: MQ-" + String(_type));
|
||||
Serial.println("* m =" + String(_m) + " ,b =" + String(_b));
|
||||
Serial.println("* RS/R0 = " + String(_ratio) + " ,Voltaje leido(ADC): " + String(this->getVoltage()));
|
||||
Serial.println("* Lectura(" + nameLecture + ")=" + String(_PPM) + " PPM");
|
||||
Serial.println("**********************");
|
||||
}
|
||||
return _PPM;
|
||||
}
|
||||
@ -103,34 +101,20 @@ void MQUnifiedsensor::setSensorCharacteristics(String nameLectureRequeired, bool
|
||||
{
|
||||
//Set default
|
||||
setDefaultGas();
|
||||
//Set ratio in clean air to calc R0
|
||||
setRatioInCleanAir();
|
||||
//Put the default into variable internally used
|
||||
nameLectureRequeired = _nameLectureRequeired;
|
||||
}
|
||||
|
||||
//Dinamic index search
|
||||
if(print)
|
||||
{
|
||||
Serial.println("Busqueda dinamica de los indices");
|
||||
}
|
||||
for (int i=0; i<sizeof(_MQ); i++) {
|
||||
if (nameLectureRequeired == _MQ[i]) { //modified here
|
||||
_lecturePosInArray = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//Serial debugging
|
||||
if(print)
|
||||
{
|
||||
String nameLecture = getnameLecture();
|
||||
|
||||
Serial.print("index in nameLectures: ");
|
||||
Serial.println(_lecturePosInArray);
|
||||
Serial.print("Slope index: ");
|
||||
Serial.println(_lecturePosInArray+1);
|
||||
Serial.print("B point index: ");
|
||||
Serial.println(_lecturePosInArray+2);
|
||||
}
|
||||
_m = stringToDouble(_MQ[_lecturePosInArray+1]);
|
||||
_b = stringToDouble(_MQ[_lecturePosInArray+2]);
|
||||
//Save the name for future calls
|
||||
@ -142,13 +126,12 @@ int MQUnifiedsensor::readPPM(int m, int b) {
|
||||
*/
|
||||
double sensor_volt = this->getVoltage();
|
||||
double RS_gas; //Define variable for sensor resistance
|
||||
double ratio; //Define variable for ratio
|
||||
|
||||
RS_gas = ((5.0 * 10.0) / sensor_volt) - 10.0; //Get value of RS in a gas
|
||||
|
||||
ratio = RS_gas / this->_R0; // Get ratio RS_gas/RS_air
|
||||
_ratio = RS_gas / this->_R0; // Get ratio RS_gas/RS_air
|
||||
|
||||
double ppm_log = (log10(ratio) - b) / m; //Get ppm value in linear scale according to the the ratio value
|
||||
double ppm_log = (log10(_ratio) - b) / m; //Get ppm value in linear scale according to the the ratio value
|
||||
double ppm = pow(10, ppm_log); //Convert ppm value to log scale
|
||||
return floor(ppm);
|
||||
}
|
||||
@ -158,8 +141,8 @@ double MQUnifiedsensor::calibrate() {
|
||||
float R0; //Define variable for R0
|
||||
float sensorValue; //Define variable for analog readings
|
||||
sensor_volt = this->getVoltage(); //Convert average to voltage
|
||||
RS_air = ((5.0 * 10.0) / sensor_volt) - 10.0; //Calculate RS in fresh air
|
||||
R0 = RS_air / 4.4; //Calculate R0
|
||||
RS_air = (5.0-sensor_volt)/sensor_volt; // omit *RL
|
||||
R0 = RS_air / _ratioInCleanAir; //Calculate R0
|
||||
return R0;
|
||||
}
|
||||
double MQUnifiedsensor::getVoltage() {
|
||||
@ -178,7 +161,6 @@ void MQUnifiedsensor::setR0(double R0) {
|
||||
}
|
||||
void MQUnifiedsensor::setDefaultGas()
|
||||
{
|
||||
Serial.println("Carga de los gases por defecto");
|
||||
if(_type == 2)
|
||||
{
|
||||
_nameLectureRequeired = defaultMQ2;
|
||||
@ -228,6 +210,57 @@ void MQUnifiedsensor::setDefaultGas()
|
||||
_nameLectureRequeired = defaultMQ309;
|
||||
}
|
||||
}
|
||||
void MQUnifiedsensor::setRatioInCleanAir()
|
||||
{
|
||||
if(_type == 2)
|
||||
{
|
||||
_ratioInCleanAir = RatioMQ2CleanAir;
|
||||
}
|
||||
else if(_type == 3)
|
||||
{
|
||||
_ratioInCleanAir = RatioMQ3CleanAir;
|
||||
}
|
||||
else if(_type == 4)
|
||||
{
|
||||
_ratioInCleanAir = RatioMQ4CleanAir;
|
||||
}
|
||||
else if(_type == 5)
|
||||
{
|
||||
_ratioInCleanAir = RatioMQ5CleanAir;
|
||||
}
|
||||
else if(_type == 6)
|
||||
{
|
||||
_ratioInCleanAir = RatioMQ6CleanAir;
|
||||
}
|
||||
else if(_type == 7)
|
||||
{
|
||||
_ratioInCleanAir = RatioMQ7CleanAir;
|
||||
}
|
||||
else if(_type == 8)
|
||||
{
|
||||
_ratioInCleanAir = RatioMQ8CleanAir;
|
||||
}
|
||||
else if(_type == 9)
|
||||
{
|
||||
_ratioInCleanAir = RatioMQ9CleanAir;
|
||||
}
|
||||
else if(_type == 131)
|
||||
{
|
||||
_ratioInCleanAir = RatioMQ131CleanAir;
|
||||
}
|
||||
else if(_type == 135)
|
||||
{
|
||||
_ratioInCleanAir = RatioMQ135CleanAir;
|
||||
}
|
||||
else if(_type == 303)
|
||||
{
|
||||
_ratioInCleanAir = RatioMQ303CleanAir;
|
||||
}
|
||||
else if(_type == 309)
|
||||
{
|
||||
_ratioInCleanAir = RatioMQ309CleanAir;
|
||||
}
|
||||
}
|
||||
double MQUnifiedsensor::stringToDouble(String & str)
|
||||
{
|
||||
return atof( str.c_str() );
|
||||
|
@ -21,24 +21,37 @@
|
||||
#define defaultMQ303 "Isobutano" //Isobutano
|
||||
#define defaultMQ309 "CO" //CO
|
||||
|
||||
#define RatioMQ2CleanAir 1
|
||||
#define RatioMQ3CleanAir 2
|
||||
#define RatioMQ4CleanAir 3
|
||||
#define RatioMQ5CleanAir 4
|
||||
#define RatioMQ6CleanAir 5
|
||||
#define RatioMQ7CleanAir 6
|
||||
#define RatioMQ8CleanAir 7
|
||||
#define RatioMQ9CleanAir 8
|
||||
#define RatioMQ131CleanAir 9
|
||||
#define RatioMQ135CleanAir 131
|
||||
#define RatioMQ303CleanAir 135
|
||||
#define RatioMQ309CleanAir 303
|
||||
|
||||
#define ADC_RESOLUTION 10 // for 10bit analog to digital converter.
|
||||
#define retries 50
|
||||
#define retry_interval 20
|
||||
|
||||
//Values consolidated
|
||||
/* Gas, Value of m (Slope) and b (Cut on x axis) points */
|
||||
const char* const _MQ2[18] = {"H2","-2.2459","2.9845","LPG","-2.2879","2.7901","CO","-2.6208","3.6075","Alcohol","-3.1157","4.5134","Propane","-2.7028","3.5595","Benzene","-2.2879","2.7901"};
|
||||
const char* const _MQ3[18] = {"LPG","-3.1851","4.7048","CH4","-17.531","28.785","CO","-4.339","6.4432","Alcohol","-1.435","0.4103","Benzene","-2.7009","0.632","Hexane","-2.7268","3.6299"};
|
||||
const char* const _MQ4[15] = {"LPG","-2.5818","3.6303","CH4","0.9873","2.6386","CO","-5.5945","5.6693","Alcohol","-11.89","9.0375","smoke","-11.189","9.0375"};
|
||||
const char* const _MQ5[15] = {"H2","-4.368","2.9667","LPG","-2.5723","1.8943","CH4","-2.4438","2.3044","CO","-4.8188","5.2023","Alcohol","-4.419","4.8044"};
|
||||
const char* const _MQ6[15] = {"H2","-3.6775","5.0286","LPG","-1.6567","2.8775","CH4","-1","3.301","CO","-12.791","14.523","Alcohol","-5.8057","7.5292"};
|
||||
const char* const _MQ7[15] = {"H2","-1.329","1.8864","LPG","-7.8626","9.1056","CH4","-5.4878","8.8387","CO","-1.4065","2.0162","Alcohol","-6.3219","9.924"};
|
||||
const char* const _MQ8[15] = {"H2","-0.7152","2.9891","LPG","-3.419","7.3513","CH4","-7.5609","15.243","CO","-7.0753","15.396","Alcohol","-1.7459","4.7575"};
|
||||
const char* const _MQ9[9] = {"LPG","-2.2535","2.9855","CH4","-1.6012","3.1476","CO","-1.749","2.827"};
|
||||
const char* const _MQ131[9] = {"Nox","-2.7245","3.3004","CL2","-1.0333","1.7117","O3","-1.2037","1.6455"};
|
||||
const char* const _MQ135[18] = {"CO","-2.7268","2.301","Alcohol","-2.8608","1.8627","CO2","-3.2819","1.9903","Tolueno","-5.7015","1.1612","NH4","-2.2119","2.0473","Acetona","-5.9682","1.0175"};
|
||||
const char* const _MQ303A[9] = {"Iso-butano","-2.3543","1.144","Hydrogeno","-2.4338","0.7558","Ethanol","-2.5597","0.4436"};
|
||||
const char* const _MQ309A[12] = {"H2","-2.1311","3.0886","CH4","-1.6554","2.985","CO","-4.7623","6.7413","Alcohol","-3.7686","5.6744"};
|
||||
const String _MQ2[18] = {"H2","-2.2459","2.9845","LPG","-2.2879","2.7901","CO","-2.6208","3.6075","Alcohol","-3.1157","4.5134","Propane","-2.7028","3.5595","Benzene","-2.2879","2.7901"};
|
||||
const String _MQ3[18] = {"LPG","-3.1851","4.7048","CH4","-17.531","28.785","CO","-4.339","6.4432","Alcohol","-1.435","0.4103","Benzene","-2.7009","0.632","Hexane","-2.7268","3.6299"};
|
||||
const String _MQ4[15] = {"LPG","-2.5818","3.6303","CH4","0.9873","2.6386","CO","-5.5945","5.6693","Alcohol","-11.89","9.0375","smoke","-11.189","9.0375"};
|
||||
const String _MQ5[15] = {"H2","-4.368","2.9667","LPG","-2.5723","1.8943","CH4","-2.4438","2.3044","CO","-4.8188","5.2023","Alcohol","-4.419","4.8044"};
|
||||
const String _MQ6[15] = {"H2","-3.6775","5.0286","LPG","-1.6567","2.8775","CH4","-1","3.301","CO","-12.791","14.523","Alcohol","-5.8057","7.5292"};
|
||||
const String _MQ7[15] = {"H2","-1.329","1.8864","LPG","-7.8626","9.1056","CH4","-5.4878","8.8387","CO","-1.4065","2.0162","Alcohol","-6.3219","9.924"};
|
||||
const String _MQ8[15] = {"H2","-0.7152","2.9891","LPG","-3.419","7.3513","CH4","-7.5609","15.243","CO","-7.0753","15.396","Alcohol","-1.7459","4.7575"};
|
||||
const String _MQ9[9] = {"LPG","-2.2535","2.9855","CH4","-1.6012","3.1476","CO","-1.749","2.827"};
|
||||
const String _MQ131[9] = {"Nox","-2.7245","3.3004","CL2","-1.0333","1.7117","O3","-1.2037","1.6455"};
|
||||
const String _MQ135[18] = {"CO","-2.7268","2.301","Alcohol","-2.8608","1.8627","CO2","-3.2819","1.9903","Tolueno","-5.7015","1.1612","NH4","-2.2119","2.0473","Acetona","-5.9682","1.0175"};
|
||||
const String _MQ303A[9] = {"Iso-butano","-2.3543","1.144","Hydrogeno","-2.4338","0.7558","Ethanol","-2.5597","0.4436"};
|
||||
const String _MQ309A[12] = {"H2","-2.1311","3.0886","CH4","-1.6554","2.985","CO","-4.7623","6.7413","Alcohol","-3.7686","5.6744"};
|
||||
|
||||
class MQUnifiedsensor
|
||||
{
|
||||
@ -49,6 +62,7 @@ class MQUnifiedsensor
|
||||
void setVoltResolution(float voltaje);
|
||||
void setSensorCharacteristics(String nameLectureReqeuired, bool print);
|
||||
void setDefaultGas();
|
||||
void setRatioInCleanAir();
|
||||
|
||||
int readSensor(String nameLectureRequeired = "", bool print = false);
|
||||
int readPPM(int m, int b);
|
||||
@ -62,8 +76,8 @@ class MQUnifiedsensor
|
||||
|
||||
|
||||
private:
|
||||
int _pin, _type, _PPM, _lecturePosInArray;
|
||||
double _R0, _m, _b;
|
||||
int _pin, _type, _PPM, _lecturePosInArray, _ratioInCleanAir;
|
||||
double RS_air,_R0 = 11.820, _m, _b, _ratio;
|
||||
String _MQ[19], _nameLectureRequeired;
|
||||
int VOLT_RESOLUTION = 5.0; // if 3.3v use 3.3
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user