arduino-libraries / ArduinoBLE

ArduinoBLE library for Arduino
GNU Lesser General Public License v2.1
310 stars 205 forks source link

Broadcast BLE data without connecting #347

Open tech9492 opened 8 months ago

tech9492 commented 8 months ago

Hi all,

I am trying to BLE broadcast IO pin readings from an Arduino RP2040 Nano Connect. This data will be picked up by a Raspberry Pi running TheengsGateway. Does the ArduinoBLE library support this mode of operation?

Below is the code I am running on the Arduino and the Raspberry Pi output the following home/TheengsGateway/BTtoMQTT/30C6F7018E72 {"name": "RP2040", "id": "30:C6:F7:01:8E:72", "rssi": -60} .

Is it possible to input the BLE data I wish to broadcast within the above output?

Thank you.

#include <ArduinoBLE.h>

const int PSON = A3;
const int SSR = 8;
const int FAN = 6;

unsigned long ssrTimer = 0;  // SSR timer
unsigned long fanTimer = 0;  // Fan timer

int psState = 0;   // PSON status
int ssState = 0;   // SSR status
int fnState = 0;   // FAN status

BLEService ControlService("7b4e5c33-e8db-44e1-a329-919a56016229");

BLEIntCharacteristic PSonCharacteristic("3b2984e2-2997-4c75-b927-09691d056836", BLERead | BLENotify | BLEBroadcast);
BLEIntCharacteristic SSRCharacteristic("3b2984e2-2997-4c75-b927-09691d056837", BLERead | BLENotify | BLEBroadcast);
BLEIntCharacteristic FANCharacteristic("3b2984e2-2997-4c75-b927-09691d056838", BLERead | BLENotify | BLEBroadcast);

void setup() {
  pinMode(PSON, INPUT);  // analog pin A3 as input
  pinMode(SSR, OUTPUT);  // digital pin 8 as output
  pinMode(FAN, OUTPUT);  // digital pin 6 as output

  digitalWrite(SSR, LOW);
  digitalWrite(FAN, LOW);

  if (!BLE.begin()) {
  }

  // set the local name peripheral advertises
  BLE.setLocalName("RP2040");

  // set the UUID for the service this peripheral advertises:
  BLE.setAdvertisedService(ControlService);

  // add the characteristics to the service
  ControlService.addCharacteristic(PSonCharacteristic);
  ControlService.addCharacteristic(SSRCharacteristic);
  ControlService.addCharacteristic(FANCharacteristic);

  // add the service
  BLE.addService(ControlService);

  PSonCharacteristic.writeValue(psState);
  SSRCharacteristic.writeValue(ssState);
  FANCharacteristic.writeValue(fnState);

  BLE.setAdvertisingInterval(8000);

  // start advertising
  BLE.advertise();
}

void loop() {

  psState = analogRead(PSON);  // read analog input status
  ssState = digitalRead(SSR);  // read digital output status
  fnState = digitalRead(FAN);  // read digital output status

  // call poll() regularly to allow the library to send MQTT and prevent disconnection by the broker
  BLE.poll();

  if (psState > 50) {
    ssrTimer = 100;  // delay SSR for 10 seconds
  }

  psOFF();  // run psOFF

  if (ssState == 1) {
    fanTimer = 100;  // delay Fan for 10 seconds
  }

  fnOFF();

  tele();
}

void psOFF() {
  static unsigned long sspreviousTime;
  unsigned long sscurrentTime = millis();  // return time since board active

  // move forward if 100 ms has elapsed
  if (sscurrentTime - sspreviousTime < 100) {
    return;
  }

  sspreviousTime = sscurrentTime;

  if (!ssrTimer) {
    digitalWrite(SSR, LOW);  // if timer is done or still 0, set digital output LOW
  } else {
    digitalWrite(SSR, HIGH);  // set digital output HIGH
    digitalWrite(FAN, HIGH);  // set ditigal output HIGH
    ssrTimer--;
  }
}

void fnOFF() {
  static unsigned long fnpreviousTime;
  unsigned long fncurrentTime = millis();  // return time since board active

  // move forward if 100 ms has elapsed
  if (fncurrentTime - fnpreviousTime < 100) {
    return;
  }

  fnpreviousTime = fncurrentTime;

  if (!fanTimer) {
    digitalWrite(FAN, LOW);  // if timer is done or still 0, set digital output LOW
  } else {
    digitalWrite(FAN, HIGH);  // set digital output HIGH
    fanTimer--;
  }
}

void tele() {
  PSonCharacteristic.writeValue(psState);
  SSRCharacteristic.writeValue(ssState);
  FANCharacteristic.writeValue(fnState);
}
paulvha commented 8 months ago

Sometime ago I wrote a small sketch that does just what you are looking for.

regards, Paul BLE_adv.zip