Send SMS once Arduino is triggered via a push sensor

What is NB_SMS ?

I suggest that you do some reading on functions. This section of code

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

could be put into a function to be called when required rather than every time through loop()

I realy don't know what you are asking here

So, i use this section of code

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

and then I write in the code for when it is required. What function would i use for that (telling it to send SMS once pressed)

It is not a single function

What you do is to save the state of the input each time you read it and compare it to the state when you next read it. If it has changed and now indicates that the button has become pressed then you do whatever you want

Look at the StateChangeDetection example in the IDE to see the principle in action

1 Like

I have only got it to happen when I do it on the computer. I am trying to somehow include the 2 sketches, so that once the state changes from OFF to ON or OPEN to CLOSED, the SMS will be sent

Now that you know what is required (State Change Detection) please post your best attempt at writing the sketch

1 Like
*/
#include <MKRNB.h>

// this constant won't change:
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // 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

// initialize the library instance
NB nbAccess;
NB_SMS sms;

  // connection state
  bool connected = false;
void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  
  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
    } else {
      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
        // send the message
  sms.beginSMS("0499700369");
  sms.print("GATE OPENED");
  sms.endSMS();
  Serial.println("\nCOMPLETE!\n");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;


  // turns on the LED every four button pushes by checking the modulo of the
  // button push counter. the modulo function gives you the remainder of the
  // division of two numbers:
  if (buttonPushCounter % 4 == 0) {
    digitalWrite(ledPin, LOW);
  } else {
    digitalWrite(ledPin, HIGH);
  }

}

What happens when you run that code ?
What do you see in the Serial monitor ?
How is the input wired ?

currently wired through the computer, will be run on Battery/Solar later on.

when i put that code in, I can see that the button is responding,(in the serial Monitor) but no SMS is being sent

And am now getting an error message for this part of the code

// send the message
  sms.beginSMS("0499700369");
  sms.print("Gate Opened");
  sms.endSMS();
  Serial.println("\nCOMPLETE!\n");

error message is saying:

'sms' does not name a type

First you got no error then you did. What did you change ? Where is the sms object created ?

I re-looked through the code, and saw a mistake. sorry about this
Using the char remoteNum[] = "phone Number" to add the phone number into the state change detection code..!

Now I don't know what your latest code looks like. Please post it in a new post so w know what you have done. Does the code do what you want now ?

No, it still is not sending the SMS when i push the button the new code is:

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

#include "arduino_secrets.h" 
// Please enter your sensitive data in the Secret tab or arduino_secrets.h
// PIN Number
const char PINNUMBER[] = SECRET_PINNUMBER;

// 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
const int ledPin = 13;       // 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() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
  

}


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == LOW) {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
    } else {
      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
   
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;

if (buttonState == LOW) int NB_SMS;
 // initialize serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  Serial.println("SMS Messages Sender");

  // connection state
  bool connected = false;

  // Start NB module
  // If your SIM has PIN, pass it as a parameter of begin() in quotes
  while (!connected) {
    if (nbAccess.begin(PINNUMBER) == NB_READY) {
      connected = true;
    } else {
      Serial.println("Not connected");
      delay(1000);
    }
  }

  Serial.println("NB initialized");

char remoteNum[] = "0499700369";

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

  // turns on the LED every four button pushes by checking the modulo of the
  // button push counter. the modulo function gives you the remainder of the
  // division of two numbers:
  if (buttonPushCounter % 4 == 0) {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }

}

The code to send the SMS should be within the code block that is executed when a button press is detected

read the button
if the button has become pressed
  send the SMS
end if

I have tried to include:

read the button
if the button has become pressed
  send the SMS
end if

but when i do, i get an error message.

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

#include "arduino_secrets.h" 
// Please enter your sensitive data in the Secret tab or arduino_secrets.h
// PIN Number
const char PINNUMBER[] = SECRET_PINNUMBER;

// 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
const int ledPin = 13;       // 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() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);

  

}


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == LOW) {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
    } else {
      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
   
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;

if (buttonState == LOW) int NB_SMS;
 // initialize serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  Serial.println("SMS Messages Sender");

  // connection state
  bool connected = false;

  // Start NB module
  // If your SIM has PIN, pass it as a parameter of begin() in quotes
  while (!connected) {
    if (nbAccess.begin(PINNUMBER) == NB_READY) {
      connected = true;
    } else {
      Serial.println("Not connected");
      delay(1000);
    }
  }

  Serial.println("NB initialized");

char remoteNum[] = "0499700369";

char txtMsg[] = "Gate Opened";
  Serial.println("SENDING");
  Serial.println();
  Serial.println("Message:");
  Serial.println(txtMsg);
  
read the button
if the button has become pressed
  send the SMS
end if

  // turns on the LED every four button pushes by checking the modulo of the
  // button push counter. the modulo function gives you the remainder of the
  // division of two numbers:
  if (buttonPushCounter % 4 == 0) {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }

}

That was not real code, it was an outline of what you need to do

Let's go back to basics. What value does buttonState have when the button is pressed ? Have you got a pullup or pulldown resistor in place to keep the button pin at a known state at all times
?

To make the code easier to read and test I suggest that you move all of the code that sends an SMS into a function with a meaningful name such as sendSMS()

According to the code, it has a value of 0.
I do have a pullup/pulldown resistor in place i think??
how do you add the code into a simpler function like sendSMS()?

A suggestion

Use INPUT_PULLUP in the pinMode() for the button pin to activate the built in pullup resistor to keep the input HIGH when the button is not pressed. Change the wiring so that the button pin is taken to GND when the button is pressed and change the program logic to use LOW as the indication that the button is pressed

1 Like

I'll Change the button pin to GND.
Do I change the other pin to anything? or leave it at 13?
Insert INPUT_PULLUP in pinMode()
Change logic to LOW (indication that the button is being pressed)

The other pin, number 13, is the one that is connected to the onboard LED. Leave its pinMode() as OUTPUT

1 Like