Skip to main content
Visitor II
November 15, 2024
Question

Issue with MQTT connection on STM32F407G and SIM7600 GSM module

  • November 15, 2024
  • 1 reply
  • 1470 views

I'm working with an STM32F407G and a SIM7600G GSM module. I've successfully configured the GSM module using AT commands, switched to PPPOS, and obtained a valid IP address, gateway, and netmask using LWIP.

I can successfully resolve the IP address of test.mosquitto.org using netconn_gethostbyname(). However, when I try to connect to the MQTT broker at this IP (5.196.78.28), I keep getting "Connection disconnected" in the MQTT connection callback.

The issue is that after attempting to connect, the callback shows "Connection disconnected" status every time. The IP resolution works, and I'm using LWIP with PPPOS for the GSM connection. Could this be related to protocol issues, or is something missing in my MQTT connection setup? Any guidance would be appreciated!

Here’s the MQTT connection callback and code I’m using:

 

 

static void mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection_status_t status) {
 switch (status) {
 case MQTT_CONNECT_ACCEPTED:
 printf("Connected to MQTT broker\n");
 break;
 case MQTT_CONNECT_REFUSED_PROTOCOL_VERSION:
 printf("Connection refused: Protocol version not supported\n");
 break;
 case MQTT_CONNECT_REFUSED_IDENTIFIER:
 printf("Connection refused: Invalid client identifier\n");
 break;
 case MQTT_CONNECT_REFUSED_SERVER:
 printf("Connection refused: Server unavailable\n");
 break;
 case MQTT_CONNECT_REFUSED_USERNAME_PASS:
 printf("Connection refused: Invalid username or password\n");
 break;
 case MQTT_CONNECT_REFUSED_NOT_AUTHORIZED_:
 printf("Connection refused: Not authorized\n");
 break;
 case MQTT_CONNECT_DISCONNECTED:
 printf("Connection disconnected\n");
 break;
 case MQTT_CONNECT_TIMEOUT:
 printf("Connection timeout\n");
 break;
 default:
 printf("Unknown connection status: %d\n", status);
 break;
 }
}

void mqtt_connect(mqtt_client_t *client) {
	ip_addr_t my_mqtt;
	struct mqtt_connect_client_info_t ci;
	memset(&ci, 0, sizeof(ci));
	ci.client_id = "lwip_testuuuuuu";
	ipaddr_aton("5.196.78.28",&my_mqtt);
 // Call the connect function with the correct parameters
	err_t err = mqtt_client_connect(client,&my_mqtt, MQTTY_PORT,
			mqtt_connection_cb, 0, &ci);

	if (err != ERR_OK) {
		printf("Failed to connect to MQTT broker: %d\r\n", err);
	} else {
		printf("Connecting to MQTT broker at IP: %s\r\n",
				ipaddr_ntoa(&my_mqtt));
	}
}

void mqtt_new(){
 vTaskDelay(1000);
 mqtt_client_t* client;
 client = mqtt_client_new();
 if (client != NULL){
 mqtt_connect(client);
 }
}

 

 

 

    This topic has been closed for replies.

    1 reply

    Super User
    November 15, 2024

    is your basic TCP/IP working?

    • Can you connect to any "plain" TCP host?
    • Can you ping a host?

     

    Are you sure your Mosquitto credentials, etc, are OK? Can you connect over a wired link?

     

    It seems you also have some other connectivity issues:

    https://community.st.com/t5/other-software/issue-with-dns-resolution-on-stm32f407g-and-sim7600-gsm-module/td-p/743352

     

    Visitor II
    November 16, 2024

    Hi,

     

    Thank you for your response.

       

    I have successfully initialized the GSM connection using PPPoS on the SIM7600, and I'm able to get a valid IP address, gateway, and netmask, as shown below:

     

    GSM: ipaddr = 100.73.194.248  

    GSM: gateway = 10.64.64.64  

    GSM: netmask = 255.255.255.255

     

    This indicates that the basic network connection is established. However, while I can resolve IP addresses (e.g., for "test.mosquitto.org" using netconn_gethostbyname()), the issue arises when I try to connect to the MQTT broker, as it shows a "Connection disconnected" error.

    Do I need to verify something else in terms of TCP/IP or MQTT settings to ensure the connection is fully functional

     

    since I am able to resolve the IP address of `test.mosquitto.org` using `netconn_gethostbyname`, which shows that DNS resolution is working. So, I haven't tried connecting to a plain TCP server yet. 

     

    Currently, I don't have `ping.h` available in my lwIP setup on STM32, so I'm unable to directly ping a host. Is there an alternative approach I can use to test this on lwIP?

     

    Regarding the MQTT issue, my credentials  are simple, and I'm not sure if the issue is related to them, since the error I’m receiving is `MQTT_CONNECT_DISCONNECTED`. Do you think the issue could be related to the PPPoS connection?

     

    Let me know if there's anything else you'd recommend testing or if there's any additional information I can provide.

     

    Thanks!