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?