三,代码片断

一,外部IO中断测试

#include <Arduino.h>
// 测试中断功能

void PinIntEvent(void)
{
  Serial.printf("Pin Event.\r\n");
}

void setup()
{
  // put your setup code here, to run once:
  Serial.begin(115200);
  pinMode(4,INPUT_PULLUP);
  attachInterrupt(4,PinIntEvent,FALLING);
  Serial.printf("PinIntEvent Init Finish.\r\n");
}

void loop()
{
  // put your main code here, to run repeatedly:
  
  delay(100);
}

二,经典蓝牙驱动测试

#include <Arduino.h>
#include<BluetoothSerial.h>
BluetoothSerial SerialBT;
void setup()
{
    Serial.begin(115200);
    SerialBT.begin("ESP32test");
    Serial.println("The device started,now you can try to connected it");
}
void loop()
{
    if(Serial.available())
    {
        SerialBT.write(Serial.read());
    }
    if(SerialBT.available())
    {
        Serial.write(SerialBT.read());
    }
    delay(1);
}

三,低功耗蓝牙

#include <Arduino.h>
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
#include <String.h>

BLECharacteristic *pCharacteristic; //创建一个BLE特性pCharacteristic
bool deviceConnected = false;       //连接否标志位
uint8_t txValue = 0;                //TX的值
long lastMsg = 0;                   //存放时间的变量
String rxload = "BlackWalnutLabs";  //RX的预置值

#define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID
#define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
#define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"

//服务器回调
class MyServerCallbacks : public BLEServerCallbacks
{
  void onConnect(BLEServer *pServer)
  {
    deviceConnected = true;
    Serial.printf("BlueTooth has connected...\r\n");
  };
  void onDisconnect(BLEServer *pServer)
  {
    deviceConnected = false;
    Serial.printf("BlueTooth has disconnected...\r\n");
  }
};

//特性回调
class MyCallbacks : public BLECharacteristicCallbacks
{
  void onWrite(BLECharacteristic *pCharacteristic)
  {
    std::string rxValue = pCharacteristic->getValue();
    if (rxValue.length() > 0)
    {
      rxload = "";
      for (int i = 0; i < rxValue.length(); i++)
      {
        rxload += (char)rxValue[i];
        Serial.print(rxValue[i]);
      }
      Serial.println("");
    }
  }
};

void setupBLE(String BLEName)
{
  const char *ble_name = BLEName.c_str(); //将传入的BLE的名字转换为指针
  BLEDevice::init(ble_name);              //初始化一个蓝牙设备

  BLEServer *pServer = BLEDevice::createServer(); // 创建一个蓝牙服务器
  pServer->setCallbacks(new MyServerCallbacks()); //服务器回调函数设置为MyServerCallbacks

  BLEService *pService = pServer->createService(SERVICE_UUID); //创建一个BLE服务

  pCharacteristic = pService->createCharacteristic(CHARACTERISTIC_UUID_TX, BLECharacteristic::PROPERTY_NOTIFY);
  //创建一个(读)特征值 类型是通知
  pCharacteristic->addDescriptor(new BLE2902());
  //为特征添加一个描述

  BLECharacteristic *pCharacteristic = pService->createCharacteristic(CHARACTERISTIC_UUID_RX, BLECharacteristic::PROPERTY_WRITE);
  //创建一个(写)特征 类型是写入
  pCharacteristic->setCallbacks(new MyCallbacks());
  //为特征添加一个回调

  pService->start();                  //开启服务
  pServer->getAdvertising()->start(); //服务器开始广播
  Serial.println("Waiting a client connection to notify...");
}
void setup()
{
  Serial.begin(115200);
  setupBLE("ESP32BLE"); //设置蓝牙名称
}
char hellostr[128] = {'a', 'b', 'c'};
void loop()
{
  long now = millis(); //记录当前时间
  static int i = 0;
  if (now - lastMsg > 10)
  { //每隔10ms发一次信号
    if (deviceConnected && rxload.length() > 0)
    {
      if (rxload == "10086")
      {
        i++;
        sprintf(hellostr, "%s%d", "ABC", i);
        //const char *newValue = str.c_str();
        pCharacteristic->setValue(hellostr);
        pCharacteristic->notify();
        rxload.clear();
      }
    }
    lastMsg = now; //刷新上一次发送数据的时间
  }
  if(Serial.available())
  {
    for(int i=0;i<128;i++)
    {
      hellostr[i]=0;
    }
    Serial.read(hellostr,128);
    // pCharacteristic->setValue(addr);
    // pCharacteristic->notify();
    Serial.print(hellostr);
    if(deviceConnected)
    {
      pCharacteristic->setValue(hellostr);
      pCharacteristic->notify();
    }
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值