|
| 1 | +/* |
| 2 | +
|
| 3 | + SARA-R5 Example |
| 4 | + =============== |
| 5 | +
|
| 6 | + Identification - using ESPSoftwareSerial |
| 7 | + |
| 8 | + Written by: Paul Clark |
| 9 | + Date: December 29th 2021 |
| 10 | +
|
| 11 | + This example demonstrates how to read the SARA's: |
| 12 | + Manufacturer identification |
| 13 | + Model identification |
| 14 | + Firmware version identification |
| 15 | + Product Serial No. |
| 16 | + IMEI identification |
| 17 | + IMSI identification |
| 18 | + SIM CCID |
| 19 | + Subscriber number |
| 20 | + Capabilities |
| 21 | + SIM state |
| 22 | +
|
| 23 | + The ESP32 core doesn't include SoftwareSerial. Instead we use the library by Peter Lerup and Dirk O. Kaar: |
| 24 | + https://github.com/plerup/espsoftwareserial |
| 25 | +
|
| 26 | + Feel like supporting open source hardware? |
| 27 | + Buy a board from SparkFun! |
| 28 | +
|
| 29 | + Licence: MIT |
| 30 | + Please see LICENSE.md for full details |
| 31 | +
|
| 32 | +*/ |
| 33 | + |
| 34 | +// Include SoftwareSerial.h _before_ including SparkFun_u-blox_SARA-R5_Arduino_Library.h |
| 35 | +// to allow the SARA-R5 library to detect SoftwareSerial.h using an #if __has_include |
| 36 | +#include <SoftwareSerial.h> //Click here to get the library: http://librarymanager/All#ESPSoftwareSerial_ESP8266/ESP32 |
| 37 | + |
| 38 | +#include <SparkFun_u-blox_SARA-R5_Arduino_Library.h> //Click here to get the library: http://librarymanager/All#SparkFun_u-blox_SARA-R5_Arduino_Library |
| 39 | + |
| 40 | +// Create a SoftwareSerial object to pass to the SARA-R5 library |
| 41 | +// Note: we need to call saraSerial.begin and saraSerial.end in setup() - see below for details |
| 42 | +SoftwareSerial saraSerial; |
| 43 | + |
| 44 | +// Create a SARA_R5 object to use throughout the sketch |
| 45 | +// Usually we would tell the library which GPIO pin to use to control the SARA power (see below), |
| 46 | +// but we can start the SARA without a power pin. It just means we need to manually |
| 47 | +// turn the power on if required! ;-D |
| 48 | +SARA_R5 mySARA; |
| 49 | + |
| 50 | +// Create a SARA_R5 object to use throughout the sketch |
| 51 | +// We need to tell the library what GPIO pin is connected to the SARA power pin. |
| 52 | +// If you're using the MicroMod Asset Tracker and the MicroMod Artemis Processor Board, |
| 53 | +// the pin name is G2 which is connected to pin AD34. |
| 54 | +// Change the pin number if required. |
| 55 | +//SARA_R5 mySARA(34); |
| 56 | + |
| 57 | +// Map SIM states to more readable strings |
| 58 | +String simStateString[] = |
| 59 | +{ |
| 60 | + "Not present", // 0 |
| 61 | + "PIN needed", // 1 |
| 62 | + "PIN blocked", // 2 |
| 63 | + "PUK blocked", // 3 |
| 64 | + "Not operational", // 4 |
| 65 | + "Restricted", // 5 |
| 66 | + "Operational" // 6 |
| 67 | +}; |
| 68 | + |
| 69 | +// processSIMstate is provided to the SARA-R5 library via a |
| 70 | +// callback setter -- setSIMstateReadCallback. (See setup()) |
| 71 | +void processSIMstate(SARA_R5_sim_states_t state) |
| 72 | +{ |
| 73 | + Serial.println(); |
| 74 | + Serial.print(F("SIM state: ")); |
| 75 | + Serial.print(String(state)); |
| 76 | + Serial.println(); |
| 77 | +} |
| 78 | + |
| 79 | +void setup() |
| 80 | +{ |
| 81 | + Serial.begin(115200); // Start the serial console |
| 82 | + |
| 83 | + // Wait for user to press key to begin |
| 84 | + Serial.println(F("SARA-R5 Example")); |
| 85 | + Serial.println(F("Press any key to begin")); |
| 86 | + |
| 87 | + while (!Serial.available()) // Wait for the user to press a key (send any serial character) |
| 88 | + ; |
| 89 | + while (Serial.available()) // Empty the serial RX buffer |
| 90 | + Serial.read(); |
| 91 | + |
| 92 | + //mySARA.enableDebugging(); // Uncomment this line to enable helpful debug messages on Serial |
| 93 | + |
| 94 | + // For the MicroMod Asset Tracker, we need to invert the power pin so it pulls high instead of low |
| 95 | + // Comment the next line if required |
| 96 | + mySARA.invertPowerPin(true); |
| 97 | + |
| 98 | + // ESPSoftwareSerial does not like repeated .begin's without a .end in between. |
| 99 | + // We need to .begin and .end the saraSerial port here, before the mySARA.begin, to set up the pin numbers etc. |
| 100 | + // E.g. to use: 57600 baud; 8 databits, no parity, 1 stop bit; RXD on pin 33; TXD on pin 32; no inversion. |
| 101 | + Serial.println(F("Configuring SoftwareSerial saraSerial")); |
| 102 | + saraSerial.begin(57600, SWSERIAL_8N1, 33, 32, false); |
| 103 | + saraSerial.end(); |
| 104 | + |
| 105 | + // Initialize the SARA |
| 106 | + if (mySARA.begin(saraSerial, 57600) ) |
| 107 | + { |
| 108 | + Serial.println(F("SARA-R5 connected!")); |
| 109 | + } |
| 110 | + else |
| 111 | + { |
| 112 | + Serial.println(F("Unable to communicate with the SARA.")); |
| 113 | + Serial.println(F("Manually power-on (hold the SARA On button for 3 seconds) on and try again.")); |
| 114 | + while (1) ; // Loop forever on fail |
| 115 | + } |
| 116 | + Serial.println(); |
| 117 | + |
| 118 | + Serial.println("Manufacturer ID: " + String(mySARA.getManufacturerID())); |
| 119 | + Serial.println("Model ID: " + String(mySARA.getModelID())); |
| 120 | + Serial.println("Firmware Version: " + String(mySARA.getFirmwareVersion())); |
| 121 | + Serial.println("Product Serial No.: " + String(mySARA.getSerialNo())); |
| 122 | + Serial.println("IMEI: " + String(mySARA.getIMEI())); |
| 123 | + Serial.println("IMSI: " + String(mySARA.getIMSI())); |
| 124 | + Serial.println("SIM CCID: " + String(mySARA.getCCID())); |
| 125 | + Serial.println("Subscriber No.: " + String(mySARA.getSubscriberNo())); |
| 126 | + Serial.println("Capabilities: " + String(mySARA.getCapabilities())); |
| 127 | + |
| 128 | + // Set a callback to return the SIM state once requested |
| 129 | + mySARA.setSIMstateReportCallback(&processSIMstate); |
| 130 | + // Now enable SIM state reporting for states 0 to 6 (by setting the reporting mode LSb) |
| 131 | + if (mySARA.setSIMstateReportingMode(1) == SARA_R5_SUCCESS) |
| 132 | + Serial.println("SIM state reports requested..."); |
| 133 | + // You can disable the SIM staus reports again by calling assetTracker.setSIMstateReportingMode(0) |
| 134 | +} |
| 135 | + |
| 136 | +void loop() |
| 137 | +{ |
| 138 | + mySARA.poll(); // Keep processing data from the SARA so we can extract the SIM status |
| 139 | +} |
0 commit comments