Send Message with GPS Location When Button is Pressed


I’m currently working on a project on how to send a message with GPS information when a button is pressed. I’m using Arduino Uno and SIM 800L. So first, I tested separate codes of sending the message and printing GPS location through the serial monitor. Both codes were successfully executed.

Now, I’m trying to send the message when the button is pressed. But it failed (no message is sent). Below is my coding.

#include <SoftwareSerial.h>
SoftwareSerial sim(10, 11);
int _timeout;
String _buffer;
String number = "+111111111"; //-> change with your number
int state = 0;
const int pin = 2;

void setup() {
  delay(7000); //delay for 7 seconds to make sure the modules get the signal
  Serial.println("System Started...");
  pinMode(pin, INPUT);

void loop() 
  if (digitalRead(pin) == HIGH && state == 0) {
        state = 1;
  else {
    state = 0;

void SendMessage()
  //Serial.println ("Sending Message");
  sim.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode
  //Serial.println ("Set SMS Number");
  sim.println("AT+CMGS=\"+60111111111\"\r"); //Mobile phone number to send message
  String SMS = "Hello, how are you?";
  sim.println((char)26);// ASCII code of CTRL+Z
   _buffer = _readSerial();

String _readSerial() {
  _timeout = 0;
  while  (!sim.available() && _timeout < 12000  )
  if (sim.available()) {
    return sim.readString();

Any opinion and solution are highly appreciated. Please bear in mind that I’m really new to Arduino so there are some terms that I might not understand.

How is the button wired? I see you are using pinMode(pin, INPUT) which requires an external resistor added to the button.

Open up the StateChange example from the Arduino examples. Get that working with your button first.