by jamie allen, Feb 2007

This code shows an implementation of a few Arduino-to-PromiESD bluetooth module AT commands. The Promi Datasheet lists a multitude of functions for this serial to bluetooth converter here. The code example below allows a simple subset of these AT commands, such as factory reset, serial baud rate setting, etc. Check the datasheet for a full description of the AT command functions.



/*
The beginnings of a serial library for thePromis ESD02 Bluetooth/Serial Module

Many configurable options on the chip are available via it's AT command interface. 
This code excerpt below allows you to query and set a few of these - all of which could be 
triggered via hardware switches, sensors, etc. 

Thanks to Tigoe for the serial string library - whew!
j.allen, 2007

*/

#include <TextString.h>

int ledPin = 13;   // select the pin for the LED
int val = 0;       // variable to store the data from the serial port
int i = 0;
char serInString3Byte[3];
char incomingByte;
char serTest[2];

boolean test = false;
boolean done = false;
boolean doneQ = false;

TextString inString = TextString(50);  // allocate a new textString
TextString returnString = TextString(100);  // allocate a new textString
char AT_hello[3] = "AT";
char AT_inquiry[10] = "AT+BTINQ?";  //note that the predefined inquiry session lasts 30s
char AT_BTinfo[11] = "AT+BTINFO?";
char AT_changeName[50] = "AT+BTNAME=\"jamieESD01\"";
char AT_BTscan[50] = "AT+BTSCAN";
char AT_factoryReset[8] = "AT&F";
char AT_UARTconfig9600[26]="AT+UARTCONFIG,9600,N,1";
char AT_UARTconfig115200[26]="AT+UARTCONFIG,115200,N,1";

char serbyte;

//Serial.print("AT+BTNAME=”jamieESD01”");
//Serial.print("AT+BTSCAN");

void setup() {
  pinMode(ledPin,OUTPUT);   // declare the LED's pin as output
  Serial.begin(9600);        // connect to the serial port
  //Serial.println("hi there");
  //Serial.println(inString.version());
}

void loop () {

//testSerial();    

/*
   while(done == false){ 
   sendCommand(AT_factoryReset,1500); 
   } 
   */

/*
   while(done == false){ 
   sendCommand(AT_UARTconfig9600,1500); 
   } 
*/   

/*
   while(done == false){ 
   sendCommand(AT_BTscan,1500); 
   } 
*/


/*
   while(done == false){ 
   sendCommand(AT_hello,1500); 
   } 
*/

/*
   while(done == false){ 
   sendCommand(AT_changeName,1500); 
   } 
*/


  while(doneQ == false)
  {
  sendQuery(AT_inquiry, 3100);   
  }


}

void sendCommand(char* commander, long timing)
{
   while(done == false)
   {
      Serial.print(commander);
      Serial.print(13,BYTE);  //need to do this for the PROMI - it doesn't want carriage & return, just carriage
      delay(timing);  //pause for response

      if (Serial.available() > 0) {
        char returnChar = Serial.read();
        while (returnChar != 10) {
          returnString.setCharAt(returnString.length(), returnChar);
          returnChar = Serial.read();
        }

        if (returnString.contains("OK"))
        {
          digitalWrite(ledPin, HIGH);
          blinker(3, ledPin, 200);
          //Serial.println(returnString.getArray());
          done = true;
        } else {
          digitalWrite(ledPin, LOW);
          done = false;
        }
       returnString.clear();
       //Serial.println();
      } 
   }
}

void sendQuery(char* commander, long timing)
{      
      Serial.print(commander);
      Serial.print(13,BYTE);
      delay(50);

      //just get everything
     while (Serial.available() > 0) {
        char returnChar = Serial.read();
        returnString.setCharAt(returnString.length(), returnChar);
     } 

    //this is a really stupid way of testing to see if we got a response
    //really should parse this properly
    if (returnString.length() > 20)
    {
    doneQ = true;
    blinker(5, ledPin, 200);
    }

    //parse the return string here

    Serial.println(returnString.getArray());

    delay(timing);
    returnString.clear();
    Serial.print("AT+BTCANCEL");
    Serial.print(13,BYTE);
    delay(1000);
}

//the requisite LED blinker
void blinker(int numberOfTimes, int pin, int timing)
{
  int k;
  for(k=0; k<numberOfTimes; k++)
  {
  digitalWrite(pin, LOW);
  delay(timing);
  digitalWrite(pin, HIGH);
  delay(timing);  
  digitalWrite(pin, LOW);
  }
}

//Simple tester for serial values
void testSerial()
{
  // read the serial port
  serbyte = Serial.read();
  // if the input is < 0 there is no data.
  if (serbyte  != -1) {
    val = serbyte;
    //echo the values back out...
    Serial.print(val);
  }

  // if the stored value is 'H' turn the LED on
  if (val == 'H') {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }
  delay(1);
}