ESP32 BLE configuration from app to esp32

sanjai s
Posts: 3
Joined: Fri Feb 14, 2025 6:37 am

ESP32 BLE configuration from app to esp32

Postby sanjai s » Thu Feb 27, 2025 5:07 am

I'm working on a project of connecting a phone to esp32 through a custom built app to send Wi-Fi credentials, and by using the credentials, I make it to connect with Wi-Fi. Well, I had to store in EEPROM, but I had to solve the receiving process first. I have created a program regarding this, but I'm facing a problem there. The device is discoverable but it is not connecting to the phone. I shall attach the program to debug and find the problem, because right now I'm learning about ESP32 and its functionalities, therefore its somewhat blurry to the problems I'm facing. I request the people to help me in this situation, in order to move forward for other tasks.

CODE:

Code: Select all

#include <stdio.h>
#include <string.h>
#include "esp_mac.h"
#include "esp_bt.h"
#include "esp_bt_main.h"
#include "esp_gap_ble_api.h"
#include "esp_gatts_api.h"
#include "esp_err.h"
#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_netif.h"
#include "nvs_flash.h"
#include "nvs.h"

uint8_t SERVICE_UUID[16] = {0x4F, 0xAF, 0xC2, 0x01, 0x1F, 0xB5, 0x45, 0x9E, 0x8F, 0xCC, 0xC5, 0xC9, 0xC3, 0x31, 0x91, 0x4B};
uint8_t CHARACTERISTIC_UUID[16] = {0xBE, 0xB5, 0x48, 0x3E, 0x36, 0xE1, 0x46, 0x88, 0xB7, 0xF5, 0xEA, 0x07, 0x36, 0x1B, 0x26, 0xA8};

uint16_t srv_hndl;
uint16_t chr_hndl;

esp_bt_controller_config_t bt_config = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
esp_ble_adv_data_t data =
{
    .set_scan_rsp = false,
    .include_name = true,
    .include_txpower = true,
    .service_uuid_len = sizeof(SERVICE_UUID),
    .p_service_uuid = SERVICE_UUID,
    .flag = ESP_BLE_ADV_FLAG_GEN_DISC | ESP_BLE_ADV_FLAG_BREDR_NOT_SPT
};

esp_ble_adv_params_t specs =
{
    .adv_int_min = 0x100,
    .adv_int_max = 0x200,
    .adv_type = ADV_TYPE_IND,
    .own_addr_type = BLE_ADDR_TYPE_PUBLIC,
    .channel_map = ADV_CHNL_ALL,
    .adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY
};

static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
{
    switch(event)
    {
        case ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT:
        {
            printf("Advertising data set, getting advertised\n");
            esp_ble_gap_start_advertising(&specs);
            break;
        }

        case ESP_GAP_BLE_ADV_START_COMPLETE_EVT:
        {
            printf("Advertising started\n");
            break;
        }

        case ESP_GAP_BLE_ADV_STOP_COMPLETE_EVT:
        {
            printf("Advertising stopped\n");
            break;
        }

        default:
        {
            printf("Miscelleneous GAP events happening\nEvent: %d\n", event);
            break;
        }
    }
}

static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param) 
{
    switch(event)
    {
        case ESP_GATTS_REG_EVT:
        {
            printf("GATT server initialized\n");
            esp_bt_uuid_t srv_uuid;
            srv_uuid.len = ESP_UUID_LEN_128;
            memcpy(srv_uuid.uuid.uuid128, SERVICE_UUID, srv_uuid.len);
            esp_gatt_srvc_id_t srv_id = 
            {
                .id = 
                {
                    .uuid = srv_uuid,
                    .inst_id = 0
                },
                .is_primary = true
            };
            gatts_if = param -> reg.app_id;
            printf("GATT interface registered: %d\n", gatts_if);

            esp_ble_gatts_create_service(gatts_if, &srv_id, 20);
            break;
        }

        case ESP_GATTS_CREATE_EVT:
        {
            printf("Service created with handle\n");

            srv_hndl = param -> create.service_handle;
            esp_ble_gatts_start_service(srv_hndl);

            esp_bt_uuid_t chr_uuid;
            chr_uuid.len = ESP_UUID_LEN_128;
            memcpy(chr_uuid.uuid.uuid128, CHARACTERISTIC_UUID, chr_uuid.len);

            esp_ble_gatts_add_char(srv_hndl, &chr_uuid, ESP_GATT_PERM_WRITE, ESP_GATT_CHAR_PROP_BIT_WRITE, NULL, NULL);
            break;    
        }

        case ESP_GATTS_START_EVT:
        {
            printf("GATT server started\n");
            break;
        }

        case ESP_GATTS_ADD_CHAR_EVT:
        {
            printf("Characteristics added to handle\n");
            chr_hndl = param -> add_char.attr_handle;
            break;
        }

        case ESP_GATTS_CONNECT_EVT:
        {
            printf("Device connected\n");
            break;
        }

        case ESP_GATTS_DISCONNECT_EVT:
        {
            printf("Device disconnected\n");
            break;
        }

        case ESP_GATTS_WRITE_EVT:
        {
            printf("Write request received\n");
            if(param -> write.handle == chr_hndl)
            {
                char wifi_cred[100] = {0};
                memcpy(wifi_cred, param -> write.value, param -> write.len);
                char *ssid = strtok(wifi_cred,",");
                char *password = strtok(NULL,",");
                printf("ssid: %s\npassword: %s\n",ssid,password);
            }
            else
            {
                printf("Error in parsing data\n");
            }
            break;
        }

        default:
        {
            printf("Miscelleneous GATT events happening\nEvent: %d\n", event);
            break;
        }
    }
}

const char *get_device_name() 
{
    static char name[18];
    uint8_t mac[6];

    esp_read_mac(mac, ESP_MAC_BT);
    sprintf(name, "WALTR-%02x%02x%02x", mac[3], mac[4], mac[5]);
    return name;
}

void flash_config()
{
    nvs_flash_erase();
    nvs_flash_init();
}

void bluetooth_config()
{
    esp_bt_controller_init(&bt_config);
    esp_bt_controller_enable(ESP_BT_MODE_BLE);

    esp_bluedroid_init();
    esp_bluedroid_enable();

    esp_ble_gap_set_device_name(get_device_name());
}

void callback_config()
{
    esp_ble_gatts_register_callback(gatts_event_handler);
    esp_ble_gap_register_callback(gap_event_handler);

    esp_ble_gatts_app_register(0);
    esp_ble_gap_config_adv_data(&data);
}

void app_main(void)
{
    flash_config();
    bluetooth_config();
    callback_config();

    while(1)
    {
        vTaskDelay(pdMS_TO_TICKS(1000));
    }
}

Who is online

Users browsing this forum: Bing [Bot] and 84 guests