pleasedontcode

# Gas Monitor rev_01

Jan 25th, 2026
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: # Gas Monitor
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2026-01-25 18:31:09
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* when gas sms is sent to sim 900a it should send */
  21.     /* the gas level to phone */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24.  
  25. /* START CODE */
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <MQUnifiedsensor.h>    //https://github.com/miguel5612/MQSensorsLib
  29. #include <SoftwareSerial.h>     //Software Serial for SIM900A communication
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34. void initMQ5Sensor(void);
  35. void initSIM900A(void);
  36. void readGasLevel(void);
  37. void sendGasSMS(float gasLevel);
  38. void sendATCommand(String command, unsigned long timeout);
  39. void waitForResponse(String expectedResponse, unsigned long timeout);
  40. void displayDebugInfo(void);
  41.  
  42. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  43. const uint8_t MQ5_MQ5_DOUT_PIN_D2           = 2;
  44.  
  45. /***** DEFINITION OF ANALOG INPUT PINS *****/
  46. const uint8_t MQ5_MQ5_AOUT_PIN_A0           = A0;
  47.  
  48. /***** DEFINITION OF SIM900A COMMUNICATION PINS *****/
  49. // SoftwareSerial(RX, TX)
  50. const uint8_t SIM900A_RX_PIN                = 10;   // RX pin for SIM900A
  51. const uint8_t SIM900A_TX_PIN                = 11;   // TX pin for SIM900A
  52.  
  53. /***** DEFINITION OF SENSOR CALIBRATION PARAMETERS *****/
  54. // MQ-5 sensor configuration for LPG detection
  55. const float MQ5_RATIO_CLEAN_AIR             = 6.5;  // RS/R0 ratio in clean air
  56. const float MQ5_RL_VALUE                    = 10.0; // Load resistance in KOhms
  57. const float MQ5_VOLTAGE_RESOLUTION          = 5.0;  // Arduino Uno voltage reference
  58. const int MQ5_ADC_BIT_RESOLUTION            = 10;   // Arduino Uno ADC resolution
  59.  
  60. /***** DEFINITION OF GSM MODULE CONFIGURATION *****/
  61. const String PHONE_NUMBER                   = "+1234567890";    // Phone number to send SMS to - CHANGE THIS
  62. const unsigned long SIM900A_BAUD_RATE       = 9600;         // SIM900A communication speed
  63. const unsigned long AT_COMMAND_TIMEOUT      = 1000;         // Timeout for AT commands (ms)
  64. const unsigned long SMS_DELAY               = 5000;         // Delay between SMS messages (ms)
  65.  
  66. /***** DEFINITION OF MEASUREMENT PARAMETERS *****/
  67. const unsigned long GAS_READING_INTERVAL    = 10000;        // Interval for gas readings (ms)
  68. const unsigned long SMS_SEND_INTERVAL       = 60000;        // Interval for SMS sending (60 seconds)
  69.  
  70. /****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
  71. // Create SoftwareSerial instance for SIM900A communication
  72. SoftwareSerial SIM900A_Serial(SIM900A_RX_PIN, SIM900A_TX_PIN);
  73.  
  74. // Create MQ5 sensor instance
  75. // MQUnifiedsensor(Board, Voltage_Resolution, ADC_Bit_Resolution, Pin, Type)
  76. MQUnifiedsensor MQ5("Arduino UNO", MQ5_VOLTAGE_RESOLUTION, MQ5_ADC_BIT_RESOLUTION, MQ5_MQ5_AOUT_PIN_A0, "MQ-5");
  77.  
  78. /***** GLOBAL VARIABLES *****/
  79. float currentGasLevel = 0.0;                    // Current gas level reading in PPM
  80. unsigned long lastGasReadTime = 0;              // Timestamp of last gas reading
  81. unsigned long lastSMSSendTime = 0;              // Timestamp of last SMS sent
  82. bool SIM900AInitialized = false;                // Flag for SIM900A initialization status
  83. String serialBuffer = "";                       // Buffer for serial communication
  84.  
  85. /****** SETUP FUNCTION *****/
  86. void setup(void)
  87. {
  88.     // Initialize Serial for debugging
  89.     Serial.begin(9600);
  90.    
  91.     // Wait for Serial to initialize
  92.     delay(100);
  93.    
  94.     Serial.println(F("\n\n=== Gas SMS Alert System Started ==="));
  95.     Serial.println(F("Initializing MQ5 Sensor..."));
  96.    
  97.     // Configure GPIO pins
  98.     pinMode(MQ5_MQ5_DOUT_PIN_D2, INPUT_PULLUP);
  99.     pinMode(MQ5_MQ5_AOUT_PIN_A0, INPUT);
  100.    
  101.     // Initialize MQ5 sensor
  102.     initMQ5Sensor();
  103.    
  104.     // Initialize SIM900A GSM module
  105.     Serial.println(F("Initializing SIM900A GSM Module..."));
  106.     initSIM900A();
  107.    
  108.     Serial.println(F("=== System Initialization Complete ===\n"));
  109.    
  110.     // Initialize timestamps
  111.     lastGasReadTime = millis();
  112.     lastSMSSendTime = millis();
  113. }
  114.  
  115. /****** MAIN LOOP *****/
  116. void loop(void)
  117. {
  118.     // Handle incoming serial data from SIM900A for debugging
  119.     while (SIM900A_Serial.available())
  120.     {
  121.         char inChar = (char)SIM900A_Serial.read();
  122.         serialBuffer += inChar;
  123.        
  124.         // Process complete lines
  125.         if (inChar == '\n')
  126.         {
  127.             Serial.print(F("SIM900A: "));
  128.             Serial.println(serialBuffer);
  129.             serialBuffer = "";
  130.         }
  131.     }
  132.    
  133.     // Perform gas level reading at specified interval
  134.     if (millis() - lastGasReadTime >= GAS_READING_INTERVAL)
  135.     {
  136.         lastGasReadTime = millis();
  137.         readGasLevel();
  138.         displayDebugInfo();
  139.     }
  140.    
  141.     // Send SMS with gas level at specified interval
  142.     if (millis() - lastSMSSendTime >= SMS_SEND_INTERVAL && SIM900AInitialized)
  143.     {
  144.         lastSMSSendTime = millis();
  145.         sendGasSMS(currentGasLevel);
  146.     }
  147. }
  148.  
  149. /****** FUNCTION IMPLEMENTATIONS *****/
  150.  
  151. // Initialize MQ5 sensor with calibration parameters
  152. void initMQ5Sensor(void)
  153. {
  154.     // Set regression method for PPM calculation
  155.     MQ5.setRegressionMethod(1);  // 1 = Exponential equation
  156.    
  157.     // Configure sensor parameters for LPG detection
  158.     // These values are based on MQ-5 datasheet calibration curve
  159.     // For LPG: PPM = a * (RS/R0)^b
  160.     MQ5.setA(1163.8);       // Coefficient 'a' for LPG
  161.     MQ5.setB(-3.207);       // Coefficient 'b' for LPG
  162.    
  163.     // Set the RL value (load resistance on the sensor module)
  164.     MQ5.setRL(MQ5_RL_VALUE);
  165.    
  166.     // Perform calibration - R0 value obtained from clean air calibration
  167.     // For a properly calibrated sensor in clean air
  168.     float R0 = MQ5.calibrate(MQ5_RATIO_CLEAN_AIR);
  169.    
  170.     Serial.print(F("MQ5 Calibration - R0: "));
  171.     Serial.println(R0);
  172.    
  173.     // Initialize sensor
  174.     MQ5.init();
  175.    
  176.     Serial.println(F("MQ5 Sensor initialized successfully"));
  177. }
  178.  
  179. // Initialize SIM900A GSM module
  180. void initSIM900A(void)
  181. {
  182.     // Initialize software serial for SIM900A
  183.     SIM900A_Serial.begin(SIM900A_BAUD_RATE);
  184.    
  185.     // Give SIM900A time to boot
  186.     delay(2000);
  187.    
  188.     // Test communication with AT command
  189.     Serial.println(F("Testing SIM900A communication..."));
  190.     sendATCommand("AT", AT_COMMAND_TIMEOUT);
  191.     delay(500);
  192.    
  193.     // Disable echo for cleaner responses
  194.     Serial.println(F("Disabling echo..."));
  195.     sendATCommand("ATE0", AT_COMMAND_TIMEOUT);
  196.     delay(500);
  197.    
  198.     // Set SMS text mode
  199.     Serial.println(F("Setting SMS text mode..."));
  200.     sendATCommand("AT+CMGF=1", AT_COMMAND_TIMEOUT);
  201.     delay(500);
  202.    
  203.     // Set SMS character set
  204.     Serial.println(F("Setting SMS character set..."));
  205.     sendATCommand("AT+CSCS=\"GSM\"", AT_COMMAND_TIMEOUT);
  206.     delay(500);
  207.    
  208.     // Check signal quality
  209.     Serial.println(F("Checking signal quality..."));
  210.     sendATCommand("AT+CSQ", AT_COMMAND_TIMEOUT);
  211.     delay(500);
  212.    
  213.     SIM900AInitialized = true;
  214.     Serial.println(F("SIM900A initialized successfully"));
  215. }
  216.  
  217. // Read gas level from MQ5 sensor
  218. void readGasLevel(void)
  219. {
  220.     // Update sensor readings
  221.     MQ5.update();
  222.    
  223.     // Read PPM value for LPG
  224.     currentGasLevel = MQ5.readSensor();
  225.    
  226.     // Ensure reading is non-negative
  227.     if (currentGasLevel < 0)
  228.     {
  229.         currentGasLevel = 0;
  230.     }
  231.    
  232.     Serial.print(F("Gas Level Read: "));
  233.     Serial.print(currentGasLevel);
  234.     Serial.println(F(" PPM"));
  235. }
  236.  
  237. // Send SMS with gas level to phone
  238. void sendGasSMS(float gasLevel)
  239. {
  240.     if (!SIM900AInitialized)
  241.     {
  242.         Serial.println(F("Error: SIM900A not initialized"));
  243.         return;
  244.     }
  245.    
  246.     Serial.println(F("\n=== Sending Gas Level SMS ==="));
  247.    
  248.     // Prepare SMS recipient number
  249.     String smsCommand = "AT+CMGS=\"" + PHONE_NUMBER + "\"";
  250.    
  251.     // Send SMS command
  252.     sendATCommand(smsCommand, AT_COMMAND_TIMEOUT);
  253.     delay(500);
  254.    
  255.     // Prepare message with gas level information
  256.     String message = "Gas Alert System - LPG Level: ";
  257.     message += gasLevel;
  258.     message += " PPM";
  259.    
  260.     // Send message content
  261.     SIM900A_Serial.print(message);
  262.    
  263.     // Send Ctrl+Z to confirm and send SMS
  264.     SIM900A_Serial.write(26);  // ASCII code for Ctrl+Z
  265.    
  266.     Serial.print(F("SMS Message: "));
  267.     Serial.println(message);
  268.     Serial.println(F("SMS sent successfully\n"));
  269.    
  270.     // Wait for SMS processing
  271.     delay(SMS_DELAY);
  272. }
  273.  
  274. // Send AT command to SIM900A
  275. void sendATCommand(String command, unsigned long timeout)
  276. {
  277.     // Clear any existing data in buffer
  278.     while (SIM900A_Serial.available())
  279.     {
  280.         SIM900A_Serial.read();
  281.     }
  282.    
  283.     // Send command
  284.     SIM900A_Serial.println(command);
  285.    
  286.     Serial.print(F(">> "));
  287.     Serial.println(command);
  288.    
  289.     // Wait for response
  290.     waitForResponse("OK", timeout);
  291. }
  292.  
  293. // Wait for response from SIM900A
  294. void waitForResponse(String expectedResponse, unsigned long timeout)
  295. {
  296.     unsigned long startTime = millis();
  297.     String response = "";
  298.    
  299.     while (millis() - startTime < timeout)
  300.     {
  301.         if (SIM900A_Serial.available())
  302.         {
  303.             char inChar = (char)SIM900A_Serial.read();
  304.             response += inChar;
  305.            
  306.             // Check if we received the expected response
  307.             if (response.indexOf(expectedResponse) != -1)
  308.             {
  309.                 Serial.print(F("<< "));
  310.                 Serial.println(response);
  311.                 return;
  312.             }
  313.         }
  314.     }
  315.    
  316.     // Timeout occurred
  317.     Serial.println(F("No response from SIM900A (timeout)"));
  318.     if (response.length() > 0)
  319.     {
  320.         Serial.print(F("Partial response: "));
  321.         Serial.println(response);
  322.     }
  323. }
  324.  
  325. // Display debug information
  326. void displayDebugInfo(void)
  327. {
  328.     Serial.print(F("System Time: "));
  329.     Serial.print(millis() / 1000);
  330.     Serial.print(F("s | Gas: "));
  331.     Serial.print(currentGasLevel);
  332.     Serial.print(F(" PPM | SIM900A: "));
  333.     Serial.println(SIM900AInitialized ? F("Ready") : F("Not Ready"));
  334. }
  335.  
  336. /* END CODE */
  337.  
Advertisement
Add Comment
Please, Sign In to add comment