ADC glitchs - I2S - ESP32

DylanR0
Posts: 12
Joined: Mon Aug 06, 2018 2:42 pm

Re: ADC glitchs - I2S - ESP32

Postby DylanR0 » Wed Aug 08, 2018 3:26 pm

Thank you John A for your explanation.
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);
            }
        }
    }
}
So I removed the "s=socket(AF_INET, SOCK_STREAM, 0);" from the loop to create it just one time.
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
...
So I tried to put "close(s)" at the end of the loop and I got :

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
...
I have one data but after, I have some errors. no9 = bad file number but why ? :?:

User avatar
fly135
Posts: 606
Joined: Wed Jan 03, 2018 8:33 pm
Location: Orlando, FL

Re: ADC glitchs - I2S - ESP32

Postby fly135 » Wed Aug 08, 2018 4:22 pm

The socket "connect" function call needs to be outside the loop. You can't connect every time you want to send a packet. Once you are connected then you can't call connect again until after the socket is closed. You DO NOT want to connect and close for every send.

What I do is have two loops. The outer loop connects to the server and the inner loop sends the data. When the inner loop gets an error it closes the socket, then exits the loop. The outer loop then connects to the server and enters the inner loop to send.

John A

DylanR0
Posts: 12
Joined: Mon Aug 06, 2018 2:42 pm

Re: ADC glitchs - I2S - ESP32

Postby DylanR0 » Thu Aug 09, 2018 1:55 pm

I tried your method with 2 loops, something like that :

Code: Select all

    int s;
    s=socket(AF_INET, SOCK_STREAM, 0);  
    ESP_LOGI(TAG,"ICI %d \n",s); 
    
     while(1)
    {
        connect(s, (struct sockaddr *)&tcpServerAddr, sizeof(tcpServerAddr));
        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) 
        { 
            ESP_LOGI(TAG,"laa %d \n",s);
            if (evt.type==I2S_EVENT_RX_DONE) 
            {
                ESP_LOGI(TAG,"laaa %d \n",s);
                while(connect(s, (struct sockaddr *)&tcpServerAddr, sizeof(tcpServerAddr)) != 0){
                    ESP_LOGI(TAG, "... connecté \n");
                    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);
                        close(s);
                        vTaskDelay(2000 / portTICK_PERIOD_MS);
                        break;
                    }
                    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);
                        break;
                    }
                    if(send(s , i2s_read_buff , i2s_read_len,0) < 0)
                    {
                        ESP_LOGE(TAG, "... Send failed \n");
                        close(s);
                        vTaskDelay(4000 / portTICK_PERIOD_MS);
                        break;
                    }
                    ESP_LOG_BUFFER_HEX("i2s_read_buff", i2s_read_buff, 16);
                }
            }
        }
I'm currently trying to solve why it stops here :
xQueueReceive(i2s_event_queue, &evt, portMAX_DELAY) == pdPASS

Code: Select all

I (5310) tcp_client: démarage du mode TCP
I (5320) tcp_client: ICI 54
I (5320) tcp_client: la 54

User avatar
fly135
Posts: 606
Joined: Wed Jan 03, 2018 8:33 pm
Location: Orlando, FL

Re: ADC glitchs - I2S - ESP32

Postby fly135 » Thu Aug 09, 2018 3:39 pm

Sorry to say Dylan, but you need to get some help with your coding. You clearly need some help with the basics of socket programming and maybe other things. I said... "You can't connect every time you want to send a packet", and in your code....

Code: Select all

                    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);
                        break;
                    }
                    if(send(s , i2s_read_buff , i2s_read_len,0) < 0)
                    {
                        ESP_LOGE(TAG, "... Send failed \n");
                        close(s);
                        vTaskDelay(4000 / portTICK_PERIOD_MS);
                        break;
                    }
You are calling connect before every "send". You call connect once! Then later if you get an error on send, you call close, and only then can you call connect again.

If this is a hobby project then you can just persevere and continue learning from your mistakes. But if this is a job, get some help from your programming peers to get this figured out.

In addition... In your code you are calling connect before you get an event indicating the wifi is connected.

Code: Select all

        connect(s, (struct sockaddr *)&tcpServerAddr, sizeof(tcpServerAddr));
        ESP_LOGI(TAG,"la %d \n",s);
        xEventGroupWaitBits(wifi_event_group,CONNECTED_BIT,false,true,portMAX_DELAY); 
Do you know why you are doing that? Do you understand why that is wrong? Do you understand what connect is doing? You are trying to connect to a TCP server on the network before you are on the network.

To make matters worse you are calling connect as the loop test. So you are calling connect twice for every packet send.

And the answer to your question, it's stuck on the xQueueReceive because nothing is sent to the queue? What is it you think is in that queue to get?

