Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: # Gas Monitor
- - Source Code NOT compiled for: Arduino Uno
- - Source Code created on: 2026-01-25 18:31:09
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* when gas sms is sent to sim 900a it should send */
- /* the gas level to phone */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <MQUnifiedsensor.h> //https://github.com/miguel5612/MQSensorsLib
- #include <SoftwareSerial.h> //Software Serial for SIM900A communication
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void initMQ5Sensor(void);
- void initSIM900A(void);
- void readGasLevel(void);
- void sendGasSMS(float gasLevel);
- void sendATCommand(String command, unsigned long timeout);
- void waitForResponse(String expectedResponse, unsigned long timeout);
- void displayDebugInfo(void);
- /***** DEFINITION OF DIGITAL INPUT PINS *****/
- const uint8_t MQ5_MQ5_DOUT_PIN_D2 = 2;
- /***** DEFINITION OF ANALOG INPUT PINS *****/
- const uint8_t MQ5_MQ5_AOUT_PIN_A0 = A0;
- /***** DEFINITION OF SIM900A COMMUNICATION PINS *****/
- // SoftwareSerial(RX, TX)
- const uint8_t SIM900A_RX_PIN = 10; // RX pin for SIM900A
- const uint8_t SIM900A_TX_PIN = 11; // TX pin for SIM900A
- /***** DEFINITION OF SENSOR CALIBRATION PARAMETERS *****/
- // MQ-5 sensor configuration for LPG detection
- const float MQ5_RATIO_CLEAN_AIR = 6.5; // RS/R0 ratio in clean air
- const float MQ5_RL_VALUE = 10.0; // Load resistance in KOhms
- const float MQ5_VOLTAGE_RESOLUTION = 5.0; // Arduino Uno voltage reference
- const int MQ5_ADC_BIT_RESOLUTION = 10; // Arduino Uno ADC resolution
- /***** DEFINITION OF GSM MODULE CONFIGURATION *****/
- const String PHONE_NUMBER = "+1234567890"; // Phone number to send SMS to - CHANGE THIS
- const unsigned long SIM900A_BAUD_RATE = 9600; // SIM900A communication speed
- const unsigned long AT_COMMAND_TIMEOUT = 1000; // Timeout for AT commands (ms)
- const unsigned long SMS_DELAY = 5000; // Delay between SMS messages (ms)
- /***** DEFINITION OF MEASUREMENT PARAMETERS *****/
- const unsigned long GAS_READING_INTERVAL = 10000; // Interval for gas readings (ms)
- const unsigned long SMS_SEND_INTERVAL = 60000; // Interval for SMS sending (60 seconds)
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
- // Create SoftwareSerial instance for SIM900A communication
- SoftwareSerial SIM900A_Serial(SIM900A_RX_PIN, SIM900A_TX_PIN);
- // Create MQ5 sensor instance
- // MQUnifiedsensor(Board, Voltage_Resolution, ADC_Bit_Resolution, Pin, Type)
- MQUnifiedsensor MQ5("Arduino UNO", MQ5_VOLTAGE_RESOLUTION, MQ5_ADC_BIT_RESOLUTION, MQ5_MQ5_AOUT_PIN_A0, "MQ-5");
- /***** GLOBAL VARIABLES *****/
- float currentGasLevel = 0.0; // Current gas level reading in PPM
- unsigned long lastGasReadTime = 0; // Timestamp of last gas reading
- unsigned long lastSMSSendTime = 0; // Timestamp of last SMS sent
- bool SIM900AInitialized = false; // Flag for SIM900A initialization status
- String serialBuffer = ""; // Buffer for serial communication
- /****** SETUP FUNCTION *****/
- void setup(void)
- {
- // Initialize Serial for debugging
- Serial.begin(9600);
- // Wait for Serial to initialize
- delay(100);
- Serial.println(F("\n\n=== Gas SMS Alert System Started ==="));
- Serial.println(F("Initializing MQ5 Sensor..."));
- // Configure GPIO pins
- pinMode(MQ5_MQ5_DOUT_PIN_D2, INPUT_PULLUP);
- pinMode(MQ5_MQ5_AOUT_PIN_A0, INPUT);
- // Initialize MQ5 sensor
- initMQ5Sensor();
- // Initialize SIM900A GSM module
- Serial.println(F("Initializing SIM900A GSM Module..."));
- initSIM900A();
- Serial.println(F("=== System Initialization Complete ===\n"));
- // Initialize timestamps
- lastGasReadTime = millis();
- lastSMSSendTime = millis();
- }
- /****** MAIN LOOP *****/
- void loop(void)
- {
- // Handle incoming serial data from SIM900A for debugging
- while (SIM900A_Serial.available())
- {
- char inChar = (char)SIM900A_Serial.read();
- serialBuffer += inChar;
- // Process complete lines
- if (inChar == '\n')
- {
- Serial.print(F("SIM900A: "));
- Serial.println(serialBuffer);
- serialBuffer = "";
- }
- }
- // Perform gas level reading at specified interval
- if (millis() - lastGasReadTime >= GAS_READING_INTERVAL)
- {
- lastGasReadTime = millis();
- readGasLevel();
- displayDebugInfo();
- }
- // Send SMS with gas level at specified interval
- if (millis() - lastSMSSendTime >= SMS_SEND_INTERVAL && SIM900AInitialized)
- {
- lastSMSSendTime = millis();
- sendGasSMS(currentGasLevel);
- }
- }
- /****** FUNCTION IMPLEMENTATIONS *****/
- // Initialize MQ5 sensor with calibration parameters
- void initMQ5Sensor(void)
- {
- // Set regression method for PPM calculation
- MQ5.setRegressionMethod(1); // 1 = Exponential equation
- // Configure sensor parameters for LPG detection
- // These values are based on MQ-5 datasheet calibration curve
- // For LPG: PPM = a * (RS/R0)^b
- MQ5.setA(1163.8); // Coefficient 'a' for LPG
- MQ5.setB(-3.207); // Coefficient 'b' for LPG
- // Set the RL value (load resistance on the sensor module)
- MQ5.setRL(MQ5_RL_VALUE);
- // Perform calibration - R0 value obtained from clean air calibration
- // For a properly calibrated sensor in clean air
- float R0 = MQ5.calibrate(MQ5_RATIO_CLEAN_AIR);
- Serial.print(F("MQ5 Calibration - R0: "));
- Serial.println(R0);
- // Initialize sensor
- MQ5.init();
- Serial.println(F("MQ5 Sensor initialized successfully"));
- }
- // Initialize SIM900A GSM module
- void initSIM900A(void)
- {
- // Initialize software serial for SIM900A
- SIM900A_Serial.begin(SIM900A_BAUD_RATE);
- // Give SIM900A time to boot
- delay(2000);
- // Test communication with AT command
- Serial.println(F("Testing SIM900A communication..."));
- sendATCommand("AT", AT_COMMAND_TIMEOUT);
- delay(500);
- // Disable echo for cleaner responses
- Serial.println(F("Disabling echo..."));
- sendATCommand("ATE0", AT_COMMAND_TIMEOUT);
- delay(500);
- // Set SMS text mode
- Serial.println(F("Setting SMS text mode..."));
- sendATCommand("AT+CMGF=1", AT_COMMAND_TIMEOUT);
- delay(500);
- // Set SMS character set
- Serial.println(F("Setting SMS character set..."));
- sendATCommand("AT+CSCS=\"GSM\"", AT_COMMAND_TIMEOUT);
- delay(500);
- // Check signal quality
- Serial.println(F("Checking signal quality..."));
- sendATCommand("AT+CSQ", AT_COMMAND_TIMEOUT);
- delay(500);
- SIM900AInitialized = true;
- Serial.println(F("SIM900A initialized successfully"));
- }
- // Read gas level from MQ5 sensor
- void readGasLevel(void)
- {
- // Update sensor readings
- MQ5.update();
- // Read PPM value for LPG
- currentGasLevel = MQ5.readSensor();
- // Ensure reading is non-negative
- if (currentGasLevel < 0)
- {
- currentGasLevel = 0;
- }
- Serial.print(F("Gas Level Read: "));
- Serial.print(currentGasLevel);
- Serial.println(F(" PPM"));
- }
- // Send SMS with gas level to phone
- void sendGasSMS(float gasLevel)
- {
- if (!SIM900AInitialized)
- {
- Serial.println(F("Error: SIM900A not initialized"));
- return;
- }
- Serial.println(F("\n=== Sending Gas Level SMS ==="));
- // Prepare SMS recipient number
- String smsCommand = "AT+CMGS=\"" + PHONE_NUMBER + "\"";
- // Send SMS command
- sendATCommand(smsCommand, AT_COMMAND_TIMEOUT);
- delay(500);
- // Prepare message with gas level information
- String message = "Gas Alert System - LPG Level: ";
- message += gasLevel;
- message += " PPM";
- // Send message content
- SIM900A_Serial.print(message);
- // Send Ctrl+Z to confirm and send SMS
- SIM900A_Serial.write(26); // ASCII code for Ctrl+Z
- Serial.print(F("SMS Message: "));
- Serial.println(message);
- Serial.println(F("SMS sent successfully\n"));
- // Wait for SMS processing
- delay(SMS_DELAY);
- }
- // Send AT command to SIM900A
- void sendATCommand(String command, unsigned long timeout)
- {
- // Clear any existing data in buffer
- while (SIM900A_Serial.available())
- {
- SIM900A_Serial.read();
- }
- // Send command
- SIM900A_Serial.println(command);
- Serial.print(F(">> "));
- Serial.println(command);
- // Wait for response
- waitForResponse("OK", timeout);
- }
- // Wait for response from SIM900A
- void waitForResponse(String expectedResponse, unsigned long timeout)
- {
- unsigned long startTime = millis();
- String response = "";
- while (millis() - startTime < timeout)
- {
- if (SIM900A_Serial.available())
- {
- char inChar = (char)SIM900A_Serial.read();
- response += inChar;
- // Check if we received the expected response
- if (response.indexOf(expectedResponse) != -1)
- {
- Serial.print(F("<< "));
- Serial.println(response);
- return;
- }
- }
- }
- // Timeout occurred
- Serial.println(F("No response from SIM900A (timeout)"));
- if (response.length() > 0)
- {
- Serial.print(F("Partial response: "));
- Serial.println(response);
- }
- }
- // Display debug information
- void displayDebugInfo(void)
- {
- Serial.print(F("System Time: "));
- Serial.print(millis() / 1000);
- Serial.print(F("s | Gas: "));
- Serial.print(currentGasLevel);
- Serial.print(F(" PPM | SIM900A: "));
- Serial.println(SIM900AInitialized ? F("Ready") : F("Not Ready"));
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment