knolleary/pubsubclient — Issue #6: Incorrect output, length error when using a String object
Author: DougieLawson
Created: 2012-03-08 00:58:13+00:00
State: open

I've got these objects defined in the prolog for my code:

/* DCF 77 signal and MQTT */
#define DEBUG

String MQTTbuffer;
char MQTTtext[400];
boolean DCF77signal[62];

I then build the string to send to Mosquitto using this:

void displayTime() {

MQTTbuffer = "** ";
for (i = 0; i < 63; i++) {
MQTTbuffer += DCF77signal[i];
}
MQTTbuffer += " **\r\n";
MQTTbuffer +="TZ:";
MQTTbuffer +=BCDtoDecimal(17,2);
MQTTbuffer +="\tmm:";
MQTTbuffer +=BCDtoDecimal(21,7);
MQTTbuffer +="\thh:";
MQTTbuffer +=BCDtoDecimal(29,6);
MQTTbuffer +="\tday:";
MQTTbuffer +=BCDtoDecimal(36,6);
MQTTbuffer +="\tdw:";
MQTTbuffer +=BCDtoDecimal(42,3);
MQTTbuffer +="\tmth:";
MQTTbuffer +=BCDtoDecimal(45,5);
MQTTbuffer +="\tyy:";
MQTTbuffer +=BCDtoDecimal(50,8);
MQTTbuffer +="\tParity:";
MQTTbuffer +=BCDtoDecimal(60,3);
MQTTbuffer.toCharArray(MQTTtext,400);

#if DEBUG
for (int i = 0; i < 400; i ++) {
Serial.print(MQTTtext[i],HEX);
}
#endif

if (client.connect("arduinoClient")) {
client.publish("arduino/DCF77",MQTTtext);
client.disconnect();
}
if (client.connect("arduinoClient")) {
client.publish("arduino/DCF77","DONE");
client.disconnect();
}
}

That gets the following printed on the Serial console:

2A2A20303130303131303030313131303130303031303130313130303031313130303030303130303031303030303131313030303031303031303030303030303130202A2ADA545A3A3196D6D3A3633968683A323096461793A34964773A3696D74683A31979793A3995061726974793A32000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

So as far as I can tell the char array MQTTtext has been built correctly (until someone proves otherwise).

But the send to Mosquitto is rejected with a length error.

Looking at a tcpdump (with Wireshark) I get the following:

CONNECT
00000000 10 .
00000001 1b .
00000002 00 06 4d 51 49 73 64 70 03 02 00 0f 00 0d 61 72 ..MQIsdp ......ar
00000012 64 75 69 6e 6f 43 6c 69 65 6e 74 duinoCli ent

CONNACK
00000000 20 02 00 00 ...

PUBLISH
0000001D 30 0
0000001E 83 .
0000001F 00 0d 61 72 64 75 69 6e 6f 2f 44 43 46 37 37 2a ..arduin o/DCF77*
0000002F 2a 20 30 30 30 30 30 30 30 30 30 30 30 30 30 30 * 000000 00000000
0000003F 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 00000000 00000000
0000004F 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 00000000 00000000
0000005F 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 00000000 00000000
0000006F 30 20 2a 2a 0d 0a 54 5a 3a 30 09 6d 6d 3a 30 09 0 **..TZ :0.mm:0.
0000007F 68 68 3a 30 09 64 61 79 3a 30 09 64 77 3a 30 09 hh:0.day :0.dw:0.
0000008F 6d 74 68 3a 30 09 79 79 3a 30 09 50 61 72 69 74 mth:0.yy :0.Parit
0000009F 79 3a 30 y:0
000000A2 e0 <=== stray byte .
000000A3 00 <=== stray null .

That x'E000' on the tail end of TCP stream (it's two extra packets) causes a length error.

I've not yet worked out where in your code those two stray bytes come from.