Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include "freertos/FreeRTOS.h"
- #include "freertos/task.h"
- #include "driver/uart.h"
- #include "driver/gpio.h"
- #include "nvs_flash.h"
- #include "esp_log.h"
- #include "esp_bt.h"
- #include "esp_bt_defs.h"
- #include "esp_bt_main.h"
- #include "esp_gap_bt_api.h"
- #include "esp_bt_device.h"
- #include "esp_spp_api.h"
- #define ECHO_TEST_TXD (GPIO_NUM_4)
- #define ECHO_TEST_RXD (GPIO_NUM_5)
- #define ECHO_TEST_RTS (UART_PIN_NO_CHANGE)
- #define ECHO_TEST_CTS (UART_PIN_NO_CHANGE)
- #define BUF_SIZE (1024)
- #define JOB_TAG "MAIN"
- typedef struct t_my_data
- {
- esp_spp_cb_param_t spp_cb_param;
- bool spp_conn;
- } t_my_data;
- t_my_data gs_my_data;
- static void esp_bt_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param)
- {
- ESP_LOGI(JOB_TAG, "esp_bt_gap_cb - event: %d", event);
- switch (event)
- {
- case ESP_BT_GAP_AUTH_CMPL_EVT:
- if (param->auth_cmpl.stat == ESP_BT_STATUS_SUCCESS)
- {
- ESP_LOGI(JOB_TAG, "authentication success: %s", param->auth_cmpl.device_name);
- }
- else
- {
- ESP_LOGE(JOB_TAG, "authentication failed, status:%d", param->auth_cmpl.stat);
- }
- break;
- case ESP_BT_GAP_PIN_REQ_EVT:
- ESP_LOGI(JOB_TAG, "ESP_BT_GAP_PIN_REQ_EVT");
- break;
- case ESP_BT_GAP_CFM_REQ_EVT:
- ESP_LOGI(JOB_TAG, "ESP_BT_GAP_CFM_REQ_EVT Compare the numeric value: %d", param->cfm_req.num_val);
- esp_bt_gap_ssp_confirm_reply(param->cfm_req.bda, true);
- break;
- case ESP_BT_GAP_KEY_NOTIF_EVT:
- ESP_LOGI(JOB_TAG, "ESP_BT_GAP_KEY_NOTIF_EVT passkey:%d", param->key_notif.passkey);
- break;
- case ESP_BT_GAP_KEY_REQ_EVT:
- ESP_LOGI(JOB_TAG, "ESP_BT_GAP_KEY_REQ_EVT Please enter passkey!");
- break;
- default:
- break;
- }
- }
- static void esp_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
- {
- ESP_LOGI(JOB_TAG, "esp_spp_cb - event:%d", event);
- switch (event)
- {
- case ESP_SPP_INIT_EVT:
- esp_bt_dev_set_device_name("Point");
- esp_bt_gap_set_scan_mode(ESP_BT_CONNECTABLE, ESP_BT_GENERAL_DISCOVERABLE);
- esp_spp_start_srv(ESP_SPP_SEC_AUTHENTICATE, ESP_SPP_ROLE_SLAVE, 0, "SPP server");
- break;
- case ESP_SPP_OPEN_EVT:
- break;
- case ESP_SPP_CLOSE_EVT:
- gs_my_data.spp_conn = false;
- break;
- case ESP_SPP_START_EVT:
- break;
- case ESP_SPP_CL_INIT_EVT:
- break;
- case ESP_SPP_DATA_IND_EVT:
- break;
- case ESP_SPP_CONG_EVT:
- break;
- case ESP_SPP_WRITE_EVT:
- break;
- case ESP_SPP_SRV_OPEN_EVT:
- ESP_LOGI(JOB_TAG, "ESP_SPP_SRV_OPEN_EVT");
- gs_my_data.spp_conn = true;
- gs_my_data.spp_cb_param = *param;
- break;
- default:
- break;
- }
- }
- void app_main(void)
- {
- esp_err_t ret = nvs_flash_init();
- if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
- {
- ESP_ERROR_CHECK(nvs_flash_erase());
- ret = nvs_flash_init();
- }
- ESP_ERROR_CHECK(ret);
- ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_BLE));
- 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,
- .source_clk = UART_SCLK_APB,
- };
- uart_driver_install(UART_NUM_0, BUF_SIZE * 2, 0, 0, NULL, 0);
- uart_param_config(UART_NUM_0, &uart_config);
- uart_set_pin(UART_NUM_0, GPIO_NUM_1, GPIO_NUM_3, ECHO_TEST_RTS, ECHO_TEST_CTS);
- // Configure a temporary buffer for the incoming data
- uint8_t *data = (uint8_t *)malloc(BUF_SIZE);
- esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
- if ((ret = esp_bt_controller_init(&bt_cfg)) != ESP_OK)
- {
- ESP_LOGE(JOB_TAG, "%s initialize controller failed: %s\n", __func__, esp_err_to_name(ret));
- return;
- }
- if ((ret = esp_bt_controller_enable(ESP_BT_MODE_CLASSIC_BT)) != ESP_OK)
- {
- ESP_LOGE(JOB_TAG, "%s enable controller failed: %s\n", __func__, esp_err_to_name(ret));
- return;
- }
- if ((ret = esp_bluedroid_init()) != ESP_OK)
- {
- ESP_LOGE(JOB_TAG, "%s initialize bluedroid failed: %s\n", __func__, esp_err_to_name(ret));
- return;
- }
- if ((ret = esp_bluedroid_enable()) != ESP_OK)
- {
- ESP_LOGE(JOB_TAG, "%s enable bluedroid failed: %s\n", __func__, esp_err_to_name(ret));
- return;
- }
- if ((ret = esp_bt_gap_register_callback(esp_bt_gap_cb)) != ESP_OK)
- {
- ESP_LOGE(JOB_TAG, "%s gap register failed: %s\n", __func__, esp_err_to_name(ret));
- return;
- }
- if ((ret = esp_spp_register_callback(esp_spp_cb)) != ESP_OK)
- {
- ESP_LOGE(JOB_TAG, "%s spp register failed: %s\n", __func__, esp_err_to_name(ret));
- return;
- }
- if ((ret = esp_spp_init(ESP_SPP_MODE_CB)) != ESP_OK)
- {
- ESP_LOGE(JOB_TAG, "%s spp init failed: %s\n", __func__, esp_err_to_name(ret));
- return;
- }
- /* Set default parameters for Secure Simple Pairing */
- esp_bt_sp_param_t param_type = ESP_BT_SP_IOCAP_MODE;
- esp_bt_io_cap_t iocap = ESP_BT_IO_CAP_IO;
- esp_bt_gap_set_security_param(param_type, &iocap, sizeof(uint8_t));
- /*
- * Set default parameters for Legacy Pairing
- * Use variable pin, input pin code when pairing
- */
- esp_bt_pin_type_t pin_type = ESP_BT_PIN_TYPE_VARIABLE;
- esp_bt_pin_code_t pin_code;
- esp_bt_gap_set_pin(pin_type, 0, pin_code);
- while (1)
- {
- int len = uart_read_bytes(UART_NUM_0, data, BUF_SIZE, 20 / portTICK_RATE_MS);
- if (len > 0 && gs_my_data.spp_conn)
- {
- ESP_LOGI(JOB_TAG, "UART data recieved:%s LENGTH:%d", (const char *)data, len);
- esp_spp_write(gs_my_data.spp_cb_param.write.handle, len, data);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement