2

这是 esp32 的完整代码

// ESP32 Example of the Bluetooth connection to the HM-10 module (CC2541) JDY-08
// Writes to the serial interface of the HM-10 module "Hello World"
// Receives serial data from the HM-10 module

#include "BLEDevice.h"

#define HM_MAC "00:15:85:14:9c:ca"

// Service und Characteristic des HM-10 Moduls
static BLEUUID serviceUUID("0000FFE0-0000-1000-8000-00805F9B34FB");
static BLEUUID charUUID("0000FFE1-0000-1000-8000-00805F9B34FB");

static boolean connect = true; 
static boolean connected = false;

static BLEAddress *pServerAddress;
static BLERemoteCharacteristic* pRemoteCharacteristic;
BLEClient*  pClient;

//    BLE Callbacks

static void notifyCallback 
(
  BLERemoteCharacteristic* pBLERemoteCharacteristic,
  uint8_t* pData,
  size_t length,
  bool isNotify)
{
  String EingangDaten = "";
  for (int i = 0; i < length; i++)EingangDaten += char(*pData++); // Append byte as character to string. Change to the next memory location
  Serial.println(EingangDaten);
}

//Connect to BLE Server

bool connectToServer(BLEAddress pAddress)
{
  Serial.println("Trying to Connect with.... ");
  Serial.println(pAddress.toString().c_str());
  pClient = BLEDevice::createClient();
  pClient->connect(pAddress);
 // Serial.println("CONNECTED");

  // Obtaining a reference to required service
  BLERemoteService* pRemoteService = pClient->getService(serviceUUID);

  if (pRemoteService == nullptr)
  {
    Serial.print("Gefunden falsche UUID: ");
    Serial.println(serviceUUID.toString().c_str());
    return false;
  }

  // reference to required characteristic
  pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID);
  if (pRemoteCharacteristic == nullptr) {
    Serial.print("Gefunden falsche Characteristic UUID: ");
    Serial.println(charUUID.toString().c_str());
    return false;
  }

  pRemoteCharacteristic->registerForNotify(notifyCallback);
  return true;
}

void setup()
{
  Serial.begin(115200);
  Serial.println("Start");
  BLEDevice::init("");
  pinMode(22,OUTPUT);
    pinMode(23,OUTPUT);

}

void loop()
{
  digitalWrite(22,HIGH);
  if (connect == true) 
  {
    pServerAddress = new BLEAddress(HM_MAC);
   // Serial.println("ServerAddress");
    //Serial.println(pServerAddress);
    if (connectToServer(*pServerAddress))
    {
      connected = true;
      connect = false;
    }
    else
    {
      Serial.println("Connection does not work");
    }
  }

  if (connected) 
  { 
    Serial.println("CONNECTED");

    String newValue = "HELLO WORLD";
    digitalWrite(23,HIGH);
    digitalWrite(22,LOW);
    pRemoteCharacteristic->writeValue(newValue.c_str(), newValue.length());
  }
  delay(1000);
}

我正在尝试通过使用 HM-10 mac 地址和 serviceUUID、charUUID(在 esp32 程序中给出)将数据从 esp32 发送到 HM-10 esp32 识别 HM-10 模块并连接但数据正在发送..

如果(连接){

Serial.println("已连接");

String newValue = "HELLO WORLD";
digitalWrite(23,HIGH);
digitalWrite(22,LOW);
pRemoteCharacteristic->writeValue(newValue.c_str(), newValue.length());

}

如果 esp32 连接到 HM-10 iam 得到输出作为已连接并且 LED 也打开但“hello world”数据没有发送到 HM-10 .. 如何在 HM-10 中编写程序以从 esp32 获取数据

4

0 回答 0