Thanks for it. I went ahead with creating a task and handleing things there it made things easy for me and acts on flags and Data I set in Callbacks. Yes I think giving enough stack size for begining is fine but I will have to figure out how to trim the stack later to because I think I would be using another task for MQTT client(may be not I would see about that later).
Anyways thanks for that awesome advice.
here is how I managed it... (just in Case some one is searching for it.)
My device advertises for with different names for different things like to log that its working it sets alive Adv packet, then ESP connects sets time on it(RTC I am doing with Ticker on NRF51 so Just to make sure it does not drift away), adv for data is for transfering data , Range is for testing Range(ya I have to figure out API for getting NRF51 RSSI value in ESP32 After connection, does anyone know?) this sends back RSSI value ESP32 sees of NRF51 to NRF51 and it blinks LEDS based on LEVEL of signal like if only -90dB then blink only one LED if -10dB then Blink all 5 LEDS. I hope this helps to someone.
Code: Select all
void bytelens_task(void *params)
{
ESP_LOGI(GATTC_TAG,"TASK Running.");
char cmd[20];
while(1)
{
switch(dev.connState){
case forALIVE:
ESP_LOGI(GATTC_TAG,"For Alive..");
if(dev.Connected && dev.notify && !dev.timer_set)
{
ESP_LOGI(GATTC_TAG,"Setting Time.");
char *time="1234567890";
cmd[0]='T';
memcpy(&cmd[1],time,11);
dev.timer_set=true;
esp_ble_gattc_write_char(dev.gattc_if,
dev.conn_id,
&my_service_id,
&Tx_char_ID, //esp_gatt_id_t *char_id, //get_char->char_id
12, //uint16_t value_len,
(uint8_t *)&cmd, //uint8_t *value,
ESP_GATT_WRITE_TYPE_NO_RSP, //esp_gatt_write_type_t write_type,
ESP_GATT_AUTH_REQ_NONE); //esp_gatt_auth_req_t auth_req)
}
else{
if(!dev.processing)
esp_ble_gattc_close(dev.gattc_if,dev.conn_id);//close connection on setting
}//for Alive
break;
case forREG:
ESP_LOGI(GATTC_TAG,"For REG..");
if(dev.Connected && dev.notify && !dev.timer_set)
{
ESP_LOGI(GATTC_TAG,"Setting Time.");
char *time="1234567890";
cmd[0]='T';
memcpy(&cmd[1],time,11);
dev.timer_set=true;
esp_ble_gattc_write_char(dev.gattc_if,
dev.conn_id,
&Bytelens_service_id,
&Tx_char_ID,//esp_gatt_id_t *char_id, //get_char->char_id
12,//uint16_t value_len,
(uint8_t *)&cmd,//uint8_t *value,
ESP_GATT_WRITE_TYPE_NO_RSP,//esp_gatt_write_type_t write_type,
ESP_GATT_AUTH_REQ_NONE);//esp_gatt_auth_req_t auth_req)
}
else{
if(!dev.processing) //causes trouble when ESP is searching for chars and this closes connection so this special flag to make sure it does not close connection while processing.
esp_ble_gattc_close(dev.gattc_if,dev.conn_id); //close connection on setting
}//for Alive
break;
case forDATA:
ESP_LOGI(GATTC_TAG,"For DATA..");
while(!dev.finish) //finised with Data? also have to add connection flag it ma get trapped if device is lost.
{
ESP_LOGI(GATTC_TAG,"Data Coming..");
vTaskDelay(500/portTICK_RATE_MS);
}
if(!dev.processing)
esp_ble_gattc_close(dev.gattc_if,dev.conn_id);//close connection on setting
break;
case forRANGE:
ESP_LOGI(GATTC_TAG,"For RANGE..");
break;
case NONE:
// ESP_LOGI(GATTC_TAG,"for NONE.. should not be the Case you should Get.");
if(!dev.processing)
esp_ble_gattc_close(dev.gattc_if,dev.conn_id);//close connection on setting
break;
default:
ESP_LOGI(GATTC_TAG,"default..(1)");
break;
}//switch
if( !dev.Connected && !dev.scanning ) //not connected and not scanning then start Scanning.
{
dev.scanning=true;
esp_ble_gap_start_scanning(Scanning_duration);
}
vTaskDelay(200 / portTICK_RATE_MS);
}//end while
Thanks.