Go Down

Topic: How do I change the baud rate? (Read 6419 times) previous topic - next topic

visualxl

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

Code: [Select]

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.

fungus

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

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

visualxl

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

Code: [Select]


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?

fungus


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?


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.

Advanced Arduino

KeithRB



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.

visualxl

#5
May 29, 2014, 02:48 am Last Edit: May 29, 2014, 03:59 am by visualxl Reason: 1

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?

Code: [Select]

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
  }
}



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."
Advanced Arduino

visualxl

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.

Code: [Select]

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

fungus


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

Code: [Select]

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



This code should work:

Code: [Select]

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


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


Advanced Arduino

Robin2

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
Two or three hours spent thinking and reading documentation solves most programming problems.

visualxl

Yes there is.

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

robtillaart

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
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

visualxl

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.

robtillaart

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?
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

visualxl

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()".

Code: [Select]

#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);
}

Go Up