I am trying to get an ESP8266 to connect to a my MQTT broker on a PI5.
The PI5 is ok because I can use mosquitto_pub to send a message remotely from another PI.
I am currently not using security on the PI5 so according to the comments in the code I have to set
MQTT_username to NULL.
How?
I have tried MQTT_username = “”
MQTT_username = 0
MQTT_username = NULL
MQTT_username = “NULL”
All of the above results in
Attempting MQTT connection…failed, rc=-2 try again in 5 seconds
Attempting MQTT connection…failed, rc=-2 try again in 5 seconds
I am sure there is a simple answer…
Steve
Hi.
Can you tell me which example exactly you’re following?
If you don’t have username and password you just need to omit them when establishing a connection with the broker.
Tell me the example you’re following and I’ll tell you the changes you need to implement.
Regards,
Sara
I was looking at this tutorial
Start your code here
// Change the credentials below, so your ESP8266 connects to your router
const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";
// MQTT broker credentials (set to NULL if not required)
const char* MQTT_username = "REPLACE_WITH_MQTT_USERNAME";
const char* MQTT_password = "REPLACE_WITH_MQTT_PASSWORD";
// Change the variable to your Raspberry Pi IP address, so it connects to your MQTT broker
const char* mqtt_server = "YOUR_BROKER_IP_ADDRESS";
It is the two lines for MQTT username and password when they are not needed.
I do now know whether I have a faulty ESP8266. because I have also looked at this tutorial
ESP8266 NodeMCU MQTT – Publish DS18B20 Temperature Readings (Arduino IDE)
I get the following error
Connecting to MQTT…
Publishing on topic esp/ds18b20/temperature at QoS 1, packetId: 0 Message: 18.56
Publishing on topic esp/ds18b20/temperature at QoS 1, packetId: 0 Message: 18.56
Publishing on topic esp/ds18b20/temperature at QoS 1, packetId: 0 Message: 18.56
Disconnected from MQTT.
Connecting to MQTT…
However, I would like to know what I set the username and passoerd variables too if they are not needed.
thanks
Steve
Hi.
I just took a look at the code.
If you don’t have credentials on your broker, you just need to upload the code as it is because the following line that sets the credentials is commented.
//mqttClient.setCredentials("REPlACE_WITH_YOUR_USER", "REPLACE_WITH_YOUR_PASSWORD");
It was something to do with my Visual Studio Code project, do not know what !
I re-built the project using arduino and it works, well, it is connecting to the broker and sending messages, just can’t see them on my node red project, but that is another problem.
Thanks for the response
Steve
Hi.
that issue is probably because you’re not subscribing to the right topics, or node-red fails to communicate with the broker.
Do you see a green “connected” message under the MQTT node?
Regards,
Sara
Yes it is connected, I am starting to look at some of the logs and see wha5 is happening.
steve
Ok.
Make sure it is subscribed to the correct topic.
Let me know if you need further help.
Regards,
Sara
It is working this morning, I am not sure what I have done, maybe changed the order in which things have booted up.
But temperature is now being displayed on my node-red dashboard
One last question on mqtt, have you ever subscribed to $SYS messages to debug what is happening?
I tried but could not see any output
mosquitto_sub -h localhost -t “$SYS/#”
Steve