cmd_wifi.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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/timers.h"
  23. #include "freertos/event_groups.h"
  24. #include "esp_wifi.h"
  25. #include "tcpip_adapter.h"
  26. #include "esp_event.h"
  27. #include "led.h"
  28. extern bool bypass_wifi_manager;
  29. #define JOIN_TIMEOUT_MS (10000)
  30. #include "platform_console.h"
  31. extern EventGroupHandle_t wifi_event_group;
  32. extern const int CONNECTED_BIT;
  33. //static const char * TAG = "cmd_wifi";
  34. /** Arguments used by 'join' function */
  35. static struct {
  36. struct arg_int *timeout;
  37. struct arg_str *ssid;
  38. struct arg_str *password;
  39. struct arg_end *end;
  40. } join_args;
  41. // todo: implement access point config - cmd_to_json(&i2cdetect_cmd);
  42. ///** Arguments used by 'join' function */
  43. //static struct {
  44. // struct arg_int *autoconnect;
  45. // struct arg_end *end;
  46. //} auto_connect_args;
  47. static void event_handler(void* arg, esp_event_base_t event_base,
  48. int32_t event_id, void* event_data)
  49. {
  50. if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
  51. led_blink_pushed(LED_GREEN, 250, 250);
  52. esp_wifi_connect();
  53. xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
  54. } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
  55. led_unpush(LED_GREEN);
  56. xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
  57. }
  58. }
  59. //bool wait_for_wifi(){
  60. //
  61. // bool connected=(xEventGroupGetBits(wifi_event_group) & CONNECTED_BIT)!=0;
  62. //
  63. // if(!connected){
  64. // ESP_LOGD(TAG,"Waiting for WiFi...");
  65. // connected = (xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT,
  66. // pdFALSE, pdTRUE, JOIN_TIMEOUT_MS / portTICK_PERIOD_MS)& CONNECTED_BIT)!=0;
  67. // if(!connected){
  68. // ESP_LOGD(TAG,"wifi timeout.");
  69. // }
  70. // else
  71. // {
  72. // ESP_LOGI(TAG,"WiFi Connected!");
  73. // }
  74. // }
  75. //
  76. //
  77. // return connected;
  78. //
  79. //}
  80. static void initialise_wifi(void)
  81. {
  82. static bool initialized = false;
  83. if (initialized) {
  84. return;
  85. }
  86. tcpip_adapter_init();
  87. wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  88. ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
  89. ESP_ERROR_CHECK( esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &event_handler, NULL) );
  90. ESP_ERROR_CHECK( esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL) );
  91. ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
  92. ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_NULL) );
  93. ESP_ERROR_CHECK( esp_wifi_start() );
  94. initialized = true;
  95. led_blink(LED_GREEN, 250, 250);
  96. }
  97. static void wifi_join(void *arg)
  98. {
  99. const char *ssid = join_args.ssid->sval[0];
  100. const char *pass = join_args.password->sval[0];
  101. int timeout_ms = join_args.timeout->ival[0];
  102. initialise_wifi();
  103. wifi_config_t wifi_config = { 0 };
  104. strncpy((char *) wifi_config.sta.ssid, ssid, sizeof(wifi_config.sta.ssid));
  105. wifi_config.sta.ssid[sizeof(wifi_config.sta.ssid) - 1] = '\0';
  106. if (pass) {
  107. strncpy((char *) wifi_config.sta.password, pass, sizeof(wifi_config.sta.password));
  108. wifi_config.sta.password[sizeof(wifi_config.sta.password) - 1] = '\0';
  109. }
  110. ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
  111. ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
  112. ESP_ERROR_CHECK( esp_wifi_connect() );
  113. int bits = xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT,
  114. pdFALSE, pdTRUE, timeout_ms / portTICK_PERIOD_MS);
  115. if (bits & CONNECTED_BIT) {
  116. ESP_LOGI(__func__, "Connected");
  117. } else {
  118. ESP_LOGW(__func__, "Connection timed out");
  119. }
  120. }
  121. //static int set_auto_connect(int argc, char **argv)
  122. //{
  123. // int nerrors = arg_parse(argc, argv, (void **) &join_args);
  124. // if (nerrors != 0) {
  125. // arg_print_errors(stderr, join_args.end, argv[0]);
  126. // return 1;
  127. // }
  128. // ESP_LOGI(__func__, "Connecting to '%s'",
  129. // join_args.ssid->sval[0]);
  130. //
  131. // /* set default value*/
  132. // if (join_args.timeout->count == 0) {
  133. // join_args.timeout->ival[0] = JOIN_TIMEOUT_MS;
  134. // }
  135. //
  136. // bool connected = wifi_join(join_args.ssid->sval[0],
  137. // join_args.password->sval[0],
  138. // join_args.timeout->ival[0]);
  139. // if (!connected) {
  140. // ESP_LOGW(__func__, "Connection timed out");
  141. // return 1;
  142. // }
  143. // ESP_LOGI(__func__, "Connected");
  144. // return 0;
  145. //}
  146. static int connect(int argc, char **argv)
  147. {
  148. int nerrors = arg_parse_msg(argc, argv,(struct arg_hdr **)&join_args);
  149. if (nerrors != 0) {
  150. return 1;
  151. }
  152. ESP_LOGI(__func__, "Connecting to '%s'",
  153. join_args.ssid->sval[0]);
  154. /* set default value*/
  155. if (join_args.timeout->count == 0) {
  156. join_args.timeout->ival[0] = JOIN_TIMEOUT_MS;
  157. }
  158. // need to use that trick to make sure we use internal stack
  159. xTimerStart(xTimerCreate("wifi_join", 1, pdFALSE, NULL, wifi_join), portMAX_DELAY);
  160. return 0;
  161. }
  162. void register_wifi_join()
  163. {
  164. join_args.timeout = arg_int0(NULL, "timeout", "<t>", "Connection timeout, ms");
  165. join_args.ssid = arg_str1(NULL, NULL, "<ssid>", "SSID of AP");
  166. join_args.password = arg_str0(NULL, NULL, "<pass>", "PSK of AP");
  167. join_args.end = arg_end(2);
  168. const esp_console_cmd_t join_cmd = {
  169. .command = "join",
  170. .help = "Join WiFi AP as a station",
  171. .hint = NULL,
  172. .func = &connect,
  173. .argtable = &join_args
  174. };
  175. ESP_ERROR_CHECK( esp_console_cmd_register(&join_cmd) );
  176. }
  177. void register_wifi()
  178. {
  179. register_wifi_join();
  180. if(bypass_wifi_manager){
  181. initialise_wifi();
  182. }
  183. }