John A

DylanR0
Posts: 12
Joined: Mon Aug 06, 2018 2:42 pm

Re: ADC glitchs - I2S - ESP32

Postby DylanR0 » Thu Aug 09, 2018 7:22 pm

I'm an intern for 2 months and this is a project that I have to retake. But it's not my field of study... But I have no choice... I try my best to understand the code and your advice. But it's the first time that I see an ESP32, sockets and things like that. Moreover, unfortunately I don't do studies in data processing but in hardware electronic.
Nobody in the startup knows how to code like that (they want to innove and to improve their designs by sending data via wifi, it's the first time). So it's "Do it we want the result. Don't be late".

That's why it's hard for me because I have a short internship and nobody to teach me.

I did the hardware part where I'm good, but cause of software and glitchs, the project is trash for my internship supervisor.

Personally I don't want to see the project fo to trash... So I'll try to better understand your advice.
I'll continue tomorrow, but thank you for your patience and your help ! :)

User avatar
fly135
Posts: 606
Joined: Wed Jan 03, 2018 8:33 pm
Location: Orlando, FL

Re: ADC glitchs - I2S - ESP32

Postby fly135 » Thu Aug 09, 2018 7:42 pm

Then it looks like you are going to need to persevere on and try your best. I recommend watching some youtube videos. This one looks like it would be useful...

https://www.youtube.com/watch?v=LtXEMwSG5-8

In that video he calls recv once (23 min in). You would be calling send and putting your while loop around the send (and the I2S read). No queues are involved.

The socket call is like buying a phone. The connect call is like making a telephone call. The close is like hanging up. The send is like speaking a sentence to who's on the other end.

You don't call (connect) someone every time you want to say (send) something. You don't make another call (connect) until you hang up (close) the first one. And you only need to buy a phone (socket) once.

John A

DylanR0
Posts: 12
Joined: Mon Aug 06, 2018 2:42 pm

Re: ADC glitchs - I2S - ESP32

Postby DylanR0 » Fri Aug 10, 2018 9:27 am

Thank you for the video and for your explanation with the example with the phone. I think I understand better.
So I writted another code and it works more or less :

Code: Select all

int s;
    s=socket(AF_INET, SOCK_STREAM, 0);  
    ESP_LOGI(TAG,"ICI %d \n",s); 
    int connexion_status = connect(s, (struct sockaddr *)&tcpServerAddr, sizeof(tcpServerAddr));

     while(1)
    {
        
        ESP_LOGI(TAG,"la %d \n",s);
        xEventGroupWaitBits(wifi_event_group,CONNECTED_BIT,false,true,portMAX_DELAY); 
        if (connexion_status == -1) //<- One connect after event
        {
            ESP_LOGI(TAG,"Error making a connection %d \n",s);
        }
        if (xQueueReceive(i2s_event_queue, &evt, portMAX_DELAY) == pdPASS) 
        { 
            ESP_LOGI(TAG,"laa %d \n",s);
            if (evt.type==I2S_EVENT_RX_DONE) 
            {
                ESP_LOGI(TAG,"laaa %d \n",s);
                while(connect(s, (struct sockaddr *)&tcpServerAddr, sizeof(tcpServerAddr)) != 0){ //<- while no problem with the connect
                    ESP_LOGI(TAG, "... connecté \n");
                    i2s_read(I2S_NUM_0, (void*)i2s_read_buff, i2s_read_len,&bytes_read, portMAX_DELAY); //<- one read      
                    if(s < 0) 
                    {
                        ESP_LOGE(TAG, "... Impossible d'alouer un socket. errno : %d \n", errno);
                        close(s);//<- close if problem and return in the loop to connect
                        vTaskDelay(2000 / portTICK_PERIOD_MS);
                        break;
                    }
                    if(send(s , i2s_read_buff , i2s_read_len,0) < 0) /<- one send
                    {
                        ESP_LOGE(TAG, "... Send failed \n");
                        close(s); //<- close if problem and return in the loop to connect
                        vTaskDelay(4000 / portTICK_PERIOD_MS);
                        break;
                    }
                    ESP_LOG_BUFFER_HEX("i2s_read_buff", i2s_read_buff, 16);
                }
            }
        }
    }
With that I have data on the terminal like this :

Code: Select all

I (21120) i2s_read_buff: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
tcp_connect: can only connect from state CLOSED
I (21130) tcp_client: ... connecté

I (21130) i2s_read_buff: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
tcp_connect: can only connect from state CLOSED
I (21140) tcp_client: ... connecté

I (21150) i2s_read_buff: 7f 00 7f 00 7f 00 7f 00 7f 00 7f 00 7f 00 7f 00
tcp_connect: can only connect from state CLOSED
I (21160) tcp_client: ... connecté

I (21170) i2s_read_buff: 87 01 87 01 87 01 87 01 87 01 87 01 85 01 87 01
tcp_connect: can only connect from state CLOSED
I (21180) tcp_client: ... connecté

I (21180) i2s_read_buff: b0 02 b1 02 b0 02 b0 02 b1 02 b0 02 b0 02 b1 02
tcp_connect: can only connect from state CLOSED
I (21190) tcp_client: ... connecté

I (21200) i2s_read_buff: c7 03 c7 03 cc 03 c7 03 cc 03 cc 03 ca 03 cc 03
tcp_connect: can only connect from state CLOSED
I don't know why I have this sentence, should I close in the loop ?

It's ok but when I stop listening I have :

Code: Select all

I (37440) tcp_client: la 54

I (37440) tcp_client: laa 54

I (37440) tcp_client: laaa 54

I (37440) tcp_client: ... connecté

E (37440) tcp_client: ... Send failed
Meaning it's ok but when I restart the listening I always have this on the screen. I have to turn off/on the power supply to have data again.

But I'm happy that the code is working again !

Unfortunatly, always glitches..
Attachments
5KHz_1Vpp_600mVoff.JPG
Duration is ok, amplitude is ok cause of the attenuation. But if I change buff-len or buff-num it changes the duration ..
5KHz_1Vpp_600mVoff.JPG (35.59 KiB) Viewed 9212 times

User avatar
fly135
Posts: 606
Joined: Wed Jan 03, 2018 8:33 pm
Location: Orlando, FL

Re: ADC glitchs - I2S - ESP32

Postby fly135 » Fri Aug 10, 2018 3:53 pm

"I don't know why I have this sentence, should I close in the loop ?"

You are getting that message because you are trying to place a call without hanging up. You are also trying to place a call every time you want to say something to the listener. You appear to be obsessed with placing the call. It seems there is nothing I can say to keep you from calling connect over and over. Why is that?

Also NO, you do not call close in the loop. Just like you shouldn't call connect in the loop. You do not "hang up" a phone call between every word spoken. And you don't close a connection after every send, unless you are only intending to make one send.

Until you understand the concept of connecting once and sending data in a loop, you will always get the discontinuities in your data that your picture is showing.

John A

DylanR0
Posts: 12
Joined: Mon Aug 06, 2018 2:42 pm

Re: ADC glitchs - I2S - ESP32

Postby DylanR0 » Fri Aug 10, 2018 10:55 pm

Ok, I'll tell you how I think my code works.

Code: Select all

int s;
    s=socket(AF_INET, SOCK_STREAM, 0);  //<- I buy my phone only one time
    ESP_LOGI(TAG,"ICI %d \n",s); 
    int connexion_status = connect(s, (struct sockaddr *)&tcpServerAddr, sizeof(tcpServerAddr)); //<- Just assign the way to connect to a variable.

     while(1) //<- First loop, the outer loop as I think used to connect.
    {
        
        ESP_LOGI(TAG,"la %d \n",s);
        xEventGroupWaitBits(wifi_event_group,CONNECTED_BIT,false,true,portMAX_DELAY); 
        if (connexion_status == -1) //<- If the connexion isn't ok, meaning that I'm not connected to Wifi, I print an error. For me I do it only one time because after I stay in the inner loop. I call someone only if I can only one time.
        {
            ESP_LOGI(TAG,"Error making a connection %d \n",s);
        }
        if (xQueueReceive(i2s_event_queue, &evt, portMAX_DELAY) == pdPASS) //<- If there is data in the queue
        { 
            ESP_LOGI(TAG,"laa %d \n",s);
            if (evt.type==I2S_EVENT_RX_DONE) 
            {
                ESP_LOGI(TAG,"laaa %d \n",s);
                while(connect(s, (struct sockaddr *)&tcpServerAddr, sizeof(tcpServerAddr)) != 0){ //<- inner loop, while there is no problem with the connection. It's not a connection, just a verification that it works, if not, just try to reconnect in the outer loop
                    ESP_LOGI(TAG, "... connecté \n");
                    i2s_read(I2S_NUM_0, (void*)i2s_read_buff, i2s_read_len,&bytes_read, portMAX_DELAY); //<- I do only one read      
                    if(s < 0) //<- If there is a problem with the socket
                    {
                        ESP_LOGE(TAG, "... Impossible d'alouer un socket. errno : %d \n", errno);
                        close(s); //<- I close if problem and return in the loop to connect, I hang-up
                        vTaskDelay(2000 / portTICK_PERIOD_MS);
                        break; //<- return in the outer loop to connect (call) after close (hanging-up)
                    }
                    if(send(s , i2s_read_buff , i2s_read_len,0) < 0) //<- If what I send is empty, I print an error else I send. If I have nothing to say, I hang-up.
                    {
                        ESP_LOGE(TAG, "... Send failed \n");
                        close(s); //<- close if problem and return in the loop to connect, I hang-up
                        vTaskDelay(4000 / portTICK_PERIOD_MS);
                        break; //<- return in the outer loop to connect (call) after close (hanging-up)
                    }
                    ESP_LOG_BUFFER_HEX("i2s_read_buff", i2s_read_buff, 16); //show the information, what I said
                } //returning at the begininng of the inner loop, so for me I don't reconnect
            }
        }
    }

So for me I connect one time then I go in the inner loop to read and send until there is a problem. If there is a problem I close the socket then I return in the outer loop to reconnect (because I closed before).
So until there is a problem, for me I don't leave the loop.
I call one time, then until I have nothing to say I speak to the person. Then if there is a problem with the call, I hang-up then recall the person.

For me it's how I think my code works.
Instead of telling me that I always call without hang-up, can you show me where is the error ? I explained to you the code with my way of thinking, and for me I think that I hang-up before recalling. (Of course not because I have the sentence telling me that I have to close the state to connect, but I still connect because I retrieve data and can make a figure).

Do you understand my point of view from someone who discovers the ESP32 ?

Thanks a lot ! :)

