Git Product home page Git Product logo

Comments (6)

Teddyz avatar Teddyz commented on July 30, 2024

My best guess is that your code failed to read 'MQTT_User' from the json-file.
I think the single u means that no username was provided. This is how it looks for me.
1502949323: New client connected from 192.168.1.77 as mega2560 (c1, k60, u'mega2560').

Good luck!

from arduino-mqtt.

Urs-Eppenberger avatar Urs-Eppenberger commented on July 30, 2024

Thanks for the support. You have even looked at my code!
I analysed the mosquitto logfile closer. I have an entry from a working connection by the same code, but different MQTT library (imroy):
1502516513: New client connected from 192.168.11.80 as sonoffth1 (c1, k15, u'mqtt').
The MQTT clientID is placed before the bracket. The user (mqtt) is inside the bracket. The password is (luckily) not in the logfile. My conclusion is:

  • the ClientID is either not sent or missing
  • the username is missing too

Therefore the problem is even worse, but at least consistent. Maybe Joël has an idea.
Kind regards,
Urs.

from arduino-mqtt.

256dpi avatar 256dpi commented on July 30, 2024

Dear Urs,

I can ensure you that the arduino-mqtt library is correctly passing along the client-id, username and password parameters if they are correct C strings.

But in your code I can see that you are allocating the StaticJsonBuffer<700> jsonBuffer; on the stack in setup(). Without knowing the ArduinoJSON library by heart I believe that the derived C strings from the JSON object are just pointers to parts of that buffer. In other words, the memory is reused when leaving setup() and entering loop(). Now since 700 bytes are a big portion of memory the data might still be there, and thats why you probaly run into inconsistencies between the libraries as they affect the memory layout.

I hope that helps.

Best,
Joël

from arduino-mqtt.

Urs-Eppenberger avatar Urs-Eppenberger commented on July 30, 2024

Hello Joël, thanks for your feedback and for having a look at my code.
I started to suspect something like that yesterday. I'm not a programmer, especially not in C and really do not have a clue about the difference between pointers and why the heck something which is once in a variable just disappears sometimes later after a few hours of running. Really. This is stupid. But it just happened with my code.
Since I have no clue about programming, I said to myself, if I copy the variable content into another variable where there is no pointer strangeness, it might solve my problem. Here is what I did, most probably ugly as hell. I just copy the code section which handle the MQTT client ID:

In the top section:
const char* Hostname;
String mqtt_id_s;
const char* mqtt_id_p;

In the setup() section:
StaticJsonBuffer<700> jsonBuffer;
JsonObject& readc = jsonBuffer.parseObject(buf.get());
Hostname = readc["Hostname"];
mqtt_id_s = String(Hostname);
mqtt_id_p = mqtt_id_s.c_str();

And then where the MQTT connection to the broker is started:
mqttClient.connect(mqtt_id_p, mqtt_user_p, mqtt_pass_p);

So far I do not see the above mentioned problems in the log. So everything seems fine.
If someone more knowledgeable than me on this topic (and there must be thousands out there) can help to make this into better code, please drop a note.
Kind regards,
Urs.

from arduino-mqtt.

256dpi avatar 256dpi commented on July 30, 2024

Converting the C string to a String object is a good approach as it will automatically allocate memory on the heap to store the data.

FYI: C has no support for string objects as other programming languages do. A const char * just points to the address of the first byte of the string. By convention any string operation will stop if a null byte is found e.g. \0. Thus, if you copy a const char * you just copy an address and not its contents. That's why Arduino provides the String object.

from arduino-mqtt.

Urs-Eppenberger avatar Urs-Eppenberger commented on July 30, 2024

Thanks for your support, I really appreciate it.
With your last hint I'm now able to make the code a little prettier, now that it works and I even understand why.

from arduino-mqtt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.