I have nrf9160 with w5500 ethernet module. I am successfully able to get IP address and connect to mqtt broker created locally on my network. Now I was trying to connect to HIVE-MQ global mqtt broker but failing to do so, I think the issue is that hive mq broker does not provide ip address and for zephyr to connect to it requires IP address, I could be wrong I am not that well versed in zephyr and this is my 2nd time doing project on it after like a year.
Here is my code and output
#define SERVER_ADDR "broker.hivemq.com"
#define SERVER_PORT 1883
#define APP_MQTT_BUFFER_SIZE 128
#define APP_CONNECT_TRIES 10
#define MQTT_CLIENTID "aws_nrf9160"
void app_mqtt_init()
{
int rc, i = 0;
while (i++ < APP_CONNECT_TRIES && !connected) {
LOG_INF("Initializing MQTT ");
struct addrinfo *result;
struct addrinfo *addr;
struct addrinfo hints = {
.ai_socktype = SOCK_STREAM
};
char addr_str[NET_IPV6_ADDR_LEN];
mqtt_client_init(&client_ctx);
struct sockaddr_in *broker4 = (struct sockaddr_in *)&broker;
broker4->sin_family = AF_INET;
broker4->sin_port = htons(SERVER_PORT);
zsock_inet_pton(AF_INET, SERVER_ADDR, &broker4->sin_addr);
/* MQTT client configuration */
client_ctx.broker = &broker;
client_ctx.evt_cb = mqtt_evt_handler;
client_ctx.client_id.utf8 = (uint8_t *)MQTT_CLIENTID;
client_ctx.client_id.size = strlen(MQTT_CLIENTID) ;
client_ctx.password = NULL;
client_ctx.user_name = NULL;
client_ctx.protocol_version = MQTT_VERSION_3_1_1;
client_ctx.transport.type = MQTT_TRANSPORT_NON_SECURE;
/* MQTT buffers configuration */
client_ctx.rx_buf = rx_buffer;
client_ctx.rx_buf_size = sizeof(rx_buffer);
client_ctx.tx_buf = tx_buffer;
client_ctx.tx_buf_size = sizeof(tx_buffer);
rc = mqtt_connect(&client_ctx);
if (rc != 0) {
LOG_INF("mqtt_connect, %d",rc);
k_msleep(500);
continue;
}
fds[0].fd = client_ctx.transport.tcp.sock;
fds[0].events = ZSOCK_POLLIN;
poll(fds, 1, 2000);
mqtt_input(&client_ctx);
if (!connected) {
mqtt_abort(&client_ctx);
}
}
while(connected)
{
poll(fds, 1, 2000);
mqtt_input(&client_ctx);
rc = mqtt_live(&client_ctx);
if (rc != 0 && rc != -EAGAIN) {
LOG_INF("mqtt_live, %d", rc);
} else if (rc == 0) {
rc = mqtt_input(&client_ctx);
if (rc != 0) {
LOG_INF("mqtt_input, %d", rc);
}
}
rc = publish(&client_ctx, MQTT_QOS_0_AT_MOST_ONCE);
LOG_INF("mqtt_publish, %d", rc);
}
[00:00:00.395,385] <inf> eth_w5500: MAC ADDRESS: 2 8 dc 86 7 e2
[00:00:00.396,881] <inf> eth_w5500: W5500 Initialized
*** Booting nRF Connect SDK 3758bcbfa5cd ***
[00:00:00.397,888] <inf> Main: Main started ....
[00:00:00.397,918] <inf> app_network: Network Initialization
[00:00:00.397,949] <inf> app_network: Start on eth_w5500@0: index=1
[00:00:00.397,979] <inf> app_network: Net management event: -536608761
[00:00:02.397,430] <inf> eth_w5500: eth_w5500@0: Link up
[00:00:02.397,552] <inf> app_network: LINK UP
[00:00:04.565,246] <inf> net_dhcpv4: Received: 192.168.1.95
[00:00:04.565,399] <inf> app_network: Your address: 192.168.1.95
[00:00:04.565,399] <inf> app_network: Lease time: 4294967295 seconds
[00:00:04.565,460] <inf> app_network: Subnet: 255.255.255.0
[00:00:04.565,521] <inf> app_network: Router: 192.168.1.1
[00:00:04.565,643] <inf> app_network: Network connectivity established
[00:00:04.565,673] <inf> Main: Ethernet is Ready
[00:00:04.565,673] <inf> app_mqtt: Initializing MQTT
[00:00:04.566,680] <err> net_tcp: net_send_data()
[00:00:04.801,879] <err> net_tcp: net_send_data()
[00:00:05.037,078] <err> net_tcp: net_send_data()
[00:00:05.272,308] <err> net_tcp: net_send_data()
[00:00:05.507,507] <err> net_tcp: net_send_data()
[00:00:05.742,706] <err> net_tcp: net_send_data()
[00:00:05.977,905] <err> net_tcp: net_send_data()
[00:00:06.213,073] <err> net_tcp: net_send_data()
[00:00:06.448,272] <err> net_tcp: net_send_data()
[00:00:06.683,471] <err> net_tcp: net_send_data()
[00:00:06.918,792] <inf> app_mqtt: mqtt_connect, -116
[00:00:07.418,853] <inf> app_mqtt: Initializing MQTT
[00:00:07.419,769] <err> net_tcp: net_send_data()
[00:00:07.681,976] <err> net_tcp: net_send_data()
[00:00:07.944,183] <err> net_tcp: net_send_data()
[00:00:08.206,390] <err> net_tcp: net_send_data()
[00:00:08.468,566] <err> net_tcp: net_send_data()
[00:00:08.730,804] <err> net_tcp: net_send_data()
[00:00:08.993,011] <err> net_tcp: net_send_data()
[00:00:09.255,218] <err> net_tcp: net_send_data()
[00:00:09.517,395] <err> net_tcp: net_send_data()
[00:00:09.779,632] <err> net_tcp: net_send_data()
[00:00:10.041,961] <inf> app_mqtt: mqtt_connect, -116
[00:00:10.542,022] <inf> app_mqtt: Initializing MQTT
[00:00:10.542,938] <err> net_tcp: net_send_data()
[00:00:10.767,181] <err> net_tcp: net_send_data()
[00:00:10.991,394] <err> net_tcp: net_send_data()
[00:00:11.215,606] <err> net_tcp: net_send_data()
[00:00:11.439,819] <err> net_tcp: net_send_data()
[00:00:11.664,001] <err> net_tcp: net_send_data()
[00:00:11.888,244] <err> net_tcp: net_send_data()
[00:00:12.112,457] <err> net_tcp: net_send_data()
[00:00:12.336,669] <err> net_tcp: net_send_data()
[00:00:12.560,882] <err> net_tcp: net_send_data()
[00:00:12.785,186] <inf> app_mqtt: mqtt_connect, -116
[00:00:13.285,247] <inf> app_mqtt: Initializing MQTT
[00:00:13.286,132] <err> net_tcp: net_send_data()
[00:00:13.493,316] <err> net_tcp: net_send_data()
[00:00:13.700,500] <err> net_tcp: net_send_data()
[00:00:13.907,653] <err> net_tcp: net_send_data()
[00:00:14.114,837] <err> net_tcp: net_send_data()
[00:00:14.321,990] <err> net_tcp: net_send_data()
[00:00:14.529,174] <err> net_tcp: net_send_data()
[00:00:14.736,358] <err> net_tcp: net_send_data()
[00:00:14.943,542] <err> net_tcp: net_send_data()
[00:00:15.150,726] <err> net_tcp: net_send_data()
[00:00:15.358,001] <inf> app_mqtt: mqtt_connect, -116
[00:00:15.858,093] <inf> app_mqtt: Initializing MQTT
[00:00:15.858,978] <err> net_tcp: net_send_data()
[00:00:16.087,219] <err> net_tcp: net_send_data()
[00:00:16.315,429] <err> net_tcp: net_send_data()
[00:00:16.543,640] <err> net_tcp: net_send_data()
[00:00:16.771,850] <err> net_tcp: net_send_data()
[00:00:17.000,061] <err> net_tcp: net_send_data()
[00:00:17.228,240] <err> net_tcp: net_send_data()
[00:00:17.456,481] <err> net_tcp: net_send_data()
[00:00:17.684,692] <err> net_tcp: net_send_data()
[00:00:17.912,902] <err> net_tcp: net_send_data()