Page 1 of 1

ESP32 wifi ap mode

Posted: Tue Oct 15, 2019 7:14 am
by Anil kumar
#ifndef __WIFI_C__
#define __WIFI_C__

#include <esp_wifi.h>
#include <esp_event_loop.h>
#include <freertos/event_groups.h>
#include <esp_system.h>
#include <nvs_flash.h>
#include <sdkconfig.h>
#include </home/mallan/esp_printer/esp-idf/components/lwip/include/lwip/lwip/err.h>
#include </home/mallan/esp_printer/esp-idf/components/lwip/include/lwip/lwip/sockets.h>
#include </home/mallan/esp_printer/esp-idf/components/lwip/include/lwip/lwip/sys.h>
#include </home/mallan/esp_printer/esp-idf/components/lwip/include/lwip/lwip/netdb.h>
#include </home/mallan/esp_printer/esp-idf/components/lwip/include/lwip/lwip/dns.h>
#include </home/mallan/esp_printer/esp-idf/components/heap/include/esp_heap_caps.h>
#include "esp_log.h"
#include "wifi.h"
#include "extern_head.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"


#define EXAMPLE_ESP_WIFI_SSID "My_WIFI_PRINTER"
#define EXAMPLE_ESP_WIFI_PASS "myprinter"
#define EXAMPLE_MAX_STA_CONN 8
#define TRUE 1
#define FALSE 0
#define PORT 8242

static EventGroupHandle_t s_wifi_event_group;
const int CONNECTED_BIT = BIT0;
static const char *TAG = "WIFI_PRINTER";


static esp_err_t event_handler(void *ctx, system_event_t *event)
{
switch(event->event_id) {
case SYSTEM_EVENT_AP_STACONNECTED:
printf( "station:"MACSTR" join, AID=%d",
MAC2STR(event->event_info.sta_connected.mac),
event->event_info.sta_connected.aid);
break;
case SYSTEM_EVENT_AP_STADISCONNECTED:
printf( "station:"MACSTR"leave, AID=%d",
MAC2STR(event->event_info.sta_disconnected.mac),
event->event_info.sta_disconnected.aid);
break;

default:
break;
}
return ESP_OK;
}

void wifi_init_softap() // calling this func from main
{

esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK( ret );
s_wifi_event_group = xEventGroupCreate();

tcpip_adapter_init();
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));

wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
wifi_config_t wifi_config = {
.ap = {
.ssid = EXAMPLE_ESP_WIFI_SSID,
.ssid_len = strlen(EXAMPLE_ESP_WIFI_SSID),
.password = EXAMPLE_ESP_WIFI_PASS,
.max_connection = EXAMPLE_MAX_STA_CONN,
.authmode = WIFI_AUTH_WPA_WPA2_PSK
},
};
if (strlen(EXAMPLE_ESP_WIFI_PASS) == 0) {
wifi_config.ap.authmode = WIFI_AUTH_OPEN;
}

ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config));
ESP_ERROR_CHECK(esp_wifi_start());
printf("wifi_init_softap finished.SSID:%s password:%s",EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
}

void tcp_server_task(void *pvParameter)
{
int opt = TRUE;
int master_socket , addrlen , new_socket , client_socket[8] ,max_clients = 8 , activity, i , valread,sd=0 ;
int max_sd;
struct sockaddr_in MySock;

MySock.sin_family = AF_INET;
MySock.sin_addr.s_addr = INADDR_ANY;
MySock.sin_port = htons( PORT );

//char buffer[990];
fd_set readfds;
char *message = "Connected to socket \r\n";

for (i = 0; i < max_clients; i++)
{
client_socket = 0;
}
if( (master_socket = socket(AF_INET , SOCK_STREAM , 0)) == 0)
{
perror("socket failed");
printf("socket failed\n");
exit(EXIT_FAILURE);
}

//set master socket to allow multiple connections ,
//this is just a good habit, it will work without this
if( setsockopt(master_socket, SOL_SOCKET, SO_REUSEADDR, (char *)&opt,
sizeof(opt)) < 0 )
{
perror("setsockopt");
exit(EXIT_FAILURE);
}


if (bind(master_socket, (struct sockaddr *)&MySock, sizeof(MySock))<0)
{
perror("bind failed");
printf("bind failed\n");
exit(EXIT_FAILURE);
}
printf("Listener on port %d \n", PORT);
if (listen(master_socket, 5) < 0)
{
perror("listen");
printf("listen failed\n");
exit(EXIT_FAILURE);
}
addrlen = sizeof(MySock);
puts("Waiting for connections ...");

while(TRUE)
{
FD_ZERO(&readfds);
vTaskDelay(10/ portTICK_PERIOD_MS);
FD_SET(master_socket, &readfds);
max_sd = master_socket;

for ( i = 0 ; i < max_clients ; i++)
{
sd = client_socket;
if(sd > 0)
FD_SET( sd , &readfds);
if(sd > max_sd)
max_sd = sd;
}
activity = select( max_sd + 1 , &readfds , NULL , NULL , NULL);

if ((activity < 0) && (errno!=EINTR))
{
printf("select error");
}
if (FD_ISSET(master_socket, &readfds))
{
if ((new_socket = accept(master_socket,
(struct sockaddr *)&MySock, (socklen_t*)&addrlen))<0)
{
perror("accept");
printf("accept failed\n");
exit(EXIT_FAILURE);
}
printf("New connection , socket fd is %d , ip is : %s , port : %d\n" , new_socket , inet_ntoa(MySock.sin_addr) , ntohs
(MySock.sin_port));
if( send(new_socket, message, strlen(message), 0) != strlen(message) )
{
perror("send");
}


puts("Welcome message sent successfully");
for (i = 0; i < max_clients; i++)
{
if( client_socket == 0 )
{
client_socket = new_socket;
printf("Adding to list of sockets as %d\n" , i);
break;
}
}
}
for (i = 0; i < max_clients; i++)
{
sd = client_socket;

if (FD_ISSET( sd , &readfds))
{
if ((valread = read( sd , CIRCULAR_RECEIVE_BUFFER, 990)) == 0)
{
getpeername(sd , (struct sockaddr*)&MySock ,(socklen_t*)&addrlen);
printf("Host disconnected , ip %s , port %d \n" ,
inet_ntoa(MySock.sin_addr) , ntohs(MySock.sin_port));
close( sd );
client_socket = 0;
}
else
{
FLAG.Wait_one_second_to_enable_print_flag = 1;
vTaskDelay(10/ portTICK_PERIOD_MS);
CIRCULAR_RECEIVE_BUFFER[valread] = '\0';

// for(int inx=0;buffer[inx]!='\0';inx++,RX_TOTAL_COUNT++)
// CIRCULAR_RECEIVE_BUFFER[RX_TOTAL_COUNT % BUFFER_SIZE] =buffer[inx];
// CIRCULAR_RECEIVE_BUFFER[RX_TOTAL_COUNT]='\0';

printf("buffer=%s\n",CIRCULAR_RECEIVE_BUFFER);
// send(sd , CIRCULAR_RECEIVE_BUFFER , strlen(CIRCULAR_RECEIVE_BUFFER) , 0 );
//memset(CIRCULAR_RECEIVE_BUFFER,0,990);
}

}
}
}
}
#endif

the above code, iam using for hosting the wifi(AP), for me its getting connected to the client device but if i want to send any command or something, it is saying like "Host is not found" ,(using TCP telnet terminal app) so please can anyone help me out.