Browse Source

接收蓝牙数据并分包

master
zwsd 3 years ago
parent
commit
17faa9c83d
  1. 16
      .vscode/settings.json
  2. 1069
      main/ble_spp_server_demo.c
  3. 38
      main/ble_spp_server_demo.h
  4. 79
      main/main.c
  5. 8
      sdkconfig
  6. 1463
      sdkconfig.old

16
.vscode/settings.json

@ -16,4 +16,20 @@
},
"terminal.integrated.defaultProfile.windows": "Windows PowerShell",
"files.autoGuessEncoding": true,
"files.associations": {
"string.h": "c",
"string_view": "c",
"regex": "c",
"*.tcc": "c",
"optional": "c",
"istream": "c",
"ostream": "c",
"ratio": "c",
"system_error": "c",
"array": "c",
"functional": "c",
"tuple": "c",
"type_traits": "c",
"utility": "c"
},
}

1069
main/ble_spp_server_demo.c
File diff suppressed because it is too large
View File

38
main/ble_spp_server_demo.h

@ -6,9 +6,19 @@
CONDITIONS OF ANY KIND, either express or implied.
*/
/*
1.
2.()
3.
*/
#pragma once
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
/*
* DEFINES
@ -17,7 +27,7 @@
//#define SUPPORT_HEARTBEAT
//#define SPP_DEBUG_MODE
#define spp_sprintf(s, ...) sprintf((char*)(s), ##__VA_ARGS__)
#define spp_sprintf(s, ...) sprintf((char *)(s), ##__VA_ARGS__)
#define SPP_DATA_MAX_LEN (512)
#define SPP_CMD_MAX_LEN (20)
#define SPP_STATUS_MAX_LEN (20)
@ -49,4 +59,28 @@ enum {
SPP_IDX_NB,
};
void ble_spp_server_demo_app_main(void);
typedef struct {
uint8_t *rxbuf;
size_t rxbufsize;
uint8_t *txbuf;
size_t txbufsize;
size_t rxpacketReceiveOvertime;
bool receive_data_processing_flag;
int maxClientNum;
const char *bleName;
} bleuart_t;
/*接收完整JSON*/
typedef void (*blerxcb_t)(uint8_t *rx, size_t rxsize);
void ble_spp_server_demo_app_main(bleuart_t *bleuart);
void bleuart_reg_cb(blerxcb_t cb);
void bleuart_send_packet(uint8_t *tx, size_t txsize);
/**
* @brief
*/
void bleuart_schedule();

79
main/main.c

