Matt
Matt

Reputation: 1177

Sending data from Arduino to app

From following this tutorial here: https://www.raywenderlich.com/85900/arduino-tutorial-integrating-bluetooth-le-ios-swift I am able to control a servo through an iPhone app. It sends data from the iPhone to the arduino board. What I want is to be able to also send data FROM the Arduino to the App, basically the other way around, but I am confused as to how exactly I would send it using the Arduino.

This is how data is sent to the servo on Arduino:

if(BLE_Shield.available()) {
    myservo.write(BLE_Shield.read());  // Write position to servo
}

All I am told is that I need to write to the UART port to transmit on the RX characteristic with the UUID A9CD2F86-8661-4EB1-B132-367A3434BC90 and to get the app to be notified of RX characteristic changes. I have a hard time understanding this and would appreciate any help! The website includes the full Swift source code on the bottom of the page.

Upvotes: 0

Views: 2109

Answers (1)

Rob
Rob

Reputation: 2660

I never used the Black Widow BLE Shield, because normally I use a simple HM-10 BLE 4.0 module, but the logic will be the same. To send data from Arduino to iPhone you should writing .... "write" :

BLE_Shield.write("Test");

And to receive data you must write the function didUpdateValueFor characteristic inside BTService.swift Class.

 func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {

    if (error != nil) {
        print("Error rading characteristics: \(error)......)")
        return;
    }

    if (characteristic.value != nil) {
        //value here.

        print("value is : \(characteristic.value)")
        let dataBLE = String(data: characteristic.value!, encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue))

        print("Data from Arduino:\(dataBLE!)")
    }
}

For this to work you must be sure that the iPhone is constantly connected to the BLE.

Upvotes: 1

Related Questions