Table of Contents:
MQTT Errors:
If you are using the MQTT protocol in any of your IoT projects to communicate with IoT Cloud, then you might aware of the following errors.
Attempting MQTT connection… Failed. rc=-1, or rc=-2 or rc=-4.
These are the general errors you will get while trying to communicate with any MQTT servers.
my channel/website comment box and email flooding with these types of errors to seek help to solve these errors every day. I have been answering these emails for a long. but the number of people is increasing and it is very difficult for me to reply to everybody. therefore, I have decided to make a video to explain these MQTT error codes and show you how to solve these.
This article is brought to you by pcbway.com,
PCBWay:
PCBWay is one of the best PCB manufacturers in the current industry.
PCBWay is offering services like PCB prototype, SMD Stencil, PCB assembly, Flexible PCBs & Advanced PCBs.
At PCBWay.com we can get 10 pieces of 2 layered PCBs at just $5 with 24 hours build time.
The Best part of PCBWay is the Instant quote feature, just enter the PCB size, choose the quantity, layers, and thickness. That’s it, we will get the instant quote. place an order by clicking on saving to cart.
Now, let’s dive into the episode.
Video Tutorial: General MQTT Errors Explained.
This tutorial is also available in video format, you can watch the below videos or continue reading this article.
MQTT:
Before jumping into the MQTT error codes and solving the procedure. let’s understand the MQTT protocol first. I will try to explain the MQTT protocol briefly.
MQTT was invented by Dr. Andy Stanford-Clark of IBM, and Arlen Nipper of Arcom (now Eurotech), in 1999.
MQTT Stands for Message Queue Telemetry Transport. MQTT is standard for IoT Messaging.
MQTT is an OASIS standard messaging protocol for the Internet of Things (IoT). It is designed as an extremely lightweight publish/subscribe messaging transport that is ideal for connecting remote devices with a small code footprint and minimal network bandwidth.
Today, MQTT is used in a wide variety of industries, such as Automotive, Logistics, Manufacturing, Smart Home, Consumer Products, Transportation
MQTT Architecture:
MQTT follows Pub-Sub Architecture.
Publisher MQTT client will publish data to the MQTT broker. on the other hand, the Subscriber-MQTT client will subscribe to the MQTT broker to receive the particular data. Based on the subscription status, the MQTT broker will decide which subscribers should receive the published data from the Publisher MQTT Client.
Coming to the error codes, these are generated when we attempt to connect to MQTT Brocker.
A broker will return a code that tells the client whether the connection attempt was successful or not.
Before going deep about the error or return codes, let’s see how this MQTT connection works.
MQTT Connection:
The MQTT protocol is based on TCP/IP. Both the client and the broker need to have a TCP/IP stack.
The MQTT connection is always between one client and the broker. Clients never connect directly.
To initiate a connection, the client sends a CONNECT message to the broker. The broker responds with a CONNACK message and a status code.
Once the connection is established, the broker keeps it open until the client sends a disconnect command or the connection breaks.
Now let’s look at the MQTT CONNECT command message. To initiate a connection, the client sends a command message to the broker.
A good-natured MQTT client sends a connect message with the following content.
If this CONNECT message is malformed or too much time passes between opening a network socket and sending the connect message, the broker closes the connection.
This behavior deters malicious clients that can slow the broker down.
When a broker receives a CONNECT message, it is obligated to respond with a CONNACK message.
The CONNACK message contains two data entries:
The session present flag
session present flag tells the client whether the broker already has a persistent session available from previous interactions with the client.
A connect return code
a return code that tells the client whether the connection attempt was successful or not. this is the code we are talking about.
MQTT Error Codes & Esplanation:
Currently, there are 6 return codes available. Code values, Return Code Responses, and their Descriptions as follows.
MQTT Error Codes & Solving Procedure.
Generally, there are 3 scenarios, which cause these issues.
1. Network Connectivity- if there is no internet connection enabled on the connected network or No internet connection on the WiFi, Cellular Signal causes these errors. also if there is an outage in the area at the local SIM card operator, you will encounter the error.
2. Credentials – In my AWS video, We will use certificates as credentials, if there is any problem with those certificates, you may get an error. General mistakes, you forgot to upload certificates to the SPIFFS memory. or you forgot to change the name of the certificates in the code.
3. Subscription – You may forget to subscribe to the topic on the MQTT broker in our case AWS IOT core.
For the past 4 years, I have been working with this MQTT protocol to connect my IoT devices to the IoT Cloud. Most probably these 3 were the issues I have encountered.
Check if your current problem matched with any of the above three general mistakes. try to solve them accordingly. if you still face any other issues. please draft a mail to “askus.electronicsinnovation@gmail.com”
That’s all about MQTT Error codes. Stay tuned to electronics innovation for more interesting episodes.
See you on the next interesting episode. bubyeeee…
Video Tutorial: General MQTT Errors Explained.
This tutorial is also available in video format, you can watch the below videos or continue reading this article.