Wifi with IDF not working, Arduino works

bastetfurry
Posts: 3
Joined: Tue Aug 24, 2021 5:33 pm

Wifi with IDF not working, Arduino works

Postby bastetfurry » Sat Sep 25, 2021 3:23 pm

I have a problem with the IDF wifi code, i took the example and made it into a class but it can't connect. Scanning works great tough.
The chip is fine as i crosschecked with the Arduino library and there it connects just fine, what could be wrong?

Logfile:
  1. I (950485) wifi:wifi driver task: 3ffd26ec, prio:23, stack:6656, core=0
  2. I (950495) wifi:wifi firmware version: c7d0450
  3. I (950495) wifi:wifi certification version: v7.0
  4. I (950495) wifi:config NVS flash: enabled
  5. I (950495) wifi:config nano formating: disabled
  6. I (950495) wifi:Init data frame dynamic rx buffer num: 32
  7. I (950505) wifi:Init management frame dynamic rx buffer num: 32
  8. I (950505) wifi:Init management short buffer num: 32
  9. I (950515) wifi:Init static tx buffer num: 16
  10. I (950515) wifi:Init tx cache buffer num: 32
  11. I (950525) wifi:Init static rx buffer size: 1600
  12. I (950525) wifi:Init static rx buffer num: 10
  13. I (950535) wifi:Init dynamic rx buffer num: 32
  14. I (950535) wifi_init: rx ba win: 6
  15. I (950535) wifi_init: tcpip mbox: 32
  16. I (950545) wifi_init: udp mbox: 6
  17. I (950545) wifi_init: tcp mbox: 6
  18. I (950545) wifi_init: tcp tx win: 5744
  19. I (950555) wifi_init: tcp rx win: 5744
  20. I (950555) wifi_init: tcp mss: 1440
  21. I (950555) wifi_init: WiFi IRAM OP enabled
  22. I (950565) wifi_init: WiFi RX IRAM OP enabled
  23. Connecting to "KatzenWave_24" with PSK "************"
  24. I (950575) wifi:mode : sta (3c:61:05:0c:10:94)
  25. I (950575) wifi:enable tsf
  26. Connecting Wifi...
  27. Retry: 0
  28. Retry: 1
  29. Retry: 2
  30. Retry: 3
  31. Retry: 4
  32. Failed!
  33. Failed test...
  34. I (962865) wifi:flush txq
  35. I (962865) wifi:stop sw txq
  36. I (962865) wifi:lmac stop hw txq
  37. I (962865) wifi:Deinit lldesc rx mblock:10
