4 Replies Latest reply on Apr 24, 2019 1:51 AM by preksha

    Sending serial data from Arduino to Android via Bluetooth

    april123

      Hi,

      I'm working on a project, and one of the things I need to do is to make my phone ring, when I push a button on arduino. I'm using App Inventor, and I want that every time that I push the button, the number 5 will be sent (in bytes) from arduino, to the my App Inventor's application (and then when the application gets the number 5, it makes the sound works). Unfortunately, the algorithm that I wrote doesn't work, and I don't know why.  This is the algorithm that I wrote, I markered the relewant lines. Thanks in advance! (btw I'm using Arduino 101)

       

      #include <CurieBLE.h>

      #include <SoftwareSerial.h> //using other pins instead of 0 and 1

      #define RxPin 8 //define software rx data pin on pin8

      #define TxPin 9 //define software tx data pin on pin9

      BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service

      SoftwareSerial blueTooth(RxPin, TxPin);

      // BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central

      BLEUnsignedCharCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);

       

      const int ledPin = 13, noisePin = 11, button=2; // pin to use for the LED

       

      void setup() {

        Serial.begin(9600);

        blueTooth.begin(9600);

        // set LED pin to output mode

      pinMode(ledPin, OUTPUT);

      pinMode(noisePin, OUTPUT);

      pinMode(button,INPUT_PULLUP);

        // begin initialization

        BLE.begin();

       

        // set advertised local name and service UUID:

        BLE.setLocalName("LED");

        BLE.setAdvertisedService(ledService);

       

        // add the characteristic to the service

        ledService.addCharacteristic(switchCharacteristic);

       

        // add service

        BLE.addService(ledService);

       

        // set the initial value for the characeristic:

        switchCharacteristic.setValue(0);

       

        // start advertising

        BLE.advertise();

       

        Serial.println("BLE LED Peripheral");

      }

       

      void loop() {

        // listen for BLE peripherals to connect:

        BLEDevice central = BLE.central();

       

        // if a central is connected to peripheral:

        if (central) {

          Serial.print("Connected to central: ");

          // print the central's MAC address:

          Serial.println(central.address());

       

          // while the central is still connected to peripheral:

          while (central.connected()) {

            // if the remote device wrote to the characteristic,

            // use the value to control the LED:

            if (switchCharacteristic.written()) {

              if (switchCharacteristic.value()==1) {   // any value other than 0

                Serial.println("LED on");

                digitalWrite(ledPin, HIGH);         // will turn the LED on

              }

              if (switchCharacteristic.value()==0) {  // a 0 value

                Serial.println(F("LED off"));

                digitalWrite(ledPin, LOW);          // will turn the LED off

              }

              if(switchCharacteristic.value()==2){

              Serial.println ("piezo off");

              digitalWrite (noisePin, LOW);

            }      if (switchCharacteristic.value()==3){

              Serial.println ("piezo on");

              digitalWrite (noisePin, HIGH);

            }

            }

            if(digitalRead(button)==1) //the button was pushed

             {

              byte data=5;

              blueTooth.write(data);

             }

          }

       

          // when the central disconnects, print it out:

          Serial.print(F("Disconnected from central: "));

          Serial.println(central.address());

        }

      }