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);
}
}
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