ESP32 a too long time of inicialization
Posted: Tue Jul 31, 2018 5:41 pm
Dear users,
I would like to ask for a advice in case of error, which appeared and which I can't solve.
Some important information:
-I work with ESP32 from Sparkfun,
-I create an app for PC which communicates with ESP module in wire way(UART) and wireless(UDP to send data to PC and TCP to send commands to ESP)
-I use ECLIPSE to write ESP software in C and QtCreator to write PC app in C++
-I work on Windows 10
I will write about process and I will show you a problem:
1. I open PC acc
2. I connect ESP
3. I click "connect" in PC app and there is my ERROR - data doesn't come REASON: After opening app TCP server is created, after connecting ESP TCP client is created, if I connect ESP and click "connect with WIFI" then ESP doesn't keep up to create a client.
I conclude that ESP's inicialization process lasts too long in comparation with PC app. Additional problem is that closing USB port make ESP reset: I click "connect with UART", then "disconnect", click "connect with WIFI" and the same problem: because of ESP resetting, ESP doen't create a client on time. I don't check if client has been created in my PC application, simply the app work like connection would exist and then i click "disconnect" and to this time, ESP made a server so data starts comming.
What actually happens, when i click connect button? There is send a command #5 g* with UART or TCP depending of connection type, on this command ESP changes a state to a opposite one sending=!sending. Its simple idea.
Could someone help me and tell how to make a long time of inicialization working with app? Or how to decrease this time.
I enclose demostrating video and important fragments of code.
Demonstation video
Github- full code
Inicialization in main.c
Function wifi_init_sta() in udp.c
Function udp_conn in main.c
Function uart_communication in main.c
If someone would like to help please look at Github- full code to see definitions of remaining functions:
spi_init() in spi.c
initAG() in mki159v1.c
initMagnet() in mki159v1.c
setPinLevel(1,1) in adc.c
initialize_adcs() in adc,c
initializeTIMER() in timer.c
Greetings!
I would like to ask for a advice in case of error, which appeared and which I can't solve.
Some important information:
-I work with ESP32 from Sparkfun,
-I create an app for PC which communicates with ESP module in wire way(UART) and wireless(UDP to send data to PC and TCP to send commands to ESP)
-I use ECLIPSE to write ESP software in C and QtCreator to write PC app in C++
-I work on Windows 10
I will write about process and I will show you a problem:
1. I open PC acc
2. I connect ESP
3. I click "connect" in PC app and there is my ERROR - data doesn't come REASON: After opening app TCP server is created, after connecting ESP TCP client is created, if I connect ESP and click "connect with WIFI" then ESP doesn't keep up to create a client.
I conclude that ESP's inicialization process lasts too long in comparation with PC app. Additional problem is that closing USB port make ESP reset: I click "connect with UART", then "disconnect", click "connect with WIFI" and the same problem: because of ESP resetting, ESP doen't create a client on time. I don't check if client has been created in my PC application, simply the app work like connection would exist and then i click "disconnect" and to this time, ESP made a server so data starts comming.
What actually happens, when i click connect button? There is send a command #5 g* with UART or TCP depending of connection type, on this command ESP changes a state to a opposite one sending=!sending. Its simple idea.
Could someone help me and tell how to make a long time of inicialization working with app? Or how to decrease this time.
I enclose demostrating video and important fragments of code.
Demonstation video
Github- full code
Inicialization in main.c
Code: Select all
void app_main()
{
wifi_init_sta();
xTaskCreate(&udp_conn, "udp_conn", 2048, NULL, 10, NULL);
xTaskCreate(&uart_communication, "uart_communication", 4096, NULL, 5, NULL);
//...
}
Function wifi_init_sta() in udp.c
Code: Select all
void wifi_init_sta()
{
udp_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 = {
.sta = {
.ssid = EXAMPLE_DEFAULT_SSID,
.password = EXAMPLE_DEFAULT_PWD
},
};
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
ESP_ERROR_CHECK(esp_wifi_start() );
ESP_LOGI(TAG, "wifi_init_sta finished.");
ESP_LOGI(TAG, "connect to ap SSID:%s password:%s \n",
EXAMPLE_DEFAULT_SSID,EXAMPLE_DEFAULT_PWD);
}
Code: Select all
static void udp_conn(void *pvParameters)
{
ESP_LOGI(TAG, "udp_conn start.");
xEventGroupWaitBits(udp_event_group, WIFI_CONNECTED_BIT,false, true, portMAX_DELAY);
/*create udp socket*/
int socket_ret;
socket_ret = create_udp_client();
if(socket_ret == ESP_FAIL) {
ESP_LOGI(TAG, "create udp socket error,stop.");
vTaskDelete(NULL);
}
ESP_LOGI(TAG, "create tcp client start.");
create_tcp_client();
/*initialize adc and timer after connecting*/
if(connection_status<2)
{
connection_status++;
if(connection_status==2)
{
spi_init();
initAG();
initMagnet();
setPinLevel(1,1);
initialize_adcs();
initializeTIMER();
}
}
/*create a task to tx/rx data*/
TaskHandle_t tx_rx_task;
xTaskCreate(&sendUDP_recvTCP_data, "send_recv_data", 4096, NULL, 4, &tx_rx_task);
/*waiting udp connected success*/
xEventGroupWaitBits(udp_event_group, UDP_CONNCETED_SUCCESS,false, true, portMAX_DELAY);
int bps;
while (1) {
total_data = 0;
//vTaskDelay(3000 / portTICK_RATE_MS);//every 3s
bps = total_data;
if (total_data <= 0) {
int err_ret = check_connected_socket();
if (err_ret == -1) { //-1 reason: low level netif error
ESP_LOGW(TAG, "udp send & recv stop.\n");
break;
}
}
#if EXAMPLE_ESP_UDP_PERF_TX
ESP_LOGI(TAG, "udp send %d byte per sec! total pack: %d \n", bps, success_pack);
#else
ESP_LOGI(TAG, "udp recv %d byte per sec! total pack: %d \n", bps, success_pack);
#endif /*EXAMPLE_ESP_UDP_PERF_TX*/
}
close_socket();
vTaskDelete(tx_rx_task);
vTaskDelete(NULL);
}
Code: Select all
static void uart_communication()
{
uart_num = UART_NUM_0;
uart_config_t uart_config = {
.baud_rate = 115200,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
.rx_flow_ctrl_thresh = 122,
};
uart_param_config(uart_num, &uart_config);
uart_set_pin(uart_num, ECHO_TEST_TXD, ECHO_TEST_RXD, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
uart_driver_install(uart_num, BUF_SIZE * 2, 0, 0, NULL, 0);
if(connection_status<2)
{
connection_status++;
if(connection_status==2)
{
spi_init();
initAG();
initMagnet();
setPinLevel(1,1);
initialize_adcs();
initializeTIMER();
}
}
uint8_t* data = (uint8_t*) malloc(BUF_SIZE); // uint8_t address of first element in data table
while(1)
{
int len = uart_read_bytes(uart_num, data, BUF_SIZE, 20 / portTICK_RATE_MS);//lenght of incoming data
if(len>0)
{
char cmd; // command to execute, f means change frequency of blinking
//char test_msg[100]; //char table used to test, contains a text from sprintf below function
int size;
connection_mode=0;
ESP_LOGI("UART", "received");
for(int i=0;i<len;i++)
{
if((char)data[i]=='#')
{
sscanf((char *)&data[i],"#%d %c",&size,&cmd);
if((char)data[i+size-1]== '*')
{
switch(cmd)
{
case 'f':
{
sscanf((char *)&data[i],"#%*d %*c %d*",&TIMER_INTERVAL1_SEC);
//sprintf(test_msg,"%d - %c - %d\n",size,cmd,TIMER_INTERVAL1_SEC); can't use data table( data is char table), because then we overwrite information in this table
refresh_timer(TIMER_GROUP_0,TIMER_1); //if frequency(TIMER_INTERVAL1_SEC) has changed, restart timer
break;
}
case 'g':
{
onoffSending();
ESP_LOGI("UART", "SENDING STATUS: %d \n", sending_data);
//while(adc_tail!=adc_head)
//{
//int frameLength=0;
//char frameData[20];
//char frame[30];
//sprintf(frameData," G %d %d*",adc1data[adc_tail],adc2data[adc_tail]);
//double frameDataLength=0;
//frameDataLength=strlen(frameData)+1;
//double LogOfFrameDataLength= log((double)frameDataLength+1.0)/log(10.0); // it equals log of base of 10th of frameDataLength
//int frameLengthNumberOfDigits = 0; //frame: #frameLengthNumberOfDigits G %d %d*
//frameLengthNumberOfDigits = floor(LogOfFrameDataLength) + 1.0;
//frameLength = frameDataLength + frameLengthNumberOfDigits;
//sprintf(frame,"#%d G %d %d*",frameLength,adc1data[adc_tail],adc2data[adc_tail]);
//uart_write_bytes(uart_num, frame, strlen(frame));
//adc_tail++;
//adc_tail%=ADC_BUFFER_SIZE;
//onoffSending();
break;
}
case 's':
{
int pin1level=0;
int pin2level=0;
sscanf((char *)&data[i],"#%*d %*c %d %d*",&pin1level,&pin2level);
setPinLevel(pin1level,pin2level);
break;
}
case 'w':
{
//sscanf((char *)&data[i], "#%*d %*c %s %s %s %SCNd16*", SSID, wifipassword, addressIP, &udpport);
//reconnect ....
break;
}
}
i=i+size-1;
}
}
//uart_write_bytes(uart_num, (const char*) test_msg, strlen((char*)test_msg));//sending test_msg to display by UART
}
}
//if(len==0)
//{
// connection_mode=1;
//}
}
}
spi_init() in spi.c
initAG() in mki159v1.c
initMagnet() in mki159v1.c
setPinLevel(1,1) in adc.c
initialize_adcs() in adc,c
initializeTIMER() in timer.c
Greetings!