1
1
// Get MAC, start radio
2
2
// Tack device's MAC address to end of friendly broadcast name
3
3
// This allows multiple units to be on at same time
4
- bool startBluetooth ()
4
+ void startBluetooth ()
5
5
{
6
+ if (radioState == WIFI_ON_NOCONNECTION || radioState == WIFI_CONNECTED)
7
+ stopWiFi ();
8
+
9
+ if (radioState == RADIO_OFF)
10
+ {
6
11
#ifdef COMPILE_BT
7
- char stateName[10 ];
8
- if (buttonPreviousState == BUTTON_ROVER)
9
- strcpy (stateName, " Rover" );
10
- else
11
- strcpy (stateName, " Base" );
12
+ char stateName[10 ];
13
+ if (buttonPreviousState == BUTTON_ROVER)
14
+ strcpy (stateName, " Rover" );
15
+ else
16
+ strcpy (stateName, " Base" );
12
17
13
- sprintf (deviceName, " %s %s-%02X%02X" , platformPrefix, stateName, unitMACAddress[4 ], unitMACAddress[5 ]); // Base mode
18
+ sprintf (deviceName, " %s %s-%02X%02X" , platformPrefix, stateName, unitMACAddress[4 ], unitMACAddress[5 ]); // Base mode
14
19
15
- if (SerialBT.begin (deviceName, false , settings.sppRxQueueSize , settings.sppTxQueueSize ) == false ) // localName, isMaster, rxBufferSize, txBufferSize
16
- {
17
- Serial.println (F (" An error occurred initializing Bluetooth" ));
18
- radioState = RADIO_OFF;
20
+ // if (SerialBT.begin(deviceName, false, settings.sppRxQueueSize, settings.sppTxQueueSize) == false) //localName, isMaster, rxBufferSize, txBufferSize
21
+ if (SerialBT.begin (deviceName, false ) == false ) // localName, isMaster
22
+ {
23
+ Serial.println (F (" An error occurred initializing Bluetooth" ));
24
+ radioState = RADIO_OFF;
19
25
20
- if (productVariant == RTK_SURVEYOR)
21
- digitalWrite (pin_bluetoothStatusLED, LOW);
22
- return ( false ) ;
23
- }
26
+ if (productVariant == RTK_SURVEYOR)
27
+ digitalWrite (pin_bluetoothStatusLED, LOW);
28
+ return ;
29
+ }
24
30
25
- // Set PIN to 1234 so we can connect to older BT devices, but not require a PIN for modern device pairing
26
- // See issue: https://github.com/sparkfun/SparkFun_RTK_Firmware/issues/5
27
- // https://github.com/espressif/esp-idf/issues/1541
28
- // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
29
- esp_bt_sp_param_t param_type = ESP_BT_SP_IOCAP_MODE;
31
+ // Set PIN to 1234 so we can connect to older BT devices, but not require a PIN for modern device pairing
32
+ // See issue: https://github.com/sparkfun/SparkFun_RTK_Firmware/issues/5
33
+ // https://github.com/espressif/esp-idf/issues/1541
34
+ // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
35
+ esp_bt_sp_param_t param_type = ESP_BT_SP_IOCAP_MODE;
30
36
31
- esp_bt_io_cap_t iocap = ESP_BT_IO_CAP_NONE; // Requires pin 1234 on old BT dongle, No prompt on new BT dongle
32
- // esp_bt_io_cap_t iocap = ESP_BT_IO_CAP_OUT; //Works but prompts for either pin (old) or 'Does this 6 pin appear on the device?' (new)
37
+ esp_bt_io_cap_t iocap = ESP_BT_IO_CAP_NONE; // Requires pin 1234 on old BT dongle, No prompt on new BT dongle
38
+ // esp_bt_io_cap_t iocap = ESP_BT_IO_CAP_OUT; //Works but prompts for either pin (old) or 'Does this 6 pin appear on the device?' (new)
33
39
34
- esp_bt_gap_set_security_param (param_type, &iocap, sizeof (uint8_t ));
40
+ esp_bt_gap_set_security_param (param_type, &iocap, sizeof (uint8_t ));
35
41
36
- esp_bt_pin_type_t pin_type = ESP_BT_PIN_TYPE_FIXED;
37
- esp_bt_pin_code_t pin_code;
38
- pin_code[0 ] = ' 1' ;
39
- pin_code[1 ] = ' 2' ;
40
- pin_code[2 ] = ' 3' ;
41
- pin_code[3 ] = ' 4' ;
42
- esp_bt_gap_set_pin (pin_type, 4 , pin_code);
43
- // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
42
+ esp_bt_pin_type_t pin_type = ESP_BT_PIN_TYPE_FIXED;
43
+ esp_bt_pin_code_t pin_code;
44
+ pin_code[0 ] = ' 1' ;
45
+ pin_code[1 ] = ' 2' ;
46
+ pin_code[2 ] = ' 3' ;
47
+ pin_code[3 ] = ' 4' ;
48
+ esp_bt_gap_set_pin (pin_type, 4 , pin_code);
49
+ // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
44
50
45
- SerialBT.register_callback (btCallback); // Controls BT Status LED on Surveyor
46
- SerialBT.setTimeout (250 );
51
+ SerialBT.register_callback (btCallback); // Controls BT Status LED on Surveyor
52
+ SerialBT.setTimeout (250 );
47
53
48
- Serial.print (F (" Bluetooth broadcasting as: " ));
49
- Serial.println (deviceName);
54
+ Serial.print (F (" Bluetooth broadcasting as: " ));
55
+ Serial.println (deviceName);
50
56
51
- radioState = BT_ON_NOCONNECTION;
57
+ // Start task for controlling Bluetooth pair LED
58
+ if (productVariant == RTK_SURVEYOR)
59
+ {
60
+ ledcWrite (ledBTChannel, 255 ); // Turn on BT LED
61
+ btLEDTask.detach (); // Slow down the BT LED blinker task
62
+ btLEDTask.attach (btLEDTaskPace2Hz, updateBTled); // Rate in seconds, callback
63
+ }
64
+ #endif
52
65
53
- // Start task for controlling Bluetooth pair LED
54
- if (productVariant == RTK_SURVEYOR)
55
- {
56
- ledcWrite (ledBTChannel, 255 ); // Turn on BT LED
57
- btLEDTask.detach (); // Slow down the BT LED blinker task
58
- btLEDTask.attach (btLEDTaskPace2Hz, updateBTled); // Rate in seconds, callback
66
+ radioState = BT_ON_NOCONNECTION;
59
67
}
60
- #endif
61
68
62
- return (true );
63
69
}
64
70
65
71
// This function stops BT so that it can be restarted later
66
72
// It also releases as much system resources as possible so that WiFi/caster is more stable
67
73
void stopBluetooth ()
68
74
{
75
+ if (radioState == BT_ON_NOCONNECTION || radioState == BT_CONNECTED)
76
+ {
69
77
#ifdef COMPILE_BT
70
- SerialBT.flush (); // Complete any transfers
71
- SerialBT.disconnect (); // Drop any clients
72
- SerialBT.end (); // SerialBT.end() will release significant RAM (~100k!) but a SerialBT.start will crash.
78
+ SerialBT.register_callback (NULL );
79
+ SerialBT.flush (); // Complete any transfers
80
+ SerialBT.disconnect (); // Drop any clients
81
+ SerialBT.end (); // SerialBT.end() will release significant RAM (~100k!) but a SerialBT.start will crash.
73
82
#endif
74
83
75
- // The following code releases the BT hardware so that it can be restarted with a SerialBT.begin
76
- customBTstop ();
77
- Serial.println (F (" Bluetooth turned off" ));
78
-
79
- radioState = RADIO_OFF;
80
- }
81
-
82
- // Starting and restarting BT is a problem. See issue: https://github.com/espressif/arduino-esp32/issues/2718
83
- // To work around the bug without modifying the core we create our own btStop() function with
84
- // the patch from github
85
- bool customBTstop () {
86
- #ifdef COMPILE_BT
87
- if (esp_bt_controller_get_status () == ESP_BT_CONTROLLER_STATUS_IDLE) {
88
- return true ;
89
- }
90
- if (esp_bt_controller_get_status () == ESP_BT_CONTROLLER_STATUS_ENABLED) {
91
- if (esp_bt_controller_disable ()) {
92
- log_e (" BT Disable failed" );
93
- return false ;
94
- }
95
- while (esp_bt_controller_get_status () == ESP_BT_CONTROLLER_STATUS_ENABLED);
96
- }
97
- if (esp_bt_controller_get_status () == ESP_BT_CONTROLLER_STATUS_INITED)
98
- {
99
- log_i (" inited" );
100
- if (esp_bt_controller_deinit ())
101
- {
102
- log_e (" BT deint failed" );
103
- return false ;
104
- }
105
- while (esp_bt_controller_get_status () == ESP_BT_CONTROLLER_STATUS_INITED)
106
- ;
107
- return true ;
84
+ log_d (" Bluetooth turned off" );
85
+ radioState = RADIO_OFF;
108
86
}
109
- log_e (" BT Stop failed" );
110
- #endif
111
- return false ;
112
87
}
113
88
114
- // Start WiFi assuming it was previously fully released
115
- // See WiFiBluetoothSwitch sketch for more info
116
- void startServerWiFi ()
89
+ void startWiFi (char * ssid, char * pw)
117
90
{
118
- #ifdef COMPILE_WIFI
119
- wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT ();
120
- esp_wifi_init (&wifi_init_config); // Restart WiFi resources
121
-
122
- Serial.printf (" Connecting to local WiFi: %s\n\r " , settings.ntripServer_wifiSSID );
123
- WiFi.begin (settings.ntripServer_wifiSSID , settings.ntripServer_wifiPW );
124
- #endif
91
+ if (radioState == BT_ON_NOCONNECTION || radioState == BT_CONNECTED)
92
+ stopBluetooth ();
125
93
126
- radioState = WIFI_ON_NOCONNECTION;
127
- }
128
-
129
- void startClientWiFi ()
130
- {
131
- #ifdef COMPILE_WIFI
132
- wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT ();
133
- esp_wifi_init (&wifi_init_config); // Restart WiFi resources
134
-
135
- Serial.printf (" Connecting to local WiFi: %s\n\r " , settings.ntripClient_wifiSSID );
136
- WiFi.begin (settings.ntripClient_wifiSSID , settings.ntripClient_wifiPW );
137
- #endif
138
-
139
- radioState = WIFI_ON_NOCONNECTION;
140
- }
141
-
142
- void startHomeWiFi ()
143
- {
94
+ if (radioState == RADIO_OFF)
95
+ {
144
96
#ifdef COMPILE_WIFI
145
- wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT ();
146
- esp_wifi_init (&wifi_init_config); // Restart WiFi resources
147
-
148
- Serial.printf (" Connecting to home WiFi: %s" , settings.home_wifiSSID );
149
- WiFi.begin (settings.home_wifiSSID , settings.home_wifiPW );
97
+ Serial.printf (" Connecting to WiFi: %s" , ssid);
98
+ WiFi.begin (ssid, pw);
150
99
#endif
151
100
152
- radioState = WIFI_ON_NOCONNECTION;
101
+ radioState = WIFI_ON_NOCONNECTION;
102
+ }
153
103
}
154
104
155
105
// Stop WiFi and release all resources
156
106
// See WiFiBluetoothSwitch sketch for more info
157
107
void stopWiFi ()
158
108
{
109
+ if (radioState == WIFI_ON_NOCONNECTION || radioState == WIFI_CONNECTED)
110
+ {
159
111
#ifdef COMPILE_WIFI
160
112
ntripServer.stop ();
161
113
WiFi.mode (WIFI_OFF);
162
-
163
- if (radioState == WIFI_ON_NOCONNECTION || radioState == WIFI_CONNECTED)
164
- esp_wifi_deinit (); // Free all resources
165
114
#endif
166
115
167
116
log_d (" WiFi Stopped" );
168
-
169
117
radioState = RADIO_OFF;
118
+ }
170
119
}
171
120
172
121
// Setup the u-blox module for any setup (base or rover)
@@ -180,12 +129,12 @@ bool configureUbloxModule()
180
129
int maxWait = 2000 ;
181
130
182
131
// Wait for initial report from module
183
- while (pvtUpdated == false )
132
+ while (pvtUpdated == false )
184
133
{
185
134
i2cGNSS.checkUblox (); // Regularly poll to get latest data and any RTCM
186
135
i2cGNSS.checkCallbacks (); // Process any callbacks: ie, eventTriggerReceived
187
136
delay (10 );
188
- if (millis () - startTime > maxWait) break ;
137
+ if (millis () - startTime > maxWait) break ;
189
138
}
190
139
191
140
// The first thing we do is go to 1Hz to lighten any I2C traffic from a previous configuration
@@ -494,7 +443,7 @@ void danceLEDs()
494
443
else
495
444
{
496
445
// Units can boot under 1s. Keep splash screen up for at least 2s.
497
- while (millis () - splashStart < 2000 ) delay (1 );
446
+ while (millis () - splashStart < 2000 ) delay (1 );
498
447
}
499
448
}
500
449
0 commit comments