Integrating gps with nodemcu

#1

Integrate gps module with nodemcu and parse NMEA values .

#2

Currently I am using this… but didn’t get success

load('api_config.js');
load('api_uart.js');
load('api_gpio.js');
load('api_net.js');
load('api_mqtt.js');
load('api_sys.js');
load('api_timer.js');
load('api_esp8266.js');
load('api_gps.js');


let isConnected = false;
let isGPSLocked = false;
let telemetrySend = false;
let deviceName = Cfg.get('device.id');

let gpsStatusPin = 3;

GPIO.set_mode(gpsStatusPin, GPIO.MODE_OUTPUT);

GPIO.write(gpsStatusPin, 0); // Turn off gps led

function getTemp() {
  return (ESP8266.temp() - 32) * 5 / 9;
}

function getParsedLatLon() {
  return GPS.getLocation();
}


_______

let getInfo = function() {
  return JSON.stringify({
    latlon: getParsedLatLon()
  });
};

let topic = 'bus/1';

function publishData() {
  let msg = getInfo();
  let ok = MQTT.pub(topic, msg);
  if (ok) {
    print('Published');
  } else {
    print('Error publishing');
  }
  return ok;
}

// Sleep function, turning off all LEDs and GSM modem.
let updateTimerId = null;
let gpsTimerId = null;
function goToSleep() {
  print('Sleeping');
  Timer.del(gpsTimerId);
  Timer.del(updateTimerId);
  Timer.set(
    8000,
    false,
    function() {
      GPIO.write(gpsStatusPin, 0); // Turn off gps led

      let updateInterval = Cfg.get('app.update_interval');

      ESP32.deepSleep(updateInterval * 1000 * 1000);
    },
    null
  );
}

// Setup a timer that keep track of the time that the device is on but didn't send data yet.
// After some tries it goes to sleep.
let sendTelemetryTries = 0;
function setUpdateTimer() {
  if (updateTimerId) {
    Timer.del(updateTimerId);
  }
  let updateInterval = Cfg.get('app.update_interval');
  print('Setting timer with ', updateInterval, ' seconds interval');
  updateTimerId = Timer.set(
    updateInterval * 1000,
    true,
    function() {
      print('Should send telemetry');
      sendTelemetryTries = sendTelemetryTries + 1;
      if (sendTelemetryTries > 1 && !telemetrySend) {
        goToSleep();
      }
      telemetrySend = false;
    },
    null
  );
}
setUpdateTimer();

// Check if GPS has valid data.
gpsTimerId = Timer.set(
  1000,
  true,
  function() {
    let geo = getParsedLatLon();
    if (geo) {
      isGPSLocked = true;
      GPIO.write(gpsStatusPin, 1);
    } else {
      isGPSLocked = false;
      GPIO.write(gpsStatusPin, 0);
    }
  },
  null
);

// Checks if is connected to mqtt server and GPS is locked
// Then send data thought mqtt and if it's all ok, go to sleep
Timer.set(
  5000,
  true,
  function() {
    if (isConnected && isGPSLocked && !telemetrySend) {
      let ok = publishData();
      telemetrySend = ok;

      if (telemetrySend) {
        goToSleep();
      }
    }
  },
  null
);

// Subscribe to Cloud IoT Core configuration topic

let configTopic = 'bus';

MQTT.sub(
  configTopic,
  function(conn, topic, msg) {
    print('Got config update:', msg.slice(0, 100));
    if (!msg) {
      return;
    }
    let obj = JSON.parse(msg);
    if (obj) {
      Cfg.set({ app: obj });
    }
    setUpdateTimer();
  },
  null
);

// Monitor connection with MQTT server
MQTT.setEventHandler(function(conn, ev) {
  if (ev === MQTT.EV_CONNACK) {
    print('MQTT CONNECTED');
    isConnected = true;
  }
}, null);

// Monitor network connectivity.
Net.setStatusEventHandler(function(ev, arg) {
  let evs = '???';
  if (ev === Net.STATUS_DISCONNECTED) {
    evs = 'DISCONNECTED';
    isConnected = false;
  } else if (ev === Net.STATUS_CONNECTING) {
    evs = 'CONNECTING';
  } else if (ev === Net.STATUS_CONNECTED) {
    evs = 'CONNECTED';
  } else if (ev === Net.STATUS_GOT_IP) {
    evs = 'GOT_IP';
  }
  print('== Net event:', ev, evs);
}, null);
#3

api_gps.js is not part of Mongoose OS.

#4

Their any other way to integrate gps module with nodemcu ??

#5

Integrate a NMEA parser in your application.

#6

I need a way to parse NMEA values from gps module using Mongoose OS.
I didn’t find any Mongoose library to get gps module data , so i went for external source library.
I am using esp8266 board and Neo-6m gps module

#7

Which is that external library?

#8

load(‘api_gps.js’)

#9

1 - This library is for ESP32, you are using ESP8266.

2 - It has some errors in it which will lead to build failure.

3 - ESP8266 UART0 is usually connected to the uart-USB convertor and is used for flashing and console output. You need to receive from the GPS, so you have to connect it to UART0 and move the console output to UART1 (which is only Tx). This might become tricky. As the ESP32 modules are not expensive nowadays, it’s better to switch to ESP32.

#10

Hi @qwertyui,
How did you manage this part?

// Monitor network connectivity.
Net.setStatusEventHandler(function(ev, arg) {
let evs = '???';
if (ev === Net.STATUS_DISCONNECTED) {
  evs = 'DISCONNECTED';
  isConnected = false;
} else if (ev === Net.STATUS_CONNECTING) {
  evs = 'CONNECTING';
} else if (ev === Net.STATUS_CONNECTED) {
  evs = 'CONNECTED';
} else if (ev === Net.STATUS_GOT_IP) {
  evs = 'GOT_IP';
}
print('== Net event:', ev, evs);
}, null); 

Taking a look into api_net.js:

  // Event handler. Expects an object with connect/data/close/event user funcs.
  _evh: function(conn, ev, edata, obj)

But it never fires. Thanks, FBP