ESP32 蓝牙。重定向串行输出
ESP32 Bluetooth. Redirecting serial output
我有一个 ESP32 开发板,我在上面连接了一个 MMA8451 加速度计,它通过串口输出数据。有没有一种简单的方法可以将数据输出到 android phone 上的蓝牙串行接收器?在部分代码中,我使用了 Serial.print 语句。是否有等效的蓝牙?
// Read the 'raw' data in 14-bit counts
mma.read();
Serial.print("X:\t"); Serial.print(mma.x);
Serial.print("\tY:\t"); Serial.print(mma.y);
Serial.print("\tZ:\t"); Serial.print(mma.z);
Serial.println();
我很快就找到了这个 googling. Or if you want to use the ESP32-idf。
在我看来,有两种可能性。你可以使用 Arduino BluetoothSerial library or the ESP-IDF SPP GATT CLIENT demo
Arduino 解决方案类似于 this:
#include "BluetoothSerial.h"
BluetoothSerial SerialBT;
void setup() {
SerialBT.begin("ESP32");
}
void loop() {
SerialBT.println("Hello World");
delay(1000);
}
ESP-IDF 版本是 bit more complex,但这是使用蓝牙发送周期性心跳的代码 api:
#ifdef SUPPORT_HEARTBEAT
void spp_heart_beat_task(void * arg)
{
uint16_t cmd_id;
for(;;) {
vTaskDelay(50 / portTICK_PERIOD_MS);
if(xQueueReceive(cmd_heartbeat_queue, &cmd_id, portMAX_DELAY)) {
while(1){
if((is_connect == true) && (db != NULL) && ((db+SPP_IDX_SPP_HEARTBEAT_VAL)->properties & (ESP_GATT_CHAR_PROP_BIT_WRITE_NR | ESP_GATT_CHAR_PROP_BIT_WRITE))){
esp_ble_gattc_write_char( spp_gattc_if,
spp_conn_id,
(db+SPP_IDX_SPP_HEARTBEAT_VAL)->attribute_handle,
sizeof(heartbeat_s),
(uint8_t *)heartbeat_s,
ESP_GATT_WRITE_TYPE_RSP,
ESP_GATT_AUTH_REQ_NONE);
vTaskDelay(5000 / portTICK_PERIOD_MS);
}else{
ESP_LOGI(GATTC_TAG,"disconnect\n");
break;
}
}
}
}
}
#endif
我有一个 ESP32 开发板,我在上面连接了一个 MMA8451 加速度计,它通过串口输出数据。有没有一种简单的方法可以将数据输出到 android phone 上的蓝牙串行接收器?在部分代码中,我使用了 Serial.print 语句。是否有等效的蓝牙?
// Read the 'raw' data in 14-bit counts
mma.read();
Serial.print("X:\t"); Serial.print(mma.x);
Serial.print("\tY:\t"); Serial.print(mma.y);
Serial.print("\tZ:\t"); Serial.print(mma.z);
Serial.println();
我很快就找到了这个 googling. Or if you want to use the ESP32-idf。
在我看来,有两种可能性。你可以使用 Arduino BluetoothSerial library or the ESP-IDF SPP GATT CLIENT demo
Arduino 解决方案类似于 this:
#include "BluetoothSerial.h"
BluetoothSerial SerialBT;
void setup() {
SerialBT.begin("ESP32");
}
void loop() {
SerialBT.println("Hello World");
delay(1000);
}
ESP-IDF 版本是 bit more complex,但这是使用蓝牙发送周期性心跳的代码 api:
#ifdef SUPPORT_HEARTBEAT
void spp_heart_beat_task(void * arg)
{
uint16_t cmd_id;
for(;;) {
vTaskDelay(50 / portTICK_PERIOD_MS);
if(xQueueReceive(cmd_heartbeat_queue, &cmd_id, portMAX_DELAY)) {
while(1){
if((is_connect == true) && (db != NULL) && ((db+SPP_IDX_SPP_HEARTBEAT_VAL)->properties & (ESP_GATT_CHAR_PROP_BIT_WRITE_NR | ESP_GATT_CHAR_PROP_BIT_WRITE))){
esp_ble_gattc_write_char( spp_gattc_if,
spp_conn_id,
(db+SPP_IDX_SPP_HEARTBEAT_VAL)->attribute_handle,
sizeof(heartbeat_s),
(uint8_t *)heartbeat_s,
ESP_GATT_WRITE_TYPE_RSP,
ESP_GATT_AUTH_REQ_NONE);
vTaskDelay(5000 / portTICK_PERIOD_MS);
}else{
ESP_LOGI(GATTC_TAG,"disconnect\n");
break;
}
}
}
}
}
#endif