Skip to content

Commit edbb88c

Browse files
committed
Combine bluetooth start functions. Remove BT radio start/stop for WiFi.
1 parent 09378c8 commit edbb88c

File tree

4 files changed

+98
-106
lines changed

4 files changed

+98
-106
lines changed

Firmware/RTK_Surveyor/Base.ino

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ bool updateNtripServer()
290290
//Turn off Bluetooth and turn on WiFi
291291
endBluetooth();
292292

293-
Serial.printf("Connecting to local WiFi: %s\n", settings.wifiSSID);
293+
Serial.printf("Connecting to local WiFi: %s", settings.wifiSSID);
294294
WiFi.begin(settings.wifiSSID, settings.wifiPW);
295295

296296
int maxTime = 10000;
@@ -304,8 +304,10 @@ bool updateNtripServer()
304304
Serial.println(F("\nFailed to connect to WiFi. Are you sure your WiFi credentials are correct?"));
305305
return (false);
306306
}
307+
308+
if(Serial.available()) return(false); //User has pressed a key
307309
}
308-
delay(10);
310+
Serial.println();
309311

310312
radioState = WIFI_CONNECTED;
311313
} //End WiFi connect check
@@ -375,7 +377,7 @@ bool updateNtripServer()
375377
} //End attempt to connect
376378
else
377379
{
378-
Serial.println(F("Failed to connect to caster"));
380+
Serial.println(F("Failed to connect to caster. Are you sure your Caster address is correct?"));
379381
delay(10); //Give RTOS some time
380382
return (false);
381383
}

Firmware/RTK_Surveyor/RTK_Surveyor.ino

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,9 @@
2929
(Done) Base - Enter fixed coordinates, survey-in settings
3030
(Done) Ports - Configure Radio and Data port baud rates
3131
(Done) Test menu
32+
(Done) Firmware upgrade menu
3233
Enable various debug outputs sent over BT
3334
34-
when user exits wifi mode, turn BT back on
35-
3635
*/
3736

3837
const int FIRMWARE_VERSION_MAJOR = 1;
@@ -194,7 +193,6 @@ const byte menuTimeout = 15; //Menus will exit/timeout after this number of seco
194193
bool inTestMode = false; //Used to re-route BT traffic while in test sub menu
195194
long systemTime_minutes = 0; //Used to test if logging is less than max minutes
196195

197-
uint32_t lastBluetoothLEDBlink = 0;
198196
uint32_t lastRoverUpdate = 0;
199197
uint32_t lastBaseUpdate = 0;
200198
uint32_t lastBattUpdate = 0;
@@ -252,7 +250,7 @@ void setup()
252250
beginFuelGauge(); //Configure battery fuel guage monitor
253251
checkBatteryLevels(); //Force display so you see battery level immediately at power on
254252

255-
beginBT(); //Get MAC, start radio
253+
beginBluetooth(); //Get MAC, start radio
256254

257255
beginGNSS(); //Connect and configure ZED-F9P
258256

@@ -289,7 +287,7 @@ void loop()
289287

290288
baseState = BASE_OFF;
291289

292-
startBluetooth(); //Restart Bluetooth with 'Rover' name
290+
beginBluetooth(); //Restart Bluetooth with 'Rover' name
293291

294292
//If we are survey'd in, but switch is rover then disable survey
295293
if (configureUbloxModuleRover() == false)
@@ -311,7 +309,7 @@ void loop()
311309

312310
//Restart Bluetooth with 'Base' name
313311
//We start BT regardless of Ntrip Server in case user wants to transmit survey-in stats over BT
314-
startBluetooth();
312+
beginBluetooth();
315313

316314
baseState = BASE_SURVEYING_IN_NOTSTARTED; //Switch to new state
317315
}

Firmware/RTK_Surveyor/System.ino

