Thank you for your patience and support with my project. Good news, we have success!!! I'll post the final code below for anyone that is having a similar issue with the BlueFruit Feather. What seemed to make it finally work were two things - 1) The direct setting of the CCCD handle to 35. 2) Moving the TX up so that it registers the notification before sending the value to the Arduino board.
In the code below I've also added the buffer conversion, and a disconnect once it receives the correct value so that you can push the Puck to start the process again.
Again, thank you so much for your patience and support!
// Blink green for 100ms
function blinkGreen() {
LED2.write(true);
setTimeout(function () { LED2.write(false); }, 100);
}
// Variables
var feather = "ff:8d:05:ae:17:64 random";
let gattServer;
// UART Service UUID
const UART_SERVICE_UUID = "6E400001-B5A3-F393-E0A9-E50E24DCCA9E";
// UART RX Characteristic UUID (for sending data to the Arduino)
const UART_RX_CHARACTERISTIC_UUID = "6E400002-B5A3-F393-E0A9-E50E24DCCA9E";
// UART TX Characteristic UUID (for receiving data from the Arduino)
const UART_TX_CHARACTERISTIC_UUID = "6E400003-B5A3-F393-E0A9-E50E24DCCA9E";
// Function to connect and send data
function connectAndSend() {
console.log("Trying to connect to Arduino Board.");
NRF.requestDevice({ filters: [{ id: feather }], active: true })
.then(function(device) {
console.log("Connecting to device...");
return device.gatt.connect();
})
.then(function(gatt) {
console.log("Connected to GATT server");
gattServer = gatt;
return gatt.getPrimaryService(UART_SERVICE_UUID);
})
.then(function(service) {
console.log("Setting up TX Service...");
return service.getCharacteristic(UART_TX_CHARACTERISTIC_UUID);
}).then(function(txcharacteristic) {
console.log("Setting up listener for incoming data...");
txcharacteristic.on('characteristicvaluechanged', function(event) {
console.log("Received: "+JSON.stringify(event.target.value.buffer));
// Convert the event target value to a Uint8Array
let receivedData = new Uint8Array(event.target.value.buffer);
// Convert the array of ASCII codes to a string
let decodedValue = String.fromCharCode.apply(null, receivedData);
// Log the received value
console.log("RX: " + decodedValue);
if (decodedValue.trim() === "1") {
disconnect();
} else {
console.log("Received unexpected value: " + decodedValue);
}
});
txcharacteristic.handle_cccd = 35;
return txcharacteristic.startNotifications();
})
.then(function() {
return gattServer.getPrimaryService(UART_SERVICE_UUID);
})
.then(function(service) {
console.log("Got UART service");
//console.log(service);
console.log(" ");
return service.getCharacteristic(UART_RX_CHARACTERISTIC_UUID);
})
.then(function(rxcharacteristic) {
console.log("Got RX characteristic, sending '1'...");
//console.log(rxcharacteristic);
console.log(" ");
return rxcharacteristic.writeValue("1");
})
.catch(function(error) {
console.log("Error: " + error);
});
}
// Function to disconnect
function disconnect() {
if (gattServer && gattServer.connected) {
gattServer.disconnect();
console.log("Disconnected");
}
}
// Button press to initiate connection and sending
setWatch(connectAndSend, BTN, {edge:"rising", repeat:true, debounce:50});
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Hi Gordon!
Thank you for your patience and support with my project. Good news, we have success!!! I'll post the final code below for anyone that is having a similar issue with the BlueFruit Feather. What seemed to make it finally work were two things - 1) The direct setting of the CCCD handle to 35. 2) Moving the TX up so that it registers the notification before sending the value to the Arduino board.
In the code below I've also added the buffer conversion, and a disconnect once it receives the correct value so that you can push the Puck to start the process again.
Again, thank you so much for your patience and support!