ESP32 WIFI-AP + TCP-Socket-Server

roge__
Posts: 2
Joined: Tue Jun 27, 2017 1:53 pm

ESP32 WIFI-AP + TCP-Socket-Server

Postby roge__ » Tue Jun 27, 2017 2:01 pm

Hello guys,
this is my first Topic and I am a really beginner...
I used different code examples to build an WLAN-AP and a Socket-Server based on kolban's examples:

Code: Select all


#include <lwip/sockets.h>
#include <esp_log.h>
#include <string.h>
#include <errno.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>

#include <esp_event.h>
#include <esp_event_loop.h>
#include <esp_system.h>
#include <esp_wifi.h>
#include <nvs_flash.h>
#include <stdio.h>
#include <string.h>
#include "driver/ledc.h"
#include "sdkconfig.h"

#define PORT_NUMBER 8001

static char tag[] = "socket_server";

static void sendData(int socket) {
	char text[80];
	int i;
	for(i=1;i<=10;i++){
		sprintf(text, "Message %d\n", i);
		send(socket, text, strlen(text), 0);
		vTaskDelay(1000/portTICK_PERIOD_MS);
	}
	close(socket);
}

esp_err_t wifi_event_handler(void *ctx, system_event_t *event) {
    return ESP_OK;
}

/**

Creates a new Wifi-AP on ESP32

*/
void wifi_start_access_point() {
    wifi_config_t wifi_config = {
        .ap = {
            .ssid="_ ESP32",
            .ssid_len=0,
            .password="12345678",
            .channel=6,
            .authmode=WIFI_AUTH_WPA2_PSK,
            .ssid_hidden=0,
            .max_connection=4,
            .beacon_interval=100
        }
    };

    tcpip_adapter_init();
    esp_event_loop_init(wifi_event_handler, NULL);
    wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT();
    esp_wifi_init(&wifi_init_config);
    esp_wifi_set_storage(WIFI_STORAGE_RAM);
    esp_wifi_set_mode(WIFI_MODE_AP);
    esp_wifi_set_config(WIFI_IF_AP, &wifi_config);
    esp_wifi_start();
}

/**
 * Create a listening socket.  We then wait for a client to connect.
 * Once a client has connected, we then read until there is no more data
 * and log the data read.  We then close the client socket and start
 * waiting for a new connection.
 */
void socket_server_task(void *ignore) {
	struct sockaddr_in clientAddress;
	struct sockaddr_in serverAddress;

	// Create a socket that we will listen upon.
	int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (sock < 0) {
		ESP_LOGE(tag, "socket: %d %s", sock, strerror(errno));
		goto END;
	}

	// Bind our server socket to a port.
	serverAddress.sin_family = AF_INET;
	serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
	serverAddress.sin_port = htons(PORT_NUMBER);
	int rc  = bind(sock, (struct sockaddr *)&serverAddress, sizeof(serverAddress));
	if (rc < 0) {
		ESP_LOGE(tag, "bind: %d %s", rc, strerror(errno));
		goto END;
	}

	// Flag the socket as listening for new connections.
	rc = listen(sock, 5);
	if (rc < 0) {
		ESP_LOGE(tag, "listen: %d %s", rc, strerror(errno));
		goto END;
	}

	while (1) {
		// Listen for a new client connection.
		ESP_LOGD(tag, "Waiting for new client connection");
		socklen_t clientAddressLength = sizeof(clientAddress);
		int clientSock = accept(sock, (struct sockaddr *)&clientAddress, &clientAddressLength);
		if (clientSock < 0) {
			ESP_LOGE(tag, "accept: %d %s", clientSock, strerror(errno));
			goto END;
		}
		sendData(clientSock);
	}
	END:
	vTaskDelete(NULL);
}

//--------
//  Main
//--------

void app_main() {
    nvs_flash_init();

    
    wifi_start_access_point();
    //socket_server_task();
    vTaskDelete(NULL);
}

My Problem is:
when i connect to the AP and type in:

Code: Select all

roge@rg-tp:~$ nc 192.168.4.1 8001
the ESP does nothing....

Does anybody has an idea?


Thank You!

roge

ESP_Sprite
Posts: 9730
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32 WIFI-AP + TCP-Socket-Server

Postby ESP_Sprite » Wed Jun 28, 2017 4:18 am

Is your network connectivity OK? That is, can you try e.g. ping'ing the ESP?

preetam
Posts: 49
Joined: Thu Jan 26, 2017 2:31 pm
Location: Germany

Re: ESP32 WIFI-AP + TCP-Socket-Server

Postby preetam » Wed Jun 28, 2017 8:20 am

Hi,

After going through the code i think you might have done this things.

you have also commented //socket_server_task(); , remove this comment and try

Code: Select all

wifi_start_access_point();
    socket_server_task();    
    vTaskDelete(NULL);
1. Checked whether the esp32 SSID is broadcasting.
2. Checked whether your computer is able to connect to esp32's SSID.
3. Checked whether you have the ip using ifconfig.
4. whether the ip passed to nc tool is the esp32's ip. if not you can make a static ip creation for your esp32 and then try the nc tool

Thanks and Regards,
Paul

roge__
Posts: 2
Joined: Tue Jun 27, 2017 1:53 pm

Re: ESP32 WIFI-AP + TCP-Socket-Server

Postby roge__ » Wed Jun 28, 2017 5:39 pm

Thank you for you answers!

I found the problem and i'm a little bit embarrassed.. :oops:

I just forgot to create the socket_server_task with xTaskCreate() in app_main...

Now it works!!

BaartCM
Posts: 17
Joined: Tue Mar 13, 2018 11:55 am

Re: ESP32 WIFI-AP + TCP-Socket-Server

Postby BaartCM » Tue Mar 20, 2018 11:48 am

Hello roge_

I too am very new to ESP32 and trying to learn wifi.

Can you please post your working code so I can learn from it?

Thanks, Steve.

marko.brelak
Posts: 23
Joined: Wed Apr 10, 2019 1:26 pm

Re: ESP32 WIFI-AP + TCP-Socket-Server

Postby marko.brelak » Mon Apr 29, 2019 5:11 pm

Hi all,

Currently I am doing the very similar thing as the author of this post was. I have a success of entering the AP mode and opening a socket on the port 8001.
When I connect to the AP with my laptop, in the console I get "softAP assign IP to station, IP is: 192.168.4.2" which is fine. That means that my laptop has that address now. Now I want to know how can I get the AP IP address so I can connect to it in order to listen the port 8001 (I use script communicator)? Can I print it somehow or where is the placeholder for that data?

Using the script communicator I have tried to connect to 192.168.4.1 and that was a success and a lucky guess.

Do you have some advice on this topic?

Thank you very much in advance.
Marko

Who is online

Users browsing this forum: No registered users and 87 guests