I am working on a project which uses geofence to dertemine if an asset has left the geofence and trigger an alert if the asset has left the geofence

// the code isn't working don't know where I'm making a mistake



#include <AltSoftSerial.h>
#include <SoftwareSerial.h>
#include <TinyGPS++.h>
#include <TinyGPSPlus.h>
#define rxGPS  4
#define txGPS   3
SoftwareSerial SIM900A(8,9);


const String PHONE = "+263781840930";

const double fences[1][10][12] = {{{17.529188, 78.361845},
                                   {17.529840, 78.361919},
                                   {17.529934, 78.362197},
                                   {17.530624, 78.362507},
                                   {17.530832, 78.363043},
                                   {17.530999, 78.363451},
                                   {17.530924, 78.363976},
                                   {17.529248, 78.363288},
                                   {17.529101, 78.362858},
                                   {17.529040, 78.362489},}
}; 
                                  
double latitude, longitude;
int sat;
String date;
char lati[12];
char longi[12];
int targetStatus;
int fence;
char cumulativeAngle[12];
int deviceStatus = 0; 

//sms start
boolean send_alert_once = true;

//keep tarck of time After how much time messages are sent
//Tracks the time since last event fired
boolean multiple_sms = false;
unsigned long previousMillis = 0;
unsigned long int previoussecs = 0;
unsigned long int currentsecs = 0;
unsigned long currentMillis = 0;
int secs = 0;
int pmints = 0;
int mints = 0;     // current mints
int interval = 1;  // updated every 1 second
//sms end

 void updateLAtLon();
 void pip();
 

SoftwareSerial gpsSerial(rxGPS, txGPS);
TinyGPSPlus gps;



                              
void setup() {
  Serial.begin(9600);
  gpsSerial.begin(9600);
  Serial.println(F("Mace Systems"));
  Serial.println(F("Livestock Antitheft & Tracking System"));
  Serial.print(F("Testing Fatsoe library v. ")); Serial.println(TinyGPSPlus::libraryVersion());
  Serial.println(F("by LATS"));
  
  SIM900A.println("AT");
  delay(500);
  SIM900A.begin(9600);

  //Check sim ready
  SIM900A.println("AT+CPIN?");
  delay(500);

  //Activate sms text mode
  SIM900A.println("AT+CMGF=1");
  delay(500);

  //Describe how newly arrived sms should be handled
  SIM900A.println("AT+CNMI=1,1,0,0,0");
  delay(500);

 

}

void loop() {
 
  
  while (gpsSerial.available()){
    deviceStatus = 1;
    updateLatLon();
    pip();
  }

  // put your main code here, to run repeatedly:
  
  while(SIM900A.available()){
     Serial.println(SIM900A.readString());
       }
   while(SIM900A.available()){
     Serial.println(Serial.readString());
      }

}
void updateLatLon(){
  Serial.print(gpsSerial.read());
  Serial.print("    ");
  Serial.println(gps.encode(gpsSerial.read()));
  if (gps.encode(gpsSerial.read()))
  {
    Serial.println("LATS Systems");
    sat = gps.satellites.value();
    latitude = gps.location.lat();
    longitude = gps.location.lng();
    dtostrf(latitude,9,7,lati);
    dtostrf(longitude,9,7,longi);
    Serial.print("SATS: ");
    Serial.println(sat);
    Serial.print("LAT: ");
    Serial.println(latitude,6);
    Serial.print("LONG: ");
    Serial.println(longitude,6);
    Serial.print("ALT: ");
    Serial.println(gps.altitude.meters());
    Serial.print("SPEED: ");
    Serial.println(gps.speed.mps());
    Serial.print("Date: ");
    date = String(gps.date.day())+"/"+gps.date.month()+"/"+gps.date.year();
    Serial.println(date);
    Serial.print("Hour: ");
    Serial.print(gps.time.hour()); Serial.print(":");
    Serial.print(gps.time.minute()); Serial.print(":");
    Serial.println(gps.time.second());
    Serial.println("---------------------------");
    Serial.println("Digital defence");
    }
}
void pip(){
 
 int fenceSize = sizeof(fences[fence - 1])/sizeof(fences[fence - 1][0]);
 double vectors[fenceSize][2];
 for(int i = 0; i < fenceSize; i++){
  vectors[i][0] = fences[fence - 1][i][0] - latitude;
  vectors[i][1] = fences[fence - 1][i][1] - longitude;
  }
  double angle = 0;
  double num, den;
  for(int i = 0; i < fenceSize; i++){
    num = (vectors[i%fenceSize][0])*(vectors[(i+1)%fenceSize][0])+ (vectors[i%fenceSize][1])*(vectors[(i+1)%fenceSize][1]);
    den = (sqrt(pow(vectors[i%fenceSize][0],2) + pow(vectors[i%fenceSize][1],2)))*(sqrt(pow(vectors[(i+1)%fenceSize][0],2) + pow(vectors[(i+1)%fenceSize][1],2)));
    angle = angle + (180*acos(num/den)/M_PI);
    }
    dtostrf(angle,9,7,cumulativeAngle);
    if (angle > 355 && angle < 365)
    targetStatus = 1;

    
    else
    targetStatus = 0;

    
}

void sendAlert()
{
 String sms_data;
 sms_data = "The Animal is outiside the fence.\r";
 sms_data += "http://maps.google.com/maps?q=loc:";
 sms_data += String(latitude) + "," + String(longitude);

 //return;
 SIM900A.print("AT+CMGF=1\r");
 delay(1000);
 SIM900A.print("AT+CMGS=\""+PHONE+"\"\r");
 delay(1000);
 SIM900A.print(sms_data);
 delay(100);
 SIM900A.write(0x1A);
 delay(1000);
 Serial.println("Sms has been sent Succeessfully");
 }

I think your mistake was assuming your coding skills were sufficient to allow you to change a complex piece of code. There are several parts which demonstrate confusion and lack of understanding. For example:

const double fences[1][10][12] = {{{17.529188, 78.361845},
                                   {17.529840, 78.361919},
                                   {17.529934, 78.362197},
                                   {17.530624, 78.362507},
                                   {17.530832, 78.363043},
                                   {17.530999, 78.363451},
                                   {17.530924, 78.363976},
                                   {17.529248, 78.363288},
                                   {17.529101, 78.362858},
                                   {17.529040, 78.362489},}
};

and

while(SIM900A.available()){
     Serial.println(Serial.readString());
      }

Your code contains some errors...

Are you sure that you need 1x10x12 triple-layers array? Your code used only 10x2 slice of it

This loops both tests SIM900 serial... Using readSting() is bad idea too.

And this seems to be out of order.... You begin() and then start sending commands...

so how do i fix the errors

This reads a character from the GPS and throws it away by sending it to Serial. It then reads a different character (which may not exist in the buffer yet) and processes that character as part of the GPS data. Since you only process every 2nd character the library won't be able to make sense of the GPS messages.

Leave this project on the shelf for 3~6 months. Start with basic Arduino tutorials, learn how to write simple code, flash LEDs etc. Then bring in your GPS, learn how to write code to take the readings from it. Progress at your own pace learning how to use arrays, how to code more complex algorithms and so on.

Attempting to create code by copying and pasting parts of others sketches without 100% understanding of what each and every line of code is doing will not result in a working sketch. It would be like writing a novel by cutting and pasting random sentences and paragraphs from other author's novels.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.