keinath

Arduino App Inventor Bluetooth

Feb 8th, 2013
613
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.28 KB | None | 0 0
  1. //Fireplace Protoype
  2. //I used a Arduino Uno and BlueSMiRF Silver http://www.sparkfun.com/products/10269
  3.  
  4. // Project page:
  5.  
  6. //Connections:
  7. // BlueSMiRF        Uno
  8. // ---------   to   ---------
  9. // VCC              5V
  10. // Gnd              Gnd
  11. // TX-0             D6 (Uno's RX - defined below)
  12. // RX-1             D7 (Uno's TX - defined below)
  13.  
  14.  
  15.  
  16. #include <SoftwareSerial.h>
  17.  
  18. //define the receive and transmit pins for the bluetooth to communicate with Uno
  19. #define RxD 6
  20. #define TxD 7
  21.  
  22. int turnONvalue=0;
  23. int led = 12;
  24.  
  25. //Base connection method for a slave device taken from
  26. SoftwareSerial blueToothSerial(RxD,TxD);
  27.  
  28. void setupBlueToothConnection(){
  29.     Serial.println("Setting up Bluetooth Link");
  30.     delay(1000);
  31.     blueToothSerial.begin(115200); //Begins Bluetooth serial connection. Set Bluetooth BaudRate to default baud rate  115200
  32.     delay(1000);
  33.     Serial.println("Sending: +INQ=0");
  34.     sendBlueToothCommand("\r\n+INQ=0\r\n");
  35.    
  36.     delay(2000);
  37.     Serial.println("Sending: +STWMOD=0");
  38.     sendBlueToothCommand("\r\n+STWMOD=0\r\n");
  39.    
  40.     sendBlueToothCommand("\r\n+STNA=ArduinoBT\r\n");
  41.     Serial.println("Sending: +STNA=ArduinoBT");
  42.     sendBlueToothCommand("\r\n+STAUTO=0\r\n");
  43.     Serial.println("Sending: +STAUTO=0");
  44.     sendBlueToothCommand("\r\n+STOAUT=1\r\n");
  45.     Serial.println("Sending: +STOAUT=1");
  46.     sendBlueToothCommand("\r\n+STPIN=0000\r\n");
  47.     Serial.println("Sending:+STPIN=0000");
  48.     delay(2000); // This delay is required.
  49.     sendBlueToothCommand("\r\n+INQ=1\r\n");
  50.     Serial.println("Sending: +INQ=1");
  51.     delay(2000); // This delay is required.
  52.     blueToothSerial.flush();
  53. }
  54.  
  55. /* turned off as it did not work, but kept just in case
  56. //Checks if the response "OK" is received.
  57. void CheckOK(){
  58.   char a,b;
  59.   while(1){
  60.     Serial.println("checking OK main loop");
  61.     if(int len = blueToothSerial.available()){
  62.       a = blueToothSerial.read();
  63.       if('O' == a){
  64.         b = blueToothSerial.read();
  65.         if('K' == b){
  66.           Serial.println("BLUETOOTH OK");
  67.           while( (a = blueToothSerial.read()) != -1){
  68.             Serial.print(a);
  69.           }
  70.           break;
  71.         }
  72.       }
  73.     }
  74.   }
  75.   while( (a = blueToothSerial.read()) != -1){
  76.     Serial.print(a);
  77.   }
  78. }
  79. */
  80.  
  81. //Send the command to Bluetooth
  82. void sendBlueToothCommand(char command[]){
  83.   blueToothSerial.print(command);
  84.     //CheckOK();  
  85.    Serial.println("done sending command, hehe");
  86. }
  87.  
  88. void setup(){
  89.   pinMode(led, OUTPUT);
  90.   Serial.begin(9600); // begins testing serial connection - to test in Arduino IDE via Tools | Serial Monitor
  91.   setupBlueToothConnection();
  92. }
  93.  
  94.  
  95. void loop() {
  96.   if (turnONvalue==22){ // check to see if 22 is sent (ON button)
  97.     digitalWrite(led, HIGH);
  98.   } else if (turnONvalue==44){ // check to see if 44 is sent (OFF button)
  99.     digitalWrite(led, LOW);
  100.   }
  101.  
  102.   //check if 1 byte is sent  
  103.     if (blueToothSerial.available() == 1) {  // if one byte is sent via the ON or OFF buttons from the app
  104.       turnONvalue=blueToothSerial.read();
  105.     }
  106.     else {
  107.       //blueToothSerial.println("Invalid Data was received");
  108.       Serial.println("Invalid Data was received");
  109.       char a;
  110.       while( (a = blueToothSerial.read()) != -1){
  111.         Serial.println(a);
  112.       }
  113.     }
  114.      
  115.  
  116. }
Advertisement
Add Comment
Please, Sign In to add comment