@@ -42,45 +42,44 @@ void radio_set_tx_power(uint8_t dbm)
4242 radio.setOutputPower (dbm);
4343}
4444
45- void NanoG2UltraSensorManager::start_gps ()
46- {
47- if (!gps_active)
48- {
49- MESH_DEBUG_PRINTLN (" starting GPS" );
50- digitalWrite (PIN_GPS_STANDBY, HIGH);
45+ void NanoG2UltraSensorManager::start_gps () {
46+ MESH_DEBUG_PRINTLN (" Starting GPS" );
47+ if (!gps_active) {
48+ digitalWrite (PIN_GPS_STANDBY, HIGH); // Wake GPS from standby
49+ Serial1.setPins (PIN_GPS_TX, PIN_GPS_RX);
50+ Serial1.begin (9600 );
51+ MESH_DEBUG_PRINTLN (" Waiting for gps to power up" );
52+ delay (1000 );
5153 gps_active = true ;
5254 }
55+ _location->begin ();
5356}
5457
55- void NanoG2UltraSensorManager::stop_gps ()
56- {
57- if (gps_active)
58- {
59- MESH_DEBUG_PRINTLN (" stopping GPS" );
60- digitalWrite (PIN_GPS_STANDBY, LOW);
58+ void NanoG2UltraSensorManager::stop_gps () {
59+ MESH_DEBUG_PRINTLN (" Stopping GPS" );
60+ if (gps_active) {
61+ digitalWrite (PIN_GPS_STANDBY, LOW); // sleep GPS
6162 gps_active = false ;
6263 }
64+ _location->stop ();
6365}
6466
6567bool NanoG2UltraSensorManager::begin ()
6668{
67- Serial1.setPins (PIN_GPS_TX, PIN_GPS_RX); // be sure to tx into rx and rx into tx
68- Serial1.begin (115200 );
69-
70- pinMode (PIN_GPS_STANDBY, OUTPUT);
7169 digitalWrite (PIN_GPS_STANDBY, HIGH); // Wake GPS from standby
72- delay (500 );
73-
74- // We'll consider GPS detected if we see any data on Serial1
75- if (Serial1.available () > 0 )
76- {
77- MESH_DEBUG_PRINTLN (" GPS detected" );
78- }
79- else
80- {
81- MESH_DEBUG_PRINTLN (" No GPS detected" );
70+ Serial1.setPins (PIN_GPS_TX, PIN_GPS_RX);
71+ Serial1.begin (9600 );
72+ MESH_DEBUG_PRINTLN (" Checking GPS switch state" );
73+ delay (1000 );
74+
75+ // Check initial switch state to determine if GPS should be active
76+ if (Serial1.available () > 0 ) {
77+ MESH_DEBUG_PRINTLN (" GPS was on at boot, GPS enabled" );
78+ start_gps ();
79+ } else {
80+ MESH_DEBUG_PRINTLN (" GPS was not on at boot, GPS disabled" );
8281 }
83- digitalWrite (GPS_EN, LOW); // Put GPS back into standby mode
82+
8483 return true ;
8584}
8685
@@ -96,17 +95,24 @@ bool NanoG2UltraSensorManager::querySensors(uint8_t requester_permissions, Cayen
9695void NanoG2UltraSensorManager::loop ()
9796{
9897 static long next_gps_update = 0 ;
98+
99+ if (!gps_active) {
100+ return ; // GPS is not active, skip further processing
101+ }
102+
99103 _location->loop ();
100- if (millis () > next_gps_update && gps_active) // don't bother if gps position is not enabled
101- {
102- if (_location->isValid ())
103- {
104- node_lat = ((double )_location->getLatitude ()) / 1000000 .;
105- node_lon = ((double )_location->getLongitude ()) / 1000000 .;
104+
105+ if (millis () > next_gps_update) {
106+ if (_location->isValid ()) {
107+ node_lat = ((double )_location->getLatitude ())/1000000 .;
108+ node_lon = ((double )_location->getLongitude ())/1000000 .;
106109 node_altitude = ((double )_location->getAltitude ()) / 1000.0 ;
107- MESH_DEBUG_PRINTLN (" lat %f lon %f" , node_lat, node_lon);
110+ MESH_DEBUG_PRINTLN (" VALID location: lat %f lon %f" , node_lat, node_lon);
111+ } else {
112+ MESH_DEBUG_PRINTLN (" INVALID location, waiting for fix" );
108113 }
109- next_gps_update = millis () + (1000 * 60 ); // after initial update, only check every minute TODO: should be configurable
114+ MESH_DEBUG_PRINTLN (" GPS satellites: %d" , _location->satellitesCount ());
115+ next_gps_update = millis () + 1000 ;
110116 }
111117}
112118
@@ -128,14 +134,10 @@ const char *NanoG2UltraSensorManager::getSettingValue(int i) const
128134
129135bool NanoG2UltraSensorManager::setSettingValue (const char *name, const char *value)
130136{
131- if (strcmp (name, " gps" ) == 0 )
132- {
133- if (strcmp (value, " 0" ) == 0 )
134- {
137+ if (strcmp (name, " gps" ) == 0 ) {
138+ if (strcmp (value, " 0" ) == 0 ) {
135139 stop_gps ();
136- }
137- else
138- {
140+ } else {
139141 start_gps ();
140142 }
141143 return true ;
0 commit comments