I tried to combine ideas from posts of you two.
Meaning that I can increase the number of buffers and the length of them.
And that I have to create one and only one socket before entering the loop of "read and send".
I modified the code and made it easier to read :
Code: Select all
#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_wifi.h"
#include "esp_event_loop.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_log.h"
#include "lwip/err.h"
#include "lwip/sockets.h"
#include "lwip/sys.h"
#include "lwip/netdb.h"
#include "lwip/dns.h"
#include "nvs.h"
#include "nvs_flash.h"
#include "driver/i2s.h"
#include "soc/syscon_reg.h"
#include "driver/adc.h"
#define BUF_LEN 512
#define TAG1 "bytes read"
#define SSID "PCDylanR"
#define PASSPHARSE "motdepasse"
#define TCPServerIP "192.168.137.1"
#define SAMPLERATE 40000
static QueueHandle_t i2s_event_queue;
static EventGroupHandle_t wifi_event_group;
const int CONNECTED_BIT = BIT0;
static const char *TAG="tcp_client";
void wifi_connect()
{
wifi_config_t cfg = {
.sta = {
.ssid = SSID,
.password = PASSPHARSE,
},
};
ESP_ERROR_CHECK( esp_wifi_disconnect() );
ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &cfg) );
ESP_ERROR_CHECK( esp_wifi_connect() );
}
static esp_err_t event_handler(void *ctx, system_event_t *event)
{
switch(event->event_id)
{
case SYSTEM_EVENT_STA_START:
wifi_connect();
break;
case SYSTEM_EVENT_STA_GOT_IP:
xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
esp_wifi_connect();
xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
break;
default:
break;
}
return ESP_OK;
}
static void initialise_wifi(void)
{
esp_log_level_set("wifi", ESP_LOG_NONE);
tcpip_adapter_init();
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
ESP_ERROR_CHECK( esp_wifi_start() );
}
void app_main()
{
ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) );
wifi_event_group = xEventGroupCreate();
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 );
initialise_wifi();
i2s_config_t i2s_config = {
.mode = I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_ADC_BUILT_IN,
.sample_rate = SAMPLERATE,
.bits_per_sample = 16,
.use_apll = true,
.communication_format = I2S_COMM_FORMAT_I2S_MSB,
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
.intr_alloc_flags =0,
.dma_buf_count =3,
.dma_buf_len = BUF_LEN
};
i2s_driver_install(I2S_NUM_0, &i2s_config, 1, &i2s_event_queue);
i2s_set_adc_mode(ADC_UNIT_1, ADC1_CHANNEL_0);
i2s_set_sample_rates(I2S_NUM_0, SAMPLERATE);
SET_PERI_REG_MASK(SYSCON_SARADC_CTRL2_REG, SYSCON_SARADC_SAR1_INV);
adc1_config_channel_atten(ADC1_CHANNEL_0,ADC_ATTEN_DB_11);
vTaskDelay(5000/portTICK_RATE_MS);
i2s_adc_enable(I2S_NUM_0);
ESP_LOGI(TAG,"démarage du mode TCP \n");
struct sockaddr_in tcpServerAddr;
tcpServerAddr.sin_addr.s_addr = inet_addr(TCPServerIP);
tcpServerAddr.sin_family = AF_INET;
tcpServerAddr.sin_port = htons( 3010 );
int i2s_read_len = BUF_LEN*2;
size_t bytes_read;
char* i2s_read_buff = (char*) calloc(i2s_read_len,sizeof(char));
i2s_event_t evt;
int s;
s=socket(AF_INET, SOCK_STREAM, 0);
ESP_LOGI(TAG,"ICI %d \n",s);
while(1)
{
ESP_LOGI(TAG,"la %d \n",s);
xEventGroupWaitBits(wifi_event_group,CONNECTED_BIT,false,true,portMAX_DELAY);
if (xQueueReceive(i2s_event_queue, &evt, portMAX_DELAY) == pdPASS)
{
if (evt.type==I2S_EVENT_RX_DONE)
{
i2s_read(I2S_NUM_0, (void*)i2s_read_buff, i2s_read_len,&bytes_read, portMAX_DELAY);
if(s < 0)
{
ESP_LOGE(TAG, "... Impossible d'alouer un socket. errno : %d \n", errno);
vTaskDelay(2000 / portTICK_PERIOD_MS);
continue;
}
if(connect(s, (struct sockaddr *)&tcpServerAddr, sizeof(tcpServerAddr)) != 0)
{
ESP_LOGE(TAG, "... problème de connexion errno=%d \n", errno);
close(s);
vTaskDelay(4000 / portTICK_PERIOD_MS);
continue;
}
ESP_LOGI(TAG, "... connecté \n");
if( send(s , i2s_read_buff , i2s_read_len,0) < 0)
{
ESP_LOGE(TAG, "... Send failed \n");
close(s);
vTaskDelay(4000 / portTICK_PERIOD_MS);
continue;
}
ESP_LOG_BUFFER_HEX("i2s_read_buff", i2s_read_buff, 16);
}
}
}
}
I added 1 buffer.
I removed the "close(s);" to avoid the open-close problem as you said.
But when I try it gives me this on terminal :
Code: Select all
I (5339) i2s_read_buff: 8f 01 85 01 98 01 8f 01 9f 01 98 01 a7 01 9f 01
I (5339) tcp_client: la 54 <------------------------------------------------------------Just to see if socket()>0
tcp_connect: can only connect from state CLOSED
E (5349) tcp_client: ... problème de connexion errno=127
I (9359) tcp_client: la 54
E (9359) tcp_client: ... problème de connexion errno=9
...
Code: Select all
I (5328) i2s_read_buff: 15 02 0c 02 1f 02 15 02 30 02 1f 02 3c 02 30 02
I (5328) tcp_client: la 54
E (5328) tcp_client: ... problème de connexion errno=9
I (9338) tcp_client: la 54
E (9338) tcp_client: ... problème de connexion errno=9
...