cmd_wifi.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 "esp_netif.h"
  26. #include "esp_event.h"
  27. #include "led.h"
  28. extern bool bypass_network_manager;
  29. #define JOIN_TIMEOUT_MS (10000)
  30. #include "platform_console.h"
  31. // To enable wifi configuration from the command line, uncomment the line below
  32. // define WIFI_CMDLINE 1
  33. extern EventGroupHandle_t network_event_group;
  34. extern const int CONNECTED_BIT;
  35. //static const char * TAG = "cmd_wifi";
  36. /** Arguments used by 'join' function */
  37. static struct {
  38. struct arg_int *timeout;
  39. struct arg_str *ssid;
  40. struct arg_str *password;
  41. struct arg_end *end;
  42. } join_args;
  43. // todo: implement access point config - cmd_to_json(&i2cdetect_cmd);
  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(network_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(network_event_group, CONNECTED_BIT);
  54. }
  55. }
  56. static void initialise_wifi(void)
  57. {
  58. static bool initialized = false;
  59. if (initialized) {
  60. return;
  61. }
  62. esp_netif_init();
  63. wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  64. ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
  65. ESP_ERROR_CHECK( esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &event_handler, NULL) );
  66. ESP_ERROR_CHECK( esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL) );
  67. ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
  68. ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_NULL) );
  69. ESP_ERROR_CHECK( esp_wifi_start() );
  70. initialized = true;
  71. led_blink(LED_GREEN, 250, 250);
  72. }
  73. static void wifi_join(void *arg)
  74. {
  75. const char *ssid = join_args.ssid->sval[0];
  76. const char *pass = join_args.password->sval[0];
  77. int timeout_ms = join_args.timeout->ival[0];
  78. initialise_wifi();
  79. wifi_config_t wifi_config = { 0 };
  80. strncpy((char *) wifi_config.sta.ssid, ssid, sizeof(wifi_config.sta.ssid));
  81. wifi_config.sta.ssid[sizeof(wifi_config.sta.ssid) - 1] = '\0';
  82. if (pass) {
  83. strncpy((char *) wifi_config.sta.password, pass, sizeof(wifi_config.sta.password));
  84. wifi_config.sta.password[sizeof(wifi_config.sta.password) - 1] = '\0';
  85. }
  86. ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
  87. ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
  88. ESP_ERROR_CHECK( esp_wifi_connect() );
  89. int bits = xEventGroupWaitBits(network_event_group, CONNECTED_BIT,
  90. pdFALSE, pdTRUE, timeout_ms / portTICK_PERIOD_MS);
  91. if (bits & CONNECTED_BIT) {
  92. ESP_LOGI(__func__, "Connected");
  93. } else {
  94. ESP_LOGW(__func__, "Connection timed out");
  95. }
  96. }
  97. //static int set_auto_connect(int argc, char **argv)
  98. //{
  99. // int nerrors = arg_parse(argc, argv, (void **) &join_args);
  100. // if (nerrors != 0) {
  101. // arg_print_errors(stderr, join_args.end, argv[0]);
  102. // return 1;
  103. // }
  104. // ESP_LOGI(__func__, "Connecting to '%s'",
  105. // join_args.ssid->sval[0]);
  106. //
  107. // /* set default value*/
  108. // if (join_args.timeout->count == 0) {
  109. // join_args.timeout->ival[0] = JOIN_TIMEOUT_MS;
  110. // }
  111. //
  112. // bool connected = wifi_join(join_args.ssid->sval[0],
  113. // join_args.password->sval[0],
  114. // join_args.timeout->ival[0]);
  115. // if (!connected) {
  116. // ESP_LOGW(__func__, "Connection timed out");
  117. // return 1;
  118. // }
  119. // ESP_LOGI(__func__, "Connected");
  120. // return 0;
  121. //}
  122. static int connect(int argc, char **argv)
  123. {
  124. int nerrors = arg_parse_msg(argc, argv,(struct arg_hdr **)&join_args);
  125. if (nerrors != 0) {
  126. return 1;
  127. }
  128. ESP_LOGI(__func__, "Connecting to '%s'",
  129. join_args.ssid->sval[0]);
  130. /* set default value*/
  131. if (join_args.timeout->count == 0) {
  132. join_args.timeout->ival[0] = JOIN_TIMEOUT_MS;
  133. }
  134. // need to use that trick to make sure we use internal stack
  135. xTimerStart(xTimerCreate("wifi_join", 1, pdFALSE, NULL, wifi_join), portMAX_DELAY);
  136. return 0;
  137. }
  138. void register_wifi_join()
  139. {
  140. join_args.timeout = arg_int0(NULL, "timeout", "<t>", "Connection timeout, ms");
  141. join_args.ssid = arg_str1(NULL, NULL, "<ssid>", "SSID of AP");
  142. join_args.password = arg_str0(NULL, NULL, "<pass>", "PSK of AP");
  143. join_args.end = arg_end(2);
  144. const esp_console_cmd_t join_cmd = {
  145. .command = "join",
  146. .help = "Join WiFi AP as a station",
  147. .hint = NULL,
  148. .func = &connect,
  149. .argtable = &join_args
  150. };
  151. ESP_ERROR_CHECK( esp_console_cmd_register(&join_cmd) );
  152. }
  153. void register_wifi()
  154. {
  155. #ifdef WIFI_CMDLINE
  156. register_wifi_join();
  157. if(bypass_network_manager){
  158. initialise_wifi();
  159. }
  160. #endif
  161. }