Update mqttMessageHandler
This commit is contained in:
+23
-15
@@ -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())
|
||||||
|
{
|
||||||
|
uint16_t mqttCount = 0;
|
||||||
|
|
||||||
while (mqttClient.available())
|
while (mqttClient.available())
|
||||||
{
|
{
|
||||||
char ch = mqttClient.read();
|
char ch = mqttClient.read();
|
||||||
//Serial.write(ch); //Pipe to serial port is fine but beware, it's a lot of binary data
|
//Serial.write(ch); //Pipe to serial port is fine but beware, it's a lot of binary data
|
||||||
mgaData[mgaCount++] = ch;
|
mqttData[mqttCount++] = ch;
|
||||||
if (mgaCount == mgaCountLimit)
|
|
||||||
{
|
if (mqttCount == mqttLimit)
|
||||||
Serial.print(F("Warning!! MQTT data exceeded "));
|
|
||||||
Serial.print(mgaCountLimit);
|
|
||||||
Serial.println(F(" bytes!!"));
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (mgaCount > 0)
|
if (mqttCount > 0)
|
||||||
{
|
{
|
||||||
//Push MGA data to GNSS module over I2C
|
//Push KEYS or SPARTN data to GNSS module over I2C
|
||||||
myGNSS.pushRawData(mgaData, mgaCount, false);
|
myGNSS.pushRawData(mqttData, mqttCount, false);
|
||||||
lastReceived_ms = millis();
|
lastReceived_ms = millis();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
delete[] mgaData;
|
delete[] mqttData;
|
||||||
}
|
}
|
||||||
|
|
||||||
//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())
|
||||||
|
{
|
||||||
|
uint16_t mqttCount = 0;
|
||||||
|
|
||||||
while (mqttClient.available())
|
while (mqttClient.available())
|
||||||
{
|
{
|
||||||
char ch = mqttClient.read();
|
char ch = mqttClient.read();
|
||||||
//Serial.write(ch); //Pipe to serial port is fine but beware, it's a lot of binary data
|
//Serial.write(ch); //Pipe to serial port is fine but beware, it's a lot of binary data
|
||||||
spartnData[spartnCount++] = ch;
|
mqttData[mqttCount++] = ch;
|
||||||
if (spartnCount == spartnCountLimit)
|
|
||||||
{
|
if (mqttCount == mqttLimit)
|
||||||
Serial.print(F("Warning!! MQTT data exceeded "));
|
|
||||||
Serial.print(spartnCountLimit);
|
|
||||||
Serial.println(F(" bytes!!"));
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
static uint16_t maxSpartnCount = 0;
|
if (mqttCount > 0)
|
||||||
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
|
//Push KEYS or SPARTN data to GNSS module over I2C
|
||||||
myGNSS.pushRawData(spartnData, spartnCount, false);
|
myGNSS.pushRawData(mqttData, mqttCount, false);
|
||||||
lastReceived_ms = millis();
|
lastReceived_ms = millis();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
delete[] spartnData;
|
delete[] mqttData;
|
||||||
}
|
}
|
||||||
|
|
||||||
//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
|
||||||
|
|||||||
+34
-18
@@ -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
|
||||||
|
if (mqttData == NULL)
|
||||||
|
{
|
||||||
|
Serial.println(F("Memory allocation for mqttData failed!"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
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())
|
||||||
|
{
|
||||||
|
uint16_t mqttCount = 0;
|
||||||
|
|
||||||
while (mqttClient.available())
|
while (mqttClient.available())
|
||||||
{
|
{
|
||||||
char ch = mqttClient.read();
|
char ch = mqttClient.read();
|
||||||
//Serial.write(ch); //Pipe to serial port is fine but beware, it's a lot of binary data
|
//Serial.write(ch); //Pipe to serial port is fine but beware, it's a lot of binary data
|
||||||
spartnData[spartnCount++] = ch;
|
mqttData[mqttCount++] = ch;
|
||||||
if (spartnCount == sizeof(spartnData))
|
|
||||||
|
if (mqttCount == mqttLimit)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (spartnCount > 0)
|
if (mqttCount > 0)
|
||||||
{
|
{
|
||||||
//Push KEYS or SPARTN data to GNSS module over I2C
|
//Push KEYS or SPARTN data to GNSS module over I2C
|
||||||
myGNSS.pushRawData(spartnData, spartnCount, false);
|
myGNSS.pushRawData(mqttData, mqttCount, false);
|
||||||
lastReceived_ms = millis();
|
lastReceived_ms = millis();
|
||||||
|
|
||||||
if ((spartnData[0] == 0xB5) // Check if this is UBX-RXM-SPARTNKEY
|
if ((mqttData[0] == 0xB5) // Check if this is UBX-RXM-SPARTNKEY
|
||||||
&& (spartnData[1] == 0x62)
|
&& (mqttData[1] == 0x62)
|
||||||
&& (spartnData[2] == 0x02) // Class: RXM
|
&& (mqttData[2] == 0x02) // Class: RXM
|
||||||
&& (spartnData[3] == 0x36)) // ID: SPARTNKEY
|
&& (mqttData[3] == 0x36)) // ID: SPARTNKEY
|
||||||
{
|
{
|
||||||
uint8_t numKeys = spartnData[7]; // Get the number of keys
|
uint8_t numKeys = mqttData[7]; // Get the number of keys
|
||||||
uint8_t keyStart = 10 + (numKeys * 8); // Point to the start of the first key
|
uint8_t keyStart = 10 + (numKeys * 8); // Point to the start of the first key
|
||||||
for (uint8_t key = 0; key < numKeys; key++)
|
for (uint8_t key = 0; key < numKeys; key++)
|
||||||
{
|
{
|
||||||
Serial.print(F("SPARTNKEY: "));
|
Serial.print(F("SPARTNKEY: "));
|
||||||
Serial.println(key);
|
Serial.println(key);
|
||||||
Serial.print(F("Valid from GPS week number: "));
|
Serial.print(F("Valid from GPS week number: "));
|
||||||
uint16_t validFromWno = ((uint16_t)spartnData[12 + (key * 8)]) | ((uint16_t)spartnData[13 + (key * 8)] << 8); // Little endian
|
uint16_t validFromWno = ((uint16_t)mqttData[12 + (key * 8)]) | ((uint16_t)mqttData[13 + (key * 8)] << 8); // Little endian
|
||||||
Serial.println(validFromWno);
|
Serial.println(validFromWno);
|
||||||
Serial.print(F("Valid from GPS time of week: "));
|
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);
|
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);
|
Serial.println(validFromTow);
|
||||||
uint8_t keyLengthBytes = spartnData[11 + (key * 8)];
|
uint8_t keyLengthBytes = mqttData[11 + (key * 8)];
|
||||||
Serial.print(F("Key length (bytes): "));
|
Serial.print(F("Key length (bytes): "));
|
||||||
Serial.println(keyLengthBytes);
|
Serial.println(keyLengthBytes);
|
||||||
Serial.print(F("Key: \""));
|
Serial.print(F("Key: \""));
|
||||||
for (uint8_t digit = 0; digit < keyLengthBytes; digit++)
|
for (uint8_t digit = 0; digit < keyLengthBytes; digit++)
|
||||||
{
|
{
|
||||||
Serial.print(spartnData[keyStart + digit] >> 4, HEX); // Print the key as ASCII Hex
|
Serial.print(mqttData[keyStart + digit] >> 4, HEX); // Print the key as ASCII Hex
|
||||||
Serial.print(spartnData[keyStart + digit] & 0x0F, HEX); // Print the key as ASCII Hex
|
Serial.print(mqttData[keyStart + digit] & 0x0F, HEX); // Print the key as ASCII Hex
|
||||||
}
|
}
|
||||||
Serial.println(F("\""));
|
Serial.println(F("\""));
|
||||||
keyStart += keyLengthBytes; // Update keyStart for the next key
|
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
|
||||||
|
|||||||
Reference in New Issue
Block a user