Hello All,
This is my first post so request mods to assist me in the right category if I have chosen the incorrect one.
My Arduino UNO is connected with the bluetooth module -HC-05 via TX/RX pins. So I am using hardware serial. I also connected a constant 3.3V on the PIN 34 of HC-05 to enable AT mode.
When I use the Serial monitor and send AT commands, everything works perfectly. Command "AT" receives a response "OK" in the serial monitor.
Now for my project I need to use the response of the AT command within my code.
I have tried to print the response in a serial monitor as well on LCD but result is same.
I know I am missing something. Any help would be highly appreciated.
Thanks in advance.
My code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
void setup()
{
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("Connecting..");
lcd.setCursor(0, 1);
lcd.print(" ");
delay(1000);
Serial.begin(38400);
ConnectHC();
}
void ConnectHC()
{
char incoming;
char incomingstr;
char str[2];
byte i = 0;
lcd.clear();
lcd.setCursor(0, 0);
delay(1000);
Serial.print("AT+RESET\r\n"); //send to HC-05 RESET
while (Serial.available() > 0)
{
if (i < 2)
{
incoming = Serial.read();
str[i] = incoming; //put received char to str
i++;
str[i] = '\0';
lcd.print(str[i]); //printing the return value
}
}
}
void loop()
{
//Nothing here
}
The code you posted improperly does not even compile. So, any talk of it working is nonsense. Read the stickies at the top of the forum and post your real code properly.
Apart from anything else the function in the code snippet that you posted has at least one error in it that would stop it compiling so we need to see the actual code that you are having problems with.
Another tip. Before posting your code use Ctrl/T in the IDE to format it.
May be I am too tired to figure out this. I used that function in the Loop and it was working.
So I knew the delay was required.
And then I started looking into other Serial commands and found out this wonderful - SerialreadString()
With Serial.readString(), I dont even need to have a while loop to get all the response.
Now its working
Code, if it helps others
String incoming;
Serial.print("AT"); //Issue AT command to HC-05
Serial.print("\r\n"); //New line
delay(1000); //This is required to wait for the serial activity
incoming = Serial.readStringUntil('\n\r'); //Reads entire response as a String
lcd.print(incoming); //Finally prints the String