Issue with publishing data to mqtt broker [SOLVED]

Hello,
I have an issue with publishing data form software serial to mqtt boker, a code publishing only 2 of 6 bytes. I suspect it’s caused that the third byte of data is 0 in DEC. My data in serial monitor looks like: 33,22,0,0,12, mqtt broker shows only 33 and 22.
Could you please help me?
if (swSerial.available())
{
lastCharTime = millis();
charBuff[charCount] = swSerial.read();
charCount++;
if (charCount == 6)
{
Serial.print("R: ");
for (int i = 0; i < 5; i++)
{
Serial.print(charBuff*, DEC);*

  • Serial.print(",");*
  • }*
  • Serial.println(charBuff[5], DEC);*
    _ mqttClient.publish(“topic/diagnostic”, (char *)charBuff);_
  • charCount = 0;*
  • }*
  • }SOLVED]*
    sketch_jul04a.ino (444 Bytes)

gibzwein:
Hello, I suspect it's caused that the third byte of data is 0 in DEC

That's exactly why. It looks like your MQTT library takes a C string parameter for the payload and C strings are terminated by a NULL (zero). What content are you trying to send?

I'm trying to send exactly what I got from software serial, as above 33 22 0 0 12 66 or something similiar.

I was really asking what the data means, what produced it. In any event, you will have to encode it in some way to send it successfully using that method - there can't be any zeros in your payload. Which library are you using?

I;m using PubSubClient, it's some kind of driver, data is state, temp, err, err, don't know yet, crc. That's why these bytes are usually 0. I woud like to receive in broker as it is in serial monitor in decimals e.g. 33, 22, 0, 0, 12, 66.

You can do it using sprintf. It’s a bit fiddly though. Here’s an example that does the first three characters:

char buf[]={33,22,0,0,12,99};
char out[32];

void setup()
{
Serial.begin(115200);
for(int lp=0;lp<6;lp++)
  {
  Serial.println(buf[lp],DEC);
  }
sprintf(out,"%d,%d,%d",(int)buf[0],(int)buf[1],(int)buf[2]);
Serial.println(out);
}

void loop()
{
}

Thank You very much wildbill.
sprintf is what I was looking for.