How to send messages and call successively with sim800l?

Hi guys, I'm working on a gas leak detector project but I'm stuck on an implementation. In a few words, my equipment performs closing and opening at detection, I have managed to successfully make a call and many messages (8 or 10), but when implementing a function for only 1 message it stops sending messages. I'm not very skilled at programming so I'm probably leaving something out.

If anyone can give me a hand I will be very grateful, thanks in advance.

This is the last code I tried to make work.

int pin8 = 8;
int sensor = A0;
int sensorValue = 0;
const int buzzer = 5;
const int buttonPin = 7;
int buttonState = 0;
int gas = 0;

unsigned int current = 0;
unsigned int previous = 0;

unsigned long previousMillis = 0;
int currentIntervalIndex = 0;
const int intervals[] = {200, 500}; // Time intervals in milliseconds

// Variable for LED status
int ledState = LOW;

#include <SoftwareSerial.h>
//Create the serial software object to communicate with the SIM800L
SoftwareSerial mySerial(3, 2); // The SIM800L Tx & Rx pins will connect to the Arduino pins #3 & #2

unsigned long CallStartTime = 0; // Variable for the call timer
bool activeCall = false; // Call status
bool callDone = false;
bool messageSent = false;

void setup() {
   //begins serial communication with Arduino and Arduino IDE (Serial Monitor)
   Serial.begin(9600);
  
   //Serial communication with Arduino and SIM800L begins
   mySerial.begin(9600);

   Serial.println("Initializing...");
   current = millis();
   previous = current;

   pinMode(buzzer, OUTPUT);
   pinMode(pin8, OUTPUT);
   pinMode(buttonPin, INPUT);
   digitalWrite(pin8, HIGH);

 
}

void loop() {
   gas = digitalRead(pin8);
   buttonState = digitalRead(buttonPin);
   SensorValue = analogRead(sensor);

   // If the sensor value exceeds a threshold and there is no active call, activate the call
   if (sensorvalue > 130 && !activecall && !madecall) {
     digitalWrite(pin8, LOW);
     activeCall = true; // Marks that the call is active
     CallStartTime = millis(); // Initialize the call timer
     mySerial.println("ATD+ +59177167799;"); // Change ZZ with the country code and xxxxxxxxxx with the number of digits of the phone
     updateSerial();
   }

   // Controls the duration of the call (20 seconds)
   if (activecall && (millis() - callStartTime >= 15000)) {
     activeCall = false; // Marks that the call is no longer active
     mySerial.println("ATH"); // Hang up
     updateSerial();
     callDone = true;
     clearBufferSerial();
   }

   if (callMade && !messageSent) {
     clearBufferSerial();
     Serial.begin(9600);
  
     //begins serial communication with Arduino and SIM800L
     mySerial.begin(9600);

     Serial.println("Initializing...");
     delay(1000);

     mySerial.println("AT"); //once the tests are done successfully, an OK will be returned
     updateSerial();
    
     mySerial.println("AT+CMGF=1"); // Configuring text mode
     updateSerial();
     mySerial.println("AT+CMGS=\"+59177167799\"");//change ZZ country code and xxxxxxxxxxxx with phone number to sms

     updateSerial();
     mySerial.print("alert, gas detected"); //text content
     updateSerial();
     mySerial.write(26);
     messageSent = true;
   }

   if (gas == LOW) {
       // Get the current time
   unsigned long currentMillis = millis();

   // Check if the current interval time has passed
   if (currentMillis - previousMillis >= intervals[currentIntervalIndex]) {
     // Save the current time as a reference for the next interval
     previousMillis = currentMillis;

     // Change LED status
     if (ledState == LOW) {
       ledState = HIGH;
     } else {
       ledState = LOW;
     }

     // Apply state to LED
     digitalWrite(buzzer, ledState);

     // Advance to the next interval (cyclic)
     currentIntervalIndex = (currentIntervalIndex + 1) % (sizeof(intervals) / sizeof(intervals[0]));
   }
  
}

   if ((sensorValue < 130) && (buttonState == HIGH)) {
     digitalWrite(pin8, HIGH);
     callMade = false; // Any button pressed hangs the call
     digitalWrite(buzzer, LOW);
     messageSent = false;

   }
}

void clearBufferSerial() {
   while (Serial.available() > 0) {
     char c = Serial.read();
   }
}

void updateSerial() {
   unsigned long currentTime = millis();
   if (currentTime - previous >= 500) {
     previous = currentTime;
     while (Serial.available()) {
       mySerial.write(Serial.read()); // forwards what the serial software port has received
     }
     while (mySerial.available()) {
       Serial.write(mySerial.read()); // forward the received serial software to the serial port
     }
   }
}

If you need any other extra information, please tell me, I will keep and eye the whole day.

Which function, how is it intended to work, how is it working with many messages and how is it not working?

  1. Send only 1 message, 2) when the sensorvalue goes >130 it must do a call and after the call ends sends a alert message, 3) it's works pretty well ( do the gates open, start the alarm and do a 1 single call) just the fact that it sends message whitout control), 4) to prevent the infinite number of message i applies the function (!mensajeEnviado) which is in false and after the message code it change to true soo only 1 menssage should be send it but the issue is there is not message :frowning: .

I apologize for my writing, English is not my native language.

Do not worry about language. If you feel the need, you can write your native language into google translate and paste the results here if you want to continue in English (some Arduino words do not translate), or you can post your topic on the Español page.

i aready change the spanish words in arduino code to english, thanks for the advice.

This is not 20 seconds:

Do you need updateSerial(); after this:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.