From b0ac32d42343f38ecd9ccaa359db6697d5d95801 Mon Sep 17 00:00:00 2001 From: GrafZeppelin Date: Sat, 3 Apr 2021 16:06:39 +0200 Subject: [PATCH] custom mq135 api --- lib/MQ135New/MQ135New.cpp | 59 ++++++++++++++++++++ lib/MQ135New/MQ135New.h | 31 +++++++++++ platformio.ini | 5 +- src/main.cpp | 112 ++++++++++++++------------------------ 4 files changed, 135 insertions(+), 72 deletions(-) create mode 100644 lib/MQ135New/MQ135New.cpp create mode 100644 lib/MQ135New/MQ135New.h diff --git a/lib/MQ135New/MQ135New.cpp b/lib/MQ135New/MQ135New.cpp new file mode 100644 index 0000000..ef91a11 --- /dev/null +++ b/lib/MQ135New/MQ135New.cpp @@ -0,0 +1,59 @@ +#include "MQ135New.h" + +MQ135::MQ135(uint8_t pin) { + _pin = pin; +} + +double MQ135::getRS() { + double voltage = getVoltage(); + double RS = (double)((double)(VIn * RL) / voltage) - RL; + return RS; +} + +double MQ135::getVoltage() { + int value = analogRead(_pin); + double voltage = (double)(value * VIn) / (double)(Resolution); + return voltage; +} + +double MQ135::getPPM(float a, float b) { + double ratio = getRS() / R0; + // double ppm_log = (log10(ratio) - b) / a; + // double ppm = pow(10, ppm_log); + double ppm = a * pow(ratio, b); + return ppm; +} + +double MQ135::getAcetona() { + return getPPM(34.668, -3.369); +} + +double MQ135::getAlcohol() { + return getPPM(77.255, -3.18); +} + +double MQ135::getCO2() { + // return getPPM(-0.3525, 0.7142) + ATMOCO2; + return getPPM(110.47, -2.862) + ATMOCO2; +} + +double MQ135::getCO() { + return getPPM(605.18, -3.937); +} + +double MQ135::getNH4() { + return getPPM(102.2, -2.473); +} + +double MQ135::getTolueno() { + return getPPM(44.947, -3.445); +} + +float MQ135::getR0() { + double r0 = getRS() / 3.6; + return r0; +} + +void MQ135::setR0(float r0) { + R0 = r0; +} diff --git a/lib/MQ135New/MQ135New.h b/lib/MQ135New/MQ135New.h new file mode 100644 index 0000000..4e1f247 --- /dev/null +++ b/lib/MQ135New/MQ135New.h @@ -0,0 +1,31 @@ +#ifndef MQ135New_H +#define MQ135New_H + +#include "Arduino.h" + +#define RL 10 +#define VIn 5 +#define Resolution 1024 + +#define ATMOCO2 397.13 + +class MQ135 { + private: + uint8_t _pin; + float R0; + double getPPM(float a, float b); + + public: + MQ135(uint8_t pin); + double getAcetona(); + double getAlcohol(); + double getCO(); + double getCO2(); + double getNH4(); + double getTolueno(); + float getR0(); + void setR0(float r0); + double getRS(); + double getVoltage(); +}; +#endif diff --git a/platformio.ini b/platformio.ini index fa8e979..ae5769a 100644 --- a/platformio.ini +++ b/platformio.ini @@ -12,5 +12,6 @@ platform = atmelavr board = megaatmega2560 framework = arduino -lib_deps = - adafruit/Adafruit BME280 Library @ ^2.1.2 \ No newline at end of file +; lib_deps = +; adafruit/Adafruit BME280 Library @ ^2.1.2 +; miguel5612/MQUnifiedsensor @ ^2.0.1 diff --git a/src/main.cpp b/src/main.cpp index 8564855..1b622b4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,7 +1,5 @@ #include -#include -#include -#include +#include #define PIN_MQ135 A0 #define PIN_LED_GREEN DD2 @@ -12,83 +10,57 @@ #define MEASURE_DELAY 1000 #define NOISE_DELAY 100 -#define SEALEVELPRESSURE_HPA (1013.25) - -MQ135 co2_sensor = MQ135(PIN_MQ135); -Adafruit_BME280 bme(10, 11, 12, 13); -const int maxCount = MEASURE_DELAY / NOISE_DELAY; -int count = -1; -float ppm = 0; -bool noise = false; +MQ135 co2_sensor(PIN_MQ135); void setup() { - pinMode(PIN_MQ135, INPUT); - pinMode(PIN_LED_GREEN, OUTPUT); - pinMode(PIN_LED_YELLOW, OUTPUT); - pinMode(PIN_LED_RED, OUTPUT); - pinMode(PIN_NOISE, OUTPUT); - Serial.begin(9600); - - if (!bme.begin()) { - Serial.println(F("Could not find a valid BME280 sensor, check wiring!")); - } + pinMode(PIN_MQ135, INPUT); + co2_sensor.setR0(66.0); } void printValues(float ppm, float temp, float humidity); void loop() { - if (count >= maxCount || count < 0) { - float temp = bme.readTemperature(); - float humidity = bme.readHumidity(); - ppm = co2_sensor.getCorrectedPPM(temp, humidity); + float value = co2_sensor.getCO2(); + Serial.println(value); + Serial.print("rs: "); + Serial.println(co2_sensor.getRS()); + Serial.print("voltage: "); + Serial.println(co2_sensor.getVoltage()); + delay(500); + // if (count >= maxCount || count < 0) { + // float temp = bme.readTemperature(); + // float humidity = bme.readHumidity(); + // ppm = co2_sensor.getCorrectedPPM(temp, humidity); - count = 0; - if (ppm < 1000) { - digitalWrite(PIN_LED_GREEN, 1); - digitalWrite(PIN_LED_YELLOW, 0); - digitalWrite(PIN_LED_RED, 0); - } - else if (ppm <= 2000) { - digitalWrite(PIN_LED_GREEN, 0); - digitalWrite(PIN_LED_YELLOW, 1); - digitalWrite(PIN_LED_RED, 0); - } - else { - digitalWrite(PIN_LED_GREEN, 0); - digitalWrite(PIN_LED_YELLOW, 0); - digitalWrite(PIN_LED_RED, 1); - } + // count = 0; + // if (ppm < 1000) { + // digitalWrite(PIN_LED_GREEN, 1); + // digitalWrite(PIN_LED_YELLOW, 0); + // digitalWrite(PIN_LED_RED, 0); + // } + // else if (ppm <= 2000) { + // digitalWrite(PIN_LED_GREEN, 0); + // digitalWrite(PIN_LED_YELLOW, 1); + // digitalWrite(PIN_LED_RED, 0); + // } + // else { + // digitalWrite(PIN_LED_GREEN, 0); + // digitalWrite(PIN_LED_YELLOW, 0); + // digitalWrite(PIN_LED_RED, 1); + // } - printValues(ppm, temp, humidity); - } + // printValues(ppm, temp, humidity); + // } - if (ppm > 2000) { - noise = !noise; - digitalWrite(PIN_NOISE, noise); - } - else { - digitalWrite(PIN_NOISE, 0); - } + // if (ppm > 2000) { + // noise = !noise; + // digitalWrite(PIN_NOISE, noise); + // } + // else { + // digitalWrite(PIN_NOISE, 0); + // } - delay(NOISE_DELAY); - count++; -} - -void printValues(float ppm, float temp, float humidity) { - Serial.print("Temperature = "); - Serial.print(temp); - Serial.println(" *C"); - - Serial.print("Humidity = "); - Serial.print(humidity); - Serial.println(" %"); - - Serial.print ("ppm = "); - Serial.println (ppm); - - Serial.print ("rzero = "); - Serial.println (co2_sensor.getCorrectedRZero(temp, humidity)); - - Serial.println(); + // delay(NOISE_DELAY); + // count++; } \ No newline at end of file