Hello,
I am very new to this, I am attaching the complete code that i'm using.
There may be extra code, as I have modified the code from Phillipe and I don't truly understand what all the components of the code are for.
With this code I do make the bluetooth link, but I am not sure how to send the open/close signal.
int buttonPin = 2;
int ledPin = 13;
int masterPin = 4;
int isMaster = 0;
int msgToken = 0;
int buttonState = 0;
int incomingByte = 0;
char gBtMsg[256];
char gBTAdr[13];
char gBtCmd[256];
int gBtKnownMACTotal = 2;
char* gBtKnownMAC[2]; //This is set to hold only two MAC adresses
void BtReceive(void){
bool keepReading = true;
int index = 0;
gBtMsg[0] = '\0';
while(keepReading){
keepReading = false;
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
if(incomingByte != 13){
gBtMsg[index++] = incomingByte;
keepReading = true;
}
}
}
gBtMsg[index] = '\0';
}
void BtSend(char* i_pBtCmd, bool i_ln = true){
if(i_ln){
Serial.println(i_pBtCmd);
} else{
Serial.print(i_pBtCmd);
}
delay(100);
BtReceive();
}
void BtInit(void){
isMaster = digitalRead(masterPin);
bool btConnect = false;
digitalWrite(ledPin,HIGH);
gBtKnownMAC[0] = "00066667ABCD"; // Change this to one of your MAC addresses
gBtKnownMAC[1] = "00066667ABCE"; // and this one too
Serial.begin(115200);
BtSend("$$$", false);
if(isMaster == LOW){
BtSend("SM,1");
BtSend("I,5");
BtSend("---");
while(!btConnect){
delay(100);
BtReceive();
int msgLen = strlen(gBtMsg);
if(msgLen > 0){
if(msgLen >= 12){
char* doneMsg = &gBtMsg[msgLen - 12];
gBtMsg[12] = '\0';
bool foundKnownMAC = false;
for(int i = 0; i < gBtKnownMACTotal && !foundKnownMAC; i++){
if(!strcmp(gBtMsg, gBtKnownMAC*)){*
-
foundKnownMAC = true;*
-
}*
-
}*
-
if(!strcmp(doneMsg, "Inquiry Done")){*
-
digitalWrite(ledPin,HIGH);*
-
if(foundKnownMAC){ *
-
gBtCmd[0] = 'C';*
-
gBtCmd[1] = ',';*
-
for(int i = 0; i < 12; i++){*
_ gBtCmd[i + 2] = gBtMsg*;_
_ }*_
* gBtCmd[15] = '\0';*
* BtSend("$$$", false);*
* BtSend(gBtCmd);*
* BtSend("---"); *
* delay(2000);*
* digitalWrite(ledPin,HIGH);*
* while(!btConnect){*
* delay(1000);*
* BtSend("$$$", false);*
* BtSend("GK");*
* int numVal = 0;*
* if(strlen(gBtMsg) > 0) {*
* numVal= atoi(gBtMsg);*
* }*
* if(numVal == 1){*
* btConnect = true;*
* }*
* BtSend("---");*
* }*
* }*
* }*
* }*
* }*
* }*
* digitalWrite(ledPin,LOW);*
* msgToken = 1;*
* } else{*
* BtSend("SM,0");*
* BtSend("---");*
* while(!btConnect){*
* delay(1000);*
* BtSend("$$$", false);*
* BtSend("GK");*
* int numVal = 0;*
* if(strlen(gBtMsg) > 0) {*
* numVal= atoi(gBtMsg);*
* }*
* if(numVal == 1){ *
* btConnect = true;*
* }*
* BtSend("---"); *
* }*
* }*
* digitalWrite(ledPin,LOW);*
}
void setup() {
* pinMode(ledPin, OUTPUT);*
* pinMode(isMaster, INPUT);*
* digitalWrite(ledPin,LOW);*
* BtInit();*
}
*void receiveMsg(){ *
* for(int i = 0; i < 10; i++){*
* if (Serial.available() > 0) {*
* // read the incoming byte:*
* incomingByte = Serial.read();*
* if(incomingByte == 'R'){*
* digitalWrite(ledPin, LOW);*
* delay(50);*
* digitalWrite(ledPin,HIGH);*
* }*
* }*
* delay(100);*
* }*
}
void sendMsg(){
* digitalWrite(ledPin,LOW);*
* if (buttonState == HIGH) { *
* Serial.print("R");*
* digitalWrite(ledPin,HIGH);*
}
}
void loop()
{
* if(msgToken == 1){*
* digitalWrite(ledPin,HIGH);*
* sendMsg();*
* delay(1000);*
* }else{*
* digitalWrite(ledPin,LOW);*
* receiveMsg();*
* }*
}
Please give me your thoughts.
Thank You
MG