Last active
October 9, 2021 07:38
-
-
Save danielgospodinow/30ac9833e73818a70af65c0c564ed4df to your computer and use it in GitHub Desktop.
Heater Sensor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <TroykaMQ.h> | |
#include "RTClib.h" | |
#include <SPI.h> | |
#include <SD.h> | |
#include <LiquidCrystal_PCF8574.h> | |
#include <Wire.h> | |
#define MAX_ACCEPTABLE_LPG_VALUE 1000 | |
#define MAX_ACCEPTABLE_METHANE_VALUE 1000 | |
#define MAX_ACCEPTABLE_SMOKE_VALUE 1500 | |
#define MAX_ACCEPTABLE_HYDROGEN_VALUE 50 | |
#define PIN_MQ2 A0 | |
#define PIN_MQ21 A1 | |
#define PIN_MQ22 A2 | |
#define PIN_MQ2_HEATER 10 | |
#define PIN_MQ2_HEATER1 9 | |
#define PIN_MQ2_HEATER2 8 | |
#define PIN_BUZZER 2 | |
#define PIN_SD_CARD 4 | |
File sensorDataFile; | |
String fileName = "data.txt"; | |
MQ2 sensorOne(PIN_MQ2, PIN_MQ2_HEATER); | |
MQ2 sensorTwo(PIN_MQ21, PIN_MQ2_HEATER1); | |
MQ2 sensorThree(PIN_MQ22, PIN_MQ2_HEATER2); | |
RTC_DS1307 rtc; | |
LiquidCrystal_PCF8574 lcd(0x27); | |
bool isDisplayEnabled = false; | |
void setup() { | |
Serial.begin(9600); | |
Serial.println("Calibration starting..."); | |
Wire.begin(); | |
Wire.beginTransmission(0x27); | |
int error = Wire.endTransmission(); | |
if (!error) { | |
Serial.println("LCD found."); | |
lcd.begin(16, 2); | |
lcd.setBacklight(255); | |
lcd.home(); | |
isDisplayEnabled = true; | |
Serial.println("LCD configured."); | |
} | |
if (isDisplayEnabled) { | |
lcd.clear(); | |
lcd.print("Calibrating..."); | |
delay(1000); | |
} | |
//Setup MQ sensors | |
sensorOne.heaterPwrHigh(); | |
sensorTwo.heaterPwrHigh(); | |
sensorThree.heaterPwrHigh(); | |
//Setup buzzer | |
pinMode(PIN_BUZZER, OUTPUT); | |
//Caibrate SD Card | |
if (SD.begin(PIN_SD_CARD)) { | |
Serial.println("SD card initialization done."); | |
sensorDataFile = SD.open(fileName, FILE_WRITE); | |
Serial.println("Sensor data output file opened successfully."); | |
} | |
//Calibrate RTC | |
if (!rtc.begin()) { | |
Serial.println("Couldn't find RTC"); | |
} | |
if (!rtc.isrunning()) { | |
Serial.println("RTC is NOT running."); | |
rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); | |
} else { | |
Serial.println("RTC is running."); | |
} | |
Serial.println("Calibration and setup complete."); | |
lcd.clear(); | |
lcd.print("Setup complete!"); | |
delay(1000); | |
lcd.clear(); | |
lcd.print("Analyzing..."); | |
} | |
void loop() { | |
bool isSensorOneAlarming = readFromSensor(sensorOne, "Sensor One"); | |
bool isSensorTwoAlarming = readFromSensor(sensorTwo, "Sensor Two"); | |
bool isSensorThreeAlarming = readFromSensor(sensorThree, "Sensor Three"); | |
if (isSensorOneAlarming || isSensorTwoAlarming || isSensorThreeAlarming) { | |
activateBuzzer(); | |
} else { | |
stopBuzzer(); | |
} | |
sensorDataFile.flush(); | |
delay(1000); | |
} | |
bool readFromSensor(MQ2 &sensor, String sensorName) { //Функция, която чете стойности от сензорите. | |
bool alarming = false; | |
if (!sensor.isCalibrated() && sensor.heatingCompleted()) { //Проверка дали самият сензор е калибриран. | |
sensor.calibrate(); //Ако сензорът не е калибриран, той бива калибриран с извикването на тази функция. | |
writeToConsoleAndSDCard(sensorName + " -> Ro = " + sensor.getRo() + "\n"); | |
} | |
if (sensor.isCalibrated() && sensor.heatingCompleted()) { | |
writeToConsoleAndSDCard(sensorName + " Data:" + "\n"); | |
writeToConsoleAndSDCard((String)"Ratio: " + sensor.readRatio() + "\n"); | |
writeToConsoleAndSDCard((String)"\tLPG: " + sensor.readLPG() + " ppm" + "\n"); | |
writeToConsoleAndSDCard((String)"\tMethane: " + sensor.readMethane() + " ppm" + "\n"); | |
writeToConsoleAndSDCard((String)"\tSmoke: " + sensor.readSmoke() + " ppm" + "\n"); | |
writeToConsoleAndSDCard((String)"\tHydrogen: " + sensor.readHydrogen() + " ppm" + "\n"); | |
writeToConsoleAndSDCard("\n"); | |
if (sensor.readLPG() > MAX_ACCEPTABLE_LPG_VALUE | |
|| sensor.readMethane() > MAX_ACCEPTABLE_METHANE_VALUE | |
|| sensor.readSmoke() > MAX_ACCEPTABLE_SMOKE_VALUE | |
|| sensor.readHydrogen() > MAX_ACCEPTABLE_HYDROGEN_VALUE) { | |
alarming = true; | |
} | |
} | |
return alarming; | |
} | |
void activateBuzzer() { | |
digitalWrite(PIN_BUZZER, HIGH); | |
} | |
void stopBuzzer() { | |
digitalWrite(PIN_BUZZER, LOW); | |
} | |
void writeToConsoleAndSDCard(String message) { | |
if (rtc.isrunning()) { | |
DateTime now = rtc.now(); | |
Serial.print("("); | |
Serial.print(now.year()); | |
Serial.print('/'); | |
Serial.print(now.month()); | |
Serial.print('/'); | |
Serial.print(now.day()); | |
Serial.print(' '); | |
Serial.print(now.hour()); | |
Serial.print(':'); | |
Serial.print(now.minute()); | |
Serial.print(':'); | |
Serial.print(now.second()); | |
Serial.print("): "); | |
} | |
Serial.print(message); | |
if (sensorDataFile) { | |
if (rtc.isrunning()) { | |
DateTime now = rtc.now(); | |
sensorDataFile.print("("); | |
sensorDataFile.print(now.year()); | |
sensorDataFile.print('/'); | |
sensorDataFile.print(now.month()); | |
sensorDataFile.print('/'); | |
sensorDataFile.print(now.day()); | |
sensorDataFile.print(' '); | |
sensorDataFile.print(now.hour()); | |
sensorDataFile.print(':'); | |
sensorDataFile.print(now.minute()); | |
sensorDataFile.print(':'); | |
sensorDataFile.print(now.second()); | |
sensorDataFile.print("): "); | |
} | |
sensorDataFile.print(message); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment