• Could it be that Start_GPS is getting called twice, so is then overwriting the value from SearchDuration? In code that I write I tend to explicitly check and clear the interval before I set it.

    Also, I'd try and call require('GPS').connect only once - as otherwise every hour it'll add another GPS handler and will probably run out of memory - even if you set gps to "", the handler will still be receiving data from Serial4.

    Just a quick JS thing too - if you say var gps in a function, that'll create a local variable rather than using a global one. So for example if you write var gps = ...; in a function, when you leave the function the reference to gps will be lost (although it'll still keep working).

    Maybe try:

    Serial4.setup(9600,{tx:C10,rx:C11});
    var SleepDuration = null;
    var SearchDuration = null;
    var foundFix = 0;
    var gps = undefined;
    
    function Start_GPS(){
      
      console.log("Starting GPS...");
      //Reset fix found
      foundFix=0;
      
      //After 120 seconds disable the GPS regardless
      if (SearchDuration) clearTimeout(SearchDuration);
      SearchDuration = setTimeout(function (e) { SearchDuration=undefined; Stop_GPS(); }, 60000);
     
      //Set enable pin mode
      pinMode(A9,'output');
      
      //Set enable pin to high
      digitalWrite(A9,1);
    }
    
    function Stop_GPS(){
      if (SearchDuration) {
        clearTimeout(SearchDuration);
        SearchDuration = undefined;
      }
      digitalWrite(A9,0);
      console.log("Disabling GPS...");
    }
    
    function onInit(){
      //load gps
        gps = require("GPS").connect(Serial4, function(data) {
        console.log(data);
        //Check if we have a fix
          if(data.fix>0)
          {
            console.log("Send SMS:" + data.lat + "|" + data.lon);
            foundFix=1;
            Stop_GPS();
          }
      });
    console.log("Starting up device & Setting timers...");
    setInterval(function (e) { Start_GPS(); }, 360000);
    Start_GPS();
    }
    onInit();
    
About

Avatar for Gordon @Gordon started