|  | 
|  | 1 | +/* | 
|  | 2 | +  Configuring the GNSS to automatically send MON COMMS reports over I2C and display them using a callback | 
|  | 3 | +  By: Paul Clark | 
|  | 4 | +  SparkFun Electronics | 
|  | 5 | +  Date: Novenber 4th, 2024 | 
|  | 6 | +  License: MIT. See license file for more information. | 
|  | 7 | +
 | 
|  | 8 | +  This example shows how to configure the u-blox GNSS to send MON COMMS reports automatically | 
|  | 9 | +  and access the data via a callback. No more polling! | 
|  | 10 | +
 | 
|  | 11 | +  Feel like supporting open source hardware? | 
|  | 12 | +  Buy a board from SparkFun! | 
|  | 13 | +  ZED-F9P RTK2: https://www.sparkfun.com/products/15136 | 
|  | 14 | +
 | 
|  | 15 | +  Hardware Connections: | 
|  | 16 | +  Plug a Qwiic cable into the GPS and a BlackBoard | 
|  | 17 | +  If you don't have a platform with a Qwiic connection use the SparkFun Qwiic Breadboard Jumper (https://www.sparkfun.com/products/14425) | 
|  | 18 | +  Open the serial monitor at 115200 baud to see the output | 
|  | 19 | +*/ | 
|  | 20 | + | 
|  | 21 | +#include <Wire.h> //Needed for I2C to GPS | 
|  | 22 | + | 
|  | 23 | +#include <SparkFun_u-blox_GNSS_v3.h> //http://librarymanager/All#SparkFun_u-blox_GNSS_v3 | 
|  | 24 | +SFE_UBLOX_GNSS myGNSS; | 
|  | 25 | + | 
|  | 26 | +// Callback: newMONCOMMS will be called when new MON COMMS data arrives | 
|  | 27 | +// See u-blox_structs.h for the full definition of UBX_MON_COMMS_data_t | 
|  | 28 | +//         _____  You can use any name you like for the callback. Use the same name when you call setAutoMONCOMMScallback | 
|  | 29 | +//        /             _____  This _must_ be UBX_MON_COMMS_data_t | 
|  | 30 | +//        |            /                _____ You can use any name you like for the struct | 
|  | 31 | +//        |            |               / | 
|  | 32 | +//        |            |               | | 
|  | 33 | +void newMONCOMMS(UBX_MON_COMMS_data_t *ubxDataStruct) | 
|  | 34 | +{ | 
|  | 35 | +  Serial.println(); | 
|  | 36 | + | 
|  | 37 | +  Serial.print(F("New MON COMMS data received. It contains data for ")); | 
|  | 38 | +  Serial.print(ubxDataStruct->header.nPorts); | 
|  | 39 | +  if (ubxDataStruct->header.nPorts == 1) | 
|  | 40 | +    Serial.println(F(" port.")); | 
|  | 41 | +  else | 
|  | 42 | +    Serial.println(F(" ports.")); | 
|  | 43 | + | 
|  | 44 | +  // Mimic the data shown in u-center | 
|  | 45 | +  for (uint8_t port = 0; port < ubxDataStruct->header.nPorts; port++) // For each port | 
|  | 46 | +  { | 
|  | 47 | +    bool known = true; | 
|  | 48 | +    switch (ubxDataStruct->port[port].portId) // Check the port ID is valid | 
|  | 49 | +    { | 
|  | 50 | +      case COM_PORT_ID_I2C: | 
|  | 51 | +      case COM_PORT_ID_UART1: | 
|  | 52 | +      case COM_PORT_ID_UART2: | 
|  | 53 | +      case COM_PORT_ID_USB: | 
|  | 54 | +      case COM_PORT_ID_SPI: | 
|  | 55 | +      break; | 
|  | 56 | +      default: | 
|  | 57 | +        known = false; | 
|  | 58 | +      break;       | 
|  | 59 | +    } | 
|  | 60 | +    if (!known) | 
|  | 61 | +      break; // Skip if port ID is not known | 
|  | 62 | + | 
|  | 63 | +    switch (ubxDataStruct->port[port].portId) // Print the port ID | 
|  | 64 | +    { | 
|  | 65 | +      case COM_PORT_ID_I2C: | 
|  | 66 | +        Serial.print(F("I2C     ")); | 
|  | 67 | +      break; | 
|  | 68 | +      case COM_PORT_ID_UART1: | 
|  | 69 | +        Serial.print(F("UART1   ")); | 
|  | 70 | +      break; | 
|  | 71 | +      case COM_PORT_ID_UART2: | 
|  | 72 | +        Serial.print(F("UART2   ")); | 
|  | 73 | +      break; | 
|  | 74 | +      case COM_PORT_ID_USB: | 
|  | 75 | +        Serial.print(F("USB     ")); | 
|  | 76 | +      break; | 
|  | 77 | +      case COM_PORT_ID_SPI: | 
|  | 78 | +        Serial.print(F("SPI     ")); | 
|  | 79 | +      break; | 
|  | 80 | +      default: | 
|  | 81 | +        Serial.print(F("UNKNOWN ")); | 
|  | 82 | +        //Serial.printf("0x%04X  ", ubxDataStruct->port[port].portId); | 
|  | 83 | +      break;       | 
|  | 84 | +    } | 
|  | 85 | + | 
|  | 86 | +    Serial.print(": txBytes "); | 
|  | 87 | +    String txBytes = String(ubxDataStruct->port[port].txBytes); | 
|  | 88 | +    Serial.print(txBytes); | 
|  | 89 | +    for (int i = 0; i < 10 - txBytes.length(); i++) | 
|  | 90 | +      Serial.print(" "); | 
|  | 91 | +     | 
|  | 92 | +    Serial.print(" : rxBytes "); | 
|  | 93 | +    String rxBytes = String(ubxDataStruct->port[port].rxBytes); | 
|  | 94 | +    Serial.print(rxBytes); | 
|  | 95 | +    for (int i = 0; i < 10 - rxBytes.length(); i++) | 
|  | 96 | +      Serial.print(" "); | 
|  | 97 | + | 
|  | 98 | +    for (int i = 0; i < 4; i++) | 
|  | 99 | +    { | 
|  | 100 | +      if (ubxDataStruct->header.protIds[i] < 0xFF) | 
|  | 101 | +      { | 
|  | 102 | +        switch (ubxDataStruct->header.protIds[i]) | 
|  | 103 | +        { | 
|  | 104 | +          case 0: | 
|  | 105 | +            Serial.print(F(" : UBX     ")); | 
|  | 106 | +          break; | 
|  | 107 | +          case 1: | 
|  | 108 | +            Serial.print(F(" : NMEA    ")); | 
|  | 109 | +          break; | 
|  | 110 | +          case 2: | 
|  | 111 | +            Serial.print(F(" : RTCM2   ")); | 
|  | 112 | +          break; | 
|  | 113 | +          case 5: | 
|  | 114 | +            Serial.print(F(" : RTCM3   ")); | 
|  | 115 | +          break; | 
|  | 116 | +          case 6: | 
|  | 117 | +            Serial.print(F(" : SPARTN  ")); | 
|  | 118 | +          break; | 
|  | 119 | +          default: | 
|  | 120 | +            Serial.print(F(" : UNKNOWN ")); | 
|  | 121 | +          break; | 
|  | 122 | +        } | 
|  | 123 | +        String msgs = String(ubxDataStruct->port[port].msgs[i]); | 
|  | 124 | +        Serial.print(msgs); | 
|  | 125 | +        for (int i = 0; i < 5 - msgs.length(); i++) | 
|  | 126 | +          Serial.print(" "); | 
|  | 127 | +      } | 
|  | 128 | +    } | 
|  | 129 | +     | 
|  | 130 | +    Serial.print(" : skipped "); | 
|  | 131 | +    Serial.print(ubxDataStruct->port[port].skipped); | 
|  | 132 | +     | 
|  | 133 | +    Serial.println(); | 
|  | 134 | +  } | 
|  | 135 | +} | 
|  | 136 | + | 
|  | 137 | +void setup() | 
|  | 138 | +{ | 
|  | 139 | +  Serial.begin(115200); | 
|  | 140 | +  while (!Serial); //Wait for user to open terminal | 
|  | 141 | +  Serial.println("SparkFun u-blox Example"); | 
|  | 142 | + | 
|  | 143 | +  Wire.begin(); | 
|  | 144 | + | 
|  | 145 | +  //myGNSS.enableDebugging(); // Uncomment this line to enable helpful debug messages on Serial | 
|  | 146 | +  //myGNSS.enableDebugging(Serial, true); // Uncomment this line to enable only the major debug messages on Serial | 
|  | 147 | + | 
|  | 148 | +  if (myGNSS.begin() == false) //Connect to the u-blox module using Wire port | 
|  | 149 | +  { | 
|  | 150 | +    Serial.println(F("u-blox GNSS not detected at default I2C address. Please check wiring. Freezing.")); | 
|  | 151 | +    while (1); | 
|  | 152 | +  } | 
|  | 153 | + | 
|  | 154 | +  myGNSS.setI2COutput(COM_TYPE_NMEA | COM_TYPE_UBX); //Set the I2C port to output NMEA and UBX | 
|  | 155 | +  myGNSS.saveConfigSelective(VAL_CFG_SUBSEC_IOPORT); //Save (only) the communications port settings to flash and BBR | 
|  | 156 | + | 
|  | 157 | +  myGNSS.setNavigationFrequency(1); //Produce one solution per second | 
|  | 158 | + | 
|  | 159 | +  // Just to prove we can, poll the MON COMMS data manually | 
|  | 160 | +  Serial.println("Polling MON COMMS data:"); | 
|  | 161 | +  UBX_MON_COMMS_data_t portInfo; | 
|  | 162 | +  if (myGNSS.getCommsPortInfo(&portInfo)) | 
|  | 163 | +    newMONCOMMS(&portInfo); // Call the callback manually to print the data | 
|  | 164 | +  else | 
|  | 165 | +    Serial.println("getCommsPortInfo failed!"); | 
|  | 166 | + | 
|  | 167 | +  // Now enable automatic (periodic) MON COMMS messages with callback to newMONCOMMS | 
|  | 168 | +  myGNSS.setAutoMONCOMMScallbackPtr(&newMONCOMMS); | 
|  | 169 | +} | 
|  | 170 | + | 
|  | 171 | +void loop() | 
|  | 172 | +{ | 
|  | 173 | +  myGNSS.checkUblox(); // Check for the arrival of new data and process it. | 
|  | 174 | +  myGNSS.checkCallbacks(); // Check if any callbacks are waiting to be processed. | 
|  | 175 | + | 
|  | 176 | +  Serial.print("."); | 
|  | 177 | +  delay(50); | 
|  | 178 | +} | 
0 commit comments