Skip to main content
deleted 196 characters in body; edited title
Source Link
dda
  • 1.6k
  • 1
  • 12
  • 17

Want to read Read all realyrelay output (high or low) from bluetoothBluetooth through androidAndroid app

I have written the following code for arduinoArduino:

#define relay2 2
#define relay3 3
#define relay4 4
#define relay5 5
#define relay6 6
#define relay7 7
#define relay8 8
#define relay9 9
byte val;
void setup() {
 
  Serial.begin(9600);
  pinMode(relay2, OUTPUT);
  pinMode(relay3, OUTPUT);
  pinMode(relay4, OUTPUT);
  pinMode(relay5, OUTPUT);
  pinMode(relay6, OUTPUT);
  pinMode(relay7, OUTPUT);
  pinMode(relay8, OUTPUT);
 
}

void loop() {
  int a = 0;
  if (Serial.available())
  {
    val = Serial.read();
    Serial.println(int(val));
    //Display received value on Serial Monitor
 
    //ASCII VALUES 
    if (int(val) == 49) //Turn Light1 ON 1
      digitalWrite(relay2, HIGH);

      
    else if (int(val) == 50) //Turn Light1 OFF 2
      digitalWrite(relay2, LOW);
 
    if (int(val) == 51) //Turn Light2 ON
      digitalWrite(relay3, HIGH);
 
    else if (int(val) == 52) //Turn Light2 OFF
      digitalWrite(relay3, LOW);
 
    if (int(val) == 53) //Turn Light3 ON
      digitalWrite(relay4, HIGH);
 
    else if (int(val) == 54) //Turn Light3 OFF
      digitalWrite(relay4, LOW);
 
    if (int(val) == 55) //Turn AC ON
      digitalWrite(relay5, HIGH);
 
    else if (int(val) == 56) //Turn AC OFF
      digitalWrite(relay5, LOW);
 
    if (int(val) == 57) //Lock the DOOR
      digitalWrite(relay6, HIGH);
 
    else if (int(val) == 48) //Unlock the DOOR
      digitalWrite(relay6, LOW);
 
  }
}

Now For androidon the Android side iI have wrritenwritten the following code:

 //Bluetooth connection code ....
  mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);

  mmSocket.connect();
                mmOutputStream = mmSocket.getOutputStream();
                mmInputStream = mmSocket.getInputStream();
       
  

 int byteCount = mmInputStream.available();
        if (byteCount > 0) {
            byte[] rawBytes = new byte[byteCount];
            mmInputStream.read(rawBytes);
            String string = new String(rawBytes);
            Toast.makeText(this, string, Toast.LENGTH_SHORT).show();
        }

Now it works perfectperfectly when iI press a button to turn the light on/off.

  But iI want to read all relay current status  (high/low) when bluetootha Bluetooth connection is made.

So basically at the first time when i amafter connecting bluetoothto the Bluetooth module then after that iI want all relay status ?.

How can iI do that  ?

Want to read all realy output (high or low) from bluetooth through android app

I have written the following code for arduino:

#define relay2 2
#define relay3 3
#define relay4 4
#define relay5 5
#define relay6 6
#define relay7 7
#define relay8 8
#define relay9 9
byte val;
void setup() {
 
  Serial.begin(9600);
  pinMode(relay2, OUTPUT);
  pinMode(relay3, OUTPUT);
  pinMode(relay4, OUTPUT);
  pinMode(relay5, OUTPUT);
  pinMode(relay6, OUTPUT);
  pinMode(relay7, OUTPUT);
  pinMode(relay8, OUTPUT);
 
}

void loop() {
  int a = 0;
  if (Serial.available())
  {
    val = Serial.read();
    Serial.println(int(val));//Display received value on Serial Monitor
 
    //ASCII VALUES 
    if (int(val) == 49) //Turn Light1 ON 1
      digitalWrite(relay2, HIGH);

      
    else if (int(val) == 50) //Turn Light1 OFF 2
      digitalWrite(relay2, LOW);
 
    if (int(val) == 51) //Turn Light2 ON
      digitalWrite(relay3, HIGH);
 
    else if (int(val) == 52) //Turn Light2 OFF
      digitalWrite(relay3, LOW);
 
    if (int(val) == 53) //Turn Light3 ON
      digitalWrite(relay4, HIGH);
 
    else if (int(val) == 54) //Turn Light3 OFF
      digitalWrite(relay4, LOW);
 
    if (int(val) == 55) //Turn AC ON
      digitalWrite(relay5, HIGH);
 
    else if (int(val) == 56) //Turn AC OFF
      digitalWrite(relay5, LOW);
 
    if (int(val) == 57) //Lock the DOOR
      digitalWrite(relay6, HIGH);
 
    else if (int(val) == 48) //Unlock the DOOR
      digitalWrite(relay6, LOW);
 
  }
}

Now For android side i have wrriten following code:

 //Bluetooth connection code ....
  mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);

  mmSocket.connect();
                mmOutputStream = mmSocket.getOutputStream();
                mmInputStream = mmSocket.getInputStream();
        

 int byteCount = mmInputStream.available();
        if (byteCount > 0) {
            byte[] rawBytes = new byte[byteCount];
            mmInputStream.read(rawBytes);
            String string = new String(rawBytes);
            Toast.makeText(this, string, Toast.LENGTH_SHORT).show();
        }

