I guess this would be the example that should get you started
https://docs.particle.io/tutorials/device-os/bluetooth-le/#device-nearby-beacon
However, you need to consider the limited space for advertising data (31 bytes minus some used by the default data leaving you with 24 bytes).
If you intended to add a device name that would count against that limit (plus 2 extra bytes).
With a custom app you’d also calculate the wattage instead of transferring it.
Try this
#include "Particle.h"
void setAdvertisingData();
float volt;
float amp;
float watt;
float temp;
uint8_t percent;
void setup() {
setAdvertisingData();
BLE.setAdvertisingInterval(130); // Advertise every 100 milliseconds. Unit is 0.625 millisecond intervals.
}
void loop() {
static uint32_t ms = 0;
if (!digitalRead(BTN)) // use MODE button to reset the dummy data
ms =
percent =
volt =
amp =
watt =
temp = 0;
if (millis() - ms < 1000) return;
ms = millis();
if (percent < 100) percent += 5;
volt += 1.5;
amp += 2.5;
watt = volt * amp;
temp += 0.5;
setAdvertisingData();
}
struct customData {
uint16_t companyID;
char data[25];
};
void setAdvertisingData() {
int retVal = 0;
customData cd;
BleAdvertisingData advData;
cd.companyID = 0xFFFF; // undefined company ID for custom data
memset(&cd.data, 0x00, sizeof(cd.data));
retVal = snprintf(cd.data, sizeof(cd.data)-1, "%.1fV%.1fA%.1fW%.1fF%u", volt, amp, watt, temp, percent);
Serial.printlnf("%s (%d)", cd.data, strlen(cd.data));
if (retVal < 0 || sizeof(cd.data)-1 < retVal) {
strcpy(cd.data, "too long! press MODE");
}
advData.appendCustomData((uint8_t*)&cd, 2 + strlen(cd.data));
BLE.advertise(&advData);
}
I’m using Android nRF Connect App for continous scanning to see the changing readings.
If you sent the data raw (not as string - dropping the units in the process) and chose the minimal datatype required (e.g. instead of float
an int16_t
for “centiVolt” and “centiAmpere”) you could get more data in that constrained space.