Skip to content

Commit 8fe9b21

Browse files
committed
Add support for ESPSoftwareSerial
1 parent e3b2a3c commit 8fe9b21

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/SparkFun_u-blox_SARA-R5_Arduino_Library.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3246,8 +3246,12 @@ SARA_R5_error_t SARA_R5::init(unsigned long baud,
32463246

32473247
if (_printDebug == true) _debugPort->println(F("Begin module init."));
32483248

3249-
if(hwAvailable() == -1)
3250-
beginSerial(baud); // If port is null, begin serial
3249+
// There's no 'easy' way to tell if the serial port has already been begun for us.
3250+
// We have to assume it has not been begun and so do it here.
3251+
// For special cases like Software Serial on ESP32, we need to begin _and_ end the port externally
3252+
// _before_ calling the SARA_R5 .begin.
3253+
// See SARA-R5_Example2_Identification_ESPSoftwareSerial for more details.
3254+
beginSerial(baud);
32513255

32523256
if (initType == SARA_R5_INIT_AUTOBAUD)
32533257
{

src/SparkFun_u-blox_SARA-R5_Arduino_Library.h

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,31 @@
4343
#endif
4444

4545
#ifdef ARDUINO_ARCH_ESP32 // ESP32 based boards
46+
// Check to see if ESP Software Serial has been included
47+
// Note: you need to #include <SoftwareSerial.h> at the very start of your script,
48+
// _before_ the #include <SparkFun_u-blox_SARA-R5_Arduino_Library.h>, for this to work.
49+
// See SARA-R5_Example2_Identification_ESPSoftwareSerial for more details.
50+
#if __has_include( <SoftwareSerial.h> )
51+
#define SARA_R5_SOFTWARE_SERIAL_ENABLED // Enable software serial
52+
#else
4653
#define SARA_R5_SOFTWARE_SERIAL_ENABLEDx // Disable software serial
4754
#endif
55+
#endif
56+
57+
#ifdef ARDUINO_ARCH_ESP8266 // ESP8266 based boards
58+
// Check to see if ESP Software Serial has been included
59+
// Note: you need to #include <SoftwareSerial.h> at the very start of your script,
60+
// _before_ the #include <SparkFun_u-blox_SARA-R5_Arduino_Library.h>, for this to work.
61+
// See SARA-R5_Example2_Identification_ESPSoftwareSerial for more details.
62+
#if __has_include( <SoftwareSerial.h> )
63+
#define SARA_R5_SOFTWARE_SERIAL_ENABLED // Enable software serial
64+
#else
65+
#define SARA_R5_SOFTWARE_SERIAL_ENABLEDx // Disable software serial
66+
#endif
67+
#endif
4868

4969
#ifdef SARA_R5_SOFTWARE_SERIAL_ENABLED
50-
#include <SoftwareSerial.h>
70+
#include <SoftwareSerial.h> // SoftwareSerial.h is guarded. It is OK to include it twice.
5171
#endif
5272

5373
#include <IPAddress.h>

0 commit comments

Comments
 (0)