Go Down

Topic: No MQTT Publish (Read 177 times) previous topic - next topic

DorNickie

Feb 10, 2018, 06:54 pm Last Edit: Feb 10, 2018, 07:04 pm by DorNickie
Hello, i try to send the ir raw code via Mqtt, but it were only recieved when it is shorter then normally, for example failmeasures). In the serial console i will get the message trough. Have my Char array an overflow or something else? I hope you can help me!

Code: [Select]

if (irrecv.decode(&results)) {

   
    String myString;
    for (int i = 1;  i < results.rawlen;  i++)
    {
      myString+= (results.rawbuf[i] * RAWTICK);
      if ( i < results.rawlen-1 )
        myString+=",";
    }

    char myValue[myString.length()];
    myString.toCharArray(myValue,myString.length());
    Serial.println(myValue);

    client.publish("Wohnung/ESP/IR/REC/",myValue);
    irrecv.resume();
  }


used librarys:
Code: [Select]
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <IRremoteESP8266.h>
#include <IRrecv.h>
#include <IRsend.h>
#include <IRutils.h>


normal output via serial connection:
Code: [Select]

528,596,526,596,526,594,526,596,526,596,528,594,528,1716,528,596,526,594,528,594,528,1716,528,1716,528,598,526,1716,528,596,526,1716,528,1694,550,1718,528,596,526,594,528,1716,52


fail measure via Mqtt:
Code: [Select]
526,594,526,1716,528,596,526,1716,528,1716,528,1694,550,594,530,594,526,1692,55

I hope you can understand my english. greetings from germany

PaulS

Code: [Select]
    for (int i = 1;  i < results.rawlen;  i++)
    {
      myString+= (results.rawbuf[i] * RAWTICK);

Array indexes start at 0. If you have some valid reason for not starting from 0, you should document that with a comment.

ASSuming that the concatenation operation succeeded is a really poor idea.

Code: [Select]
    char myValue[myString.length()];
    myString.toCharArray(myValue,myString.length());

The second argument to the toCharArray() method is the number of characters that the array can hold, NOT the number of characters in the String. The String instance already KNOWS that.

You are NOT allowing space for the required NULL terminator.

There is NO excuse for using the String class. But, if you just must, because you don't know any better, and won't learn a better way, then it is completely unnecessary to make a copy of the string that the String wraps. The c_str() method exposes the wrapped string, which is all that publish() cares about.

This seems like a complete abuse of the MQTT publish/subscribe mechanism. It is NOT intended to pass novels back and forth.
The art of getting good answers lies in asking good questions.

Go Up