使用arduino Nano 32 IOTs BLE制作LED显示板

Using arduino Nano 32 IOTs BLE to create LED display boards

我正在做一个项目,我需要一个有 7 个按钮的面板来点亮 5 米外显示器上的 LED 灯带。到目前为止,我已经做到了,所以我可以用一个按钮控制 1 个 LED 灯条,效果很好。我现在对如何让其他 6 个通过 BLE 连接到同一个 arduino 感到困惑。这个想法是让一个 arduino 连接所有的按钮,然后每个 LED 灯带 1 个 arduino。您按下按钮 arduino 上的按钮 1,这会向显示器 1 arduino 发送信号,点亮显示器。

到目前为止,这是我的代码,我需要对其做些什么才能在其中添加多个按钮?

谢谢!!

//this code is loaded onto the board that is connected to the led strip
//if the code doesnt work it seems to kick start it by opening the serial monitor and then it will connect, not sure why this is
#include <ArduinoBLE.h>
#include <Adafruit_DotStar.h>
#include <SPI.h> 

#define NUMPIXELS 144 // Number of LEDs in strip
#define BUTTON_PIN 9 //pin the button is on
#define DATAPIN    4 //the pin the data is plugged into
#define CLOCKPIN   5 //the pin the clock wire is plugged into

BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // adress of the ledstrip that is referenced in the other set of code  

Adafruit_DotStar strip(NUMPIXELS, DATAPIN, CLOCKPIN, DOTSTAR_BRG);

// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);

const int ledPin = 2; // pin to use for the LED

void setup() {

  // set LED pin to output mode
  pinMode(ledPin, OUTPUT);

  // begin initialization
  if (!BLE.begin()) {
    Serial.println("starting BLE failed!");

    while (1);
  }

  // set advertised local name and service UUID:
  BLE.setLocalName("LED");
  BLE.setAdvertisedService(ledService);

  // add the characteristic to the service
  ledService.addCharacteristic(switchCharacteristic);

  // add service
  BLE.addService(ledService);

  // set the initial value for the characeristic:
  switchCharacteristic.writeValue(0);

  // start advertising
  BLE.advertise();

  Serial.println("BLE LED Peripheral");

  
  strip.begin();
  strip.show(); //pixels to 'off'
}

  uint32_t white= strip.Color(255, 255, 255); //colour you want the lights
  uint32_t off= strip.Color(0, 0, 0); //colour 'off'
  
void loop() {
  // listen for BLE peripherals to connect:
  BLEDevice central = BLE.central();

  // if a central is connected to peripheral:
  if (central) {
    Serial.print("Connected to central: ");
    // print the central's MAC address:
    Serial.println(central.address());

    // while the central is still connected to peripheral:
    while (central.connected()) {
      // if the remote device wrote to the characteristic,
      // use the value to control the LED:
      if (switchCharacteristic.written()) {
        if (switchCharacteristic.value()) {   // any value other than 0
          strip.fill(white, 0, 144); //fill(Color,first pixel,last pixel)
          strip.setBrightness(5); //set the brightness of the leds here, would keep about 40, doesnt like anything above that
          strip.show(); //update the leds
          delay(7000); //time you want the LEDs 
          strip.fill(off, 0, 144); //turns off leds
          strip.setBrightness(0);
          strip.show();
          
        } else {                              // a 0 value
        strip.fill(off, 0, 144); 
        strip.setBrightness(0);
        strip.show();
          
        }
      }
    }

    // when the central disconnects, print it out:
    Serial.print(F("Disconnected from central: "));
    Serial.println(central.address());
  }
}

//this code gets loaded onto the button board

#include <ArduinoBLE.h>

// variables for button
const int buttonPin = 2;
int oldButtonState = LOW;

void setup() {
  

 // configure the button pin as input
  pinMode(buttonPin, INPUT);

  // initialize the BLE hardware
  BLE.begin();

  Serial.println("BLE Central - LED control");

  // start scanning for peripherals
  BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214"); //Put the adress of what you want the button to control here 
}

void loop() {
  // check if a peripheral has been discovered
  BLEDevice peripheral = BLE.available();

  if (peripheral) {
    // discovered a peripheral, print out address, local name, and advertised service
    Serial.print("Found ");
    Serial.print(peripheral.address());
    Serial.print(" '");
    Serial.print(peripheral.localName());
    Serial.print("' ");
    Serial.print(peripheral.advertisedServiceUuid());
    Serial.println();

    if (peripheral.localName() != "LED") {
      return;
    }

    // stop scanning
    BLE.stopScan();

    controlLed(peripheral);

    // peripheral disconnected, start scanning again
    BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214");
  }
}

void controlLed(BLEDevice peripheral) {
  // connect to the peripheral
  Serial.println("Connecting ...");

  if (peripheral.connect()) {
    Serial.println("Connected");
  } else {
    Serial.println("Failed to connect!");
    return;
  }

  // discover peripheral attributes
  Serial.println("Discovering attributes ...");
  if (peripheral.discoverAttributes()) {
    Serial.println("Attributes discovered");
  } else {
    Serial.println("Attribute discovery failed!");
    peripheral.disconnect();
    return;
  }

  // retrieve the LED characteristic
  BLECharacteristic ledCharacteristic = peripheral.characteristic("19b10001-e8f2-537e-4f6c-d104768a1214");

  if (!ledCharacteristic) {
    Serial.println("Peripheral does not have LED characteristic!");
    peripheral.disconnect();
    return;
  } else if (!ledCharacteristic.canWrite()) {
    Serial.println("Peripheral does not have a writable LED characteristic!");
    peripheral.disconnect();
    return;
  }

  while (peripheral.connected()) {
    // while the peripheral is connected

    // read the button pin
    int buttonState = digitalRead(buttonPin);

    if (oldButtonState != buttonState) {
      // button changed
      oldButtonState = buttonState;

      if (buttonState) {
        Serial.println("button pressed");

        // button is pressed, write 0x01 to turn the LED on
        ledCharacteristic.writeValue((byte)0x01);
      } else {
        Serial.println("button released");

        // button is released, write 0x00 to turn the LED off
        ledCharacteristic.writeValue((byte)0x00);
      }
    }
  }

  Serial.println("Peripheral disconnected");

使用 BLE 执行此操作的典型方法是将按钮作为外围设备,将 LED 灯条作为中央设备。 Central 将连接到 Peripheral 并订阅有关“按钮”特征的通知。通常 libraries/hardware 未设置为将多个 Central 同时连接到一个 Peripheral。这似乎排除了使用您所需的硬件设置这样做的可能性。

另一种方法是将按钮作为中央设备,将 LED 灯条作为外围设备。 Central 已经知道 Peripheral 设备的详细信息并启动连接,然后在按下按钮时进行写入。我希望在按下按钮和使用此设置的 LED 灯条上发生某些事情之间会有很多滞后。

另一种选择是使用无连接 BLE,如果安全不是问题的话。按钮板可以充当 BLE 信标,您可以在服务数据或制造商数据中编码有关按下哪个按钮的信息。 LED 条将是 扫描器 从信标读取数据。我对 Arduino 上的 BLE 库不是很熟悉,似乎有 set the Manufacturer Data 的命令,但我找不到任何读取数据的命令。