kt_wifi.cpp
  1. #include <string.h>
  2.  
  3. #include "kt_wifi.h"
  4.  
  5. #define DEFAULT_SCAN_LIST_SIZE 28
  6. #define WIFI_CONNECTED_BIT BIT0
  7. #define WIFI_FAIL_BIT BIT1
  8.  
  9. static EventGroupHandle_t s_wifi_event_group;
  10. static int s_retry_num = 0;
  11.  
  12. static void event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
  13. {
  14.     if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START)
  15.     {
  16.         printf("Connecting Wifi...\n");
  17.         esp_wifi_connect();
  18.     } else
  19.     if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED)
  20.     {
  21.         if (s_retry_num < 5)
  22.         {
  23.             printf("Retry: %d\n",s_retry_num);
  24.             esp_wifi_connect();
  25.             s_retry_num++;
  26.         }
  27.         else
  28.         {
  29.             printf("Failed!\n");
  30.             xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
  31.         }
  32.     }
  33.     else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP)
  34.     {
  35.         printf("Connected!\n");
  36.         ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
  37.         printf("IP: "IPSTR"\n", IP2STR(&event->ip_info.ip));
  38.         s_retry_num = 0;
  39.         xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
  40.     }
  41. }
  42.  
  43. KittenWifi::KittenWifi()
  44. {
  45.     esp_err_t ret = nvs_flash_init();
  46.     if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  47.       ESP_ERROR_CHECK(nvs_flash_erase());
  48.       ret = nvs_flash_init();
  49.     }
  50.     ESP_ERROR_CHECK(ret);
  51.  
  52.  
  53.     s_wifi_event_group = xEventGroupCreate();
  54.    
  55.     ESP_ERROR_CHECK(esp_netif_init());
  56.  
  57.     ESP_ERROR_CHECK(esp_event_loop_create_default());
  58.  
  59.  
  60.     sta_netif = esp_netif_create_default_wifi_sta();
  61.     assert(sta_netif);
  62.  
  63.     cfg = WIFI_INIT_CONFIG_DEFAULT();
  64.     ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  65.     ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );    
  66.  
  67.     ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
  68.                                                         ESP_EVENT_ANY_ID,
  69.                                                         &event_handler,
  70.                                                         NULL,
  71.                                                         &instance_any_id));
  72.     ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
  73.                                                         IP_EVENT_STA_GOT_IP,
  74.                                                         &event_handler,
  75.                                                         NULL,
  76.                                                         &instance_got_ip));
  77. }
  78.  
  79. KittenWifi::~KittenWifi()
  80. {
  81.     esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, &instance_any_id);
  82.     esp_event_handler_instance_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, &instance_got_ip);
  83.  
  84.     ESP_ERROR_CHECK(esp_event_loop_delete_default());
  85.     esp_netif_destroy(sta_netif);
  86.     vEventGroupDelete(s_wifi_event_group);
  87.     memset(&s_wifi_event_group, 0, sizeof(s_wifi_event_group));
  88. }
  89.  
  90. void KittenWifi::WifiStart()
  91. {
  92.     if (!wifiIsActive)
  93.     {      
  94.         ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
  95.         ESP_ERROR_CHECK(esp_wifi_start());
  96.         wifiIsActive = true;
  97.     }
  98. }
  99.  
  100. void KittenWifi::WifiStop()
  101. {
  102.     if (wifiIsActive)
  103.     {
  104.         ESP_ERROR_CHECK(esp_wifi_disconnect());
  105.         ESP_ERROR_CHECK(esp_wifi_stop());
  106.         ESP_ERROR_CHECK(esp_wifi_deinit());
  107.  
  108.         wifiIsActive = false;
  109.     }
  110. }
  111.  
  112. bool KittenWifi::WifiStartConnectAP(std::string SSID, std::string PSK)
  113. {
  114.     if (wifiIsActive) return false;
  115. #pragma GCC diagnostic push
  116. #pragma GCC diagnostic ignored "-Wstringop-truncation"
  117.  
  118.     memset(&wifi_config, 0, sizeof(wifi_config));
  119.  
  120.     strncpy((char *)wifi_config.sta.ssid, SSID.c_str(), sizeof(wifi_config.sta.ssid));
  121.     strncpy((char *)wifi_config.sta.password, PSK.c_str(), sizeof(wifi_config.sta.password));
  122.  
  123.     printf("Connecting to \"%s\" with PSK \"%s\"\n",wifi_config.sta.ssid,wifi_config.sta.password);
  124.  
  125. #pragma GCC diagnostic pop
  126.  
  127.     wifi_config.sta.threshold.authmode = WIFI_AUTH_WPA_WPA2_PSK; // TODO: Make this user configurable if something under WPA2 is allowed
  128.     wifi_config.sta.pmf_cfg.capable = true;
  129.     wifi_config.sta.pmf_cfg.required = false;
  130.     wifi_config.sta.bssid_set = false;
  131.     wifi_config.sta.scan_method = WIFI_ALL_CHANNEL_SCAN;
  132.     wifi_config.sta.channel = 0;
  133.  
  134.     s_retry_num = 0;
  135.  
  136.     ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
  137.     ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
  138.     ESP_ERROR_CHECK(esp_wifi_start());
  139.     wifiIsActive = true;
  140.  
  141.     EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
  142.                                            WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
  143.                                            pdFALSE,
  144.                                            pdFALSE,
  145.                                            portMAX_DELAY);
  146.  
  147.     if (bits & WIFI_CONNECTED_BIT)
  148.         return true;
  149.     return false;
  150. }
  151.  
  152. char* KittenWifi::GetURL(std::string URL, int *returncode)
  153. {
  154.     esp_http_client_config_t httpclientconfig = {
  155.         .url = URL.c_str()
  156.         };
  157.  
  158.     esp_http_client_handle_t client = esp_http_client_init(&httpclientconfig);
  159.     esp_err_t err = esp_http_client_perform(client);
  160.  
  161.     if (err == ESP_OK)
  162.     {
  163.         /* ESP_LOGI(TAG, "Status = %d, content_length = %d",
  164.                 esp_http_client_get_status_code(client),
  165.                 esp_http_client_get_content_length(client)); */
  166.         int statuscode = esp_http_client_get_status_code(client);
  167.         *returncode = statuscode;
  168.         if (statuscode != 200)
  169.             return nullptr;
  170.  
  171.         uint32_t len = esp_http_client_get_content_length(client);
  172.         char *buffer = (char *)malloc(len);
  173.  
  174.         int actualdataread = esp_http_client_read(client,buffer,len);
  175.  
  176.         printf("Do we have a corruption? \"%s\"\n",buffer);
  177.  
  178.         esp_http_client_close(client);
  179.         esp_http_client_cleanup(client);    
  180.         return buffer;
  181.     }
  182.     esp_http_client_cleanup(client);
  183.  
  184.     return nullptr;
  185. }
  186.  
  187. std::map<std::string, std::string> KittenWifi::ScanNetwork()
  188. {
  189.     std::map<std::string, std::string> returnmap;
  190.  
  191.     uint16_t number = DEFAULT_SCAN_LIST_SIZE;
  192.     wifi_ap_record_t ap_info[DEFAULT_SCAN_LIST_SIZE];
  193.     uint16_t ap_count = 0;
  194.     memset(ap_info, 0, sizeof(ap_info));
  195.    
  196.     ESP_ERROR_CHECK(esp_wifi_disconnect());
  197.     esp_wifi_scan_start(NULL, true);
  198.     ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&number, ap_info));
  199.     ESP_ERROR_CHECK(esp_wifi_scan_get_ap_num(&ap_count));
  200.  
  201.     for (int i = 0; i < number + 1; i++)
  202.     {
  203.         std::string key = "";
  204.         std::string value = "";
  205.         key.insert(0, (char *)ap_info[i].ssid);
  206.  
  207.         switch (ap_info[i].pairwise_cipher)
  208.         {
  209.         case WIFI_CIPHER_TYPE_NONE:
  210.             break;
  211.         case WIFI_CIPHER_TYPE_WEP40:
  212.             value += "P:WEP40 ";
  213.             break;
  214.         case WIFI_CIPHER_TYPE_WEP104:
  215.             value += "P:WEP104 ";
  216.             break;
  217.         case WIFI_CIPHER_TYPE_TKIP:
  218.             value += "P:TKIP ";
  219.             break;
  220.         case WIFI_CIPHER_TYPE_CCMP:
  221.             value += "P:CCMP ";
  222.             break;
  223.         case WIFI_CIPHER_TYPE_TKIP_CCMP:
  224.             value += "P:TKIP_CCMP ";
  225.             break;
  226.         default:
  227.             value += "P:??? ";
  228.             break;
  229.         }
  230.  
  231.         switch (ap_info[i].group_cipher)
  232.         {
  233.         case WIFI_CIPHER_TYPE_NONE:
  234.             break;
  235.         case WIFI_CIPHER_TYPE_WEP40:
  236.             value += "G:WEP40";
  237.             break;
  238.         case WIFI_CIPHER_TYPE_WEP104:
  239.             value += "G:WEP104";
  240.             break;
  241.         case WIFI_CIPHER_TYPE_TKIP:
  242.             value += "G:TKIP";
  243.             break;
  244.         case WIFI_CIPHER_TYPE_CCMP:
  245.             value += "G:CCMP";
  246.             break;
  247.         case WIFI_CIPHER_TYPE_TKIP_CCMP:
  248.             value += "G:TKIP_CCMP";
  249.             break;
  250.         default:
  251.             value += "G:???";
  252.             break;
  253.         }
  254.  
  255.         returnmap[key] = value;
  256.     }
  257.  
  258.     return returnmap;
  259. }
