• 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

Connect ESP32 to Cloud MQTT Broker (Digital Ocean) – Connection fails

Q&A Forum › Category: ESP32 › Connect ESP32 to Cloud MQTT Broker (Digital Ocean) – Connection fails
0 Vote Up Vote Down
Carlos Garzon asked 1 year ago

Hello. I am trying to connect the TCALL ESP32 SIM800L to a Digital Ocean droplet via MQTT broker, using the code from this tutorial: https://randomnerdtutorials.com/esp32-cloud-mqtt-broker-sim800l/#more-99705

Unfortunately, the connection fails. I am using the IP address of that droplet and the user and password configured in mosquitto.
Any idea what am I doing wrong?

Question Tags: MQTT
19 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 1 year ago

Hi.
Can you provide more information about the error you’re getting?
Regards,
Sara

0 Vote Up Vote Down
Carlos Garzon answered 1 year ago

Hi Sara, it is difficult to get more information about the error, as far as I know. The part of the code trying to make the connection is this:

 boolean status = mqtt.connect("GsmClientN", mqttUsername, mqttPassword);

  if (status == false) {
    SerialMon.println(" fail");
    ESP.restart();
    return false;
  }

Is this code correct? 

0 Vote Up Vote Down
Sara Santos Staff answered 1 year ago

Hi.
Do you know if your module is connecting correctly to the internet?
Regards,
Sara

0 Vote Up Vote Down
Carlos Garzon answered 1 year ago

Hi Sara. Apparently, the SIM card works well and this part of the code works properly;

 SerialMon.print("Connecting to APN: ");
  SerialMon.print(apn);
  if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
    SerialMon.println(" fail");
    ESP.restart();
  }
  else {
    SerialMon.println(" OK");
  }
  
  if (modem.isGprsConnected()) {
    SerialMon.println("GPRS connected");
  }

I get OK and GPRS connected. 

0 Vote Up Vote Down
Sara Santos Staff answered 1 year ago

Hi again.
Have you tried connecting to the broker using a “regular” ESP32?

Just to make sure we can make a connection with the broker…

Regards,
Sara

0 Vote Up Vote Down
Carlos Garzon answered 1 year ago

Hi Sara, I can try with an ESP32 with WIFI conectivity, for example following your tutorial:
Cloud MQTT Mosquitto Broker – Access Anywhere Digital Ocean | Random Nerd Tutorials
The first problem I get is that the tutorial refers to a library called ‘Async TCP’, but the code is actually using another one called ‘AsyncMqttClient’. So, that code cannot work.
Regards

 
0 Vote Up Vote Down
Sara Santos Staff answered 1 year ago

Hi.
I’m sorry, but I didn’t understand your issue…
The code uses both libraries. Check the part about installing libraries. We mention to install both.
The AsyncMQTT client needs the AsyncTCP library to work.
You can test that code by installing the right libraries and see if the board connects to the broker.
 
Regards,
Sara

0 Vote Up Vote Down
Sara Santos Staff answered 1 year ago

Another thing. Do you have more than one board trying to connect to that broker with the same code?
If so, they should have a different MQTTClient name. See the following line.

  boolean status = mqtt.connect("GsmClientN", mqttUsername, mqttPassword);

The name is “GsmClientN”. If you have more than one board, they should have different name.

0 Vote Up Vote Down
Carlos Garzon answered 1 year ago

Thanks Sara. No, I only have one board connected (or trying to). But that name “GsmClientN” is something we need to set up in the server?

0 Vote Up Vote Down
Sara Santos Staff answered 1 year ago

No.
You don’t need to do that.
Regards,
Sara

0 Vote Up Vote Down
Carlos Garzon answered 1 year ago

Hello. Now I have installed both Mqtt libraries and trying the code on page 152 of the Smart Home book with ESP-Dev module. It is also not connecting to MQTT:

Connecting to MQTT...
Disconnected from MQTT.
Connecting to MQTT...
Disconnected from MQTT.
Connecting to MQTT...

 

0 Vote Up Vote Down
Carlos Garzon answered 1 year ago

Now it works, after re-establishing the password in mosquito:

IP address: 
192.168.178.119
Connecting to MQTT...
Connected to MQTT.
Session present: 0
Subscribing at QoS 2, packetId: 167
Publishing at QoS 0
Publishing at QoS 1, packetId: 168
Publishing at QoS 2, packetId: 169
Subscribe acknowledged.
  packetId: 167
  qos: 2
Publish acknowledged.
  packetId: 168
Publish acknowledged.
  packetId: 169

 

0 Vote Up Vote Down
Sara Santos Staff answered 1 year ago

Great.
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

0 Vote Up Vote Down
Carlos Garzon answered 1 year ago

Hi Sara, Sorry, the connection using ESP32 via WiFi is resolved, but my initial issue (see the initial question) was regarding the connection of an ESP32 T-CALL via gsm, which is still not resolved. Using the same mosquito credentials does not solve the problem for the gsm connection.

0 Vote Up Vote Down
Sara Santos Staff answered 1 year ago

Hi again.
I’m sorry, you’re right.
 
So, it means the broker is working….
 
Try changing the name of your device… give a different name than GsmClientN:

 boolean status = mqtt.connect("GsmClientN", mqttUsername, mqttPassword);

Let me know the results.
Regards,
Sara

0 Vote Up Vote Down
Carlos Garzon answered 1 year ago

Hi Sara. I am still struggling with this. I was wondering if we need to open some ports in the Firewall of the droplet at Digital Ocean? However, with an ESP32 board with Wifi it works. So, must be something related to the SIM card, but I have tried different cards from different operators.

0 Vote Up Vote Down
Sara Santos Staff answered 1 year ago

Hi Carlos.
 
I’m not sure what might be the problem.
In our case, we didn’t need to do anything different from what is described in the tutorial…
 
What do you get exactly on the serial monitor with that board? Does it connect to the internet properly first? Does it connect and get data service?
 
Regards,
Sara

0 Vote Up Vote Down
Carlos Garzon answered 1 year ago

SOLVED!!
I have first tested the connectivity to a public mqtt like test.mosquitto.org, and it was ok.
Then I tested the mosquitto broker installed at the digital ocean droplet using this tool: mqtt-explorer.com, and noticed that the stablished port was 8883 and not 1883. Then I corrected the port in the script and it works!!  

0 Vote Up Vote Down
Sara Santos Staff answered 1 year ago

Hi.
Great.
I’m glad that you finally found the solution.
Thanks for sharing it.
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.