User avatar
fly135
Posts: 606
Joined: Wed Jan 03, 2018 8:33 pm
Location: Orlando, FL

Re: ADC glitchs - I2S - ESP32

Postby fly135 » Fri Aug 10, 2018 11:46 pm

I made some changes to your code. One mistake in my phone analogy is that that when you hang up you also have to buy the phone again because the socket is freed.

Changes...

1) I put the whole connecting to the server in a subroutine ConnectToServer().
2) I wait for the wifi connect even before calling ConnectToServer().
3) I made a global flag that indicates connected. Not really needed (could use return value) but could be useful.
4) I removed the queue stuff for I2S because the read call is blocking.
5) If send fails, the socket is closed, connected is set false, and the inner loop is exited and you call ConnectToServer again in the outer loop.
6) I removed the test for "s" because the connect will fail long before you get to the inner loop.

Other things to consider is examining "errno" on a failed call to send. Since you didn't make this a blocking send it could fail with EAGAIN, which means simply that the socket isn't ready to accept more data. It doesn't mean that the connection is down. You can set the send to blocking with setsockopt, but that still doesn't guarantee that you won't get EAGAIN. The timeout could be too small and you could be sending data too fast. There are some ambiguities that checking errors for can handle.

Code: Select all

int s = NULL;
bool connected = false;

