#include <doxygen.h>
#include <NexButton.h>
#include <NexCheckbox.h>
#include <NexConfig.h>
#include <NexCrop.h>
#include <NexDualStateButton.h>
#include <NexGauge.h>
#include <NexGpio.h>
#include <NexHardware.h>
#include <NexHotspot.h>
#include <NexNumber.h>
#include <NexObject.h>
#include <NexPage.h>
#include <NexPicture.h>
#include <NexProgressBar.h>
#include <NexRadio.h>
#include <NexRtc.h>
#include <NexScrolltext.h>
#include <NexSlider.h>
#include <NexText.h>
#include <NexTimer.h>
#include <Nextion.h>
#include <NexTouch.h>
#include <NexUpload.h>
#include <NexVariable.h>
#include <NexWaveform.h>
#include "RTClib.h"
#include <DHT.h>
#include <DHT_U.h>
#include <SPI.h>
#include <avr/wdt.h>
#include <SD.h>
#include <MQUnifiedsensor.h>
#include <Adafruit_Sensor.h>
#include <EEPROM.h>
#include <Adafruit_ADS1X15.h>
#include "Nextion.h"
#include <Wire.h>
#define flashp A15
#define mq2pin A0
#define mq3pin A1
#define mq4pin A2
#define mq5pin A3
#define mq6pin A6
#define mq7pin A7
#define mq8pin A8
#define mq9pin A9
#define mq135pin A10
#define Board ("Arduino MEGA")
#define Type2 ("MQ-2") //MQ2
#define Type3 ("MQ-3") //MQ3
#define Type4 ("MQ-4") //MQ4
#define Type5 ("MQ-5") //MQ5
#define Type6 ("MQ-6") //MQ6
#define Type7 ("MQ-7") //MQ7
#define Type8 ("MQ-8") //MQ8
#define Type9 ("MQ-9") //MQ9
#define Type135 ("MQ-135") //MQ135
#define Voltage_Resolution5 (5)
#define ADC_Bit_Resolution (10) // For arduino UNO/MEGA/NANO
#define RatioMQ2CleanAir (9.83) //RS / R0 = 9.83 ppm
#define RatioMQ3CleanAir (60) //RS / R0 = 9.83 ppm
#define RatioMQ4CleanAir (4.4) //RS / R0 = 9.83 ppm
#define RatioMQ5CleanAir (6.5) //RS / R0 = 9.83 ppm
#define RatioMQ6CleanAir (10) //RS / R0 = 9.83 ppm
#define RatioMQ7CleanAir (27.5) //RS / R0 = 9.83 ppm
#define RatioMQ8CleanAir (70) //RS / R0 = 9.83 ppm
#define RatioMQ9CleanAir (9.6) //RS / R0 = 9.83 ppm
#define RatioMQ135CleanAir (3.6) //RS / R0 = 9.83 ppm
MQUnifiedsensor MQ2(Board, Voltage_Resolution5, ADC_Bit_Resolution, A0, Type2);
MQUnifiedsensor MQ3(Board, Voltage_Resolution5, ADC_Bit_Resolution, A1, Type3);
MQUnifiedsensor MQ4(Board, Voltage_Resolution5, ADC_Bit_Resolution, A2, Type4);
MQUnifiedsensor MQ5(Board, Voltage_Resolution5, ADC_Bit_Resolution, A3, Type5);
MQUnifiedsensor MQ6(Board, Voltage_Resolution5, ADC_Bit_Resolution, A6, Type6);
MQUnifiedsensor MQ7(Board, Voltage_Resolution5, ADC_Bit_Resolution, A7, Type7);
MQUnifiedsensor MQ8(Board, Voltage_Resolution5, ADC_Bit_Resolution, A8, Type8);
MQUnifiedsensor MQ9(Board, Voltage_Resolution5, ADC_Bit_Resolution, A9, Type9);
MQUnifiedsensor MQ135(Board, Voltage_Resolution5, ADC_Bit_Resolution, A10, Type135);
unsigned long timenow = millis();
int dread = 500;
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
const int chipSelect = 10;
DHT dht(A11,DHT11);
RTC_DS1307 rtc;
int16_t adc0, adc1, adc2, adc3; //ADC Stuff
uint32_t number1 = 0;
uint32_t number2 = 0;
uint32_t number3 = 0;
uint32_t number4 = 0;
uint32_t number5 = 0;
uint32_t number6 = 0;
uint32_t number7 = 0;
uint32_t number8 = 0;
uint32_t number9 = 0;
uint32_t number10 = 0;
uint32_t CurrentPage = 0;
const int numReadings = 10; //How many samples to take before averaging
const int numReadings2= 10; //How many samples to take before averaging
const int numReadings3 = 10; //How many samples to take before averaging
const int numReadings4 = 10; //How many samples to take before averaging
int readings[numReadings]; // the readings from the Temp Sensor
int readIndex = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
int readings2[numReadings2]; // the readings from the Temp Sensor
int readIndex2 = 0; // the index of the current reading
int total2 = 0; // the running total
int average2 = 0; // the average
int readings3[numReadings3]; // the readings from the Temp Sensor
int readIndex3 = 0; // the index of the current reading
int total3 = 0; // the running total
int average3 = 0; // the average
int readings4[numReadings4]; // the readings from the Temp Sensor
int readIndex4 = 0; // the index of the current reading
int total4 = 0; // the running total
int average4 = 0; // the average
int flash = 0;
//If the RL value is different from 10K please assign your RL value with the following method:
// MQ2.setRL(10);
/*
* Nextion Screen Setup and Startup
*/
NexDSButton sdr = NexDSButton(3, 5, "sdr");
NexButton sde = NexButton(3, 2, "sde");
NexButton set = NexButton(1, 8, "set");
NexButton sm = NexButton(1, 9, "sm");
NexButton bt = NexButton(3, 6, "bt");
NexButton clr = NexButton(4, 2, "clr");
NexButton cal = NexButton(1, 17, "cal");
NexDSButton p1 = NexDSButton(1, 11, "p1");
NexDSButton p2 = NexDSButton(1, 12, "p2");
NexDSButton p3 = NexDSButton(1, 13, "p3");
NexDSButton p4 = NexDSButton(1, 14, "p4");
NexDSButton p5 = NexDSButton(1, 15, "p5");
NexDSButton p6 = NexDSButton(1, 16, "p6");
NexCheckbox c2 = NexCheckbox(5, 6, "c2"); // Checkbox added
NexCheckbox c3 = NexCheckbox(5, 7, "c3"); // Checkbox added
NexCheckbox c4 = NexCheckbox(5, 8, "c4"); // Checkbox added
NexCheckbox c5 = NexCheckbox(5, 9, "c5"); // Checkbox added
NexCheckbox c6 = NexCheckbox(5, 10, "c6"); // Checkbox added
NexCheckbox c7 = NexCheckbox(5, 11, "c7"); // Checkbox added
NexCheckbox c8 = NexCheckbox(5, 3, "c8"); // Checkbox added
NexCheckbox c9 = NexCheckbox(5, 5, "c9"); // Checkbox added
NexCheckbox c13 = NexCheckbox(2, 4, "c13"); // Checkbox added
NexPage page0 = NexPage(0, 0, "page0"); // Page added as a touch event
NexPage page1 = NexPage(1, 0, "page1"); // Page1 added as a touch event
NexPage page2 = NexPage(2, 0, "page2"); // Page2 added as a touch event
NexPage page3 = NexPage(3, 0, "page3"); // Page3 added as a touch event
NexPage page4 = NexPage(4, 0, "page4"); // Page4 added as a touch event
NexPage page5 = NexPage(5, 0, "page5"); // Page5 added as a touch event
NexTouch *nex_listen_list[] =
{
&sde,
&sm,
&sdr,
&clr,
&cal,
&bt,
&p1,
&p2,
&p3,
&p4,
&p5,
&p6,
&c2,
&c3,
&c4,
&c5,
&c6,
&c7,
&c8,
&c9,
&c13,
&set,
&page0,
&page1,
&page2,
&page3,
&page4,
&page5,
NULL
};
//void setGain(adsGain_t gain)
//{ads.setGain(GAIN_SIXTEEN);} // 2/3x gain +/- 6.144V 1 bit = 3mV (default)
void calPushCallback(void *ptr) // Press event for button st
{
/*
* Calibrate Sensors
*/
calibratesensors();
}
void clrPushCallback(void *ptr) // Press event for button st
{
//clear the nextion screen monitor here
}
void btPushCallback(void *ptr) // Press event for button st
{
}
void sdPushCallback(void *ptr) // Press event for button st
{
Serial.print("Initializing SD card...");
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
Serial2.print("sds.val=");
Serial2.print(0);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
// don't do anything more:
while (1);
}
Serial.println("card initialized.");
Serial2.print("sds.val=");
Serial2.print(1);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
}
void sdePushCallback(void *ptr) // Press event for button st
{
String dataString = "";
File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile)
{dataFile.println(dataString);
Serial.println("Datalog.txt erased!");}
else
{Serial.println("error opening datalog.txt");}
}
void sdrPushCallback(void *ptr) // Press event for button st
{ //record values onto SD card
sdr.getValue(&number6);
if (number6=1)
{String dataString2 = "";
int sensor2 = analogRead(0);
dataString2 += String(sensor2);
dataString2 += ",";
String dataString3 = "";
int sensor3 = analogRead(1);
dataString3 += String(sensor3);
dataString3 += ",";
String dataString4 = "";
int sensor4 = analogRead(2);
dataString4 += String(sensor4);
dataString4 += ",";
String dataString5 = "";
int sensor5 = analogRead(3);
dataString5 += String(sensor5);
dataString5 += ",";
String dataString6 = "";
int sensor6 = analogRead(6);
dataString6 += String(sensor6);
dataString6 += ",";
String dataString7 = "";
int sensor7 = analogRead(7);
dataString7 += String(sensor7);
dataString7+= ",";
String dataString8 = "";
int sensor8 = analogRead(8);
dataString8 += String(sensor8);
dataString8 += ",";
String dataString9 = "";
int sensor9 = analogRead(9);
dataString9 += String(sensor9);
dataString9 += ",";
String dataString135 = "";
int sensor135 = analogRead(10);
dataString135 += String(sensor135);
dataString135 += ",";
File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile)
{dataFile.println(dataString2);
Serial.println(dataString2);
dataFile.println(dataString3);
Serial.println(dataString3);
dataFile.println(dataString4);
Serial.println(dataString4);
dataFile.println(dataString5);
Serial.println(dataString5);
dataFile.println(dataString6);
Serial.println(dataString6);
dataFile.println(dataString7);
Serial.println(dataString7);
dataFile.println(dataString8);
Serial.println(dataString8);
dataFile.println(dataString9);
Serial.println(dataString9);
dataFile.println(dataString135);
Serial.println(dataString135);}}
else
{Serial.println("error opening datalog.txt");}
if (number6=0)
{ File dataFile = SD.open("datalog.txt", FILE_WRITE);
dataFile.close();}
}
void smPushCallback(void *ptr) // Press event for button st
{
serialprint();
}
void p1PushCallback(void *ptr) //Program One goes here.
{
p1.getValue(&number1);
if (number1=1)
{
program1();
p1.getValue(&number1);
nexLoop(nex_listen_list);}
if (number1=0)
{//what to do or clean up when program done
}
}
void p2PushCallback(void *ptr) //Program Two goes here.
{
p2.getValue(&number1);
if (number2=1)
{
program2();
p2.getValue(&number1);
nexLoop(nex_listen_list);}
if (number2=0)
{//what to do or clean up when program done
}
}
void p3PushCallback(void *ptr) //Program Three goes here.
{
p3.getValue(&number3);
if (number3=1)
{
program3();
p3.getValue(&number3);
nexLoop(nex_listen_list);}
if (number3=0)
{//what to do or clean up when program done
}
}
void p4PushCallback(void *ptr) //Program Four goes here.
{
p4.getValue(&number4);
if (number4=1)
{
program4();
p4.getValue(&number4);
nexLoop(nex_listen_list);}
if (number4=0)
{//what to do or clean up when program done
}
}
void p5PushCallback(void *ptr) //Program Five goes here.
{
p5.getValue(&number5);
if (number5=1)
{
program5();
p5.getValue(&number5);
nexLoop(nex_listen_list);}
if (number5=0)
{//what to do or clean up when program done
}
}
void p6PushCallback(void *ptr) //Program Six goes here.
{
p6.getValue(&number6);
if (number6=1)
{
program6();
nexLoop(nex_listen_list);}
if (number6=0)
{//what to do or clean up when program done
}
}
void page0PushCallback(void *ptr) // If page 0 is loaded on the display, the following is going to execute:
{
CurrentPage = 0; // Set variable as 0 so from now on arduino knows page 0 is loaded on the display
} // End of press event
void page1PushCallback(void *ptr) // If page 1 is loaded on the display, the following is going to execute:
{
CurrentPage = 1; // Set variable as 1 so from now on arduino knows page 0 is loaded on the display
} // End of press event
void page2PushCallback(void *ptr) // If page 2 is loaded on the display, the following is going to execute:
{
CurrentPage = 2; // Set variable as 0 so from now on arduino knows page 0 is loaded on the display
} // End of press event
void page3PushCallback(void *ptr) // If page 3 is loaded on the display, the following is going to execute:
{
CurrentPage = 3; // Set variable as 0 so from now on arduino knows page 0 is loaded on the display
} // End of press event
void page4PushCallback(void *ptr) // If page 4 is loaded on the display, the following is going to execute:
{
CurrentPage = 4; // Set variable as 0 so from now on arduino knows page 0 is loaded on the display
} // End of press event
void page5PushCallback(void *ptr) // If page 5 is loaded on the display, the following is going to execute:
{
CurrentPage = 5; // Set variable as 0 so from now on arduino knows page 0 is loaded on the display
} // End of press event
void setup() {
Serial.begin(115200);
Serial2.begin(115200);
Serial3.begin(115200);
Wire.begin();
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running, let's set the time!");}
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
for (int thisReading = 0; thisReading < numReadings; thisReading++)
{readings[thisReading] = 0;}
for (int thisReading2 = 0; thisReading2 < numReadings2; thisReading2++)
{readings2[thisReading2] = 0;}
for (int thisReading3 = 0; thisReading3 < numReadings3; thisReading3++)
{readings3[thisReading3] = 0;}
for (int thisReading4 = 0; thisReading4 < numReadings4; thisReading4++)
{readings4[thisReading4] = 0;}
cal.attachPush(calPushCallback, &cal);
clr.attachPush(clrPushCallback, &clr);
sde.attachPush(smPushCallback, &sm);
sdr.attachPush(smPushCallback, &sm);
sm.attachPush(smPushCallback, &sm);
set.attachPush(sdPushCallback, &set);
p1.attachPush(p1PushCallback, &p1);
p2.attachPush(p2PushCallback, &p2);
p3.attachPush(p3PushCallback, &p3);
p4.attachPush(p4PushCallback, &p4);
p5.attachPush(p5PushCallback, &p5);
p6.attachPush(p6PushCallback, &p6);
//int16_t adc0, adc1, adc2, adc3;
//adc0 = ads.readADC_SingleEnded(0);
//adc1 = ads.readADC_SingleEnded(1);
//adc2 = ads.readADC_SingleEnded(2);
//adc3 = ads.readADC_SingleEnded(3);
pinMode(flashp, INPUT_PULLUP);
pinMode(53, OUTPUT);
pinMode(16, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
if (analogRead(flashp) == HIGH)
{flash = 1;}
else
{flash = 0;}
Serial2.print("sds.val=");
Serial2.print(0);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
dht.begin();
}
void loop()
{ nexLoop(nex_listen_list);
delay(50);
if (Serial3.available())
{Serial.write(Serial3.read());} // read it and send it out Serial1 (pins 0 & 1)
float temp = dht.readTemperature(true);
float humid = dht.readHumidity();
/*ADC Smoothing*/
total = total - readings[readIndex];
readings[readIndex] = temp;
total = total + readings[readIndex];
readIndex = readIndex + 1;
if (readIndex >= numReadings) {
readIndex = 0;}
average = total / numReadings;
total2 = total2 - readings2[readIndex2];
readings2[readIndex2] = humid;
total2 = total2 + readings2[readIndex2];
readIndex2 = readIndex2 + 1;
if (readIndex2 >= numReadings2) {
readIndex2 = 0;}
average2 = total2 / numReadings2;
total3 = total3 - readings3[readIndex3];
readings3[readIndex3] = adc2;
total3 = total3 + readings3[readIndex3];
readIndex3 = readIndex3 + 1;
if (readIndex3 >= numReadings3) {
readIndex3 = 0;}
average3 = total3 / numReadings3;
total4 = total4 - readings4[readIndex4];
readings4[readIndex4] = adc3;
total4 = total4 + readings4[readIndex4];
readIndex4 = readIndex4 + 1;
if (readIndex4 >= numReadings4) {
readIndex4 = 0;}
average4 = total4 / numReadings4;
MQ2.update(); // Update data, the arduino will be read the voltage on the analog pin
MQ2.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup}
MQ3.update(); // Update data, the arduino will be read the voltage on the analog pin
MQ3.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
MQ4.update(); // Update data, the arduino will be read the voltage on the analog pin
MQ4.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
MQ5.update(); // Update data, the arduino will be read the voltage on the analog pin
MQ5.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
MQ6.update(); // Update data, the arduino will be read the voltage on the analog pin
MQ6.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
MQ7.update(); // Update data, the arduino will be read the voltage on the analog pin
MQ7.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
MQ8.update(); // Update data, the arduino will be read the voltage on the analog pin
MQ8.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
MQ9.update(); // Update data, the arduino will be read the voltage on the analog pin
MQ9.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
MQ135.update(); // Update data, the arduino will be read the voltage on the analog pin
MQ135.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
MQ135.setA(605.18); MQ135.setB(-3.937); // Configurate the ecuation values to get CO concentration
float CO = MQ135.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
MQ135.setA(77.255); MQ135.setB(-3.18); // Configurate the ecuation values to get Alcohol concentration
float Alcohol = MQ135.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
MQ135.setA(110.47); MQ135.setB(-2.862); // Configurate the ecuation values to get CO2 concentration
float CO2 = MQ135.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
MQ135.setA(44.947); MQ135.setB(-3.445); // Configurate the ecuation values to get Tolueno concentration
float Tolueno = MQ135.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
MQ135.setA(102.2 ); MQ135.setB(-2.473); // Configurate the ecuation values to get NH4 concentration
float NH4 = MQ135.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
MQ135.setA(34.668); MQ135.setB(-3.369); // Configurate the ecuation values to get Acetona concentration
float Acetona = MQ135.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
//nexLoop(nex_listen_list);
if(millis()>=timenow+dread)
{timenow+=dread;
Serial.print("Temperature: ");
Serial.println(average);
Serial.print("Humidity: ");
Serial.println(average2);
Serial2.print("tmp.val=");
Serial2.print(average);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
DateTime now = rtc.now();
Serial2.print("yr.val=");
Serial2.print(now.year(), DEC);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.print("mnt.val=");
Serial2.print(now.month(), DEC);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.print("day.val=");
Serial2.print(now.day(), DEC);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.print("hor.val=");
Serial2.print(now.hour(), DEC);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.print("min.val=");
Serial2.print(now.minute(), DEC);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.print("sec.val=");
Serial2.print(now.second(), DEC);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);}
if (digitalRead(13) == HIGH) //If wifi has turned on built in LED to signal connection
{Serial2.print("wif.val=");
Serial2.print(1);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);}
else
{Serial2.print("wif.val=");
Serial2.print(0);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);}
}
void program1()
{ digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
}
void program2()
{
digitalWrite(LED_BUILTIN, brightness);
brightness = brightness + fadeAmount;
if (brightness <= 0 || brightness >= 255)
{fadeAmount = -fadeAmount;}
delay(30);
}
void program3()
{ digitalWrite(LED_BUILTIN, LOW); } // turn the LED on (HIGH is the voltage level)
void program4()
{}
void program5()
{
}
void program6()
{
MQ2.serialDebug(); // Will print the table on the serial port
Serial.println("MQ2 Table:");
MQ3.serialDebug(); // Will print the table on the serial port
Serial.println("MQ3 Table:");
MQ4.serialDebug(); // Will print the table on the serial port
Serial.println("MQ4 Table:");
MQ5.serialDebug(); // Will print the table on the serial port
Serial.println("MQ5 Table:");
MQ6.serialDebug(); // Will print the table on the serial port
Serial.println("MQ6 Table:");
MQ7.serialDebug(); // Will print the table on the serial port
Serial.println("MQ7 Table:");
MQ8.serialDebug(); // Will print the table on the serial port
Serial.println("MQ8 Table:");
MQ9.serialDebug(); // Will print the table on the serial port
Serial.println("MQ9 Table:");
MQ135.serialDebug(); // Will print the table on the serial port
Serial.println("MQ135 Table:");
delay(500); //Sampling frequency
}
void calibratesensors()
{
/*Calibrate MQ2 Sensor*/
MQ2.setRegressionMethod(1); //_PPM = a*ratio^b
MQ2.setA(574.25); MQ2.setB(-2.222); // Configurate the ecuation values to get LPG concentration
MQ2.init();
Serial.print("Calibrating MQ2 please wait.");
Serial2.print("c2.val=");
Serial2.print(0);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
float calcR2 = 0;
for(int i = 1; i<=10; i ++)
{
MQ2.update(); // Update data, the arduino will be read the voltage on the analog pin
calcR2 += MQ2.calibrate(RatioMQ2CleanAir);
Serial.print(".");
}
MQ2.setR0(calcR2/10);
Serial.println(" done!.");
Serial2.print("c2.val=");
Serial2.print(1);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
if(isinf(calcR2)) {Serial.println("Warning: Conection issue founded, R2 is infite (Open circuit detected) please check your wiring and supply"); while(1);}
if(calcR2 == 0){Serial.println("Warning: Conection issue founded, R2 is zero (Analog pin with short circuit to ground) please check your wiring and supply"); while(1);}
MQ2.serialDebug(true);
/*Calibrate MQ3 Sensor*/
MQ3.setRegressionMethod(1); //_PPM = a*ratio^b
MQ3.setA(4.8387); MQ3.setB(-2.68); // Configurate the ecuation values to get LPG concentration
MQ3.init();
Serial.print("Calibrating MQ3 please wait.");
Serial2.print("c3.val=");
Serial2.print(0);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
float calcR3 = 0;
for(int i = 1; i<=10; i ++)
{
MQ3.update(); // Update data, the arduino will be read the voltage on the analog pin
calcR3 += MQ3.calibrate(RatioMQ3CleanAir);
Serial.print(".");
}
MQ3.setR0(calcR3/10);
Serial.println(" done!.");
Serial2.print("c3.val=");
Serial2.print(1);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
if(isinf(calcR3)) {Serial.println("Warning: Conection issue founded, R3 is infite (Open circuit detected) please check your wiring and supply"); while(1);}
if(calcR3 == 0){Serial.println("Warning: Conection issue founded, R3 is zero (Analog pin with short circuit to ground) please check your wiring and supply"); while(1);}
MQ3.serialDebug(true);
/*Calibrate MQ4 Sensor*/
MQ4.setRegressionMethod(1); //_PPM = a*ratio^b
MQ4.setA(1012.7); MQ4.setB(-2.786); // Configurate the ecuation values to get LPG concentration
MQ4.init();
Serial.print("Calibrating MQ4 please wait.");
Serial2.print("c4.val=");
Serial2.print(0);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
float calcR4 = 0;
for(int i = 1; i<=10; i ++)
{
MQ4.update(); // Update data, the arduino will be read the voltage on the analog pin
calcR4 += MQ4.calibrate(RatioMQ4CleanAir);
Serial.print(".");
}
MQ4.setR0(calcR4/10);
Serial.println(" done!.");
Serial2.print("c4.val=");
Serial2.print(1);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
if(isinf(calcR4)) {Serial.println("Warning: Conection issue founded, R4 is infite (Open circuit detected) please check your wiring and supply"); while(1);}
if(calcR4 == 0){Serial.println("Warning: Conection issue founded, R4 is zero (Analog pin with short circuit to ground) please check your wiring and supply"); while(1);}
MQ4.serialDebug(true);
/*Calibrate MQ5 Sensor*/
MQ5.setRegressionMethod(1); //_PPM = a*ratio^b
MQ5.setA(1163.8); MQ5.setB(-3.874); // Configurate the ecuation values to get LPG concentration
MQ5.init();
Serial.print("Calibrating MQ5 please wait.");
Serial2.print("c5.val=");
Serial2.print(0);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
float calcR5 = 0;
for(int i = 1; i<=10; i ++)
{
MQ5.update(); // Update data, the arduino will be read the voltage on the analog pin
calcR5 += MQ5.calibrate(RatioMQ5CleanAir);
Serial.print(".");
}
MQ5.setR0(calcR5/10);
Serial.println(" done!.");
Serial2.print("c5.val=");
Serial2.print(1);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
if(isinf(calcR5)) {Serial.println("Warning: Conection issue founded, R5 is infite (Open circuit detected) please check your wiring and supply"); while(1);}
if(calcR5 == 0){Serial.println("Warning: Conection issue founded, R5 is zero (Analog pin with short circuit to ground) please check your wiring and supply"); while(1);}
MQ5.serialDebug(true);
/*Calibrate MQ6 Sensor*/
MQ6.setRegressionMethod(1); //_PPM = a*ratio^b
MQ6.setA(2127.2); MQ6.setB(-2.526); // Configurate the ecuation values to get LPG concentration
MQ6.init();
Serial.print("Calibrating MQ6 please wait.");
Serial2.print("c6.val=");
Serial2.print(0);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
float calcR6 = 0;
for(int i = 1; i<=10; i ++)
{
MQ6.update(); // Update data, the arduino will be read the voltage on the analog pin
calcR6 += MQ6.calibrate(RatioMQ6CleanAir);
Serial.print(".");
}
MQ6.setR0(calcR6/10);
Serial.println(" done!.");
Serial2.print("c6.val=");
Serial2.print(1);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
if(isinf(calcR6)) {Serial.println("Warning: Conection issue founded, R6 is infite (Open circuit detected) please check your wiring and supply"); while(1);}
if(calcR6 == 0){Serial.println("Warning: Conection issue founded, R6 is zero (Analog pin with short circuit to ground) please check your wiring and supply"); while(1);}
MQ6.serialDebug(true);
/*Calibrate MQ7 Sensor*/
MQ7.setRegressionMethod(1); //_PPM = a*ratio^b
MQ7.setA(99.04); MQ7.setB(-1.518); // Configurate the ecuation values to get LPG concentration
MQ7.init();
Serial.print("Calibrating MQ7 please wait.");
Serial2.print("c7.val=");
Serial2.print(0);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
float calcR7 = 0;
for(int i = 1; i<=10; i ++)
{
MQ7.update(); // Update data, the arduino will be read the voltage on the analog pin
calcR7 += MQ7.calibrate(RatioMQ7CleanAir);
Serial.print(".");
}
MQ7.setR0(calcR7/10);
Serial.println(" done!.");
Serial2.print("c7.val=");
Serial2.print(1);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
if(isinf(calcR7)) {Serial.println("Warning: Conection issue founded, R7 is infite (Open circuit detected) please check your wiring and supply"); while(1);}
if(calcR7 == 0){Serial.println("Warning: Conection issue founded, R7 is zero (Analog pin with short circuit to ground) please check your wiring and supply"); while(1);}
MQ7.serialDebug(true);
/*Calibrate MQ8 Sensor*/
MQ8.setRegressionMethod(1); //_PPM = a*ratio^b
MQ8.setA(976.97); MQ8.setB(-0.688); // Configurate the ecuation values to get LPG concentration
MQ8.init();
Serial.print("Calibrating MQ8 please wait.");
Serial2.print("c8.val=");
Serial2.print(0);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
float calcR8 = 0;
for(int i = 1; i<=10; i ++)
{
MQ8.update(); // Update data, the arduino will be read the voltage on the analog pin
calcR8 += MQ8.calibrate(RatioMQ8CleanAir);
Serial.print(".");
}
MQ8.setR0(calcR8/10);
Serial.println(" done!.");
Serial2.print("c8.val=");
Serial2.print(1);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
if(isinf(calcR8)) {Serial.println("Warning: Conection issue founded, R8 is infite (Open circuit detected) please check your wiring and supply"); while(1);}
if(calcR8 == 0){Serial.println("Warning: Conection issue founded, R8 is zero (Analog pin with short circuit to ground) please check your wiring and supply"); while(1);}
MQ8.serialDebug(true);
/*Calibrate MQ9 Sensor*/
MQ9.setRegressionMethod(1); //_PPM = a*ratio^b
MQ9.setA(1000.5); MQ9.setB(-2.186); // Configurate the ecuation values to get LPG concentration
MQ9.init();
Serial.print("Calibrating MQ8 please wait.");
Serial2.print("c9.val=");
Serial2.print(0);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
float calcR9 = 0;
for(int i = 1; i<=10; i ++)
{
MQ9.update(); // Update data, the arduino will be read the voltage on the analog pin
calcR9 += MQ9.calibrate(RatioMQ9CleanAir);
Serial.print(".");
}
MQ9.setR0(calcR9/10);
Serial.println(" done!.");
Serial2.print("c9.val=");
Serial2.print(1);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
if(isinf(calcR9)) {Serial.println("Warning: Conection issue founded, R9 is infite (Open circuit detected) please check your wiring and supply"); while(1);}
if(calcR9 == 0){Serial.println("Warning: Conection issue founded, R9 is zero (Analog pin with short circuit to ground) please check your wiring and supply"); while(1);}
MQ9.serialDebug(true);
/*Calibrate MQ135 Sensor*/
MQ135.setRegressionMethod(1); //_PPM = a*ratio^b
MQ135.init();
Serial.print("Calibrating MQ135 please wait.");
Serial2.print("c13.val=");
Serial2.print(0);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
float calcR135 = 0;
for(int i = 1; i<=10; i ++)
{
MQ135.update(); // Update data, the arduino will be read the voltage on the analog pin
calcR135 += MQ135.calibrate(RatioMQ135CleanAir);
Serial.print(".");
}
MQ135.setR0(calcR135/10);
Serial.println(" done!.");
Serial2.print("c13.val=");
Serial2.print(1);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
if(isinf(calcR135)) {Serial.println("Warning: Conection issue founded, R135 is infite (Open circuit detected) please check your wiring and supply"); while(1);}
if(calcR135 == 0){Serial.println("Warning: Conection issue founded, R135 is zero (Analog pin with short circuit to ground) please check your wiring and supply"); while(1);}
MQ135.serialDebug(true);
}
void serialprint()
{ Serial2.print("sp1.txt=");
Serial2.print("Temperature:");
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
delay(500);
Serial2.print("sp1.txt+=");
Serial2.print(average);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
delay(500);
Serial2.print("sp1.txt+=");
Serial2.print("Humidity:");
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
delay(500);
Serial2.print("sp1.txt+=");
Serial2.print(average2);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
delay(500);
}