Fona808 SMS message from alarm

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
kevcorks
 
Posts: 7
Joined: Thu Apr 14, 2016 8:16 am

Fona808 SMS message from alarm

Post by kevcorks »

Hi

I hope someone can help me here, I have two sketches that I am attempting to combine. They both work independently but appear to be getting confused and I can't seem to figure out why.

The first sketch is based on the knock example where a vibration on the sensor will send an SMS message to a phone. The code for this is here:

Code: Select all

const int sensorPin=0;
const int ledPin= 13;
const int threshold= 10;

#include "Adafruit_FONA.h"
#include <SoftwareSerial.h>

#define FONA_RX 2
#define FONA_TX 3
#define FONA_RST 4

SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX);
SoftwareSerial *fonaSerial = &fonaSS;

Adafruit_FONA fona = Adafruit_FONA(FONA_RST);

void setup()
{
while (!Serial);

  Serial.begin(115200);
  Serial.println(F("FONA SMS caller ID test"));
  Serial.println(F("Initializing....(May take 3 seconds)"));

  // make it slow so its easy to read!
  fonaSerial->begin(4800);
  if (! fona.begin(*fonaSerial)) {
    Serial.println(F("Couldn't find FONA"));
    while(1);
  }
  Serial.println(F("FONA is OK"));

 
  }
  
  Serial.println("FONA Ready");
pinMode(ledPin, OUTPUT);
}

void loop()
{
int val= analogRead(sensorPin);
if (val >= threshold)
{
char sendto[13] = "07737775715";
fona.sendSMS(sendto, "ALARM STATUS!!!!  Motion detected, please check my location!") ;
delay(5000);

}
else 
{
digitalWrite(ledPin, LOW);
}
} 
The second sketch is based on the SMS response sketch and will recieve a text and respond with the devices GPS location. This sketch is here:

Code: Select all

const int sensorPin=0;
const int ledPin= 13;
const int threshold= 10;

#include "Adafruit_FONA.h"                 //This is the library for the FONA module
#include <SoftwareSerial.h>                //This is the library for the software serial interface

#define FONA_RX 2                          //These define the pin assignment
#define FONA_TX 3                          //on the Arduino board and determines how it is connected
#define FONA_RST 4                         

// this is a large buffer for replies
char replybuffer[255];

SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX);
SoftwareSerial *fonaSerial = &fonaSS;


Adafruit_FONA fona = Adafruit_FONA(FONA_RST);

uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout = 0);

void setup() {
  while (!Serial);

  Serial.begin(115200);
  Serial.println(F("GPS SMS Tracking Program"));
  Serial.println(F("Initializing Program, please be patient)"));

  
  fonaSerial->begin(4800);
  if (! fona.begin(*fonaSerial)) {
    Serial.println(F("Unable to locate FONA"));
    while(1);
  }
  Serial.println(F("FONA is ready"));
  
  Serial.println("FONA Ready");
  fona.enableGPS(true);                //This is to enable the GPS module and start to get an initial location
  delay(6000);                         //This delay is to give the module a little time to establish a sattelite fix
pinMode(ledPin, OUTPUT);
}

  
char fonaInBuffer[64];          //for notifications that come from the FONA

