Skip to content

Commit 27057af

Browse files
author
Nathan Seidle
committed
Add WiFi to Bluetooth switching test sketches
1 parent b3e554d commit 27057af

File tree

2 files changed

+483
-0
lines changed

2 files changed

+483
-0
lines changed
Lines changed: 388 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,388 @@
1+
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// Sketch shows how to switch between WiFi and BlueTooth or use both
16+
// Button is attached between GPIO 0 and GND and modes are switched with each press
17+
18+
//This is all the settings that can be set on RTK Surveyor. It's recorded to NVM and the config file.
19+
struct struct_settings {
20+
bool enableNtripServer = false;
21+
char casterHost[50] = "rtk2go.com";
22+
uint16_t casterPort = 2101;
23+
char mountPoint[50] = "bldr_dwntwn2";
24+
char mountPointPW[50] = "WR5wRo4H";
25+
char wifiSSID[50] = "sparkfun-guest";
26+
char wifiPW[50] = "sparkfun6333";
27+
} settings;
28+
29+
//Connection settings to NTRIP Caster
30+
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
31+
#include <WiFi.h>
32+
WiFiClient caster;
33+
const char * ntrip_server_name = "SparkFun_RTK_Surveyor";
34+
35+
unsigned long lastServerSent_ms = 0; //Time of last data pushed to caster
36+
unsigned long lastServerReport_ms = 0; //Time of last report of caster bytes sent
37+
int maxTimeBeforeHangup_ms = 10000; //If we fail to get a complete RTCM frame after 10s, then disconnect from caster
38+
39+
uint32_t serverBytesSent = 0; //Just a running total
40+
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
41+
42+
//Hardware serial and BT buffers
43+
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
44+
#include "BluetoothSerial.h"
45+
BluetoothSerial SerialBT;
46+
47+
HardwareSerial GPS(2);
48+
#define RXD2 16
49+
#define TXD2 17
50+
51+
#define SERIAL_SIZE_RX 16384 //Using a large buffer. This might be much bigger than needed but the ESP32 has enough RAM
52+
uint8_t rBuffer[SERIAL_SIZE_RX]; //Buffer for reading F9P
53+
uint8_t wBuffer[SERIAL_SIZE_RX]; //Buffer for writing to F9P
54+
TaskHandle_t F9PSerialReadTaskHandle = NULL; //Store handles so that we can kill them if user goes into WiFi NTRIP Server mode
55+
TaskHandle_t F9PSerialWriteTaskHandle = NULL; //Store handles so that we can kill them if user goes into WiFi NTRIP Server mode
56+
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
57+
58+
#include <Wire.h> //Needed for I2C to GNSS
59+
60+
//GNSS configuration
61+
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
62+
#define MAX_PAYLOAD_SIZE 384 // Override MAX_PAYLOAD_SIZE for getModuleInfo which can return up to 348 bytes
63+
64+
#include "SparkFun_Ublox_Arduino_Library.h" //Click here to get the library: http://librarymanager/All#SparkFun_Ublox_GPS
65+
SFE_UBLOX_GPS myGPS;
66+
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
67+
68+
69+
char deviceName[20]; //The serial string that is broadcast. Ex: 'Surveyor Base-BC61'
70+
71+
#define AP_SSID "esp32"
72+
73+
void WiFiEvent(WiFiEvent_t event) {
74+
switch (event) {
75+
case SYSTEM_EVENT_AP_START:
76+
Serial.println("AP Started");
77+
WiFi.softAPsetHostname(AP_SSID);
78+
break;
79+
case SYSTEM_EVENT_AP_STOP:
80+
Serial.println("AP Stopped");
81+
break;
82+
case SYSTEM_EVENT_STA_START:
83+
Serial.println("WiFi STA Started");
84+
WiFi.setHostname(AP_SSID);
85+
break;
86+
case SYSTEM_EVENT_STA_CONNECTED:
87+
Serial.println("WiFi STA Connected");
88+
WiFi.enableIpV6();
89+
break;
90+
case SYSTEM_EVENT_AP_STA_GOT_IP6:
91+
Serial.print("WiFi STA IPv6: ");
92+
Serial.println(WiFi.localIPv6());
93+
break;
94+
case SYSTEM_EVENT_STA_GOT_IP:
95+
Serial.print("WiFi STA IPv4: ");
96+
Serial.println(WiFi.localIP());
97+
break;
98+
case SYSTEM_EVENT_STA_DISCONNECTED:
99+
Serial.println("WiFi STA Disconnected");
100+
break;
101+
case SYSTEM_EVENT_STA_STOP:
102+
Serial.println("WiFi STA Stopped");
103+
break;
104+
default:
105+
break;
106+
}
107+
}
108+
109+
void setup() {
110+
Serial.begin(115200);
111+
delay(100);
112+
113+
Wire.begin();
114+
115+
beginGNSS();
116+
117+
WiFi.onEvent(WiFiEvent);
118+
119+
Serial.print("ESP32 SDK: ");
120+
Serial.println(ESP.getSdkVersion());
121+
Serial.println("Press the button to select the next mode");
122+
}
123+
124+
void loop() {
125+
126+
if (Serial.available())
127+
{
128+
byte incoming = Serial.read();
129+
130+
Serial.println();
131+
Serial.println("1) Start Bluetooth");
132+
Serial.println("2) Start Wifi");
133+
134+
if (incoming == '1')
135+
{
136+
Serial.println("Starting BT");
137+
startBluetooth();
138+
}
139+
else if (incoming == '2')
140+
{
141+
Serial.println("Stopping BT");
142+
btStop();
143+
144+
updateNtripServer();
145+
}
146+
147+
}
148+
149+
delay(100);
150+
151+
if (caster.connected() == true)
152+
{
153+
myGPS.checkUblox();
154+
155+
//Close socket if we don't have new data for 10s
156+
//RTK2Go will ban your IP address if you abuse it. See http://www.rtk2go.com/how-to-get-your-ip-banned/
157+
//So let's not leave the socket open/hanging without data
158+
if (millis() - lastServerSent_ms > maxTimeBeforeHangup_ms)
159+
{
160+
Serial.println(F("RTCM timeout. Disconnecting from caster."));
161+
caster.stop();
162+
}
163+
164+
//Report some statistics every 250
165+
if (millis() - lastServerReport_ms > 250)
166+
{
167+
lastServerReport_ms += 250;
168+
Serial.printf("Total bytes sent to caster: %d\n", serverBytesSent);
169+
}
170+
}
171+
}
172+
173+
//Tack device's MAC address to end of friendly broadcast name
174+
//This allows multiple units to be on at same time
175+
bool startBluetooth()
176+
{
177+
if (caster.connected()) caster.stop();
178+
179+
//Make sure WiFi is off
180+
// if (WiFi.getMode() != WIFI_MODE_NULL)
181+
// {
182+
WiFi.mode(WIFI_OFF);
183+
delay(200);
184+
// }
185+
186+
187+
btStart();
188+
189+
if (SerialBT.begin("Surveyor Name") == false)
190+
return (false);
191+
Serial.print("Bluetooth broadcasting as: ");
192+
Serial.println("Surveyor Name");
193+
194+
//Start the tasks for handling incoming and outgoing BT bytes to/from ZED-F9P
195+
// xTaskCreate(F9PSerialReadTask, "F9Read", 10000, NULL, 0, &F9PSerialReadTaskHandle);
196+
// xTaskCreate(F9PSerialWriteTask, "F9Write", 10000, NULL, 0, &F9PSerialWriteTaskHandle);
197+
//
198+
// SerialBT.setTimeout(1);
199+
200+
return (true);
201+
}
202+
203+
//Call regularly to get data from u-blox module and send out over local WiFi
204+
//We make sure we are connected to WiFi, then
205+
bool updateNtripServer()
206+
{
207+
//Is WiFi setup?
208+
if (WiFi.isConnected() == false)
209+
{
210+
//Turn off Bluetooth and turn on WiFi
211+
endBluetooth();
212+
213+
Serial.printf("Connecting to local WiFi: %s\n", settings.wifiSSID);
214+
WiFi.begin(settings.wifiSSID, settings.wifiPW);
215+
216+
int maxTime = 10000;
217+
long startTime = millis();
218+
while (WiFi.status() != WL_CONNECTED) {
219+
delay(500);
220+
Serial.print(F("."));
221+
222+
if (millis() - startTime > maxTime)
223+
{
224+
Serial.println(F("Failed to connect to WiFi. Are you sure your WiFi credentials are correct?"));
225+
return (false);
226+
}
227+
}
228+
} //End WiFi connect check
229+
230+
//Are we connected to caster?
231+
if (caster.connected() == false)
232+
{
233+
Serial.printf("Opening socket to %s\n", settings.casterHost);
234+
235+
if (caster.connect(settings.casterHost, settings.casterPort) == true) //Attempt connection
236+
{
237+
Serial.printf("Connected to %s:%d\n", settings.casterHost, settings.casterPort);
238+
239+
const int SERVER_BUFFER_SIZE = 512;
240+
char serverBuffer[SERVER_BUFFER_SIZE];
241+
242+
snprintf(serverBuffer, SERVER_BUFFER_SIZE, "SOURCE %s /%s\r\nSource-Agent: NTRIP %s/%s\r\n\r\n",
243+
settings.mountPointPW, settings.mountPoint, ntrip_server_name, "App Version 1.0");
244+
245+
Serial.printf("Sending credentials:\n%s\n", serverBuffer);
246+
caster.write(serverBuffer, strlen(serverBuffer));
247+
248+
//Wait for response
249+
unsigned long timeout = millis();
250+
while (caster.available() == 0)
251+
{
252+
if (millis() - timeout > 5000)
253+
{
254+
Serial.println(F("Caster failed to respond. Do you have your caster address and port correct?"));
255+
caster.stop();
256+
return (false);
257+
}
258+
delay(10);
259+
}
260+
261+
delay(10); //Yield to RTOS
262+
263+
//Check reply
264+
bool connectionSuccess = false;
265+
char response[512];
266+
int responseSpot = 0;
267+
while (caster.available())
268+
{
269+
response[responseSpot++] = caster.read();
270+
if (strstr(response, "200") > 0) //Look for 'ICY 200 OK'
271+
connectionSuccess = true;
272+
if (responseSpot == 512 - 1) break;
273+
}
274+
response[responseSpot] = '\0';
275+
276+
Serial.printf("Caster responded with: %s\n", response);
277+
278+
if (connectionSuccess == false)
279+
{
280+
Serial.printf("Caster responded with bad news: %s. Are you sure your caster credentials are correct?\n", response);
281+
}
282+
else
283+
{
284+
//We're connected!
285+
Serial.println(F("Connected to caster"));
286+
287+
//Reset flags
288+
lastServerReport_ms = millis();
289+
lastServerSent_ms = millis();
290+
serverBytesSent = 0;
291+
}
292+
} //End attempt to connect
293+
else
294+
{
295+
Serial.println(F("Failed to connect to caster. Is your caster host and port correct?"));
296+
delay(10); //Give RTOS some time
297+
return (false);
298+
}
299+
} //End connected == false
300+
301+
Serial.println("Begin pushing");
302+
303+
return (true);
304+
}
305+
306+
//This function gets called from the SparkFun u-blox Arduino Library.
307+
//As each RTCM byte comes in you can specify what to do with it
308+
//Useful for passing the RTCM correction data to a radio, Ntrip broadcaster, etc.
309+
void SFE_UBLOX_GPS::processRTCM(uint8_t incoming)
310+
{
311+
if (caster.connected() == true)
312+
{
313+
caster.write(incoming); //Send this byte to socket
314+
serverBytesSent++;
315+
lastServerSent_ms = millis();
316+
}
317+
}
318+
319+
bool endBluetooth()
320+
{
321+
//Kill tasks if running
322+
if (F9PSerialReadTaskHandle != NULL)
323+
vTaskDelete(F9PSerialReadTaskHandle);
324+
if (F9PSerialWriteTaskHandle != NULL)
325+
vTaskDelete(F9PSerialWriteTaskHandle);
326+
327+
SerialBT.end();
328+
btStop();
329+
330+
Serial.println(F("Bluetooth turned off"));
331+
}
332+
333+
//If the phone has any new data (NTRIP RTCM, etc), read it in over Bluetooth and pass along to ZED
334+
//Task for writing to the GNSS receiver
335+
void F9PSerialWriteTask(void *e)
336+
{
337+
while (true)
338+
{
339+
340+
if (SerialBT.available())
341+
{
342+
while (SerialBT.available())
343+
{
344+
//Pass bytes to GNSS receiver
345+
auto s = SerialBT.readBytes(wBuffer, SERIAL_SIZE_RX);
346+
GPS.write(wBuffer, s);
347+
}
348+
}
349+
350+
taskYIELD();
351+
}
352+
}
353+
354+
//If the ZED has any new NMEA data, pass it out over Bluetooth
355+
//Task for reading data from the GNSS receiver.
356+
void F9PSerialReadTask(void *e)
357+
{
358+
while (true)
359+
{
360+
if (GPS.available())
361+
{
362+
auto s = GPS.readBytes(rBuffer, SERIAL_SIZE_RX);
363+
364+
if (SerialBT.connected())
365+
{
366+
SerialBT.write(rBuffer, s);
367+
}
368+
}
369+
taskYIELD();
370+
}
371+
}
372+
373+
//Connect to and configure ZED-F9P
374+
void beginGNSS()
375+
{
376+
if (myGPS.begin() == false)
377+
{
378+
//Try again with power on delay
379+
delay(1000); //Wait for ZED-F9P to power up before it can respond to ACK
380+
if (myGPS.begin() == false)
381+
{
382+
Serial.println(F("u-blox GNSS not detected at default I2C address. Hard stop."));
383+
while (1);
384+
}
385+
}
386+
387+
Serial.println(F("GNSS configuration complete"));
388+
}

0 commit comments

Comments
 (0)