Lines changed: 26 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,32 @@
1-
//Tack device's MAC address to end of friendly broadcast name
2-
//This allows multiple units to be on at same time
3-
bool startBluetooth()
4-
{
5-
//Shutdown any previous WiFi
6-
caster.stop();
7-
WiFi.mode(WIFI_OFF);
8-
radioState = RADIO_OFF;
9-
10-
btStart();
11-
12-
if (digitalRead(baseSwitch) == HIGH)
13-
{
14-
//Rover mode
15-
sprintf(deviceName, "Surveyor Rover-%02X%02X", unitMACAddress[4], unitMACAddress[5]);
16-
}
17-
else
18-
{
19-
//Base mode
20-
sprintf(deviceName, "Surveyor Base-%02X%02X", unitMACAddress[4], unitMACAddress[5]);
21-
}
22-
23-
if (SerialBT.begin(deviceName) == false)
24-
{
25-
Serial.println(F("An error occurred initializing Bluetooth"));
26-
radioState = RADIO_OFF;
27-
digitalWrite(bluetoothStatusLED, LOW);
28-
return (false);
29-
}
30-
31-
Serial.print(F("Bluetooth broadcasting as: "));
32-
Serial.println(deviceName);
33-
34-
radioState = BT_ON_NOCONNECTION;
35-
digitalWrite(bluetoothStatusLED, HIGH);
36-
lastBluetoothLEDBlink = millis();
37-
38-
//Start the tasks for handling incoming and outgoing BT bytes to/from ZED-F9P
39-
//Reduced stack size from 10,000 to 1,000 to make room for WiFi/NTRIP server capabilities
40-
xTaskCreate(F9PSerialReadTask, "F9Read", 1000, NULL, 0, &F9PSerialReadTaskHandle);
41-
xTaskCreate(F9PSerialWriteTask, "F9Write", 1000, NULL, 0, &F9PSerialWriteTaskHandle);
42-
43-
SerialBT.setTimeout(1);
44-
45-
return (true);
46-
}
47-
48-
//Turn off BT so we can go into WiFi mode
49-
bool endBluetooth()
50-
{
51-
//Kill tasks if running
52-
if (F9PSerialReadTaskHandle != NULL)
53-
{
54-
vTaskDelete(F9PSerialReadTaskHandle);
55-
F9PSerialReadTaskHandle = NULL;
56-
}
57-
if (F9PSerialWriteTaskHandle != NULL)
58-
{
59-
vTaskDelete(F9PSerialWriteTaskHandle);
60-
F9PSerialWriteTaskHandle = NULL;
61-
}
62-
63-
//SerialBT.end();
64-
customBTstop(); //Gracefully turn off Bluetooth so we can turn it back on if needed
65-
66-
Serial.println(F("Bluetooth turned off"));
67-
}
68-
691
//Starting and restarting BT is a problem. See issue: https://github.com/espressif/arduino-esp32/issues/2718
702
//To work around the bug without modifying the core we create our own btStop() function with
713
//the patch from github
72-
bool customBTstop() {
73-
if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_IDLE) {
74-
return true;
75-
}
76-
if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_ENABLED) {
77-
if (esp_bt_controller_disable()) {
78-
log_e("BT Disable failed");
79-
return false;
80-
}
81-
while (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_ENABLED);
82-
}
83-
if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_INITED)
84-
{
85-
log_i("inited");
86-
if (esp_bt_controller_deinit())
87-
{
88-
log_e("BT deint failed");
89-
return false;
90-
}
91-
while (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_INITED)
92-
;
93-
return true;
94-
}
95-
log_e("BT Stop failed");
96-
return false;
97-
}
4+
//bool customBTstop() {
5+
// if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_IDLE) {
6+
// return true;
7+
// }
8+
// if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_ENABLED) {
9+
// if (esp_bt_controller_disable()) {
10+
// log_e("BT Disable failed");
11+
// return false;
12+
// }
13+
// while (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_ENABLED);
14+
// }
15+
// if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_INITED)
16+
// {
17+
// log_i("inited");
18+
// if (esp_bt_controller_deinit())
19+
// {
20+
// log_e("BT deint failed");
21+
// return false;
22+
// }
23+
// while (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_INITED)
24+
// ;
25+
// return true;
26+
// }
27+
// log_e("BT Stop failed");
28+
// return false;
29+
//}
9830