bool ConnectToServer()
{
 if (connected) return true;
 
  s=socket(AF_INET, SOCK_STREAM, 0);  //<- I buy my phone only one time
  ESP_LOGI(TAG,"ICI %d \n",s); 
  int connexion_status = connect(s, (struct sockaddr *)&tcpServerAddr, sizeof(tcpServerAddr)); //<- Just assign the way to connect to a variable.
  if (connexion_status == -1) //<- If the connexion isn't ok
  {
    ESP_LOGI(TAG,"Error making a connection %d \n",s);
    return false;
  }
  connected = true;
  return true;
}

.... start of function
xEventGroupWaitBits(wifi_event_group,CONNECTED_BIT,false,true,portMAX_DELAY);
while (1)
{
  ConnectToServer();
  if ( !connected) // could also check return value from ConnectToServer"
  {
    // Try again later, maybe the server isn't running right now
    vTaskDelay(2000 / portTICK_PERIOD_MS);
    // break, or not because the connected parameter in the loop variable below will handle it
    break;
  }  
  while(connected)
  {
        ESP_LOGI(TAG,"laaa %d \n",s);
        i2s_read(I2S_NUM_0, (void*)i2s_read_buff, i2s_read_len,&bytes_read, portMAX_DELAY); //<- I do only one read      
        if(send(s , i2s_read_buff , i2s_read_len,0) < 0) //<- If what I send is empty, I print an error else I send. If I have nothing to say, I hang-up.
        {
            ESP_LOGE(TAG, "... Send failed \n");
            close(s); //<- close if problem and return in the loop to connect, I hang-up
            vTaskDelay(4000 / portTICK_PERIOD_MS);
            // set the connect variable to false
            connect = false;
            break; //<- return in the outer loop to connect (call) after close (hanging-up)
        }
        ESP_LOG_BUFFER_HEX("i2s_read_buff", i2s_read_buff, 16); //show the information, what I said
   }
 }

Who is online

Users browsing this forum: Bing [Bot], bobioknight, Google [Bot] and 71 guests