@@ -328,58 +328,74 @@ void loop()
328328WiFiClientSecure wifiClient = WiFiClientSecure();
329329MqttClient mqttClient (wifiClient);
330330
331- void mqttMessageHandler (int messageSize) {
332- uint8_t spartnData[512 * 4 ]; // Most incoming data is around 500 bytes but may be larger
333- int spartnCount = 0 ;
334- Serial.print (F (" Pushed data from " ));
331+ void mqttMessageHandler (int messageSize)
332+ {
333+ const uint16_t mqttLimit = 512 ;
334+ uint8_t *mqttData = new uint8_t [mqttLimit]; // Allocate memory to hold the MQTT data
335+ if (mqttData == NULL )
336+ {
337+ Serial.println (F (" Memory allocation for mqttData failed!" ));
338+ return ;
339+ }
340+
341+ Serial.print (F (" Pushing data from " ));
335342 Serial.print (mqttClient.messageTopic ());
336343 Serial.println (F (" topic to ZED" ));
344+
337345 while (mqttClient.available ())
338346 {
339- char ch = mqttClient.read ();
340- // Serial.write(ch); //Pipe to serial port is fine but beware, it's a lot of binary data
341- spartnData[spartnCount++] = ch;
342- if (spartnCount == sizeof (spartnData))
343- break ;
344- }
347+ uint16_t mqttCount = 0 ;
345348
346- if (spartnCount > 0 )
347- {
348- // Push KEYS or SPARTN data to GNSS module over I2C
349- myGNSS.pushRawData (spartnData, spartnCount, false );
350- lastReceived_ms = millis ();
351-
352- if ((spartnData[0 ] == 0xB5 ) // Check if this is UBX-RXM-SPARTNKEY
353- && (spartnData[1 ] == 0x62 )
354- && (spartnData[2 ] == 0x02 ) // Class: RXM
355- && (spartnData[3 ] == 0x36 )) // ID: SPARTNKEY
349+ while (mqttClient.available ())
350+ {
351+ char ch = mqttClient.read ();
352+ // Serial.write(ch); //Pipe to serial port is fine but beware, it's a lot of binary data
353+ mqttData[mqttCount++] = ch;
354+
355+ if (mqttCount == mqttLimit)
356+ break ;
357+ }
358+
359+ if (mqttCount > 0 )
356360 {
357- uint8_t numKeys = spartnData[7 ]; // Get the number of keys
358- uint8_t keyStart = 10 + (numKeys * 8 ); // Point to the start of the first key
359- for (uint8_t key = 0 ; key < numKeys; key++)
361+ // Push KEYS or SPARTN data to GNSS module over I2C
362+ myGNSS.pushRawData (mqttData, mqttCount, false );
363+ lastReceived_ms = millis ();
364+
365+ if ((mqttData[0 ] == 0xB5 ) // Check if this is UBX-RXM-SPARTNKEY
366+ && (mqttData[1 ] == 0x62 )
367+ && (mqttData[2 ] == 0x02 ) // Class: RXM
368+ && (mqttData[3 ] == 0x36 )) // ID: SPARTNKEY
360369 {
361- Serial.print (F (" SPARTNKEY: " ));
362- Serial.println (key);
363- Serial.print (F (" Valid from GPS week number: " ));
364- uint16_t validFromWno = ((uint16_t )spartnData[12 + (key * 8 )]) | ((uint16_t )spartnData[13 + (key * 8 )] << 8 ); // Little endian
365- Serial.println (validFromWno);
366- Serial.print (F (" Valid from GPS time of week: " ));
367- uint32_t validFromTow = ((uint32_t )spartnData[14 + (key * 8 )]) | ((uint32_t )spartnData[15 + (key * 8 )] << 8 ) | ((uint32_t )spartnData[16 + (key * 8 )] << 16 ) | ((uint32_t )spartnData[17 + (key * 8 )] << 24 );
368- Serial.println (validFromTow);
369- uint8_t keyLengthBytes = spartnData[11 + (key * 8 )];
370- Serial.print (F (" Key length (bytes): " ));
371- Serial.println (keyLengthBytes);
372- Serial.print (F (" Key: \" " ));
373- for (uint8_t digit = 0 ; digit < keyLengthBytes; digit++)
370+ uint8_t numKeys = mqttData[7 ]; // Get the number of keys
371+ uint8_t keyStart = 10 + (numKeys * 8 ); // Point to the start of the first key
372+ for (uint8_t key = 0 ; key < numKeys; key++)
374373 {
375- Serial.print (spartnData[keyStart + digit] >> 4 , HEX); // Print the key as ASCII Hex
376- Serial.print (spartnData[keyStart + digit] & 0x0F , HEX); // Print the key as ASCII Hex
374+ Serial.print (F (" SPARTNKEY: " ));
375+ Serial.println (key);
376+ Serial.print (F (" Valid from GPS week number: " ));
377+ uint16_t validFromWno = ((uint16_t )mqttData[12 + (key * 8 )]) | ((uint16_t )mqttData[13 + (key * 8 )] << 8 ); // Little endian
378+ Serial.println (validFromWno);
379+ Serial.print (F (" Valid from GPS time of week: " ));
380+ uint32_t validFromTow = ((uint32_t )mqttData[14 + (key * 8 )]) | ((uint32_t )mqttData[15 + (key * 8 )] << 8 ) | ((uint32_t )mqttData[16 + (key * 8 )] << 16 ) | ((uint32_t )mqttData[17 + (key * 8 )] << 24 );
381+ Serial.println (validFromTow);
382+ uint8_t keyLengthBytes = mqttData[11 + (key * 8 )];
383+ Serial.print (F (" Key length (bytes): " ));
384+ Serial.println (keyLengthBytes);
385+ Serial.print (F (" Key: \" " ));
386+ for (uint8_t digit = 0 ; digit < keyLengthBytes; digit++)
387+ {
388+ Serial.print (mqttData[keyStart + digit] >> 4 , HEX); // Print the key as ASCII Hex
389+ Serial.print (mqttData[keyStart + digit] & 0x0F , HEX); // Print the key as ASCII Hex
390+ }
391+ Serial.println (F (" \" " ));
392+ keyStart += keyLengthBytes; // Update keyStart for the next key
377393 }
378- Serial.println (F (" \" " ));
379- keyStart += keyLengthBytes; // Update keyStart for the next key
380394 }
381395 }
382396 }
397+
398+ delete[] mqttData;
383399}
384400
385401// Connect to MQTT broker, receive dynamic keys and push to ZED module over I2C
0 commit comments