monstrenyatko / ArduinoMqtt

MQTT client for Arduino
MIT License
72 stars 13 forks source link

Username and Key #17

Closed rapasal closed 6 years ago

rapasal commented 6 years ago

I cannot for the life of me see where to enter a username and key for a broker. I've tried adding the following in amongst the other options that are set in the examples:

unsigned char user[10] = "mqtt_username";
unsigned char pass[40] = "mqtt_key";
unsigned char *uPoint = user;
unsigned char *pPoint = pass;
writeMQTTString(&uPoint, options.username);
writeMQTTString(&pPoint, options.password);

This compiles, but I still cannot get anything in or out of the broker. I am assuming this is to do with my credentials. Error output as below:

.
.
.
Connected to WiFi
IP: 192.168.1.118
Connecting
MQTT - Connect, clean-session: 1, ts: 47011
MQTT - Wait for message, type: 2, tm: 9999 ms
MQTT - Process message, type: 2
MQTT - Connect ack received
MQTT - Connect ack, code: 0
MQTT - Keepalive interval: 12 sec
MQTT - Session is not present => reset subscription
MQTT - Subscribe, to: mqtt_username/feed/sub, qos: 0
MQTT - Wait for message, type: 9, tm: 9995 ms
MQTT - Process message, type: 3
MQTT - Publish received, qos: 0
MQTT - Unexpected message
MQTT - Process message, type: 9
MQTT - Subscribe ack received
Subscribe error: -2
Drop connection
MQTT - Disconnecting, ts: 47249
Connecting
Can't establish the TCP connection

This is running on an ESP Huzzah with a WiFiClient. Can anyone point me in the right direction?

monstrenyatko commented 6 years ago

@rapasal You do not need to use writeMQTTString function. In ConnectEsp8266WiFiClient example you can find the connect paramaters structure: MQTTPacket_connectData options. It contains username and password fields:

The MQTTString type could be initialized just with symple c-string like I did for options.clientID:

options.clientID.cstring = (char*)MQTT_ID;

so the following should work (please adjust to the types of user and pass):

options.username.cstring = (char*) user;
options.password.cstring = (char*) pass;
rapasal commented 6 years ago

I thought there'd be a cleaner way of doing it. Working great now! Thanks.