Now it works perfect when i press button to turn light on/off.

  But i want to read all relay current status(high/low) when bluetooth connection is made.

So basically at the first time when i am connecting bluetooth module then after that i want all relay status ?

How can i do that  ?

Read all relay output (high or low) from Bluetooth through Android app

I have written the following code for Arduino:

#define relay2 2
#define relay3 3
#define relay4 4
#define relay5 5
#define relay6 6
#define relay7 7
#define relay8 8
#define relay9 9
byte val;
void setup() {
  Serial.begin(9600);
  pinMode(relay2, OUTPUT);
  pinMode(relay3, OUTPUT);
  pinMode(relay4, OUTPUT);
  pinMode(relay5, OUTPUT);
  pinMode(relay6, OUTPUT);
  pinMode(relay7, OUTPUT);
  pinMode(relay8, OUTPUT);
}

void loop() {
  int a = 0;
  if (Serial.available()) {
    val = Serial.read();
    Serial.println(int(val));
    //Display received value on Serial Monitor
    //ASCII VALUES 
    if (int(val) == 49) //Turn Light1 ON 1
      digitalWrite(relay2, HIGH);
    else if (int(val) == 50) //Turn Light1 OFF 2
      digitalWrite(relay2, LOW);
    if (int(val) == 51) //Turn Light2 ON
      digitalWrite(relay3, HIGH);
    else if (int(val) == 52) //Turn Light2 OFF
      digitalWrite(relay3, LOW);
    if (int(val) == 53) //Turn Light3 ON
      digitalWrite(relay4, HIGH);
    else if (int(val) == 54) //Turn Light3 OFF
      digitalWrite(relay4, LOW);
    if (int(val) == 55) //Turn AC ON
      digitalWrite(relay5, HIGH);
    else if (int(val) == 56) //Turn AC OFF
      digitalWrite(relay5, LOW);
    if (int(val) == 57) //Lock the DOOR
      digitalWrite(relay6, HIGH);
    else if (int(val) == 48) //Unlock the DOOR
      digitalWrite(relay6, LOW);
  }
}

Now on the Android side I have written the following code:

 //Bluetooth connection code ....
  mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);

  mmSocket.connect();
  mmOutputStream = mmSocket.getOutputStream();
  mmInputStream = mmSocket.getInputStream();
 
  int byteCount = mmInputStream.available();
  if (byteCount > 0) {
    byte[] rawBytes = new byte[byteCount];
    mmInputStream.read(rawBytes);
    String string = new String(rawBytes);
    Toast.makeText(this, string, Toast.LENGTH_SHORT).show();
  }

Now it works perfectly when I press a button to turn the light on/off. But I want to read all relay current status  (high/low) when a Bluetooth connection is made.

So basically after connecting to the Bluetooth module I want all relay status.

How can I do that?

Source Link
deepak
  • 101
  • 2

Want to read all realy output (high or low) from bluetooth through android app

I have written the following code for arduino:

#define relay2 2
#define relay3 3
#define relay4 4
#define relay5 5
#define relay6 6
#define relay7 7
#define relay8 8
#define relay9 9
byte val;
void setup() {

  Serial.begin(9600);
  pinMode(relay2, OUTPUT);
  pinMode(relay3, OUTPUT);
  pinMode(relay4, OUTPUT);
  pinMode(relay5, OUTPUT);
  pinMode(relay6, OUTPUT);
  pinMode(relay7, OUTPUT);
  pinMode(relay8, OUTPUT);

}

void loop() {
  int a = 0;
  if (Serial.available())
  {
    val = Serial.read();
    Serial.println(int(val));//Display received value on Serial Monitor

    //ASCII VALUES 
    if (int(val) == 49) //Turn Light1 ON 1
      digitalWrite(relay2, HIGH);

      
    else if (int(val) == 50) //Turn Light1 OFF 2
      digitalWrite(relay2, LOW);

    if (int(val) == 51) //Turn Light2 ON
      digitalWrite(relay3, HIGH);

    else if (int(val) == 52) //Turn Light2 OFF
      digitalWrite(relay3, LOW);

    if (int(val) == 53) //Turn Light3 ON
      digitalWrite(relay4, HIGH);

    else if (int(val) == 54) //Turn Light3 OFF
      digitalWrite(relay4, LOW);

    if (int(val) == 55) //Turn AC ON
      digitalWrite(relay5, HIGH);

    else if (int(val) == 56) //Turn AC OFF
      digitalWrite(relay5, LOW);

    if (int(val) == 57) //Lock the DOOR
      digitalWrite(relay6, HIGH);

    else if (int(val) == 48) //Unlock the DOOR
      digitalWrite(relay6, LOW);

  }
}

Now For android side i have wrriten following code:

 //Bluetooth connection code ....
  mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);

  mmSocket.connect();
                mmOutputStream = mmSocket.getOutputStream();
                mmInputStream = mmSocket.getInputStream();
        

int byteCount = mmInputStream.available();
        if (byteCount > 0) {
            byte[] rawBytes = new byte[byteCount];
            mmInputStream.read(rawBytes);
            String string = new String(rawBytes);
            Toast.makeText(this, string, Toast.LENGTH_SHORT).show();
        }

Now it works perfect when i press button to turn light on/off.

But i want to read all relay current status(high/low) when bluetooth connection is made.

So basically at the first time when i am connecting bluetooth module then after that i want all relay status ?

How can i do that ?