How do I change the baud rate?

I hope my question does not sound silly. But I really have no idea on how do I change the baud rate.

I know that you set the baud rate by calling

Serial.begin(115200); //how do you change this in the loop?

First of all, I am using a BLUNO. It is basically an Uno + BLE.

I noticed that If I were to use a GSM shield, it only works if I set the Serial baud rate to 9600. However, I need a Serial baud rate of 115200 if I were to retrieve the RSSI of the BLE. As such, I do not know whether is it possible to change the baud rate in the loop?

Thank you in advance.

You can use "Serial.begin();" as many times as you need.

Just make sure that all transmissions have finished or you might lose data.

I can do that from setup(). But if I were to change it under the loop(), it won't work

void setup() {
   Serial.begin(9600);
   Serial.begin(115200);  //this works. But won't work if I move it under the loop
}

How do I check the baud rate before I change the baud rate by the way?

visualxl: I can do that from setup(). But if I were to change it under the loop(), it won't work

How do you know that?

visualxl: How do I check the baud rate before I change the baud rate by the way?

Your program set it...your program can also remember what it is.

visualxl: How do I check the baud rate before I change the baud rate by the way?

Brute force.

The traditional way - to unlock a bricked micro over serial for example, is just try each baud rate in return with a "set baudrate command" - whatever that means for your device. If the device has a known response to a command like "OK" you can just send the command at each baudrate and look for the "OK" to come back.

fungus:
How do you know that?

Because I could not get the RSSI displayed on the LCD if I were to set the baud rate again in the loop, which also means that the Serial is empty?

void getRSSI() {
  Serial.println("AT+RSSI=?"); //get the RSSI from the BLE
  delay(150);
  while(Serial.available() > 0) {
    char rssiString[MAXCHARS];  // RSSI string to received from BLE
    readSerial(rssiString);  //read serial and store it inside rssiString
    String responseString = "";
    int i = 1; //ignore negative
    for(i; i<4; i++)  //get RSSI from the array
      responseString += rssiString[i];
    rssi = abs(responseString.toInt());  //parse string to int
    lcd.setCursor(0,1);
    lcd.print(rssi);   //It never come here when it is 9600
  }
}

visualxl:

fungus: How do you know that?

Because I could not get the RSSI displayed on the LCD if I were to set the baud rate again in the loop, which also means that the Serial is empty?

Remember I said: "make sure that all transmissions have finished or you might lose data."

Enlighten me please.

Would appreciate if you can give some example(s).

I did try the below which I think its not the correct way.

void loop() {
   Serial.flush();
   Serial.delay(150);
  //Serial.end();
   Serial.begin(115200);
}

visualxl: I did try the below which I think its not the correct way.

void loop() {
   Serial.flush();
   Serial.delay(150);
  //Serial.end();
   Serial.begin(115200);
}

This code should work:

   Serial.flush();
   Serial.begin(115200);

If it doesn't work, the problem is somewhere else.

There is not much point changing the baud rate in an Arduino program unless the device it is talking to also changes its baud rate at the same time.

How is that happening?

...R

Yes there is.

My arduino can only get the RSSI of the BLE if it is 115200, whereby GSM cannot be setup on 115200.

if you have 2 serial device sending to one serial port will seldom work.
You need to stop the one device completely before you can listen to the other.
Their signals will interfere and corrupt the sent data.

Use a soft serial for the slower one

The GSM is already using a SoftwareSerial internally on Pin number 2 and 3. Otherwise, It won't be able to send the SMS right? :)

http://arduino.cc/en/Main/ArduinoGSMShield

But nevertheless, I still don't understand why do I need to set the Serial to 9600 instead of leaving it at 115200.

if you are using 2 ports you set the serial of one to 9600 and the other to 115200.

maybe time to post the whole code?

I only have 1 Serial port. GSM shield uses pin number 2 and 3.

If I were to uncommentt “setupGSM()”, the codes under “checkRange()”, that evaluates whether RSSI is 0 or not will always get executed. This also means that the Serial is empty as RSSI cannot be retrieved.

On the other hand, if I comment the setupGSM(), the LED will only blink if RSSI is 0. The reason I uses LED as an indicator is because I am gonna get lots of SMS if I uncomment “sendSMS()”.

#include <GSM.h> // Include the GSM library

/***********************************************RSSI VARIABLES*/
#define MAXCHARS 50
int rssi;
int rssiCounter = 0; // Don't play alarm unless counter has reached

/***********************************************GSM VARIABLES*/
#define PINNUMBER ""
//GSM gsmAccess;
GSM gsmAccess(true);
GSM_SMS sms;
char remoteNum[20] = "98997412";  // telephone number to send sms
char txtMsg[200] = "Your device is out of range."; //sms content
const int gsmLedPin = 10;

/***********************************************SWITCH VARIABLES*/
const int switchPin =  12;      // Switch Pin
const int ledPin = 11;
int switchState = 0;         // variable for reading the switch status
boolean bSwitch = false;  //The flag defaults to NOT HIGH

/*********************************************CODES BODY*/
void setup() {
  Serial.begin(9600); //CENTRAL is running 115200. Only Baud Rate of 11520 can get the RSSI values
  if(!Serial) {
    //proceed after Serial is ready only
  }
  //setupGSM();  //Set GSM
  switchSetup();  //Setup the flag. If high, can start checking the range
  Serial.flush();
  Serial.begin(115200);  //When GSM has been initialized, change back the baud rate
  if(!Serial) {
    //proceed after Serial is ready only
  }
  pinMode(gsmLedPin, OUTPUT);
  slaveConfiguration(); //BLE configuration for slave
  initialized();  //everything has been setup
  //lcdSetup();
}