void loop() {{
{
int val= analogRead(sensorPin);
if (val >= threshold)
{
   

fona.enableGPS(true);                            //Enable the GPS location so that it refreshes the data before sending
  delay(6000);                                   //Delay in order to get an accurate fix
      
     float latitude, longitude;                  
    fona.getGPS(&latitude, &longitude);

    Serial.print(latitude,5) ; Serial.print("\t") ; 
    Serial.print(longitude,5) ; Serial.print("\t") ; 

  char message[28];
char LAT[9], LONG[9];
dtostrf(latitude, 6, 5, LAT);                      //gathering GPS data in a format that can be sent
dtostrf(longitude, 6, 5, LONG);
sprintf(message, "%s, %s", LAT, LONG);             //showing how the message is to be made up.
Serial.println(message) ;                          //prints the message in the serial monitor before sending
char sendto[13] = "07737775715";
fona.sendSMS(sendto, message) ;            //sends the message via SMS to the number stored in the callerIDbuffer
delay(10000);                                      //delay to ensure task is completed
      
    
}
else 
{

}
}
}
  
 { 
  char* bufPtr = fonaInBuffer; 
  
  if (fona.available())                          //is there any data available from the FONA?
  {
    int slot = 0;                                //this will be the slot number of the SMS
    int charCount = 0;
    
   
    do  {
      *bufPtr = fona.read();                     //This is to read the notification into fonaInBuffer that was setup above
      Serial.write(*bufPtr);
      delay(1);
    } while ((*bufPtr++ != '\n') && (fona.available()) && (++charCount < (sizeof(fonaInBuffer)-1)));
    
    //Add a terminal NULL to the notification string
    *bufPtr = 0;
    
    //Scan the notification string for an SMS received notification.
    //If it's an SMS message, we'll get the slot number in 'slot'
    
    if (1 == sscanf(fonaInBuffer, "+CMTI: \"SM\",%d", &slot)) {
      Serial.print("slot: "); Serial.println(slot);
      
      char callerIDbuffer[32];                    //we'll store the SMS sender number in here, this will be used to respond to
      
      // Retrieve SMS sender phone number.
      
      if (! fona.getSMSSender(slot, callerIDbuffer, 31)) {
        Serial.println("Didn't find SMS message in slot!");
      }
      Serial.print(F("FROM: ")); Serial.println(callerIDbuffer);

fona.enableGPS(true);                            //Enable the GPS location so that it refreshes the data before sending
  delay(6000);                                   //Delay in order to get an accurate fix
      
     float latitude, longitude;                  
    fona.getGPS(&latitude, &longitude);

    Serial.print(latitude,5) ; Serial.print("\t") ; 
    Serial.print(longitude,5) ; Serial.print("\t") ; 

   char message[28];
char LAT[9], LONG[9];
dtostrf(latitude, 6, 5, LAT);                      //gathering GPS data in a format that can be sent
dtostrf(longitude, 6, 5, LONG);
sprintf(message, "%s, %s", LAT, LONG);             //showing how the message is to be made up.
Serial.println(message) ;                          //prints the message in the serial monitor before sending
fona.sendSMS(callerIDbuffer, message) ;            //sends the message via SMS to the number stored in the callerIDbuffer
delay(10000);                                      //delay to ensure task is completed
      
      // This next section is used to delete
      // the SMS from the SIM once it has been processed
      // this is to avoid the SMS slots getting filled up
      
      if (fona.deleteSMS(slot)) {
        Serial.println(F("Deleted OK"));
      } else {
        Serial.println(F("Unable to delete"));
      }
      delay(10000);
    }
  }
 }
}

I have tried in various ways to combine these two so that any vibration will send a warning message and a text sent to the device will respond with GPS coordinates. No matter what I do the response I get for both is the warning, it appears to be ignoring the GPS section.

I have tried the following as an example thinking that it would work:

Code: Select all

const int sensorPin=0;
const int threshold= 50;

#include "Adafruit_FONA.h"
#include <SoftwareSerial.h>

#define FONA_RX 2
#define FONA_TX 3
#define FONA_RST 4

// this is a large buffer for replies
char replybuffer[255];

SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX);
SoftwareSerial *fonaSerial = &fonaSS;

Adafruit_FONA fona = Adafruit_FONA(FONA_RST);

uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout = 0);

void setup()
{
while (!Serial);

  Serial.begin(115200);
  Serial.println(F("FONA SMS caller ID test"));
  Serial.println(F("Initializing....(May take 3 seconds)"));

  // make it slow so its easy to read!
  fonaSerial->begin(4800);
  if (! fona.begin(*fonaSerial)) {
    Serial.println(F("Couldn't find FONA"));
    while(1);
  }
  Serial.println(F("FONA is OK"));


  }
  
  Serial.println("FONA Ready");
  fona.enableGPS(true);                //This is to enable the GPS module and start to get an initial location
  delay(6000);                         //This delay is to give the module a little time to establish a sattelite fix
}

char fonaInBuffer[64];          //for notifications that come from the FONA