9931
//If the phone has any new data (NTRIP RTCM, etc), read it in over Bluetooth and pass along to ZED
10032
//Task for writing to the GNSS receiver
@@ -641,7 +573,6 @@ void btCallback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) {
641573
Serial.println(F("Client disconnected"));
642574
radioState = BT_ON_NOCONNECTION;
643575
digitalWrite(bluetoothStatusLED, LOW);
644-
lastBluetoothLEDBlink = millis();
645576
}
646577
}
647578

Firmware/RTK_Surveyor/begin.ino

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,14 +153,75 @@ void beginGNSS()
153153
}
154154

155155
//Get MAC, start radio
156-
void beginBT()
156+
//Tack device's MAC address to end of friendly broadcast name
157+
//This allows multiple units to be on at same time
158+
bool beginBluetooth()
157159
{
160+
//Shutdown any previous WiFi
161+
caster.stop();
162+
WiFi.mode(WIFI_OFF);
163+
radioState = RADIO_OFF;
164+
165+
//Due to a known issue, you cannot call esp_bt_controller_enable() a second time
166+
//to change the controller mode dynamically. To change controller mode, call
167+
//esp_bt_controller_disable() and then call esp_bt_controller_enable() with the new mode.
168+
169+
//btStart(); //In v1.1 we do not turn off or on the bt radio. See WiFiBluetooSwitch sketch for more info:
170+
158171
//Get unit MAC address
159172
esp_read_mac(unitMACAddress, ESP_MAC_WIFI_STA);
160173
unitMACAddress[5] += 2; //Convert MAC address to Bluetooth MAC (add 2): https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/system.html#mac-address
161174

175+
if (digitalRead(baseSwitch) == HIGH)
176+
sprintf(deviceName, "Surveyor Rover-%02X%02X", unitMACAddress[4], unitMACAddress[5]); //Rover mode
177+
else
178+
sprintf(deviceName, "Surveyor Base-%02X%02X", unitMACAddress[4], unitMACAddress[5]); //Base mode
179+
180+
if (SerialBT.begin(deviceName) == false)
181+
{
182+
Serial.println(F("An error occurred initializing Bluetooth"));
183+
radioState = RADIO_OFF;
184+
digitalWrite(bluetoothStatusLED, LOW);
185+
return (false);
186+
}
162187
SerialBT.register_callback(btCallback);
163-
startBluetooth();
188+
SerialBT.setTimeout(1);
189+
190+
Serial.print(F("Bluetooth broadcasting as: "));
191+
Serial.println(deviceName);
192+
193+
radioState = BT_ON_NOCONNECTION;
194+
digitalWrite(bluetoothStatusLED, HIGH);
195+
196+
//Start the tasks for handling incoming and outgoing BT bytes to/from ZED-F9P
197+
//Reduced stack size from 10,000 to 1,000 to make room for WiFi/NTRIP server capabilities
198+
if(F9PSerialReadTaskHandle == NULL) xTaskCreate(F9PSerialReadTask, "F9Read", 1000, NULL, 0, &F9PSerialReadTaskHandle);
199+
if(F9PSerialWriteTaskHandle == NULL) xTaskCreate(F9PSerialWriteTask, "F9Write", 1000, NULL, 0, &F9PSerialWriteTaskHandle);
200+
201+
return (true);
202+
}
203+
204+
//Turn off BT so we can go into WiFi mode
205+
bool endBluetooth()
206+
{
207+
//Delete tasks if running
208+
// if (F9PSerialReadTaskHandle != NULL)
209+
// {
210+
// vTaskDelete(F9PSerialReadTaskHandle);
211+
// F9PSerialReadTaskHandle = NULL;
212+
// }
213+
// if (F9PSerialWriteTaskHandle != NULL)
214+
// {
215+
// vTaskDelete(F9PSerialWriteTaskHandle);
216+
// F9PSerialWriteTaskHandle = NULL;
217+
// }
218+
219+
SerialBT.flush();
220+
SerialBT.disconnect();
221+
//SerialBT.end(); //Do not call both end and custom stop
222+
//customBTstop(); //Gracefully turn off Bluetooth so we can turn it back on if needed
223+
224+
Serial.println(F("Bluetooth turned off"));
164225
}
165226

166227
//Set LEDs for output and configure PWM

0 commit comments

Comments
 (0)