How to grab responses from Serial Terminal?

I have some AT commands that I wanna write and store its response.

Say, I issue "AT+NAME=?"

If I were to key in that manually after changing to 'AT' mode, in the Serial Monitor, I will get a response from the above command like "WhateverName".

How do I get that response?

#include <SoftwareSerial.h>

SoftwareSerial bleSerial(0, 1); // RX, TX

void setup() {
    Serial.begin(115200);   //initial the Serial
    while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
    }
    bleSerial.begin(115200);
    Serial.write("Setting up!");
    getName();
}

/*
 * Called when data on serial is available
 */
void serialEvent(){
  Serial.println("Data is avalable for read!");
  Serial.println(bleSerial.read());
}

void getName() {
  int bSerialWritten =  bleSerial.write("AT+NAME=whyNoChange\r\n"); //This guy set the BLE name. 
  Serial.println(bSerialWritten);
  if(bleSerial.isListening()) {
    Serial.println("BLE Serial is listening!");
  }
}

void loop() {
    if(bleSerial.available()) { //For some reason, this guy is always empty.
        Serial.println("Data is available!");
        Serial.write(bleSerial.read());    //Read incoming serial data
    }
}

Can SoftwareSerial work at 115200 baud? Can you use a lower baud rate?

You don't seem to have any calls to serialEvent() and getName().

...R

  1. I think yes it can work at 115200 since my BLE name was successfully being set by the Uno.

  2. getName() is being called at the setup()

  3. I don't have to call serialEvent manually do I? From what I have read, it says that it will be automatically being called if data is available. But I guess there is no data, that is why it always empty, and I don't know why no data either.
    serialEvent() - Arduino Reference

You seem to be correct about serialEvent(). I've never used it.

However it is only triggered by hardwareSerial not by softwareSerial(). I think your code is waiting for input from softwareSerial().

...R

  1. Now for some reason, I am no longer able to set my BLE with the above commands. I don't know what went wrong.

  2. From the way you say, does that mean 'Serial' refers to hardware serial, whereby 'bleSerial' refers to softwareSerial?
    If that is the case, how do I grab the response from the AT commands?

Which Arduino are you using?

You could set SoftwareSerial up on pins 2 & 3, and use Serial. to communicate to the computer via the USB cable, and bleSerial. to communicate with your device.

If you want to interact with your device, i.e. enter commands in Serial Monitor, and then display the device's response, try a sketch called SerialRelay. There's a version in my signature link.

I am using Arduino Uno with Bluetooth (BLUNO). I believe it only has 2 serials (port 0 and 1).

I don't really understand what were you saying. However, I took a look at your codes.

If I understand you correctly, are you saying that my hardware serial (Serial) and SoftwareSerial (bleSerial 0 & 1) do not refer to the same thing? OMG!

No, I am saying that they do refer to the same thing, but you should only try to talk to one device per serial port! You seem to be trying to talk to two?

The response will come as a sequence of characters. The available() method of the software serial port object will return the number of characters that have been received and are available for your sketch to read. Your sketch would need to call available() repeatedly and read characters as they become available. It is up to you whether you buffer them until you have received the complete response, or process them individually. Note that the serial port is very slow relative to the Arduino's processing speed so there will be a significant delay between consecutive characters arriving.

dannable:
No, I am saying that they do refer to the same thing, but you should only try to talk to one device per serial port! You seem to be trying to talk to two?

Umm.. Ok.. So, I rewrote it again to just set the name of the device. However, may I kindly know why it is not setting the name anymore like how it did previously?

#include <SoftwareSerial.h>

SoftwareSerial bleSerial(0, 1); // RX, TX

void setup() {
  Serial.begin(115200);   //initial the Serial
  bleSerial.begin(115200);
  setBleName();
  Serial.println("Setting up!");
}

void loop() {
}

void setBleName() {
  bleSerial.write("AT+NAME=BABY\r");
  Serial.println("Name set!");
}
SoftwareSerial bleSerial(0, 1); // RX, TX

is connecting to the USB cable, and therefore through to your serial monitor.

Change to 2 & 3, and connect your device to those pins.

SoftwareSerial bleSerial(0, 1); // RX, TX

You can NOT do software serial on the hardware serial pins.

  Serial.begin(115200);   //initial the Serial

Especially not while also trying to do hardware serial on the same pins.

Get over it.

dannable:
is connecting to the USB cable, and therefore through to your serial monitor.

Change to 2 & 3, and connect your device to those pins.

What do you mean by connecting the device to those pin? The BLE is already on the chip.

PaulS:
You can NOT do software serial on the hardware serial pins.
Especially not while also trying to do hardware serial on the same pins.
Get over it.

Does that mean that I don't actually need the SoftwareSerial to send the AT commands?

Below is a working sample code for BLUNO. What it does is it only display whatever text is being sent from an app that connects to the BLE. So, the BLE here is set as peripheral.

I can set the BLE as central, and configure the BLE manually through the serial monitor. But I want to sent the AT commands through the arduino in this case.

void setup() {
    Serial.begin(115200);               //initial the Serial
}

void loop()
{
    if(Serial.available())
    {
        Serial.write(Serial.read());    //send what has been received
    }
}

It really helps to tell up front about hardware, especially when you're using a non-standard board.
I looked up the BLUNO and it appears that there is no USB to PC on it, that the radio uses Serial instead.

I see a switch to go between AT commands and Normal Serial and scratch my head over that.
That board has its own peculiarties. I'm sure the reasons are good but those have to be understood anyway.

GoForSmoke:
It really helps to tell up front about hardware, especially when you're using a non-standard board.
I looked up the BLUNO and it appears that there is no USB to PC on it, that the radio uses Serial instead.

I see a switch to go between AT commands and Normal Serial and scratch my head over that.
That board has its own peculiarties. I'm sure the reasons are good but those have to be understood anyway.

By default, the switch is set to 'NORM'. So, if you were to do Serial.println("Hello");. Then you bring up your serial monitor, you will just see a "Hello".

If you you were switch it to 'AT', you can straight away configure/access the BLE manually. like AT+NAME=?

For now, I just need to be able to configure & grab responses from the Arduino itself instead of manually switching it to 'AT'.

I tried to register at the dfRobot forum but have not been approved yet. Till then, bear with me for awhile ~ :slight_smile:

If you hadn't posted here then I wouldn't even know about these boards yet.

Below is a quick fix that I got it to work. You need to modify if you were to call the AT commands from the loop. The codes below were taken from one of my post DFRobot Tech Support Forum.

#include <LiquidCrystal.h>

// select the pins used on the LCD panel
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
char inData[] = "";
int index = 0;
String inString = "";
String inParse[] = "";
byte charReceived = 0;
String responseString = "";
int responseLength = 0;

void setup() {  
  Serial.begin(115200);
  Serial.println("AT+NAME=?");
  lcd.begin(16, 2);              // start the library
  lcd.setCursor(0,0);
  lcd.print("BLE Values: "); // print a simple message
}

void serialEvent() {
  while(Serial.available()) {
    lcd.setCursor(0,1);            // move to the begining of the second line
    char inChar = Serial.read();
    inData[index] = inChar;
    index++;
    inString += inChar;
    if (inChar == '\n') {
      index = 0;
      responseLength = strlen(inData)-2; //Get rid of CR & LF
      for(int i=0; i<responseLength; i++)
        responseString += inData[i];
      lcd.print(responseString);
    }
  }
}

void loop(){
}