cmd_wifi.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /* Console example — WiFi commands
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. // cmd_wifi has been replaced by wifi-manager
  8. /* Console example — WiFi commands
  9. This example code is in the Public Domain (or CC0 licensed, at your option.)
  10. Unless required by applicable law or agreed to in writing, this
  11. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. CONDITIONS OF ANY KIND, either express or implied.
  13. */
  14. #include "cmd_wifi.h"
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include "cmd_decl.h"
  18. #include "esp_log.h"
  19. #include "esp_console.h"
  20. #include "argtable3/argtable3.h"
  21. #include "freertos/FreeRTOS.h"
  22. #include "freertos/event_groups.h"
  23. #include "esp_wifi.h"
  24. #include "tcpip_adapter.h"
  25. #include "esp_event.h"
  26. #include "led.h"
  27. extern bool bypass_wifi_manager;
  28. #define JOIN_TIMEOUT_MS (10000)
  29. extern EventGroupHandle_t wifi_event_group;
  30. extern const int CONNECTED_BIT;
  31. //static const char * TAG = "cmd_wifi";
  32. /** Arguments used by 'join' function */
  33. static struct {
  34. struct arg_int *timeout;
  35. struct arg_str *ssid;
  36. struct arg_str *password;
  37. struct arg_end *end;
  38. } join_args;
  39. ///** Arguments used by 'join' function */
  40. //static struct {
  41. // struct arg_int *autoconnect;
  42. // struct arg_end *end;
  43. //} auto_connect_args;
  44. static void event_handler(void* arg, esp_event_base_t event_base,
  45. int32_t event_id, void* event_data)
  46. {
  47. if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
  48. led_blink_pushed(LED_GREEN, 250, 250);
  49. esp_wifi_connect();
  50. xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
  51. } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
  52. led_unpush(LED_GREEN);
  53. xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
  54. }
  55. }
  56. //bool wait_for_wifi(){
  57. //
  58. // bool connected=(xEventGroupGetBits(wifi_event_group) & CONNECTED_BIT)!=0;
  59. //
  60. // if(!connected){
  61. // ESP_LOGD(TAG,"Waiting for WiFi...");
  62. // connected = (xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT,
  63. // pdFALSE, pdTRUE, JOIN_TIMEOUT_MS / portTICK_PERIOD_MS)& CONNECTED_BIT)!=0;
  64. // if(!connected){
  65. // ESP_LOGD(TAG,"wifi timeout.");
  66. // }
  67. // else
  68. // {
  69. // ESP_LOGI(TAG,"WiFi Connected!");
  70. // }
  71. // }
  72. //
  73. //
  74. // return connected;
  75. //
  76. //}
  77. static void initialise_wifi(void)
  78. {
  79. static bool initialized = false;
  80. if (initialized) {
  81. return;
  82. }
  83. tcpip_adapter_init();
  84. // Now moved to esp_app_main: wifi_event_group = xEventGroupCreate();
  85. ESP_ERROR_CHECK(esp_event_loop_create_default());
  86. wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  87. ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
  88. ESP_ERROR_CHECK( esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &event_handler, NULL) );
  89. ESP_ERROR_CHECK( esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL) );
  90. ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
  91. ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_NULL) );
  92. ESP_ERROR_CHECK( esp_wifi_start() );
  93. initialized = true;
  94. led_blink(LED_GREEN, 250, 250);
  95. }
  96. static bool wifi_join(const char *ssid, const char *pass, int timeout_ms)
  97. {
  98. initialise_wifi();
  99. wifi_config_t wifi_config = { 0 };
  100. strncpy((char *) wifi_config.sta.ssid, ssid, sizeof(wifi_config.sta.ssid));
  101. if (pass) {
  102. strncpy((char *) wifi_config.sta.password, pass, sizeof(wifi_config.sta.password));
  103. }
  104. ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
  105. ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
  106. ESP_ERROR_CHECK( esp_wifi_connect() );
  107. int bits = xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT,
  108. pdFALSE, pdTRUE, timeout_ms / portTICK_PERIOD_MS);
  109. return (bits & CONNECTED_BIT) != 0;
  110. }
  111. //static int set_auto_connect(int argc, char **argv)
  112. //{
  113. // int nerrors = arg_parse(argc, argv, (void **) &join_args);
  114. // if (nerrors != 0) {
  115. // arg_print_errors(stderr, join_args.end, argv[0]);
  116. // return 1;
  117. // }
  118. // ESP_LOGI(__func__, "Connecting to '%s'",
  119. // join_args.ssid->sval[0]);
  120. //
  121. // /* set default value*/
  122. // if (join_args.timeout->count == 0) {
  123. // join_args.timeout->ival[0] = JOIN_TIMEOUT_MS;
  124. // }
  125. //
  126. // bool connected = wifi_join(join_args.ssid->sval[0],
  127. // join_args.password->sval[0],
  128. // join_args.timeout->ival[0]);
  129. // if (!connected) {
  130. // ESP_LOGW(__func__, "Connection timed out");
  131. // return 1;
  132. // }
  133. // ESP_LOGI(__func__, "Connected");
  134. // return 0;
  135. //}
  136. static int connect(int argc, char **argv)
  137. {
  138. int nerrors = arg_parse(argc, argv, (void **) &join_args);
  139. if (nerrors != 0) {
  140. arg_print_errors(stderr, join_args.end, argv[0]);
  141. return 1;
  142. }
  143. ESP_LOGI(__func__, "Connecting to '%s'",
  144. join_args.ssid->sval[0]);
  145. /* set default value*/
  146. if (join_args.timeout->count == 0) {
  147. join_args.timeout->ival[0] = JOIN_TIMEOUT_MS;
  148. }
  149. bool connected = wifi_join(join_args.ssid->sval[0],
  150. join_args.password->sval[0],
  151. join_args.timeout->ival[0]);
  152. if (!connected) {
  153. ESP_LOGW(__func__, "Connection timed out");
  154. return 1;
  155. }
  156. ESP_LOGI(__func__, "Connected");
  157. return 0;
  158. }
  159. void register_wifi_join()
  160. {
  161. join_args.timeout = arg_int0(NULL, "timeout", "<t>", "Connection timeout, ms");
  162. join_args.ssid = arg_str1(NULL, NULL, "<ssid>", "SSID of AP");
  163. join_args.password = arg_str0(NULL, NULL, "<pass>", "PSK of AP");
  164. join_args.end = arg_end(2);
  165. const esp_console_cmd_t join_cmd = {
  166. .command = "join",
  167. .help = "Join WiFi AP as a station",
  168. .hint = NULL,
  169. .func = &connect,
  170. .argtable = &join_args
  171. };
  172. ESP_ERROR_CHECK( esp_console_cmd_register(&join_cmd) );
  173. }
  174. void register_wifi()
  175. {
  176. register_wifi_join();
  177. if(bypass_wifi_manager){
  178. initialise_wifi();
  179. }
  180. }