Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2020
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.47 KB | None | 0 0
  1. /*  WiFi softAP Example
  2.  
  3.    This example code is in the Public Domain (or CC0 licensed, at your option.)
  4.  
  5.    Unless required by applicable law or agreed to in writing, this
  6.    software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  7.    CONDITIONS OF ANY KIND, either express or implied.
  8. */
  9. #include <string.h>
  10. #include <sys/param.h>
  11. #include "freertos/FreeRTOS.h"
  12. #include "freertos/task.h"
  13. #include "freertos/event_groups.h"
  14. #include "esp_system.h"
  15. #include "esp_wifi.h"
  16. #include "esp_event_loop.h"
  17. #include "esp_log.h"
  18. #include "nvs_flash.h"
  19. //#include "esp_vfs.h"
  20. //#include "esp_vfs_dev.h"
  21. #include "driver/uart.h"
  22.  
  23. #include "lwip/err.h"
  24. #include "lwip/sockets.h"
  25. #include "lwip/sys.h"
  26. #include <lwip/netdb.h>
  27.  
  28. /* The examples use WiFi configuration that you can set via 'make menuconfig'.
  29.  
  30.    If you'd rather not, just change the below entries to strings with
  31.    the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"
  32. */
  33. #define EXAMPLE_ESP_WIFI_SSID   "BMTIMPLANT"
  34. #define EXAMPLE_ESP_WIFI_PASS   "lsbmt123"
  35. #define EXAMPLE_MAX_STA_CONN    3
  36. #define PORT                    5005
  37.  
  38. #define EX_UART_NUM UART_NUM_1
  39. #define UART_BUF_SIZE (1024)
  40.  
  41. /* FreeRTOS event group to signal when we are connected*/
  42. static EventGroupHandle_t s_wifi_event_group;
  43.  
  44. static const char *TAG0 = "wifi softAP";
  45. static const char *TAG1 = "uart_events";
  46.  
  47. static esp_err_t event_handler(void *ctx, system_event_t *event)
  48. {
  49.     switch(event->event_id) {
  50.     case SYSTEM_EVENT_AP_STACONNECTED:
  51.         ESP_LOGI(TAG0, "station:"MACSTR" join, AID=%d",
  52.                  MAC2STR(event->event_info.sta_connected.mac),
  53.                  event->event_info.sta_connected.aid);
  54.         break;
  55.     case SYSTEM_EVENT_AP_STADISCONNECTED:
  56.         ESP_LOGI(TAG0, "station:"MACSTR"leave, AID=%d",
  57.                  MAC2STR(event->event_info.sta_disconnected.mac),
  58.                  event->event_info.sta_disconnected.aid);
  59.         break;
  60.     default:
  61.         break;
  62.     }
  63.     return ESP_OK;
  64. }
  65.  
  66. void wifi_init_softap()
  67. {
  68.     s_wifi_event_group = xEventGroupCreate();
  69.  
  70.     tcpip_adapter_init();
  71.     ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
  72.  
  73.     wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  74.     ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  75.     wifi_config_t wifi_config = {
  76.         .ap = {
  77.             .ssid = EXAMPLE_ESP_WIFI_SSID,
  78.             .ssid_len = strlen(EXAMPLE_ESP_WIFI_SSID),
  79.             .password = EXAMPLE_ESP_WIFI_PASS,
  80.             .max_connection = EXAMPLE_MAX_STA_CONN,
  81.             .authmode = strlen(EXAMPLE_ESP_WIFI_PASS) ? WIFI_AUTH_WPA_WPA2_PSK : WIFI_AUTH_OPEN
  82.         },
  83.     };
  84.  
  85.     ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
  86.     ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config));
  87.     ESP_ERROR_CHECK(esp_wifi_start());
  88.  
  89.     ESP_LOGI(TAG0, "wifi_init_softap finished.SSID:%s password:%s", EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
  90. }
  91.  
  92. int setup_server_socket()
  93. {
  94.     struct sockaddr_in destAddr;
  95.     destAddr.sin_addr.s_addr= htonl(INADDR_ANY);
  96.     destAddr.sin_family     = AF_INET;
  97.     destAddr.sin_port       = htons(PORT);
  98.  
  99.     int tcp_server_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
  100.     if (tcp_server_sock < 0) {
  101.         ESP_LOGE(TAG0, "Unable to create socket: errno %d", errno);
  102.         return 0;
  103.     }
  104.     ESP_LOGI(TAG0, "Socket created");
  105.  
  106.     int err = bind(tcp_server_sock, (struct sockaddr *)&destAddr, sizeof(destAddr));
  107.     if (err != 0) {
  108.         ESP_LOGE(TAG0, "Socket unable to bind: errno %d", errno);
  109.         return 0;
  110.     }
  111.     ESP_LOGI(TAG0, "Socket bound");
  112.  
  113.     err = listen(tcp_server_sock, 1);
  114.     if (err != 0) {
  115.         ESP_LOGE(TAG0, "Error occured during listen: errno %d", errno);
  116.         return 0;
  117.     }
  118.     ESP_LOGI(TAG0, "Socket listening");
  119.  
  120.     return tcp_server_sock;
  121. }
  122.  
  123. void setup_uart()
  124. {
  125.     uart_config_t uart_config = {
  126.         .baud_rate = 4000000,
  127.         .data_bits = UART_DATA_8_BITS,
  128.         .parity    = UART_PARITY_DISABLE,
  129.         .stop_bits = UART_STOP_BITS_1,
  130.         .flow_ctrl = UART_HW_FLOWCTRL_DISABLE
  131.     };
  132.  
  133.     uart_param_config(EX_UART_NUM, &uart_config);
  134.     // Set UART pins(TX: IO16 (UART2 default), RX: IO17 (UART2 default), RTS: IO18, CTS: IO19)
  135.     ESP_ERROR_CHECK(uart_set_pin(EX_UART_NUM, 16, 17, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
  136.     ESP_ERROR_CHECK(uart_driver_install(EX_UART_NUM, UART_BUF_SIZE * 2, UART_BUF_SIZE * 2, 0, NULL, 0));
  137. }
  138.  
  139. bool handle_uart_rx(int tcp_client_sock)
  140. {
  141.     uint8_t buf[UART_BUF_SIZE];
  142.  
  143.     int len = uart_read_bytes(EX_UART_NUM, buf, sizeof(buf), 0);
  144.     //ESP_LOGI(TAG1, "uart_rx: %d %d", len, tcp_client_sock);
  145.  
  146.     if(tcp_client_sock)
  147.     {
  148.         int err = send(tcp_client_sock, buf, len, 0);
  149.         //ESP_LOGI(TAG1, "tcp sent");
  150.         if (err < 0)
  151.         {
  152.             ESP_LOGE(TAG0, "Error occured during sending: errno %d", errno);
  153.             return false;
  154.         }
  155.     }
  156.  
  157.     return true;
  158. }
  159.  
  160. int handle_tcp_connect(int tcp_server_sock)
  161. {
  162.     struct sockaddr_in6 sourceAddr; // Large enough for both IPv4 or IPv6
  163.     uint addrLen = sizeof(sourceAddr);
  164.     int sock = accept(tcp_server_sock, (struct sockaddr *)&sourceAddr, &addrLen);
  165.     if (sock < 0)
  166.     {
  167.         ESP_LOGE(TAG0, "Unable to accept connection: errno %d", errno);
  168.         return 0;
  169.     }
  170.     ESP_LOGI(TAG0, "Socket accepted");
  171.  
  172.     return sock;
  173. }
  174.  
  175. void close_tcp_client_sock(int sock)
  176. {
  177.     if (sock != -1)
  178.     {
  179.         //ESP_LOGE(TAG0, "Shutting down socket");
  180.         //shutdown(sock, 2);
  181.         //vTaskDelay(20 / portTICK_PERIOD_MS);
  182.         ESP_LOGE(TAG0, "closing socket");
  183.         close(sock);
  184.         ESP_LOGE(TAG0, "now...");
  185.     }
  186. }
  187.  
  188. bool handle_tcp_recv(int sock)
  189. {
  190.     char rx_buffer[128];
  191.  
  192.     int len = recv(sock, rx_buffer, sizeof(rx_buffer), 0);
  193.     // Error occured during receiving
  194.     if (len < 0)
  195.     {
  196.         ESP_LOGE(TAG0, "recv failed: errno %d", errno);
  197.         return false;
  198.     }
  199.     // Connection closed
  200.     else if (len == 0)
  201.     {
  202.         ESP_LOGI(TAG0, "Connection closed");
  203.         return false;
  204.     }
  205.     // Data received
  206.     else
  207.     {
  208.         uart_write_bytes(EX_UART_NUM, rx_buffer, len);
  209.     }
  210.     return true;
  211. }
  212.  
  213. int check_for_actions(int tcp_server_sock, int tcp_client_sock)
  214. {
  215.     fd_set rfds;
  216.  
  217.     FD_ZERO(&rfds);
  218.     //FD_SET(uart_sock, &rfds);
  219.     FD_SET(tcp_client_sock ? tcp_client_sock : tcp_server_sock, &rfds);//either accept a new client or serve the existing one
  220.  
  221.     struct timeval tv = {
  222.         .tv_sec = 0,
  223.         .tv_usec = 0,
  224.     };
  225.  
  226.     int nfs = MAX(tcp_server_sock, tcp_client_sock);
  227.     int ret = select(nfs + 1, &rfds, NULL, NULL, &tv);
  228.  
  229.     size_t size;
  230.     ESP_ERROR_CHECK(uart_get_buffered_data_len(EX_UART_NUM, &size));
  231.    
  232.     if (ret < 0)
  233.     {
  234.         ESP_LOGE(TAG0, "Select failed: errno %d", errno);
  235.         return tcp_client_sock;
  236.     }
  237.  
  238.     if (FD_ISSET(tcp_server_sock, &rfds))
  239.         tcp_client_sock = handle_tcp_connect(tcp_server_sock);
  240.     else if ((size && !handle_uart_rx(tcp_client_sock)) || (FD_ISSET(tcp_client_sock, &rfds)    && !handle_tcp_recv(tcp_client_sock)))
  241.     {
  242.         close_tcp_client_sock(tcp_client_sock);
  243.         return 0;
  244.     }
  245.  
  246.     if (ret == 0 && size == 0)
  247.     {
  248.         vTaskDelay(10 / portTICK_PERIOD_MS);
  249.     }
  250.  
  251.     return tcp_client_sock;
  252. }
  253.  
  254. static void tcp_server_task(void *pvParameters)
  255. {
  256.     int tcp_client_sock = 0;
  257.     int tcp_server_sock = setup_server_socket();
  258.     setup_uart();
  259.  
  260.     while (tcp_server_sock)
  261.     {
  262.         tcp_client_sock = check_for_actions(tcp_server_sock, tcp_client_sock);
  263.     }
  264.     vTaskDelete(NULL);
  265. }
  266.  
  267. void app_main()
  268. {
  269.     //Initialize NVS
  270.     esp_err_t ret = nvs_flash_init();
  271.     if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  272.       ESP_ERROR_CHECK(nvs_flash_erase());
  273.       ret = nvs_flash_init();
  274.     }
  275.     ESP_ERROR_CHECK(ret);
  276.    
  277.     ESP_LOGI(TAG0, "ESP_WIFI_MODE_AP");
  278.     wifi_init_softap();
  279.     xTaskCreate(tcp_server_task, "tcp_server", 4096, NULL, 5, NULL);
  280. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement