Update mqttMessageHandler

This commit is contained in:
PaulZC
2022-03-18 14:41:19 +00:00
parent 94ad6fa1b1
commit 432167b00a
3 changed files with 113 additions and 91 deletions
@@ -93,35 +93,43 @@ void loop()
WiFiClientSecure wifiClient = WiFiClientSecure(); WiFiClientSecure wifiClient = WiFiClientSecure();
MqttClient mqttClient(wifiClient); MqttClient mqttClient(wifiClient);
void mqttMessageHandler(int messageSize) { void mqttMessageHandler(int messageSize)
const uint16_t mgaCountLimit = 16384; {
uint8_t *mgaData = new uint8_t[mgaCountLimit]; const uint16_t mqttLimit = 512;
uint16_t mgaCount = 0; uint8_t *mqttData = new uint8_t[mqttLimit]; // Allocate memory to hold the MQTT data
if (mqttData == NULL)
{
Serial.println(F("Memory allocation for mqttData failed!"));
return;
}
Serial.print(F("Pushing data from ")); Serial.print(F("Pushing data from "));
Serial.print(mqttClient.messageTopic()); Serial.print(mqttClient.messageTopic());
Serial.println(F(" topic to ZED")); Serial.println(F(" topic to ZED"));
while (mqttClient.available()) while (mqttClient.available())
{ {
char ch = mqttClient.read(); uint16_t mqttCount = 0;
//Serial.write(ch); //Pipe to serial port is fine but beware, it's a lot of binary data
mgaData[mgaCount++] = ch; while (mqttClient.available())
if (mgaCount == mgaCountLimit)
{ {
Serial.print(F("Warning!! MQTT data exceeded ")); char ch = mqttClient.read();
Serial.print(mgaCountLimit); //Serial.write(ch); //Pipe to serial port is fine but beware, it's a lot of binary data
Serial.println(F(" bytes!!")); mqttData[mqttCount++] = ch;
break;
if (mqttCount == mqttLimit)
break;
}
if (mqttCount > 0)
{
//Push KEYS or SPARTN data to GNSS module over I2C
myGNSS.pushRawData(mqttData, mqttCount, false);
lastReceived_ms = millis();
} }
} }
if (mgaCount > 0) delete[] mqttData;
{
//Push MGA data to GNSS module over I2C
myGNSS.pushRawData(mgaData, mgaCount, false);
lastReceived_ms = millis();
}
delete[] mgaData;
} }
//Connect to MQTT broker, receive MGA, and push to ZED module over I2C //Connect to MQTT broker, receive MGA, and push to ZED module over I2C
@@ -247,45 +247,43 @@ void loop()
WiFiClientSecure wifiClient = WiFiClientSecure(); WiFiClientSecure wifiClient = WiFiClientSecure();
MqttClient mqttClient(wifiClient); MqttClient mqttClient(wifiClient);
void mqttMessageHandler(int messageSize) { void mqttMessageHandler(int messageSize)
// Testing with /pp/ubx/0236/ip + /pp/ip/eu + /pp/ubx/mga shows the initial data length can be more than 13KBytes {
const uint16_t spartnCountLimit = 16384; const uint16_t mqttLimit = 512;
uint8_t *spartnData = new uint8_t[spartnCountLimit]; uint8_t *mqttData = new uint8_t[mqttLimit]; // Allocate memory to hold the MQTT data
uint16_t spartnCount = 0; if (mqttData == NULL)
{
Serial.println(F("Memory allocation for mqttData failed!"));
return;
}
Serial.print(F("Pushing data from ")); Serial.print(F("Pushing data from "));
Serial.print(mqttClient.messageTopic()); Serial.print(mqttClient.messageTopic());
Serial.println(F(" topic to ZED")); Serial.println(F(" topic to ZED"));
while (mqttClient.available()) while (mqttClient.available())
{ {
char ch = mqttClient.read(); uint16_t mqttCount = 0;
//Serial.write(ch); //Pipe to serial port is fine but beware, it's a lot of binary data
spartnData[spartnCount++] = ch; while (mqttClient.available())
if (spartnCount == spartnCountLimit)
{ {
Serial.print(F("Warning!! MQTT data exceeded ")); char ch = mqttClient.read();
Serial.print(spartnCountLimit); //Serial.write(ch); //Pipe to serial port is fine but beware, it's a lot of binary data
Serial.println(F(" bytes!!")); mqttData[mqttCount++] = ch;
break;
if (mqttCount == mqttLimit)
break;
}
if (mqttCount > 0)
{
//Push KEYS or SPARTN data to GNSS module over I2C
myGNSS.pushRawData(mqttData, mqttCount, false);
lastReceived_ms = millis();
} }
} }
static uint16_t maxSpartnCount = 0; delete[] mqttData;
if (spartnCount > maxSpartnCount)
{
maxSpartnCount = spartnCount;
Serial.print(F("Maximum MQTT data length is "));
Serial.print(maxSpartnCount);
Serial.println(F(" bytes"));
}
if (spartnCount > 0)
{
//Push KEYS or SPARTN data to GNSS module over I2C
myGNSS.pushRawData(spartnData, spartnCount, false);
lastReceived_ms = millis();
}
delete[] spartnData;
} }
//Connect to STARTN MQTT broker, receive RTCM, and push to ZED module over I2C //Connect to STARTN MQTT broker, receive RTCM, and push to ZED module over I2C
@@ -328,58 +328,74 @@ void loop()
WiFiClientSecure wifiClient = WiFiClientSecure(); WiFiClientSecure wifiClient = WiFiClientSecure();
MqttClient mqttClient(wifiClient); MqttClient mqttClient(wifiClient);
void mqttMessageHandler(int messageSize) { void mqttMessageHandler(int messageSize)
uint8_t spartnData[512 * 4]; //Most incoming data is around 500 bytes but may be larger {
int spartnCount = 0; const uint16_t mqttLimit = 512;
Serial.print(F("Pushed data from ")); uint8_t *mqttData = new uint8_t[mqttLimit]; // Allocate memory to hold the MQTT data
Serial.print(mqttClient.messageTopic()); if (mqttData == NULL)
Serial.println(F(" topic to ZED"));
while (mqttClient.available())
{ {
char ch = mqttClient.read(); Serial.println(F("Memory allocation for mqttData failed!"));
//Serial.write(ch); //Pipe to serial port is fine but beware, it's a lot of binary data return;
spartnData[spartnCount++] = ch;
if (spartnCount == sizeof(spartnData))
break;
} }
if (spartnCount > 0) Serial.print(F("Pushing data from "));
{ Serial.print(mqttClient.messageTopic());
//Push KEYS or SPARTN data to GNSS module over I2C Serial.println(F(" topic to ZED"));
myGNSS.pushRawData(spartnData, spartnCount, false);
lastReceived_ms = millis();
if ((spartnData[0] == 0xB5) // Check if this is UBX-RXM-SPARTNKEY while (mqttClient.available())
&& (spartnData[1] == 0x62) {
&& (spartnData[2] == 0x02) // Class: RXM uint16_t mqttCount = 0;
&& (spartnData[3] == 0x36)) // ID: SPARTNKEY
while (mqttClient.available())
{ {
uint8_t numKeys = spartnData[7]; // Get the number of keys char ch = mqttClient.read();
uint8_t keyStart = 10 + (numKeys * 8); // Point to the start of the first key //Serial.write(ch); //Pipe to serial port is fine but beware, it's a lot of binary data
for (uint8_t key = 0; key < numKeys; key++) mqttData[mqttCount++] = ch;
if (mqttCount == mqttLimit)
break;
}
if (mqttCount > 0)
{
//Push KEYS or SPARTN data to GNSS module over I2C
myGNSS.pushRawData(mqttData, mqttCount, false);
lastReceived_ms = millis();
if ((mqttData[0] == 0xB5) // Check if this is UBX-RXM-SPARTNKEY
&& (mqttData[1] == 0x62)
&& (mqttData[2] == 0x02) // Class: RXM
&& (mqttData[3] == 0x36)) // ID: SPARTNKEY
{ {
Serial.print(F("SPARTNKEY: ")); uint8_t numKeys = mqttData[7]; // Get the number of keys
Serial.println(key); uint8_t keyStart = 10 + (numKeys * 8); // Point to the start of the first key
Serial.print(F("Valid from GPS week number: ")); for (uint8_t key = 0; key < numKeys; key++)
uint16_t validFromWno = ((uint16_t)spartnData[12 + (key * 8)]) | ((uint16_t)spartnData[13 + (key * 8)] << 8); // Little endian
Serial.println(validFromWno);
Serial.print(F("Valid from GPS time of week: "));
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);
Serial.println(validFromTow);
uint8_t keyLengthBytes = spartnData[11 + (key * 8)];
Serial.print(F("Key length (bytes): "));
Serial.println(keyLengthBytes);
Serial.print(F("Key: \""));
for (uint8_t digit = 0; digit < keyLengthBytes; digit++)
{ {
Serial.print(spartnData[keyStart + digit] >> 4, HEX); // Print the key as ASCII Hex Serial.print(F("SPARTNKEY: "));
Serial.print(spartnData[keyStart + digit] & 0x0F, HEX); // Print the key as ASCII Hex Serial.println(key);
Serial.print(F("Valid from GPS week number: "));
uint16_t validFromWno = ((uint16_t)mqttData[12 + (key * 8)]) | ((uint16_t)mqttData[13 + (key * 8)] << 8); // Little endian
Serial.println(validFromWno);
Serial.print(F("Valid from GPS time of week: "));
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);
Serial.println(validFromTow);
uint8_t keyLengthBytes = mqttData[11 + (key * 8)];
Serial.print(F("Key length (bytes): "));
Serial.println(keyLengthBytes);
Serial.print(F("Key: \""));
for (uint8_t digit = 0; digit < keyLengthBytes; digit++)
{
Serial.print(mqttData[keyStart + digit] >> 4, HEX); // Print the key as ASCII Hex
Serial.print(mqttData[keyStart + digit] & 0x0F, HEX); // Print the key as ASCII Hex
}
Serial.println(F("\""));
keyStart += keyLengthBytes; // Update keyStart for the next key
} }
Serial.println(F("\""));
keyStart += keyLengthBytes; // Update keyStart for the next key
} }
} }
} }
delete[] mqttData;
} }
//Connect to MQTT broker, receive dynamic keys and push to ZED module over I2C //Connect to MQTT broker, receive dynamic keys and push to ZED module over I2C