Comments (6)
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.
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.
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.
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.
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.
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)
- MQTT reconnection when not disconnected. ESP8266 HOT 6
- Unreliable behaviour with reconnections HOT 3
- MQTT HOT 1
- Websockets support HOT 3
- MQTT SSL connection issue with QoS1 and QoS2 messages HOT 1
- DCU: cannot connect to local MQTT broker HOT 1
- This library uses some posix functions that are not provided for Uno R4 HOT 3
- Subscribe query HOT 1
- Release version `v2.5.1` different of master version HOT 1
- 0
- Error connect MQTT SSL/TLS with module CBU use platform libretiny
- RP2040 seems to work fine HOT 2
- payload length / reconnect HOT 1
- Random error in MQTTClient::loop() function HOT 2
- Cosmetics only in MQTTClient.h / MQTTClient(...)
- Wrong version in README.md?
- ESP32 PSRAM for MQTT buffer?
- if available bytes on the network is available, close connection. HOT 1
- MQTT5 HOT 1
- subscribe fails HARD
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from arduino-mqtt.