• 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

Controlling Outputs with ESP using MQTT

Q&A Forum › Category: Home Automation › Controlling Outputs with ESP using MQTT
0 Vote Up Vote Down
Jayme Nery Filho asked 3 years ago

Hello again,
I am now having trouble to connect tp the MQTT server installed in my Raspberry pi.
I am following the instructions in the course “Build a Home Automation System for $100”.
It all works well until Module 7 – Unit 1.
I have Node-Red and mosquitto (v. 2.011, on port 1883) installed in the RPI, all parts up to here worked well,
Putty finds the RPI and I can connect and execute commands remotelly from a windows computer.
But when I try the program on Unit 1 (page 118) I cannot manage to get a connection.
I have always a  message: Connection failed rc=-2…
On Node-Red the flow shows the status of the nodes as ‘connected’, but no message is received.
What could be possibly wrong?
Thanks,
Jayme

Question Tags: MQTT Broker connection
7 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 3 years ago

Hi.
Did you insert the Raspberry Pi IP address on your ESP code?
Did you change anything on the code? Have you modified the topics?
Did you edit the Node-RED MQTT nodes to use the localhost as Mosquitto broker?
Regards,
Sara

0 Vote Up Vote Down
Jayme Nery Filho answered 3 years ago

Hi Sara, yes I have the same IP address in the ESP code (same IP that tested ok with Putty).
No, I have made no changes to the code, only changed the wifi data and RPI IP.
One thing seems strange to me. To access the RPI using PUTTY, I have to supply the RPI user and password. But I don’t see that on the ESP code. The rc=-2 means it failed to connect to network, right? Isn’t it because it can’t log to RPI as user/pswd is missing?

I have MQTT broker defined on Node-red nodes as localhost:1883, but nowhere I specified that the broker is Mosquitto. Is that necessary?

thanks!
Jayme

0 Vote Up Vote Down
Sara Santos Staff answered 3 years ago

Hi again.
 
I’m really sorry for that issue.
I think it is related to the newest mosquito version.
It requires some additional steps during installation. That is not covered in the eBook because at the time of writing the book, mosquito was on version 1.
 
Can you check this tutorial and install the mosquito broker again, but following these instructions: https://randomnerdtutorials.com/how-to-install-mosquitto-broker-on-raspberry-pi/
 
Let me know if this solves the issue.
 
We really need to update the Home Automation eBook and we’ll start working on that soon.
 
Regards,
Sara
 

0 Vote Up Vote Down
Jayme Nery Filho answered 3 years ago

Hello Sara, I tried the indicated solution, but I am sorry to say that the result is still the same.
I included the 2 lines in the Mosquitto config file, saved it restarted the Pi.
I also downloaded a fresh program listing, and changed the parameters, paying attention to the correct pi IP address (confirmed once again with Putty, which connected perfectly).
I can ping and also ssh the pi from my windows desktop.
But still receive same rc=-2 error message when running the sample in page 118.
I am really confused about what could be the issue, since it does not seem to be on the Pi side, as it is responding to putty, ping and ssh.
To me it looks like something is missing in the ESP8266 program to be able to connect to the pi (even before accessing Mosquitto).
Any other ideas about what I could try?
Thanks a lot for your support!
BR
Jayme
 
 
 

0 Vote Up Vote Down
Sara Santos Staff answered 3 years ago

Hi.
I tested the setup and the ESP8266 is connecting properly to the Mosquitto broker.
Are the ESP8266 and the Raspberry Pi on the same network?
How did you get the Pi IP address? Can you show me the line of code where you insert the IP address?
I think you might need to uninstall mosquito and install it again with the new instructions. Maybe the changes didn’t take effect.
Regards,
Sara

0 Vote Up Vote Down
Jayme Nery Filho answered 3 years ago

Hi Sara, thank you for your support.
just to give you a quick feedback that the issue was solved.
for some reason I was having error messages when the config file was loaded, but didn’t realize that.
I took out all lines leaving only the two added for version 2 of Mosquitto. Somehow that solved the issue.
 Thanks again,
Jayme

0 Vote Up Vote Down
Sara Santos Staff answered 3 years ago

Great!
I’m glad the issue is solved.
I’ll mark this issue as resolved. If you need further help, you just need to open a new question in our forum.
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

  • [eBook Updated] Learn Raspberry Pi Pico/Pico W with MicroPython eBook – Version 1.2 May 26, 2025
  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition 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.