Подключение ESP32 к SMART S-2435 по Bluetooth classic НАВТЕЛЕКОМ

#include "BluetoothSerial.h"

#define USE_NAME // Comment this to use MAC address instead of a slaveName
const char *pin = "1234"; // Change this to reflect the pin expected by the real slave BT device

#if !defined(CONFIG_BT_SPP_ENABLED)
#error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
#endif

BluetoothSerial SerialBT;

#ifdef USE_NAME
String slaveName = "S-2435:081559"; // Change this to reflect the real name of your slave BT device
#else
String MACadd = "48:E6:C0:C9:C5:0D"; // This only for printing
uint8_t address[6] = {0x48, 0xE6, 0xC0, 0xC9, 0xC5, 0x0D}; // Change this to reflect real MAC address of your slave BT device
//uint8_t address[6] = {0xC0, 0x10, 0xB1, 0x99, 0xD8, 0x42};
#endif

String myName = "ESP32-BT-Master";
bool connected;

//Запросы
//*?V
char send_version_ble[19] = {0x40, 0x4e, 0x54, 0x43, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x43, 0x58, 0x2a, 0x3f, 0x56};
//Конец описания запросов

void setup() {

Serial.begin(115200);

SerialBT.begin(myName, true);
#ifndef USE_NAME
//SerialBT.setPin(pin);
//Serial.println("Using PIN");
#endif
SerialBT.register_callback(BT_EventHandler);
Serial.printf("The device \"%s\" started in master mode, make sure slave BT device is on!\n", myName.c_str());
BLEConnect(connected);
}

void loop() {


if(connected){
for(int i = 0; i<19; i++){
SerialBT.write(send_version_ble[i]);
Serial.print(send_version_ble[i],HEX);
}}
else{
BLEConnect(connected);
}
Serial.println();/*
delay(100);
if(SerialBT.available()){
while (SerialBT.available()) {
Serial.write(SerialBT.read());
}
}*/

delay(3000);

}

void BLEConnect(bool& connected){
#ifdef USE_NAME
connected = SerialBT.connect(slaveName);
Serial.printf("Connecting to slave BT device named \"%s\"\n", slaveName.c_str());
#else
//connected = SerialBT.connect(address);
//Serial.print("Connecting to slave BT device with MAC "); Serial.println(MACadd);
#endif

if(connected) {
Serial.println("Connected Successfully!");
connected = true;
} else {
while(!SerialBT.connected(10000)) {
Serial.println("Failed to connect. Make sure remote device is available and in range, then restart app.");
delay(1000);
}
}
// Disconnect() may take up to 10 secs max
if (SerialBT.disconnect()) {
Serial.println("Disconnected Successfully!");
connected = false;
}
// This would reconnect to the slaveName(will use address, if resolved) or address used with connect(slaveName/address).
connected = SerialBT.connect(slaveName);
if(connected) {
Serial.println("Reconnected Successfully!");
} else {
while(!SerialBT.connected(10000)) {
Serial.println("Failed to reconnect. Make sure remote device is available and in range, then restart app.");
delay(1000);
}
}
}

void BT_EventHandler(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) {
if (event == ESP_SPP_START_EVT) {
Serial.println("Initialized SPP");
connected = true;
}
else if (event == ESP_SPP_SRV_OPEN_EVT ) {
Serial.println("Client connected");
connected = true;
}
else if (event == ESP_SPP_CLOSE_EVT ) {
Serial.println("Client disconnected");
connected = false;
}
else if (event == ESP_SPP_DATA_IND_EVT ) {
Serial.println("Data received");
while (SerialBT.available()) {
uint8_t incoming = SerialBT.read();
Serial.print(incoming, HEX);
}
Serial.println();
}
}
ESP32 - это микроконтроллер, который позволяет подключаться к различным устройствам по разным протоколам, включая Bluetooth. SMART S-2435 - это устройство от компании НАВТЕЛЕКОМ, которое поддерживает Bluetooth Classic.
Поиск информации по сайту мониторинга транспорта TREKBERRY
© TREKBERRY 2017-2024, Дмитрий В.М. Все права защищены.
Копирование материала без ссылки на источник запрещено. Информация размещенная на сайте не является публичной офертой. Часть текстов написано нейросетью ChatGPT может содержать не точности.