Send SMS once Arduino is triggered via a push sensor

Just wondering how to get an Arduino MKR NB 1500 to send an SMS, once it has been triggered by a sensor (push sensor).
I have already got the Arduino to send an SMS, but not once triggered… Any help will be amazing
Planning on using the ChangeStateDetection sketch.
Thanks

The StateChangeDetection example is a good start. Where are you stuck in detecting the state change and triggering the SMS ?

1 Like

I’m finding it a little difficult, as I believe that I need to include the SendSMS as a page on the code?
And I am not too sure as how to do this?!

Post your full sketch that sends an SMS

This topic has been moved to the Programming category of the forum

Moderator edit : Auto Formatted and posted in code tags

// Include the NB library
#include <MKRNB.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;

void setup()
{
  // 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");
}

void loop()
{
  Serial.print("Enter a mobile number: ");
  char remoteNum[20]; // telephone number to send SMS
  readSerial(remoteNum);
  Serial.println(remoteNum);
  // SMS text
  Serial.print("Now, enter SMS content: ");
  char txtMsg[200];
  readSerial(txtMsg);
  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");
}

/*
Read input serial
*/
int readSerial(char result)
{
  int i = 0;
  while (1)
  {
    while (Serial.available() > 0)
    {
      char inChar = Serial.read();
      if (inChar == '\n')
      {
        result[i] = '\0';
        Serial.flush();
        return 0;
      }
      if (inChar != '\r')
      {
        result[i] = inChar;
        i++;
      }
    }
  }
}

@isaac_mitchell please note and do this in future. See How to get the best out of this forum

This is the Code for the CHANGESTATEDETECTION

/*
  State change detection (edge detection)

  Often, you don't need to know the state of a digital input all the time, but
  you just need to know when the input changes from one state to another.
  For example, you want to know when a button goes from OFF to ON. This is called
  state change detection, or edge detection.

  This example shows how to detect when a button or button changes from off to on
  and on to off.

  The circuit:
  - pushbutton attached to pin 2 from +5V
  - 10 kilohm resistor attached to pin 2 from ground
  - LED attached from pin 13 to ground (or use the built-in LED on most
    Arduino boards)

  created  27 Sep 2005
  modified 30 Aug 2011
  by Tom Igoe

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/ButtonStateChange
*/

// 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 == 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");
    }
    // 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, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }

}

Should the user be prompted for a 'phone number when the button is pressed or should pressing the button send the SMS to a fixed number ?

To a fixed number, needs to be the same every time

And the same message every time ?

yes. it is going to be on a gate, so something like. Mamre Road Gate Opened. something like this is what I want it to say every time the sensor is pushed, then reset back.

In that case, all you need to do is have loop() check for switch closure, then send the message.

All the requests for user input should be removed, and the answers replaced with appropriate character strings.

For example, replace:

Serial.print("Enter a mobile number: ");
  char remoteNum[20]; // telephone number to send SMS
  readSerial(remoteNum);
  Serial.println(remoteNum);

with

char remoteNum[]="800-555-1212";
1 Like

That worked,
char remoteNum[]="PHONE NUMBER";

Wondering if this will work for a pre-written Message as well?
Thanks, Isaac

How to Initialize character strings

(One of countless C/C++ tutorials on line).

1 Like

A little Update...
I have included an issue that I keep coming to with.
When I try to put a Pre-written message, it works, but sends multiple of the same message, but doesn't stop until I unplug the device.
Wondering if there is a simple coding mistake that I am missing?

 Serial.println("NB initialized");
}

void loop() {

 
  char remoteNum[]= "04******69";  


  // SMS text

  char txtMsg[]= "Cattle Yard Gate Open";


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

And still wondering how I can include a "when pushed, the SMS will send

  • Isaac

Can you guess from it's name what the loop() function does ?

Step 1 to solving your problem is to put the code that sends the SMS into a function and call it from setup()

Step 2 is to read the state of an input pin, test it, and when it indicates that the button is pressed call the function. See the Button example in the IDE

Step 3 is to read the state of an input pin, test it, and when it indicates that the button has become pressed call the function

Start by getting steps 1 and 2 working then look at the StateChangeDetection example in the IDE to see how to do step 3

1 Like

How do I get the Arduino to "reset" after it has sent an SMS? so basically when the "gate" gets opened, it sends an SMS, how does it reset ready to send another one when it gets opened again...and so on....? thanks, Isaac

You do not need to reset anything

When the gate becomes open send the SMS is all you need to do. Once it is closed the next time that it becomes open another text will be sent

1 Like

Is that done by the steps that you provided earlier?. I am having a bit of trouble but will continue trying to do this.
Thanks

Yes

1 Like

How am i supposed to include NB_SMS into a function?
then insert that into the State change Detection code,
void setup()...?
int setup()
Confused as to what to do..?