# SparkFun Iridium 9604 Library

A library to allow you to communicate over both LTE-M and SBD using the Iridium 9604 IoT Module. Written for the SparkFun RockBLOCK 9604 Shield, but compatible with any microcontroller interfaced to the Iridium 9604 over UART.
You can install this library using the Arduino IDE Library Manager: search for _**SparkFun Iridium 9604**_
You can also install this library as an Espressif ESP-IDF Component
## Repository Contents
* [**examples**](https://github.com/sparkfun/SparkFun_Iridium_9604_Library/blob/main/examples) - Example sketches for the library (.ino). Run these from the Arduino IDE.
* [**idf_examples**](https://github.com/sparkfun/SparkFun_Iridium_9604_Library/blob/main/idf_examples) - Example projects for the library (main.cpp). Build these with the Espressif ESP-IDF.
* [**src**](https://github.com/sparkfun/SparkFun_Iridium_9604_Library/blob/main/src) - Source files for the library (.cpp, .h).
* **keywords.txt** - Keywords from this library that will be highlighted in the Arduino IDE.
* **library.properties** - General library properties for the Arduino package manager.
* **CMakeLists.txt** and **idf_component.yml** - The ESP-IDF component build file and manifest.
## Using the library with the Arduino IDE
```
#include <SparkFun_Iridium_9604.h> // http://librarymanager/All#SparkFun_Iridium_9604
SFE_IRIDIUM_9604 myModule; // Or: SFE_IRIDIUM_9604 myModule(pwrOnPin);
void setup()
{
Serial.begin(115200);
if (!myModule.begin(Serial1, 115200)) // Use a hardware serial port. (SoftwareSerial is not supported)
Serial.println(F("Iridium 9604 not detected!"));
myModule.setRAT(IRIDIUM_9604_RAT_SATELLITE); // Or IRIDIUM_9604_RAT_LTE_CAT_M1
Serial.println(myModule.getSatelliteIMEI());
myModule.sendSBDText("Hello, world!");
}
void loop()
{
myModule.bufferedPoll(); // Process any URCs: socket data, NMEA messages, +CIEV, +AREG etc.
}
```
On ESP32, if you need non-default serial pins, call `Serial1.begin(baud, SERIAL_8N1, rxPin, txPin)` before `myModule.begin(Serial1, baud)`. The library preserves your pin selection.
The library uses `<vector>`, so it does not build on the standard AVR (Uno / Mega) boards.
Examples 13 and 14 use u-blox AssistNow (Live Orbits and Predictive Orbits, via Thingstream Zero Touch Provisioning) with the Iridium 9604's internal u-blox M10 GNSS. Examples 15 and 16 need the [SparkFun u-blox GNSS v4](https://github.com/sparkfun/SparkFun_u-blox_GNSS_v4) library and an external u-blox RTK GNSS module (e.g. ZED-F9P) connected via I2C: the M10's firmware does not support RTCM corrections.
## Using the library with the Espressif ESP-IDF
Add the component to your project's `main/idf_component.yml`:
```
dependencies:
sparkfun/sparkfun_iridium_9604_library:
version: "*"
```
Or clone this repository into your project's `components` folder - in a folder called `sparkfun_iridium_9604_library`. (ESP-IDF uses the directory name as the component name.) ESP-IDF v5.3 or later is required.
```
#include "driver/uart.h"
#include "SparkFun_Iridium_9604.h"
using namespace SparkFun_Iridium_9604; // nmeaMessage, SfeIPAddress etc. are inside this namespace
static SFE_IRIDIUM_9604 myModule;
extern "C" void app_main(void)
{
// Install and configure the UART first. Use an RX buffer of at least 2048 bytes (4096 if NMEA URCs are enabled)
uart_config_t cfg = {};
cfg.baud_rate = 115200;
cfg.data_bits = UART_DATA_8_BITS;
cfg.parity = UART_PARITY_DISABLE;
cfg.stop_bits = UART_STOP_BITS_1;
cfg.flow_ctrl = UART_HW_FLOWCTRL_DISABLE;
cfg.source_clk = UART_SCLK_DEFAULT;
uart_driver_install(UART_NUM_1, 4096, 0, 0, NULL, 0);
uart_param_config(UART_NUM_1, &cfg);
uart_set_pin(UART_NUM_1, 17, 16, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
if (myModule.begin(UART_NUM_1, 115200))
printf("Satellite IMEI: %s\n", myModule.getSatelliteIMEI().c_str());
}
```
On ESP-IDF, text values (`sfe_string_t`) are `std::string` and IP addresses (`sfe_ip_address_t`) are `SfeIPAddress`. On Arduino they are `String` and `IPAddress`. Debug messages go to `stdout` by default: `myModule.enableDebugging()`. All of the Arduino examples have ESP-IDF versions in [idf_examples](./idf_examples).
## SparkFun RockBLOCK 9604 Shield
The examples in this library can be run on the ESP32-S3 processor on the SparkFun RockBLOCK 9604 Shield, but some extra code is needed to adapt the examples so they will run on the Shield hardware. This extra code is described in the Shield repository. Please consult [Iridium_9604_Library.md](https://github.com/sparkfun/SparkFun_RockBLOCK_9604_Shield/blob/main/docs/Iridium_9604_Library.md) for more details.
## PWR_ON and RESET_N
The 9604 PWR_ON and RESET_N inputs are active low, with internal pull-ups. They must not be driven high by an external device (PWR_ON's absolute maximum input is 1.65 V). If they are connected to GPIO pins, pass the pins to the constructor - `SFE_IRIDIUM_9604 myModule(powerPin, resetPin);` - and the library pulls them low and releases them (makes them inputs). Use `invertPowerPin(true)` if PWR_ON is driven through an inverting FET.
If the pins are not connected directly to GPIO pins, use `setPowerOnPinWriteCallback()` and `setResetPinWriteCallback()` to provide your own write functions: `void callback(bool state)`. `state` is the level the 9604 pin should see: `false` = pull it low (assert it); `true` = release it. The callbacks are used by `modulePowerOn()`, `hwReset()` (RESET_N low for 200 ms: reset / reboot) and `hwEmergencyShutdown()` (PWR_ON low for 20 s), and by `begin()` if it needs to power-cycle the module.
## Library structure
The library is based on the [SparkFun u-blox SARA-R5 Arduino Library](https://github.com/sparkfun/SparkFun_u-blox_SARA-R5_Arduino_Library) v1.1.12, restructured following the [SparkFun u-blox GNSS v4](https://github.com/sparkfun/SparkFun_u-blox_GNSS_v4) library so that it builds for both Arduino and ESP-IDF. The SARA-R5 methods are all still available - without the SARA-R5 name: e.g. `SARA_R5_SUCCESS` is now `IRIDIUM_9604_SUCCESS`.
* **The bus**: `SparkFun_Iridium_9604::IridiumDeviceBus` is the abstract UART interface. `SfeSerial` implements it, using `HardwareSerial` on Arduino and the ESP-IDF UART driver on ESP-IDF (see `sfe_bus.h` and `sfe_bus_esp_idf.h`). You can provide your own bus with `setCommunicationBus()`.
* **Debug printing**: `SfeDebugPrint` / `SfePrint` replace the SARA-R5 `_debugPort` prints. `enableDebugging()` and `enableAtDebugging()` take any `Print` (Arduino) or `SfeOutput` (ESP-IDF).
* **URCs**: each Unsolicited Result Code has its own handler class in [src/urcHandlers](./src/urcHandlers). The handlers self-register - like the GNSS v4 message classes - and are listed in `src/urcHandlerVector.h`. Comment a handler out to save memory. Register callbacks with the typed helpers (`setSocketReadCallback()`, `setPingCallback()`, `setSatelliteSignalQualityCallback()` ...) or generically with `setURCCallback(prefix, callback, index)`. See `src/urcHandler.h` for how to add a new URC.
* **NMEA**: the GNSS v4 NMEA message classes are in [src/nmeaMessages](./src/nmeaMessages): all 13 messages supported by AT+UGNMEA. `setNMEAoutput()` enables a message as a `+UUGNMEA` URC (output 2) or in module memory (output 1). `setNmeaCallbackPtr()` registers a callback. `getNMEA()` / `getNMEAfield()` poll a message (AT+UGNMEA?) and read a field by name. `getNmeaMessageField()`, `getNmeaMessageBlockField()` and their `...Callback()` versions read fields by name. These replace the SARA-R5 `gpsEnableRmc()`, `gpsGetRmc()` and `parseGPRMCString()` (+UGRMC is deprecated on the 9604).
* **Satellite**: every command in the "Satellite" section of the Iridium 9604 AT commands manual is supported: +USATSN, +USATINFO, +CIER (and the +CIEV URC), +CULK, +SBDAREG (and +AREG), +SBDD, +SBDDET, +SBDDSC, +SBDI, +SBDIX / +SBDIXA, +SBDLOE, +SBDR, +SBDST, +SBDSX and +SBDW. Plus +SBDREG? and +RFSCAN. The high-level `sendSBDText()`, `sendSBDBinary()`, `sendReceiveSBDText()` and `sendReceiveSBDBinary()` are similar to the SparkFun IridiumSBD library. `setRAT()` / `getRAT()` (+URAT) select LTE Cat M1 or satellite.
## Iridium 9604 AT command notes
All of the AT commands used by the SARA-R5 library were checked against the Iridium 9604 AT commands manual (v1.5 BETA) and adapted where needed.
* The 9604 supports LTE Cat M1 or satellite, one at a time: `setRAT()` (AT+URAT). The module reboots when the RAT is changed.
* `rssi()` (AT+CSQ) is cellular only. The satellite signal quality comes from the +CIEV URC: `setIndicatorEventReporting()`, `getSatelliteSignalQuality()`, `waitForSatelliteSignalQuality()`.
* The 9604 SBD commands use HEX-encoded data (+SBDW, +SBDR). The library encodes and decodes it for you. The +SBDIX, +SBDDET and +AREG error codes can be converted to text with `getSBDErrorString()`.
* The GNSS is a u-blox M10. The NMEA URC is `+UUGNMEA: "$...*hh"`. The GNSS system enum includes BeiDou B1C and QZSS L1S; the aiding mode enum includes PointPerfect.
* +UGRMC is deprecated on the 9604: `gpsEnableRmc()` and `gpsGetRmc()` have been replaced by the NMEA methods above.
* The 9604 does not support the SARA-R5's u-blox data profiles: +UPSD, +UPSDA and +UPSND return errors. `setPDPconfiguration()`, `performPDPaction()`, `getNetworkAssignedIPAddress()` and `setPSDActionCallback()` have been removed. The internal TCP/IP stack (sockets, ping, DNS, HTTP, MQTT) uses the PDP (Packet Data Protocol) context set by AT+UDCONF=19 (cid 1 - the default EPS bearer - activated during the LTE attach). Use `getDefaultPDPcontext()` (AT+UDCONF=19), `getPDPcontextState()` (AT+CGACT?), `activatePDPcontext()` (AT+CGACT) and `getPDPaddress()` (AT+CGPADDR) instead.
* Not documented for the 9604, but kept (marked in `Iridium_9604_AT_commands.h`): +ULOC, +UULOC and +ULOCCELL (CellLocate). +USECPRF and +USECMNG are referenced by the manual, but have no section of their own.
* The +UGPIOC modes, +UMNOPROF profiles and supported baud rates (now including 460800 and 921600) have been updated to match the 9604 manual.
## Documentation
API documentation is generated with Doxygen and published to GitHub Pages from the `main` branch:
[**https://docs.sparkfun.com/SparkFun_Iridium_9604_Library**](https://docs.sparkfun.com/SparkFun_Iridium_9604_Library)
## License Information
This library is _**open source**_!
Please see [LICENSE.md](https://github.com/sparkfun/SparkFun_Iridium_9604_Library/blob/main/LICENSE.md) for full details.
- Your friends at SparkFun.
idf.py add-dependency "sparkfun/sparkfun_iridium_9604_library^1.0.0"