the following code is what I used for send out data which captured from ADC via BT SPP. what I met problem is the data no longer send out after two times connect / disconnect from android BT serial terminal app. after put some debug code in loop(), I found after two times connect / disconnect, the data did'nt send out.
where is my missing point? if this a potential bug for real application or product prototyping?
// the loop function runs over and over again forever
void loop() {
int dist_val = 0;
unsigned long start_time = 0;
unsigned long stop_time = 0;
int diff_time = 0;
Dear Sir:
the following code is what I used for send out data which captured from ADC via BT SPP. what I met problem is the data no longer send out after two times connect / disconnect from android BT serial terminal app. after put some debug code in loop(), I found after two times connect / disconnect, the data did'nt send out. where is my missing point? if this a potential bug for real application or product prototyping?
Thanks in advance.
=============================================================
include "BluetoothSerial.h"
include "esp_bt_main.h"
include "esp_bt_device.h"
if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
error Bluetooth is not enabled! Please run
make menuconfig
to and enable itendif
define LED_BUILTIN 22
define DIST_SENSOR_PIN 33
define uS_TO_S_FACTOR 1000000
define DELAY_MS 10
typedef enum { BT_UNKOWN = -1, BT_DISCONNTECT = 0, BT_CONNECTED = 1, };
char sensor_name[16] = {0}; int bt_event_type = BT_UNKOWN;
BluetoothSerial SerialBT;
void callback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) { if (event == ESP_SPP_SRV_OPEN_EVT) { Serial.println("Client Connected"); bt_event_type = BT_CONNECTED; }
if (event == ESP_SPP_CLOSE_EVT ) { Serial.println("Client disconnected"); bt_event_type = BT_DISCONNTECT; } }
bool initBluetooth() { if (!btStart()) { Serial.println("Failed to initialize controller"); return false; }
if (esp_bluedroid_init() != ESP_OK) { Serial.println("Failed to initialize bluedroid"); return false; }
if (esp_bluedroid_enable() != ESP_OK) { Serial.println("Failed to enable bluedroid"); return false; }
return true; }
void printDeviceAddress() { const uint8_t* point = esp_bt_dev_get_address();
memset(sensor_name, 0x00, 16); sprintf(sensorname, "DIST%02X%02X", point[4], point[5]);
for (int i = 0; i < 6; i++) { char str[3]; sprintf(str, "%02X", (int)point[i]); Serial.print(str); if (i < 5) { Serial.print(":"); } } Serial.println(""); Serial.println(""); }
// the setup function runs once when you press reset or power the board void setup() { pinMode(LED_BUILTIN, OUTPUT); pinMode(DIST_SENSOR_PIN, INPUT);
Serial.begin(115200);
delay(100); Serial.println("");
analogSetCycles(8); analogSetSamples(2); adcStart(DIST_SENSOR_PIN); digitalWrite(LED_BUILTIN, LOW);
Serial.println("Distance Sensor V2.0"); Serial.println("");
initBluetooth(); printDeviceAddress();
SerialBT.begin(sensor_name); SerialBT.register_callback(callback); Serial.println(sensor_name); bt_event_type = BT_DISCONNTECT; }
// the loop function runs over and over again forever void loop() { int dist_val = 0; unsigned long start_time = 0; unsigned long stop_time = 0; int diff_time = 0;
//start_time = millis(); digitalWrite(LED_BUILTIN, HIGH);
dist_val = analogRead(DIST_SENSOR_PIN);
diff_time = SerialBT.available();
if (bt_event_type == BT_CONNECTED) { SerialBT.println(String(dist_val)); //} else if(bt_event_type == BT_DISCONNTECT) { // Serial.println(String(dist_val)); //} else { // Serial.println("no data"); }
Serial.println(String(dist_val) + " " + String(diff_time));
//stop_time = millis(); //diff_time = DELAY_MS - (stop_time - start_time); //if (diff_time > 0) delay(diff_time); delay(DELAY_MS);
digitalWrite(LED_BUILTIN, LOW); delay(DELAY_MS); }
BR, Akio