kt_wifi.h
  1. #ifndef __KT_WIFI_H__
  2. #define __KT_WIFI_H__
  3.  
  4. #include <string>
  5. #include <vector>
  6. #include <map>
  7.  
  8. #include "freertos/FreeRTOS.h"
  9. #include "freertos/event_groups.h"
  10. #include "esp_wifi.h"
  11. #include "esp_log.h"
  12. #include "esp_event.h"
  13. #include "esp_http_client.h"
  14. #include "nvs_flash.h"
  15.  
  16. class KittenWifi
  17. {
  18. public:
  19.     KittenWifi();
  20.     ~KittenWifi();
  21.  
  22.     // Start and stop the wifi. This should save some power when the wifi is not needed by the current application.
  23.     void WifiStart();
  24.     void WifiStop();
  25.  
  26.     // Connect to a given access point
  27.     bool WifiStartConnectAP(std::string SSID, std::string PSK);
  28.  
  29.     // Grab the content of an URL and return it in buffer. Return value is a buffer, it is the callers duty to remove it with free()
  30.     char* GetURL(std::string URL, int *returncode);
  31.  
  32.     //Scan the network for open APs and return a string list of them
  33.     std::map<std::string,std::string> ScanNetwork();
  34. private:
  35.     std::string connectedAPName = "";
  36.     esp_netif_t *sta_netif = nullptr;
  37.     wifi_init_config_t cfg;
  38.     wifi_config_t wifi_config;
  39.     esp_event_handler_instance_t instance_any_id;
  40.     esp_event_handler_instance_t instance_got_ip;    
  41.  
  42.     bool wifiIsActive = false;
  43. };
  44.  
  45. #endif

Who is online

Users browsing this forum: No registered users and 108 guests