void loop() {
  getRSSI();  //retrieve RSSI
  //printRSSI();  //print RSSI on LCD
  checkSwitch();  //If switch is HIGH, checkRange.
  checkRange();  //check RSSI range, only if flag is HIGH. It uses bSwitch
  
}

void getRSSI() {
  Serial.println("AT+RSSI=?");
  delay(150);
  while(Serial.available() > 0) {
    char rssiString[MAXCHARS];  // RSSI string to received from BLE
    readSerial(rssiString);  //read serial and store it inside rssiString
    String responseString = "";
    int i = 1; //ignore negative
    for(i; i<4; i++)  //get RSSI from the array
      responseString += rssiString[i];
    rssi = abs(responseString.toInt());  //parse string to int
  }
}

void slaveConfiguration() {
  Serial.print("+++");  //Enter AT command
  delay(250);
  Serial.println("AT+SETTING=DEFPERIPHERAL");
  delay(250);
  Serial.println("AT+ROLE=ROLE_PERIPHERAL");
  delay(150);
  Serial.println("AT+NAME=BabyClient");
}

/*
  Read input serial
 */
int readSerial(char result[]) {
  int i = 0;
  while(1) {
    while (Serial.available() > 0) {
      char inChar = Serial.read();
      if (inChar == '\n') {
        result[i] = '\0';
        Serial.flush();
        return 0;
      }
      if(inChar!='\r') {
        result[i] = inChar;
        i++;
      }
    }
  }
}

/*
 * Check range of the RSSI. If it is out of range:
 * - Activate GPS
 * - Activate GSM
 * - Send coordinates. If cannot get coordinates, send some string
 */
void checkRange() {
 
  if(bSwitch && (rssi==0)) {//If the switch is on, can send SMS if disconnected
      digitalWrite(gsmLedPin, LOW);
      delay(150);
      //Serial.flush();
      //Serial.begin(9600);
      //sendSMS();
      digitalWrite(gsmLedPin, HIGH);
  }
    //else
      //digitalWrite(gsmLedPin, HIGH);
}

/*
 * Switch codes
 */
void switchSetup() {
  pinMode(switchPin, INPUT);   
  pinMode(ledPin, OUTPUT);
}

void checkSwitch() {
  switchState = digitalRead(switchPin);  //Read switch
  //Serial.println(switchState);
  delay(150);
  if (switchState == HIGH) {     
    bSwitch = true;
    digitalWrite(ledPin, HIGH);  //turn LED on
  } 
  else {
    bSwitch = false;
    digitalWrite(ledPin, LOW); // turn LED off
  }
}

/*
 * GSM codes
 */
void setupGSM() {
  // connection state
  boolean notConnected = true;
  // Start GSM shield
  // If your SIM has PIN, pass it as a parameter of begin() in quotes
  while(notConnected) {
    if(gsmAccess.begin(PINNUMBER)==GSM_READY)
      notConnected = false;
    else {
      Serial.println("Not connected");
      delay(1000);
    }
  }
}

void sendSMS() {
  // send the message
  Serial.begin(9600);
  sms.beginSMS(remoteNum);
  sms.print(txtMsg);
  sms.endSMS();
  Serial.begin(115200);  //Baud rate needed to retrieve RSSI
}

/*
 *  Codes initialized indicator
 */
 
void initialized() {
  //When green LED is up, it means everything has been initialized
  pinMode(gsmLedPin, OUTPUT);
  digitalWrite(gsmLedPin, HIGH);
}

I only have 1 Serial port. GSM shield uses pin number 2 and 3.

That means you hardware serial port (pin 0 and 1 ) uses the BLE right?

The GSM shield uses pin 2,3

So you only need to set the baud rate once for the BLE (115200) and once for the GSM (9600)

I'll do a quick scan of your code (I'll be back ;)

I see that PINNUMBER is defined as an empty “” → I think a number should be in probably 2 or 3
#define PINNUMBER “”

Also your code has much comments that do not add value ==> remove it as it will only distract.
usage of better variable /function names makes most comments obsolete

//GSM gsmAccess; <<<<<<<<<<<<<<<<< no value as it just is an echo of the line beneath
GSM gsmAccess(true);

char remoteNum[20] = “98997412”; // telephone number to send sms
so why not
**char telephoneNumber[20] = “98997412”; **
no comments needed as the code becomes self explaining.

char txtMsg[200] = “Your device is out of range.”; //sms content <<<<<<<<<<<< ??
==>
char smsContent[200] = “…”;
or
chat textMessage[200] …

slaveConfiguration(); //BLE configuration for slave
==>
BLEslaveConfiguration();

etc

Yes you are right on that. Pin 0 and 1 is for the BLE.

So you only need to set the baud rate once for the BLE (115200) and once for the GSM (9600)

So, how do i do that ya especially for the GSM shield?

Quote So you only need to set the baud rate once for the BLE (115200) and once for the GSM (9600)

So, how do i do that ya especially for the GSM shield?

as I said

I see that PINNUMBER is defined as an empty "" -> I think a number should be in probably 2 or 3

define PINNUMBER ""

what GSM library do you use?

That is not right.

PINNUMBER = ""; //PIN on the SIM card if there is. Optional.

I am using this GSM library.

http://arduino.cc/en/Reference/GSM