Send SMS once Arduino is triggered via a push sensor

i Have removed that, it now just utomattically enters something into the serial monitor, without pressing, says that the SMS is complete, but nothing gets sent.

See reply #58

You really should no better by now

Ok,

/*
*/
#include <MKRNB.h>
#include <NB_SMS.h>

const char PINNUMBER[] = "7051";
// initialize the library instance
NB nbAccess;
NB_SMS sms;


// this constant won't change:
const int  buttonPin = 16;    // the pin that the pushbutton is attached to
const int ledPin = 2;       // the pin that the LED is attached to
// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button


void setup()
{
  Serial.begin(9600);
  while (!Serial);
  pinMode(buttonPin, INPUT_PULLUP);

}

void loop()
{
  buttonState = digitalRead(buttonPin);
  if (buttonState != lastButtonState && buttonState == LOW)
  {
    sendSMS();
  }
  lastButtonState = buttonState;
}

void sendSMS()
{
  Serial.println("SMS Messages Sender");

    Serial.println("NB initialized");

    char remoteNum[] = "0499700369";

    // SMS text

    char txtMsg[] = "Gate Opened";
    Serial.println("SENDING");
    Serial.println();
    Serial.println("Message:");
    Serial.println(txtMsg);

    // send the message
    sms.beginSMS(remoteNum);
    sms.print(txtMsg);
    sms.endSMS();
    Serial.println("\nCOMPLETE!\n");

  
}

I am still none the wiser as to which pin the button is connected to or how it is wired

Unless the button is connected to pin 16 and takes the pin to GND when pushed then teh code will not work as required

the pin is connected to: 2, GND
i cannot write GN in the sketch, as an error occurs
Not sure what the 16 is!

The code says that the button is connected to pin 16 and the LED is connected to pin 2

You say

I assume that you mean that the buttton is connected between pin 2 and GND

No wonder the sketch does not work

Delete the code line relating to the LED. You are not using one. Change the code so that buttonPin has a value of 2 to match your circuit

i have tried this before, i will try again!

When you change the code post it again

Just tried this code, and the SAME issue, serial monitor says SMS SENT WITHOUT PRESSING THE BUTTON

/*
*/
#include <MKRNB.h>
#include <NB_SMS.h>

const char PINNUMBER[] = "7051";
// initialize the library instance
NB nbAccess;
NB_SMS sms;


// this constant won't change:
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button


void setup()
{
  Serial.begin(9600);
  while (!Serial);
  pinMode(buttonPin, INPUT_PULLUP);

}

void loop()
{
  buttonState = digitalRead(buttonPin);
  if (buttonState != lastButtonState && buttonState == LOW)
  {
    sendSMS();
  }
  lastButtonState = buttonState;
}

void sendSMS()
{
  Serial.println("SMS Messages Sender");

    Serial.println("NB initialized");

    char remoteNum[] = "0499700369";

    // SMS text

    char txtMsg[] = "Gate Opened";
    Serial.println("SENDING");
    Serial.println();
    Serial.println("Message:");
    Serial.println(txtMsg);

    // send the message
    sms.beginSMS(remoteNum);
    sms.print(txtMsg);
    sms.endSMS();
    Serial.println("\nCOMPLETE!\n");

  
}

It's time to do some debugging

Upload this code


const int  buttonPin = A2;    // the pin that the pushbutton is attached to
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button


void setup()
{
  Serial.begin(9600);
  while (!Serial);
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop()
{
  buttonState = digitalRead(buttonPin);
  if (buttonState != lastButtonState && buttonState == LOW)
  {
    sendSMS();
  }
  lastButtonState = buttonState;
}

void sendSMS()
{
  Serial.println("SMS Messages Sender");
}

What messages do you see on the screen when you upload it before the button is pressed ?
What messages do you see on the screen when you press the button ?

Self promotion…
If you get in too deep, take a look at
www.SL4P.net.
A product that might do exactly what you’re looking for - and more.

I now believe there is a problem with my MKR NB 1500 board. It is not locating any networks, not showing me the IMEI and not giving me any information when I upload the sketches. The serial Monitor just prints: "not connected". The SIM card does have charge, lights do come on the device, it lets me upload to the device, but nothing happens. I think internally there is a problem that is not letting the device link to the network to access anything.