Skip to content

Commit 432167b

Browse files
committed
Update mqttMessageHandler
1 parent 94ad6fa commit 432167b

File tree

3 files changed

+110
-88
lines changed

3 files changed

+110
-88
lines changed

examples/AssistNow/AssistNow_Online/Example5_AssistNowOnline_MQTT/Example5_AssistNowOnline_MQTT.ino

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -93,35 +93,43 @@ void loop()
9393
WiFiClientSecure wifiClient = WiFiClientSecure();
9494
MqttClient mqttClient(wifiClient);
9595

96-
void mqttMessageHandler(int messageSize) {
97-
const uint16_t mgaCountLimit = 16384;
98-
uint8_t *mgaData = new uint8_t[mgaCountLimit];
99-
uint16_t mgaCount = 0;
96+
void mqttMessageHandler(int messageSize)
97+
{
98+
const uint16_t mqttLimit = 512;
99+
uint8_t *mqttData = new uint8_t[mqttLimit]; // Allocate memory to hold the MQTT data
100+
if (mqttData == NULL)
101+
{
102+
Serial.println(F("Memory allocation for mqttData failed!"));
103+
return;
104+
}
105+
100106
Serial.print(F("Pushing data from "));
101107
Serial.print(mqttClient.messageTopic());
102108
Serial.println(F(" topic to ZED"));
109+
103110
while (mqttClient.available())
104111
{
105-
char ch = mqttClient.read();
106-
//Serial.write(ch); //Pipe to serial port is fine but beware, it's a lot of binary data
107-
mgaData[mgaCount++] = ch;
108-
if (mgaCount == mgaCountLimit)
112+
uint16_t mqttCount = 0;
113+
114+
while (mqttClient.available())
109115
{
110-
Serial.print(F("Warning!! MQTT data exceeded "));
111-
Serial.print(mgaCountLimit);
112-
Serial.println(F(" bytes!!"));
113-
break;
116+
char ch = mqttClient.read();
117+
//Serial.write(ch); //Pipe to serial port is fine but beware, it's a lot of binary data
118+
mqttData[mqttCount++] = ch;
119+
120+
if (mqttCount == mqttLimit)
121+
break;
114122
}
115-
}
116123

117-
if (mgaCount > 0)
118-
{
119-
//Push MGA data to GNSS module over I2C
120-
myGNSS.pushRawData(mgaData, mgaCount, false);
121-
lastReceived_ms = millis();
124+
if (mqttCount > 0)
125+
{
126+
//Push KEYS or SPARTN data to GNSS module over I2C
127+
myGNSS.pushRawData(mqttData, mqttCount, false);
128+
lastReceived_ms = millis();
129+
}
122130
}
123131

124-
delete[] mgaData;
132+
delete[] mqttData;
125133
}
126134

127135
//Connect to MQTT broker, receive MGA, and push to ZED module over I2C

examples/ZED-F9P/Example18_PointPerfectClient/Example18_PointPerfectClient.ino

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -247,45 +247,43 @@ void loop()
247247
WiFiClientSecure wifiClient = WiFiClientSecure();
248248
MqttClient mqttClient(wifiClient);
249249

250-
void mqttMessageHandler(int messageSize) {
251-
// Testing with /pp/ubx/0236/ip + /pp/ip/eu + /pp/ubx/mga shows the initial data length can be more than 13KBytes
252-
const uint16_t spartnCountLimit = 16384;
253-
uint8_t *spartnData = new uint8_t[spartnCountLimit];
254-
uint16_t spartnCount = 0;
250+
void mqttMessageHandler(int messageSize)
251+
{
252+
const uint16_t mqttLimit = 512;
253+
uint8_t *mqttData = new uint8_t[mqttLimit]; // Allocate memory to hold the MQTT data
254+
if (mqttData == NULL)
255+
{
256+
Serial.println(F("Memory allocation for mqttData failed!"));
257+
return;
258+
}
259+
255260
Serial.print(F("Pushing data from "));
256261
Serial.print(mqttClient.messageTopic());
257262
Serial.println(F(" topic to ZED"));
263+
258264
while (mqttClient.available())
259265
{
260-
char ch = mqttClient.read();
261-
//Serial.write(ch); //Pipe to serial port is fine but beware, it's a lot of binary data
262-
spartnData[spartnCount++] = ch;
263-
if (spartnCount == spartnCountLimit)
266+
uint16_t mqttCount = 0;
267+
268+
while (mqttClient.available())
264269
{
265-
Serial.print(F("Warning!! MQTT data exceeded "));
266-
Serial.print(spartnCountLimit);
267-
Serial.println(F(" bytes!!"));
268-
break;
270+
char ch = mqttClient.read();
271+
//Serial.write(ch); //Pipe to serial port is fine but beware, it's a lot of binary data
272+
mqttData[mqttCount++] = ch;
273+
274+
if (mqttCount == mqttLimit)
275+
break;
269276
}
270-
}
271-
272-
static uint16_t maxSpartnCount = 0;
273-
if (spartnCount > maxSpartnCount)
274-
{
275-
maxSpartnCount = spartnCount;
276-
Serial.print(F("Maximum MQTT data length is "));
277-
Serial.print(maxSpartnCount);
278-
Serial.println(F(" bytes"));
279-
}
280277

281-
if (spartnCount > 0)
282-
{
283-
//Push KEYS or SPARTN data to GNSS module over I2C
284-
myGNSS.pushRawData(spartnData, spartnCount, false);
285-
lastReceived_ms = millis();
278+
if (mqttCount > 0)
279+
{
280+
//Push KEYS or SPARTN data to GNSS module over I2C
281+
myGNSS.pushRawData(mqttData, mqttCount, false);
282+
lastReceived_ms = millis();
283+
}
286284
}
287285

288-
delete[] spartnData;
286+
delete[] mqttData;
289287
}
290288

291289
//Connect to STARTN MQTT broker, receive RTCM, and push to ZED module over I2C

examples/ZED-F9P/Example20_PMP_with_L-Band_Keys_via_MQTT/Example20_PMP_with_L-Band_Keys_via_MQTT.ino

Lines changed: 56 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -328,58 +328,74 @@ void loop()
328328
WiFiClientSecure wifiClient = WiFiClientSecure();
329329
MqttClient 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

Comments
 (0)