• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

How do I set MQTT_username to NUll

Q&A Forum › Category: Other › How do I set MQTT_username to NUll
0 Vote Up Vote Down
Steve Gale asked 7 months ago

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

Question Tags: MQTT
9 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 7 months ago

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

0 Vote Up Vote Down
Steve Gale answered 7 months ago

I was looking at this tutorial
 

ESP8266 and Node-RED with MQTT (Publish and Subscribe)


 

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

 
0 Vote Up Vote Down
Sara Santos Staff answered 7 months ago

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");
0 Vote Up Vote Down
Steve Gale answered 7 months ago

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
 

0 Vote Up Vote Down
Sara Santos Staff answered 7 months ago

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

0 Vote Up Vote Down
Steve Gale answered 7 months ago

Yes it is connected, I am starting to look at some of the logs and see wha5 is happening.
 
steve

0 Vote Up Vote Down
Sara Santos Staff answered 7 months ago

Ok.
Make sure it is subscribed to the correct topic.
Let me know if you need further help.
Regards,
Sara

0 Vote Up Vote Down
Steve Gale answered 7 months ago

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

0 Vote Up Vote Down
Sara Santos Staff answered 7 months ago

Hi.
I’m glad the issue is solved.
 
No, I haven’t tried it. I’m not sure exactly how it works…
Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025
  • [eBook Updated] Learn ESP32 with Arduino IDE eBook – Version 3.2 April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.