|
| 1 | +#include <IridiumSBD.h> // Click here to get the library: http://librarymanager/All#IridiumSBDI2C |
| 2 | +#include <time.h> |
| 3 | +#include <Wire.h> //Needed for I2C communication |
| 4 | + |
| 5 | +/* |
| 6 | + * Production Test Part 2 |
| 7 | + * |
| 8 | + * LED_BUILTIN will flash at 0.5Hz (1sec on, 1sec off) if the test succeeds. |
| 9 | + * If the LED is off, the test is either in progress or has failed. |
| 10 | + * |
| 11 | + * This sketch demonstrates how to retrieve the Iridium system time |
| 12 | + * from the modem using the getSystemTime method. This uses |
| 13 | + * the Iridium command AT-MSSTM to acquire the time. The method will |
| 14 | + * fail if the Iridium network has not yet been acquired. |
| 15 | + * |
| 16 | + * Assumptions |
| 17 | + * |
| 18 | + * The sketch assumes the SparkFun Qwiic Iridium 9603N is connected via I2C. |
| 19 | + */ |
| 20 | + |
| 21 | +#define IridiumWire Wire |
| 22 | +#define DIAGNOSTICS false // Change this to see diagnostics |
| 23 | + |
| 24 | +// Declare the IridiumSBD object using default I2C address |
| 25 | +IridiumSBD modem(IridiumWire); |
| 26 | + |
| 27 | +// Globals to hold the current and previous satellite time |
| 28 | +struct tm old_t; // struct tm is defined in time.h |
| 29 | +struct tm new_t; // struct tm is defined in time.h |
| 30 | +time_t old_secs, new_secs; |
| 31 | + |
| 32 | +void setup() |
| 33 | +{ |
| 34 | + int err; |
| 35 | + |
| 36 | + // Latest epoch began at May 11, 2014, at 14:23:55 UTC. |
| 37 | + old_t.tm_year = 2014 - 1900; |
| 38 | + old_t.tm_mon = 5 - 1; |
| 39 | + old_t.tm_mday = 11; |
| 40 | + old_t.tm_hour = 14; |
| 41 | + old_t.tm_min = 23; |
| 42 | + old_t.tm_sec = 55; |
| 43 | + old_secs = mktime(&old_t); // Convert to seconds |
| 44 | + |
| 45 | + new_t.tm_year = 2014 - 1900; |
| 46 | + new_t.tm_mon = 5 - 1; |
| 47 | + new_t.tm_mday = 11; |
| 48 | + new_t.tm_hour = 14; |
| 49 | + new_t.tm_min = 23; |
| 50 | + new_t.tm_sec = 55; |
| 51 | + new_secs = mktime(&new_t); // Convert to seconds |
| 52 | + |
| 53 | + // Start the console serial port |
| 54 | + Serial.begin(115200); |
| 55 | + //while (!Serial); // Wait for the user to open the serial monitor |
| 56 | + Serial.println(F("Qwiic Iridium 9603N - Production Test Part 2")); |
| 57 | + |
| 58 | + //empty the serial buffer |
| 59 | + while(Serial.available() > 0) Serial.read(); |
| 60 | + |
| 61 | + //wait for the user to press any key before beginning |
| 62 | + //Serial.println(F("Press any key to start example.")); |
| 63 | + //while(Serial.available() == 0); |
| 64 | + |
| 65 | + //clean up |
| 66 | + while(Serial.available() > 0) Serial.read(); |
| 67 | + |
| 68 | + // Disable the LED |
| 69 | + pinMode(LED_BUILTIN, OUTPUT); |
| 70 | + digitalWrite(LED_BUILTIN, LOW); |
| 71 | + |
| 72 | + // Start the I2C wire port connected to the satellite modem |
| 73 | + Wire.begin(); |
| 74 | + Wire.setClock(400000); //Set I2C clock speed to 400kHz |
| 75 | + |
| 76 | + // Check that the Qwiic Iridium is attached |
| 77 | + if (!modem.isConnected()) |
| 78 | + { |
| 79 | + Serial.println(F("Qwiic Iridium is not connected! Please check wiring. Freezing.")); |
| 80 | + while(1); |
| 81 | + } |
| 82 | + |
| 83 | + // Enable the supercapacitor charger |
| 84 | + Serial.println(F("Enabling the supercapacitor charger...")); |
| 85 | + modem.enableSuperCapCharger(true); |
| 86 | + |
| 87 | + // Wait for the supercapacitor charger PGOOD signal to go high |
| 88 | + while (!modem.checkSuperCapCharger()) |
| 89 | + { |
| 90 | + Serial.println(F("Waiting for supercapacitors to charge...")); |
| 91 | + delay(1000); |
| 92 | + } |
| 93 | + Serial.println(F("Supercapacitors charged!")); |
| 94 | + |
| 95 | + // Enable power for the 9603N |
| 96 | + Serial.println(F("Enabling 9603N power...")); |
| 97 | + modem.enable9603Npower(true); |
| 98 | + |
| 99 | + // Begin satellite modem operation |
| 100 | + Serial.println(F("Starting modem...")); |
| 101 | + modem.setPowerProfile(IridiumSBD::USB_POWER_PROFILE); // Assume 'USB' power (slow recharge) |
| 102 | + err = modem.begin(); |
| 103 | + if (err != ISBD_SUCCESS) |
| 104 | + { |
| 105 | + Serial.print(F("Begin failed: error ")); |
| 106 | + Serial.println(err); |
| 107 | + if (err == ISBD_NO_MODEM_DETECTED) |
| 108 | + Serial.println(F("No modem detected: check wiring.")); |
| 109 | + digitalWrite(LED_BUILTIN, LOW); // Disable the LED |
| 110 | + while(1); // Do nothing more |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +void loop() |
| 115 | +{ |
| 116 | + int err = modem.getSystemTime(new_t); // Ask the 9603N for the system time |
| 117 | + if (err == ISBD_SUCCESS) // Was it successful? |
| 118 | + { |
| 119 | + old_secs = mktime(&old_t); // Convert to seconds |
| 120 | + new_secs = mktime(&new_t); |
| 121 | + |
| 122 | + if (new_secs > old_secs) |
| 123 | + { |
| 124 | + digitalWrite(LED_BUILTIN, (new_secs % 2)); // Flash the LED at 0.5Hz |
| 125 | + |
| 126 | + char buf[32]; |
| 127 | + sprintf(buf, "%d-%02d-%02d %02d:%02d:%02d", |
| 128 | + new_t.tm_year + 1900, new_t.tm_mon + 1, new_t.tm_mday, new_t.tm_hour, new_t.tm_min, new_t.tm_sec); |
| 129 | + Serial.print(F("Iridium date/time is ")); |
| 130 | + Serial.println(buf); |
| 131 | + } |
| 132 | + |
| 133 | + old_t = new_t; // Update the old time |
| 134 | + } |
| 135 | + |
| 136 | + else if (err == ISBD_NO_NETWORK) // Did it fail because the 9603N has not yet seen the network? |
| 137 | + { |
| 138 | + Serial.println(F("No network detected.")); |
| 139 | + digitalWrite(LED_BUILTIN, LOW); |
| 140 | + } |
| 141 | + |
| 142 | + else |
| 143 | + { |
| 144 | + Serial.print(F("Unexpected error ")); |
| 145 | + Serial.println(err); |
| 146 | + digitalWrite(LED_BUILTIN, LOW); |
| 147 | + } |
| 148 | + |
| 149 | + // Delay |
| 150 | + delay(100UL); |
| 151 | +} |
| 152 | + |
| 153 | +#if DIAGNOSTICS |
| 154 | +void ISBDConsoleCallback(IridiumSBD *device, char c) |
| 155 | +{ |
| 156 | + Serial.write(c); |
| 157 | +} |
| 158 | + |
| 159 | +void ISBDDiagsCallback(IridiumSBD *device, char c) |
| 160 | +{ |
| 161 | + Serial.write(c); |
| 162 | +} |
| 163 | +#endif |
0 commit comments