Hi all,
I have been tinkering with wireless communication between 2 Argons via BLE for quite a while now. My idea is to read information from the sensor (on the peripheral node) and transmit it to the central node which will then serial print the sensor reading. Have gone through numerous forum threads, studied the BLE tutorials extensively and tried to replicate them; but to no avail.
Most importantly is how to get the central node to scan and connect with the peripheral? (this is assuming that I have correctly attached the sensor values onto the advertised data). Another issue is that I cant seem to see any sensor values on nRF Connect (apple ios)… Hereby tapping into the expertise of the Particle community to assist me on this!
Sharing the codes for the central and the peripheral nodes below.
Peripheral Node
#include "Particle.h"
#include <Adafruit_BME280.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
SYSTEM_MODE(MANUAL);
SYSTEM_THREAD(ENABLED);
const size_t UART_TX_BUF_SIZE = 20;
const unsigned long UPDATE_INTERVAL = 2000;
unsigned long lastUpdate = 0;
#define SEALEVELPRESSURE_HPA (1013.25)
#define BME_ADDRESS 0x77
Adafruit_BME280 bme;
void onDataReceived(const uint8_t* data, size_t len, const BlePeerDevice& peer, void* context);
void setAdvertisingData();
float temp;
// These UUIDs were defined by Nordic Semiconductor and are now the defacto standard for
// UART-like services over BLE. Many apps support the UUIDs now, like the Adafruit Bluefruit app.
const BleUuid serviceUuid("6E400001-B5A3-F393-E0A9-E50E24DCCA9E");
const BleUuid rxUuid("6E400002-B5A3-F393-E0A9-E50E24DCCA9E");
const BleUuid txUuid("6E400003-B5A3-F393-E0A9-E50E24DCCA9E");
BleCharacteristic txCharacteristic("tx", BleCharacteristicProperty::NOTIFY, txUuid, serviceUuid);
BleCharacteristic rxCharacteristic("rx", BleCharacteristicProperty::WRITE_WO_RSP, rxUuid, serviceUuid, onDataReceived, NULL);
void onDataReceived(const uint8_t* data, size_t len, const BlePeerDevice& peer, void* context)
{
for (size_t ii = 0; ii < len; ii++)
{
Serial.write(data[ii]);
}
}
void setup()
{
Wire.begin();
Serial.begin();
pinMode(D7,OUTPUT);
while (!bme.begin())
{
delay(500);
Serial.println("Trying to connect BME280 Sensor");
}
BLE.on();
BLE.setTxPower(8);
BLE.addCharacteristic(txCharacteristic);
BLE.addCharacteristic(rxCharacteristic);
BleAdvertisingData data;
data.appendServiceUUID(serviceUuid);
setAdvertisingData();
BLE.setAdvertisingInterval(130);
}
void loop()
{
unsigned long currentMillis = millis();
if (currentMillis - lastUpdate >= UPDATE_INTERVAL)
{
lastUpdate = millis();
temp = bme.readTemperature();
setAdvertisingData();
}
}
//adapted from https://community.particle.io/t/need-help-sending-ble-uart-broadcast/50806
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, "%.2f *C", temp);
Serial.printlnf("%s (%d)", cd.data, strlen(cd.data));
advData.appendCustomData((uint8_t*)&cd, 2 + strlen(cd.data));
BLE.advertise(&advData);
}
Central Node
#include "Particle.h"
SYSTEM_MODE(MANUAL);
SYSTEM_THREAD(ENABLED);
const BleUuid serviceUuid("6E400001-B5A3-F393-E0A9-E50E24DCCA9E");
const BleUuid rxUuid("6E400002-B5A3-F393-E0A9-E50E24DCCA9E");
const BleUuid txUuid("6E400003-B5A3-F393-E0A9-E50E24DCCA9E");
const size_t UART_TX_BUF_SIZE = 20;
const size_t SCAN_RESULT_COUNT = 20;
BleScanResult scanResults[SCAN_RESULT_COUNT];
BleCharacteristic peerTxCharacteristic;
BleCharacteristic peerRxCharacteristic;
BlePeerDevice peer;
uint8_t txBuf[UART_TX_BUF_SIZE];
size_t txLen = 0;
float temp = 0;
const unsigned long SCAN_PERIOD_MS = 2000;
unsigned long lastScan = 0;
void onDataReceived(const uint8_t* data, size_t len, const BlePeerDevice& peer, void* context)
{
for (size_t ii = 0; ii < len; ii++)
{
Serial.write(data[ii]);
}
}
void setup()
{
Serial.begin();
BLE.on();
BLE.setScanPhy(BlePhy::BLE_PHYS_AUTO);
peerTxCharacteristic.onDataReceived(onDataReceived, &peerTxCharacteristic);
}
void loop()
{
Serial.println("Scanning for Peripheral Device");
if (millis() - lastScan >= SCAN_PERIOD_MS)
{
// Time to scan
lastScan = millis();
size_t count = BLE.scan(scanResults, SCAN_RESULT_COUNT);
if (count > 0)
{
for (uint8_t ii = 0; ii < count; ii++)
{
BleUuid foundServiceUuid;
size_t svcCount = scanResults[ii].advertisingData().serviceUUID(&foundServiceUuid, 1);
if (svcCount > 0 && foundServiceUuid == serviceUuid)
{
peer = BLE.connect(scanResults[ii].address());
if (peer.connected())
{
peer.getCharacteristicByUUID(peerTxCharacteristic, txUuid);
peer.getCharacteristicByUUID(peerRxCharacteristic, rxUuid);
peerTxCharacteristic.getValue(&temp);
Serial.printlnf("Temp %.2f °C", temp);
}
break;
}
}
}
}
}
Some threads that I referenced: Link 1, Link 2, Link 3
Appreciate all the help in advance!
Cheers