Help for the TCP/IP Server in access point mode

DaniGia
Posts: 3
Joined: Thu Nov 16, 2023 1:59 pm

Help for the TCP/IP Server in access point mode

Postby DaniGia » Thu Nov 16, 2023 2:27 pm

I would need a hand to write the code by setting the wifi as an access point, and then communicating via tcp/ip.
I would like to create non-blocking code that is mostly made up of functions so that I can then call them in main.

This is the part of the code where I set the wifi and tcp/ip:
  1. /* BSD non-blocking socket 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 <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <sys/param.h>
  13. #include <lwip/netdb.h>
  14.  
  15. #include "freertos/FreeRTOS.h"
  16. #include "freertos/task.h"
  17.  
  18. #include "sys/socket.h"
  19.  
  20. #include "netdb.h"
  21. #include "errno.h"
  22. #include "esp_mac.h"
  23. #include "esp_system.h"
  24. #include "esp_event.h"
  25. #include "esp_log.h"
  26. #include "esp_wifi.h"
  27.  
  28. #include "nvs_flash.h"
  29. #include "driver/gpio.h"
  30.  
  31. #include "TCP-IP.h"
  32.  
  33. #define SSID_WIFI       "ESP32-C3 Access Point"
  34. #define PASSWORD_WIFI   "esp32c3accesspoint"
  35. #define PORT_NUMBER 8001
  36.  
  37. static const char *TAG = "wifi softAP";
  38.  
  39. static void wifi_event_handler(void* arg, esp_event_base_t event_base,
  40.                                     int32_t event_id, void* event_data)
  41. {
  42.     if (event_id == WIFI_EVENT_AP_STACONNECTED) {
  43.         wifi_event_ap_staconnected_t* event = (wifi_event_ap_staconnected_t*) event_data;
  44.         ESP_LOGI(TAG, "station "MACSTR" join, AID=%d",
  45.                  MAC2STR(event->mac), event->aid);
  46.     } else if (event_id == WIFI_EVENT_AP_STADISCONNECTED) {
  47.         wifi_event_ap_stadisconnected_t* event = (wifi_event_ap_stadisconnected_t*) event_data;
  48.         ESP_LOGI(TAG, "station "MACSTR" leave, AID=%d",
  49.                  MAC2STR(event->mac), event->aid);
  50.     }
  51. }
  52.  
  53. void WiFiInit(void)
  54. {
  55.     wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  56.  
  57.     wifi_config_t ApConfig = {
  58.         .ap = {
  59.             .ssid = SSID_WIFI,
  60.             .ssid_len = strlen(SSID_WIFI),
  61.             .password = PASSWORD_WIFI,
  62.             .channel=6,
  63.             .authmode=WIFI_AUTH_WPA2_PSK,
  64.             .ssid_hidden=0,
  65.             .max_connection=4,
  66.             .beacon_interval=100
  67.         },
  68.     };
  69.     ESP_ERROR_CHECK(nvs_flash_init());
  70.     ESP_ERROR_CHECK(esp_netif_init());
  71.     ESP_ERROR_CHECK(esp_event_loop_create_default());
  72.  
  73.     esp_netif_create_default_wifi_ap();
  74.    
  75.     ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL, NULL));
  76.  
  77.     esp_wifi_init(&cfg);
  78.     esp_wifi_set_storage(WIFI_STORAGE_RAM);
  79.     esp_wifi_set_mode(WIFI_MODE_AP);
  80.     esp_wifi_set_config(ESP_IF_WIFI_AP, &ApConfig);
  81.     esp_wifi_start();
  82.  
  83.     ESP_LOGI(TAG, "wifi_init_softap finished. SSID:%s password:%s", SSID_WIFI, PASSWORD_WIFI);
  84. }
  85.  
  86. void socket_server_task(void) {
  87.     struct sockaddr_in clientAddress;
  88.     struct sockaddr_in serverAddress;
  89.  
  90.     // Create a socket that we will listen upon.
  91.     int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  92.     if (sock < 0) {
  93.         ESP_LOGE(TAG, "socket: %d %s", sock, strerror(errno));
  94.         goto END;
  95.     }
  96.  
  97.     // Bind our server socket to a port.
  98.     serverAddress.sin_family = AF_INET;
  99.     serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
  100.     serverAddress.sin_port = htons(PORT_NUMBER);
  101.     int rc  = bind(sock, (struct sockaddr *)&serverAddress, sizeof(serverAddress));
  102.     if (rc < 0) {
  103.         ESP_LOGE(TAG, "bind: %d %s", rc, strerror(errno));
  104.         goto END;
  105.     }
  106.  
  107.     // Flag the socket as listening for new connections.
  108.     rc = listen(sock, 5);
  109.     if (rc < 0) {
  110.         ESP_LOGE(TAG, "listen: %d %s", rc, strerror(errno));
  111.         goto END;
  112.     }
  113.  
  114.     while (1) {
  115.         // Listen for a new client connection.
  116.         ESP_LOGD(TAG, "Waiting for new client connection");
  117.         socklen_t clientAddressLength = sizeof(clientAddress);
  118.         int clientSock = accept(sock, (struct sockaddr *)&clientAddress, &clientAddressLength);
  119.         if (clientSock < 0) {
  120.             ESP_LOGE(TAG, "accept: %d %s", clientSock, strerror(errno));
  121.             goto END;
  122.         }
  123.         sendData(clientSock);
  124.     }
  125.     END:
  126.     vTaskDelete(NULL);
  127. }
  128.  
  129. static void sendData(int socket) {
  130.     char text[80];
  131.     int i;
  132.     for(i=1;i<=10;i++){
  133.         sprintf(text, "Message %d\n", i);
  134.         send(socket, text, strlen(text), 0);
  135.         vTaskDelay(1000/portTICK_PERIOD_MS);
  136.     }
  137.     close(socket);
  138. }

This is the main part:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #include "TCP-IP.h"
  6.  
  7. void app_main(void)
  8. {
  9.     WiFiInit();
  10.     socket_server_task();
  11. }
  12.  
Thanks.

chris33
Posts: 9
Joined: Wed Dec 22, 2021 10:35 am

Re: Help for the TCP/IP Server in access point mode

Postby chris33 » Sat Nov 18, 2023 5:44 pm

I'm not a sockets Guru but I think you should change

Code: Select all

serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
to

Code: Select all

inet_pton(AF_INET, "192.168.4.1", &serverAddress.sin_addr);
because "192.168.4.1" is the IP addr of the softAP thing, and this is the (iface ? dunno what to call it) you want to bind your socket to.

DaniGia
Posts: 3
Joined: Thu Nov 16, 2023 1:59 pm

Re: Help for the TCP/IP Server in access point mode

Postby DaniGia » Fri Nov 24, 2023 1:11 pm

chris33 wrote:
Sat Nov 18, 2023 5:44 pm
I'm not a sockets Guru but I think you should change

Code: Select all

serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
to

Code: Select all

inet_pton(AF_INET, "192.168.4.1", &serverAddress.sin_addr);
because "192.168.4.1" is the IP addr of the softAP thing, and this is the (iface ? dunno what to call it) you want to bind your socket to.
In the end the code above is correct, I was wrong about the IP I was accessing. I was trying to access the IP address given to the device instead of my own (192.168.4.1).

Thanks anyway for the reply. :D

Who is online

Users browsing this forum: No registered users and 68 guests