Skip to content

Instantly share code, notes, and snippets.

@allrobot
Created March 29, 2022 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save allrobot/69c48cab6c418b8a5c45aed76f306a8a to your computer and use it in GitHub Desktop.
Save allrobot/69c48cab6c418b8a5c45aed76f306a8a to your computer and use it in GitHub Desktop.
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
// #include <periph_ctr
#define SERIAL_RX_BUFFER_SIZE 2048
BLEServer *pServer = NULL;
BLECharacteristic *pTxCharacteristic;
bool deviceConnected = false;
bool oldDeviceConnected = false;
boolean newData = false;
const byte numChars = 36;
uint8_t txValue = 0;
char receivedChars[numChars];
// See the following for generating UUIDs:
// https://www.uuidgenerator.net/
#define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID
#define CHARACTERISTIC_UUID_RX "f78ebbff-c8b7-4107-93de-889a6a06d408"
#define CHARACTERISTIC_UUID_TX "ca73b3ba-39f6-4ab3-91ae-186dc9577d99"
class MyServerCallbacks : public BLEServerCallbacks
{
void onConnect(BLEServer *pServer)
{
deviceConnected = true;
};
void onDisconnect(BLEServer *pServer)
{
deviceConnected = false;
}
};
class MyCallbacks : public BLECharacteristicCallbacks
{
void onWrite(BLECharacteristic *pCharacteristic)
{
std::string rxValue = pCharacteristic->getValue();
if (rxValue.length() > 0)
{
Serial2.println("*********");
Serial2.print("Received Value: ");
for (int i = 0; i < rxValue.length(); i++)
Serial2.print(rxValue[i]);
Serial2.println();
Serial2.println("*********");
}
}
};
void setup()
{
Serial1.begin(500000, SERIAL_8N1, 3, 1);
Serial2.begin(500000, SERIAL_8N1, 16, 17);
// Create the BLE Device
BLEDevice::init("UART Service");
// Create the BLE Server
pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
// Create the BLE Service
BLEService *pService = pServer->createService(SERVICE_UUID);
// Create a BLE Characteristic
pTxCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID_TX,
BLECharacteristic::PROPERTY_NOTIFY);
pTxCharacteristic->addDescriptor(new BLE2902());
BLECharacteristic *pRxCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID_RX,
BLECharacteristic::PROPERTY_WRITE);
pRxCharacteristic->setCallbacks(new MyCallbacks());
// Start the service
pService->start();
// Start advertising
pServer->getAdvertising()->start();
// // 确定传感器的连接状态
// // 缓冲区没有字符时,每半秒发送1次
// while (true)
// {
// if (Serial1.available() <= 0)
// {
// Serial1.print('A');
// delay(500);
// }
// else
// {
// while (Serial1.available()>=0)
// {
// Serial1.read();
// }
// break;
// }
// }
Serial2.println("\nSet Serial1 ok!");
Serial2.println("Waiting a client connection to notify...");
}
// void loop()
// {
// recvWithEndMarker();
// showNewData();
// }
// 接收串口数据,并
void recvWithEndMarker()
{
static byte ndx = 0;
static boolean recvInProgress = false;
char startMaker = '<';
char endMarker = '>';
char rc;
// if (Serial1.available() > 0) {
while (Serial1.available() > 0 && newData == false)
{
rc = Serial1.read();
if (recvInProgress == true)
{
if (rc != endMarker)
{
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars)
{
ndx = numChars - 1;
}
}
else
{
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMaker)
{
recvInProgress = true;
}
}
}
// 输出数据
void showNewData()
{
if (newData == true)
{
Serial2.println(receivedChars);
pTxCharacteristic->setValue(receivedChars);
pTxCharacteristic->notify();
newData = false;
}
}
void loop()
{
// disconnecting
if (!deviceConnected && oldDeviceConnected)
{
delay(500); // 让蓝牙堆栈有机会做好准备
pServer->startAdvertising(); // 重启广播
Serial2.println("disconnecting start advertising");
oldDeviceConnected = deviceConnected;
ESP.restart();
}
// connecting
if (deviceConnected && !oldDeviceConnected)
{
// do stuff here on connecting在连接上做点事情
oldDeviceConnected = deviceConnected;
Serial2.println("connecting start advertising");
}
if (deviceConnected)
{
recvWithEndMarker();
showNewData();
// long time = millis();
// for (int i = 0; i < 7000; i++)
// {
// recvWithEndMarker();
// showNewData();
// }
// float a = 1000 / ((millis() - time) / 1000);
// Serial2.println();
// Serial2.println();
// Serial2.println();
// Serial2.println();
// Serial2.println();
// Serial2.println();
// Serial2.println(xiao.data());
// delay(100000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment