ハロー ブログ

日々のつぶやき @c2c2c2c221

BLE連携の試行

MacBookからBLE接続でI2Cスレーブ応答できました。

 

<pre>

#define I2C_BUFFER_LENGTH 16

#include "Wire.h"

#define I2C_SDA 14
#define I2C_SCL 12

#define I2C_DEV_ADDR 0x28


uint32_t i = 0;

uint32_t sts_f_req = 0;
uint32_t cmd_f_req = 0;
uint32_t cmd_i = 0;
uint32_t cmd_sed_f = 50;
uint32_t cmd_sed_fast_f = 1;


uint8_t cmd_table[10] = {  26 ,
27  ,
39 ,
41 ,
35 ,
90 ,
23 ,
125  ,
25
};

TwoWire I2CSensors = TwoWire(0);
uint8_t onnseists[3] = { 0x00, 0x00, 0x00 };
uint8_t onnseicmd[2] = { 0x00, 0x00 };
    
void onRequest(){
  if (sts_f_req == 1 ) {
    if (cmd_sed_f != 0 ) {
      onnseicmd[1] = cmd_i  ;
      cmd_sed_f -- ;
      Serial.printf("onRequest sts int in %x",onnseicmd[1]);
      Serial.println("");
    } else {
      onnseicmd[1] = 00 ;
      cmd_i =0 ;
    }
    I2CSensors.slaveWrite(onnseicmd,2);
  }
  if (cmd_f_req == 1 ) { 
    if ( cmd_sed_f != 0 ) {
      onnseists[2] = 0x01 ;
    } else {
      onnseists[2] = 0x00 ;
    }
    I2CSensors.slaveWrite(onnseists,3);
  }
}

void onReceive(int len){
  while(I2CSensors.available()){
    char robi_cmd_data = I2CSensors.read();
    if ( robi_cmd_data == 0x0c ) {
      sts_f_req = 1;
    } else {
      sts_f_req = 0;
    }
    if ( robi_cmd_data == 0x20 ) {
      cmd_f_req = 1;
    } else {
      cmd_f_req = 0;
    }
  }
  Serial.println();
}

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

BLEServer* pServer = NULL;
BLECharacteristic* pCharacteristic = NULL;
bool deviceConnected = false;
bool oldDeviceConnected = false;
uint32_t value = 0;

// See the following for generating UUIDs:
// https://www.uuidgenerator.net/

#define SERVICE_UUID      "AAA"
#define CHARACTERISTIC_UUID "BBB"


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 value = pCharacteristic->getValue();

      if (value.length() > 0) {
        Serial.println("*********");
        Serial.print("New value: ");
        cmd_i = value[0];
        //cmd_i = 26;
        for (int i = 0; i < value.length(); i++)
          Serial.print(value[i]);

        Serial.println();
        Serial.println("*********");
      }
    }
    void onRead(BLECharacteristic *pCharacteristic) {
       Serial.println("read");
       pCharacteristic->setValue("Hello World! read");
  }
};

void setup() {
  Serial.begin(115200);
  Serial.setDebugOutput(true);

  I2CSensors.onReceive(onReceive);
  I2CSensors.onRequest(onRequest);
  Serial.println("onXend");
  delay(10000);
  Serial.println("delayend");
  I2CSensors.begin*1;

  // Create the BLE Service
  BLEService *pService = pServer->createService(SERVICE_UUID);

  // Create a BLE Characteristic
  pCharacteristic = pService->createCharacteristic(
                      CHARACTERISTIC_UUID,
                      BLECharacteristic::PROPERTY_READ   |
                      BLECharacteristic::PROPERTY_WRITE  |
                      BLECharacteristic::PROPERTY_NOTIFY |
                      BLECharacteristic::PROPERTY_INDICATE
                    );

  // Create a BLE Descriptor
  pCharacteristic->addDescriptor(new BLE2902());

  // Start the service
  pService->start();

  pCharacteristic->setCallbacks(new MyCallbacks());

  pCharacteristic->setValue("Hello World");
  
  // Start advertising
  BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  pAdvertising->addServiceUUID(SERVICE_UUID);
  pAdvertising->setScanResponse(false);
  pAdvertising->setMinPreferred(0x0);  // set value to 0x00 to not advertise this parameter
  BLEDevice::startAdvertising();
  Serial.println("Waiting a client connection to notify...");
}

void loop() {
    // notify changed value
    if (deviceConnected) {
        const char *mes1 = "EOM";
        Serial.printf("%s",mes1);
        Serial.printf(":%0x",mes1);
        //value = 0x454f4d;
        value = 0x4d4f4500;
        
        pCharacteristic->setValue((uint8_t*)&value, 4);
        pCharacteristic->notify();
        value++;
        delay(1000); // bluetooth stack will go into congestion, if too many packets are sent, in 6 hours test i was able to go as low as 3ms
    }
    // disconnecting
    if (!deviceConnected && oldDeviceConnected) {
        delay(1000); // give the bluetooth stack the chance to get things ready
        pServer->startAdvertising(); // restart advertising
        Serial.println("start advertising");
        oldDeviceConnected = deviceConnected;
    }
    // connecting
    if (deviceConnected && !oldDeviceConnected) {
        // do stuff here on connecting
        oldDeviceConnected = deviceConnected;
    }
   }
}

</pre>

 

*1:uint8_t)I2C_DEV_ADDR,I2C_SDA,I2C_SCL,400000);

  Serial.println("setupend");
  
  // Create the BLE Device
  BLEDevice::init("ESP32");

  // Create the BLE Server
  pServer = BLEDevice::createServer();
  pServer->setCallbacks(new MyServerCallbacks(