Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MQTTS client connection to MQTT broker on IPV6 only instance. (IDFGH-14445) #15222

Open
3 tasks done
Zeni241 opened this issue Jan 16, 2025 · 5 comments
Open
3 tasks done
Assignees
Labels
Status: Opened Issue is new

Comments

@Zeni241
Copy link

Zeni241 commented Jan 16, 2025

Answers checklist.

  • I have read the documentation ESP-IDF Programming Guide and the issue is not addressed there.
  • I have updated my IDF branch (master or release) to the latest version and checked that the issue is present there.
  • I have searched the issue tracker for a similar issue and not found a similar issue.

General issue report

My mosquitto MQTT broker is hosted on AWS EC2, which only has IPV6 address attached to it (no IPV4 address, as it costs 😉 ).
The problem is I cannot connect my MQTT client on my ESP32 to this broker. I am sure this is due to IPV6 only address of my EC2 because if I attach an IPV4 elastic IP to this instance, the MQTT client on ESP32 can easily connect to the broker on EC2.

Any solution please.

@espressif-bot espressif-bot added the Status: Opened Issue is new label Jan 16, 2025
@github-actions github-actions bot changed the title MQTTS client connection to MQTT broker on IPV6 only instance. MQTTS client connection to MQTT broker on IPV6 only instance. (IDFGH-14445) Jan 16, 2025
@euripedesrocha
Copy link
Collaborator

Hi @Zeni241, could you provide logs of the connection phase with the errors? Please enable VERBOSE log level for the components, refer to mqtt client examples on IDF for the list of most important ones and how to do it.

@Zeni241
Copy link
Author

Zeni241 commented Jan 18, 2025

Hi @euripedesrocha Thanks for your response . In fact I am not able to get IPV6 address for my ESP32 when I connect to wifi. I only get IPV4 address and this may be the reason I cannot connect to mqtt broker behind IPV6.

To connect to wifi my code is

    `ESP_ERROR_CHECK(esp_netif_init());
    ESP_ERROR_CHECK(esp_event_loop_create_default());
    esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta();
    assert(sta_netif);
   esp_netif_create_ip6_linklocal(sta_netif);
    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
    ESP_ERROR_CHECK(esp_wifi_init(&cfg));
    ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));`

And in event handler I have added"

`else if (event_base == IP_EVENT && event_id == IP_EVENT_GOT_IP6) 
{
    ip_event_got_ip6_t *event = (ip_event_got_ip6_t *)event_data;
    ESP_LOGI(TAG, "got ipv6: " IPV6STR, IPV62STR(event->ip6_info.ip));
    if (event->ip6_info.ip.addr[0] == 0) {
        ESP_LOGW("IPv6", "Only link-local IPv6 assigned (fe80::). No global IPv6.");
    } else {
        ESP_LOGI("IPv6", "ESP32 has a global IPv6 address!");
    }
   
} `

I have checked that my router supports SLAAC and has IPV6 global address. Can you please guide me how can I get IPV6 address when I connect ESP32 to wifi. Presently it get only IPV4 address.

@nopnop2002
Copy link

nopnop2002 commented Jan 19, 2025

Can you please guide me how can I get IPV6 address when I connect ESP32 to wifi.

The easiest way is to use example_connect.

https://github.com/espressif/esp-idf/blob/master/examples/protocols/mqtt/tcp/main/app_main.c#L159

I (11619) esp_netif_handlers: example_netif_sta ip: 192.168.10.114, mask: 255.255.255.0, gw: 192.168.10.1
I (11619) example_connect: Got IPv4 event: Interface "example_netif_sta" address: 192.168.10.114
I (12599) example_connect: Got IPv6 event: Interface "example_netif_sta" address: fe80:0000:0000:0000:bad6:1aff:fe67:7660, type: ESP_IP6_ADDR_IS_LINK_LOCAL
I (12599) example_common: Connected to example_netif_sta
I (12609) wifi:<ba-add>idx:0 (ifx:0, f8:b7:97:36:de:52), tid:5, ssn:0, winSize:64I (12609) example_common: - IPv4 address: 192.168.10.114,

I (12619) example_common: - IPv6 address: fe80:0000:0000:0000:bad6:1aff:fe67:7660, type: ESP_IP6_ADDR_IS_LINK_LOCAL

The source code for example_connect tells you a lot about IPV6 addresses.

@Zeni241
Copy link
Author

Zeni241 commented Jan 19, 2025

Thanks @euripedesrocha for guiding me I will go through this example and give my feedback here.

@Zeni241
Copy link
Author

Zeni241 commented Jan 19, 2025

Dear @euripedesrocha thanks for the link, Now I am able to get Global IPV6 address in my ESP-32 WIFI connection and now I can connect my MQTT client on my ESP32 to this broker behind IPV6 only address.
The main mistake I was making was that I should put the line esp_netif_create_ip6_linklocal(sta_netif); in wifi connected callback code (once the wifi is connected) instead of in wifi_ init() or at any other place.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Opened Issue is new
Projects
None yet
Development

No branches or pull requests

4 participants