void loop()
{
int val= analogRead(sensorPin);
if (val >= threshold)

{
char sendto[13] = "07737775715";
fona.sendSMS(sendto, "ALARM STATUS!!!!  Motion detected, please check my location!") ;

}
else 
{
char* bufPtr = fonaInBuffer; 
  
  if (fona.available())                          //is there any data available from the FONA?
  {
    int slot = 0;                                //this will be the slot number of the SMS
    int charCount = 0;
    
   
    do  {
      *bufPtr = fona.read();                     //This is to read the notification into fonaInBuffer that was setup above
      Serial.write(*bufPtr);
      delay(1);
    } while ((*bufPtr++ != '\n') && (fona.available()) && (++charCount < (sizeof(fonaInBuffer)-1)));
    
    //Add a terminal NULL to the notification string
    *bufPtr = 0;
    
    //Scan the notification string for an SMS received notification.
    //If it's an SMS message, we'll get the slot number in 'slot'
    
    if (1 == sscanf(fonaInBuffer, "+CMTI: \"SM\",%d", &slot)) {
      Serial.print("slot: "); Serial.println(slot);
      
      char callerIDbuffer[32];                    //we'll store the SMS sender number in here, this will be used to respond to
      
      // Retrieve SMS sender phone number.
      
      if (! fona.getSMSSender(slot, callerIDbuffer, 31)) {
        Serial.println("Didn't find SMS message in slot!");
      }
      Serial.print(F("FROM: ")); Serial.println(callerIDbuffer);

fona.enableGPS(true);                            //Enable the GPS location so that it refreshes the data before sending
  delay(6000);                                   //Delay in order to get an accurate fix
      
     float latitude, longitude;                  
    fona.getGPS(&latitude, &longitude);

    Serial.print(latitude,5) ; Serial.print("\t") ; 
    Serial.print(longitude,5) ; Serial.print("\t") ; 

   char message[28];
char LAT[9], LONG[9];
dtostrf(latitude, 6, 5, LAT);                      //gathering GPS data in a format that can be sent
dtostrf(longitude, 6, 5, LONG);
sprintf(message, "%s, %s", LAT, LONG);             //showing how the message is to be made up.
Serial.println(message) ;                          //prints the message in the serial monitor before sending
fona.sendSMS(callerIDbuffer, message) ;            //sends the message via SMS to the number stored in the callerIDbuffer
delay(10000);                                      //delay to ensure task is completed
      
      // This next section is used to delete
      // the SMS from the SIM once it has been processed
      // this is to avoid the SMS slots getting filled up
      
      if (fona.deleteSMS(slot)) {
        Serial.println(F("Deleted OK"));
      } else {
        Serial.println(F("Unable to delete"));
      }
      delay(10000);
    }
  }
}
} 

Any help would be very much appreciated.

Thanks

Kev

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Fona808 SMS message from alarm

Post by adafruit_support_rick »

You need some debug statements.

Start by printing out fonaInBuffer before you check for CMTI:

Code: Select all

      //Scan the notification string for an SMS received notification.
      //If it's an SMS message, we'll get the slot number in 'slot'
      Serial.print("fonaInBuffer: "); Serial.print(fonaInBuffer);  //debug
      if (1 == sscanf(fonaInBuffer, "+CMTI: \"SM\",%d", &slot)) {
        Serial.print("slot: "); Serial.println(slot);
You're probably not getting the string you think you're getting.
Also, you need to disable GPS after you send your test.
Also, you don't want to do delay(10000) at the end of the loop. Just let the loop run.

User avatar
isaac47
 
Posts: 2
Joined: Sat Apr 02, 2016 5:33 pm

Re: Fona808 SMS message from alarm

Post by isaac47 »

Hi to all,

here is my problem.I sent this two commands and I want to store the result of AT+CPBR in a variable on my arduino code.how can't I do that.

sendATcommand("AT+CPBS=\"SM\"", 500) ; //Select the SIM phonebook

sendATcommand("AT+CPBR=1,99", 100) ; // To read ALL phonebook example of result : +CPBR:1,"690506990",129,"ANDROID"

When i sent AT+CPBR ,the serial monitor reply me +CPBR:1,"690506990",129,"ANDROID" and my problem is to store all this answer or specialy the phone number and use it in my code.

please help me!!

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Fona808 SMS message from alarm

Post by adafruit_support_rick »

isaac47 - we're already helping someone else in this thread. Please start a new thread.

User avatar
isaac47
 
Posts: 2
Joined: Sat Apr 02, 2016 5:33 pm

Re: Fona808 SMS message from alarm

Post by isaac47 »

allright?please can you sent me the link?because I don't see it.

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Fona808 SMS message from alarm

Post by adafruit_support_rick »

viewforum.php?f=25
Click 'Post A Topic'

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”