@ -1,3 +1,5 @@
#include "driver/gpio.h"
#include "driver/timer.h"
#include "esp_log.h"
#include "esp_system.h"
#include "freertos/FreeRTOS.h"
@ -5,33 +7,76 @@
#include "freertos/task.h"
#include "nvs_flash.h"
#include "string.h"
#include "driver/gpio.h"
//
#include "ble_spp_server_demo.h"
#include "motor_drive.h"
#define ble_uart_tx_size 128
#define ble_uart_rx_size 128
#define Electric_relay1 25
#define Electric_relay2 26
static uint8_t bluetooth_tx_buffer[ble_uart_tx_size] = {0};
static uint8_t bluetooth_rx_buffer[ble_uart_rx_size] = {0};
#define GPIO_OUTPUT_PIN_SEL ((1ULL << Electric_relay1) | (1ULL << Electric_relay2))
uint8_t ble_rx_buffer_now_len = 0;
uint8_t ble_rx_buffer_off = 0;
uint32_t start_time = 0;
uint16_t modbus_uart_rx_off_before = 0;
void gpio_rgb_init() {
gpio_config_t gpio_grb_led_structer;
bleuart_t ble_uart_init_struct = {
.txbuf = bluetooth_tx_buffer,
.txbufsize = sizeof(bluetooth_tx_buffer),
.rxbuf = bluetooth_rx_buffer,
.rxbufsize = sizeof(bluetooth_rx_buffer),
.rxpacketReceiveOvertime = 200,
.receive_data_processing_flag = false,
.maxClientNum = 1,
.bleName = "yimei_ble",
};
gpio_grb_led_structer.intr_type = GPIO_INTR_DISABLE;
gpio_grb_led_structer.mode = GPIO_MODE_OUTPUT;
gpio_grb_led_structer.pin_bit_mask = GPIO_OUTPUT_PIN_SEL;
gpio_grb_led_structer.pull_down_en = 0;
gpio_grb_led_structer.pull_up_en = 0;
void blerxcb(uint8_t *rxmessage, size_t rxsize) {
if ((rxsize + ble_rx_buffer_now_len) > ble_uart_init_struct.rxbufsize) {
ESP_LOGW("MAIN", "rx buffer overstep");
return;
}
for (int i = 0; i < rxsize; i++) {
ble_uart_init_struct.rxbuf[ble_rx_buffer_now_len++] = rxmessage[i];
ble_rx_buffer_off++;
start_time = esp_log_timestamp();
}
}
gpio_config(&gpio_grb_led_structer);
void buffer_all_init() {
ble_rx_buffer_now_len = 0;
memset(ble_uart_init_struct.rxbuf, 0, ble_uart_init_struct.rxbufsize);
}
void wait_ble_uart_receive_ms(uint16_t wait_time_ms) {
start_time = esp_log_timestamp();
while ((esp_log_timestamp() - start_time) < wait_time_ms) {
}
}
void app_main(void) {
// ble_spp_server_demo_app_main();
gpio_rgb_init();
ble_spp_server_demo_app_main(&ble_uart_init_struct);
bleuart_reg_cb(blerxcb);
while (true) {
if (ble_rx_buffer_off != 0) {
modbus_uart_rx_off_before = ble_rx_buffer_off;
wait_ble_uart_receive_ms(100);
gpio_set_level(Electric_relay1,0);
gpio_set_level(Electric_relay2,0);
if (ble_rx_buffer_off == modbus_uart_rx_off_before) {
ble_uart_init_struct.receive_data_processing_flag = true;
ESP_LOGI("MAIN", "%s", ble_uart_init_struct.rxbuf);
}
return;
if (ble_uart_init_struct.receive_data_processing_flag == true) {
buffer_all_init();
ble_uart_init_struct.receive_data_processing_flag = false;
ble_rx_buffer_off = 0;
}
}
}
}

8
sdkconfig

@ -757,8 +757,8 @@ CONFIG_ESP_INT_WDT_CHECK_CPU1=y
CONFIG_ESP_TASK_WDT=y
# CONFIG_ESP_TASK_WDT_PANIC is not set
CONFIG_ESP_TASK_WDT_TIMEOUT_S=5
CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU0=y
CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU1=y
# CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU0 is not set
# CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU1 is not set
# CONFIG_ESP_PANIC_HANDLER_IRAM is not set
CONFIG_ESP_SYSTEM_CHECK_INT_LEVEL_5=y
# end of ESP System Settings
@ -1757,8 +1757,8 @@ CONFIG_INT_WDT_CHECK_CPU1=y
CONFIG_TASK_WDT=y
# CONFIG_TASK_WDT_PANIC is not set
CONFIG_TASK_WDT_TIMEOUT_S=5
CONFIG_TASK_WDT_CHECK_IDLE_TASK_CPU0=y
CONFIG_TASK_WDT_CHECK_IDLE_TASK_CPU1=y
# CONFIG_TASK_WDT_CHECK_IDLE_TASK_CPU0 is not set
# CONFIG_TASK_WDT_CHECK_IDLE_TASK_CPU1 is not set
CONFIG_TIMER_TASK_STACK_SIZE=3584
CONFIG_SW_COEXIST_ENABLE=y
# CONFIG_ESP32_ENABLE_COREDUMP_TO_FLASH is not set

1463
sdkconfig.old
File diff suppressed because it is too large
View File

Loading…
Cancel
Save