@@ -47,10 +47,23 @@ static Adafruit_INA3221 INA3221;
4747static Adafruit_INA219 INA219 (TELEM_INA219_ADDRESS);
4848#endif
4949
50+ #if ENV_INCLUDE_GPS && RAK_BOARD
51+ static uint32_t gpsResetPin = 0 ;
52+ static bool i2cGPSFlag = false ;
53+ static bool serialGPSFlag = false ;
54+ #define TELEM_RAK12500_ADDRESS 0x42 // RAK12500 Ublox GPS via i2c
55+ #include < SparkFun_u-blox_GNSS_Arduino_Library.h>
56+ static SFE_UBLOX_GNSS ublox_GNSS;
57+ #endif
58+
5059bool EnvironmentSensorManager::begin () {
5160 #if ENV_INCLUDE_GPS
61+ #if RAK_BOARD
62+ rakGPSInit (); // probe base board/sockets for GPS
63+ #else
5264 initBasicGPS ();
5365 #endif
66+ #endif
5467
5568 #if ENV_INCLUDE_AHTX0
5669 if (AHTX0.begin (&Wire, 0 , TELEM_AHTX_ADDRESS)) {
@@ -296,8 +309,85 @@ void EnvironmentSensorManager::initBasicGPS() {
296309 gps_active = false ; // Set GPS visibility off until setting is changed
297310}
298311
312+ #ifdef RAK_BOARD
313+ void EnvironmentSensorManager::rakGPSInit (){
314+
315+ Serial1.setPins (PIN_GPS_TX, PIN_GPS_RX);
316+
317+ #ifdef GPS_BAUD_RATE
318+ Serial1.begin (GPS_BAUD_RATE);
319+ #else
320+ Serial1.begin (9600 );
321+ #endif
322+
323+ // search for the correct IO standby pin depending on socket used
324+ if (gpsIsAwake (WB_IO2)){
325+ // MESH_DEBUG_PRINTLN("RAK base board is RAK19007/10");
326+ // MESH_DEBUG_PRINTLN("GPS is installed on Socket A");
327+ }
328+ else if (gpsIsAwake (WB_IO4)){
329+ // MESH_DEBUG_PRINTLN("RAK base board is RAK19003/9");
330+ // MESH_DEBUG_PRINTLN("GPS is installed on Socket C");
331+ }
332+ else if (gpsIsAwake (WB_IO5)){
333+ // MESH_DEBUG_PRINTLN("RAK base board is RAK19001/11");
334+ // MESH_DEBUG_PRINTLN("GPS is installed on Socket F");
335+ }
336+ else {
337+ MESH_DEBUG_PRINTLN (" No GPS found" );
338+ gps_active = false ;
339+ gps_detected = false ;
340+ return ;
341+ }
342+
343+ #ifndef FORCE_GPS_ALIVE // for use with repeaters, until GPS toggle is implimented
344+ // Now that GPS is found and set up, set to sleep for initial state
345+ stop_gps ();
346+ #endif
347+ }
348+
349+ bool EnvironmentSensorManager::gpsIsAwake (uint8_t ioPin){
350+
351+ // set initial waking state
352+ pinMode (ioPin,OUTPUT);
353+ digitalWrite (ioPin,LOW);
354+ delay (500 );
355+ digitalWrite (ioPin,HIGH);
356+ delay (500 );
357+
358+ // Try to init RAK12500 on I2C
359+ if (ublox_GNSS.begin (Wire) == true ){
360+ MESH_DEBUG_PRINTLN (" RAK12500 GPS init correctly with pin %i" ,ioPin);
361+ ublox_GNSS.setI2COutput (COM_TYPE_NMEA);
362+ ublox_GNSS.saveConfigSelective (VAL_CFG_SUBSEC_IOPORT);
363+ gpsResetPin = ioPin;
364+ i2cGPSFlag = true ;
365+ gps_active = true ;
366+ gps_detected = true ;
367+ return true ;
368+ }
369+ else if (Serial1){
370+ MESH_DEBUG_PRINTLN (" Serial GPS init correctly and is turned on" );
371+ if (PIN_GPS_EN){
372+ gpsResetPin = PIN_GPS_EN;
373+ }
374+ serialGPSFlag = true ;
375+ gps_active = true ;
376+ gps_detected = true ;
377+ return true ;
378+ }
379+ MESH_DEBUG_PRINTLN (" GPS did not init with this IO pin... try the next" );
380+ return false ;
381+ }
382+ #endif
383+
299384void EnvironmentSensorManager::start_gps () {
300385 gps_active = true ;
386+ #ifdef RAK_BOARD
387+ pinMode (gpsResetPin, OUTPUT);
388+ digitalWrite (gpsResetPin, HIGH);
389+ return ;
390+ #endif
301391 #ifdef PIN_GPS_EN
302392 pinMode (PIN_GPS_EN, OUTPUT);
303393 digitalWrite (PIN_GPS_EN, HIGH);
@@ -309,6 +399,11 @@ void EnvironmentSensorManager::start_gps() {
309399
310400void EnvironmentSensorManager::stop_gps () {
311401 gps_active = false ;
402+ #ifdef RAK_BOARD
403+ pinMode (gpsResetPin, OUTPUT);
404+ digitalWrite (gpsResetPin, LOW);
405+ return ;
406+ #endif
312407 #ifdef PIN_GPS_EN
313408 pinMode (PIN_GPS_EN, OUTPUT);
314409 digitalWrite (PIN_GPS_EN, LOW);
@@ -324,11 +419,28 @@ void EnvironmentSensorManager::loop() {
324419 _location->loop ();
325420
326421 if (millis () > next_gps_update) {
327- if (gps_active && _location->isValid ()) {
422+ if (gps_active){
423+ #ifndef RAK_BOARD
424+ if (_location->isValid ()) {
425+ node_lat = ((double )_location->getLatitude ())/1000000 .;
426+ node_lon = ((double )_location->getLongitude ())/1000000 .;
427+ MESH_DEBUG_PRINTLN (" lat %f lon %f" , node_lat, node_lon);
428+ }
429+ #else
430+ if (i2cGPSFlag){
431+ node_lat = ((double )ublox_GNSS.getLatitude ())/10000000 .;
432+ node_lon = ((double )ublox_GNSS.getLongitude ())/10000000 .;
433+ MESH_DEBUG_PRINTLN (" lat %f lon %f" , node_lat, node_lon);
434+ }
435+ else if (serialGPSFlag && _location->isValid ()) {
328436 node_lat = ((double )_location->getLatitude ())/1000000 .;
329437 node_lon = ((double )_location->getLongitude ())/1000000 .;
330438 MESH_DEBUG_PRINTLN (" lat %f lon %f" , node_lat, node_lon);
331439 }
440+ // else
441+ // MESH_DEBUG_PRINTLN("No valid GPS data");
442+ #endif
443+ }
332444 next_gps_update = millis () + 1000 ;
333445 }
334446}
0 commit comments