I have an hc-06(using arduino uno) paired with an android phone. I can send data from the phone and the arduino board will print it. For some reasons after a period of time, instead of printing what I'm sending, it prints the code below. I have no idea why this is happening, or how to solve it.
#include <SoftwareSerial.h>
SoftwareSerial bluetooth(2, 4); // RX, TX
String inputString = "";
String inputString2 = "";
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
// while (!Serial) {
// ; // wait for serial port to connect. Needed for Leonardo only
// }
// set the data rate for the SoftwareSerial port
bluetooth.begin(9600);
bluetooth.println("Hello, world?");
}
void loop() // run over and over
{
if(bluetooth.available()){
while (bluetooth.available()) {
// get the new byte:
char inChar = (char)bluetooth.read();
//Serial.write(inChar);
// add it to the inputString:
inputString += inChar;
inputString2 = inputString;
}}
if(inputString2.substring(0,3).toInt() == 999){
bluetooth.println("TEST9");
inputString = "";}
else if(inputString2.substring(0,3).toInt() == 777){
bluetooth.println("TEST7");
inputString = "";
}
else if(inputString2.substring(0,3).toInt() == 666){
bluetooth.println("TEST6");
inputString = "";
}
Serial.println(inputString+" "+inputString2);
}
After sending some code a couple of times, it will start displaying random characters instead of the code. I tried using some delay too, but not much .
Can you show us an example of what it looks like when it works properly?
I wonder if there is a memory problem because you are using Strings (large S) rather than strings (small s). Strings don't always work reliably on the Arduino because of its limited memory.
I reckon Robin2 is probably right but I also think you should elaborate on what you are trying to do. What is the objective? Is there a real one, or is it just some intellectual exercise? Why are you sending data from Android to Arduino? "Normal" traffic is the other way.
I raise this because the is a raft of information on data collecting from Arduino and commands from Android. Below is an example of both
//Daily files can be read via bluetooth by sending MMDD
#include "Wire.h" // MUST HAVE lib for LCD disp, SD card
#include <SD.h>
#include <SPI.h> // SD
#include <string.h> //Date As Filename
#define DS1307_ADDRESS 0x68
char filename[13],charBuf [13],strYr[4];
File myFile, dumpFile;
int second, minute, hour, weekDay, monthDay, month, year;
const int chipSelect = 4;
String readString;
String stringFive, stringSix;
void setup() {
Wire.begin();
Serial.begin(115200);
Serial.println("Bare test");
Serial.println("Init SD CARD");
// make sure that the default chip select pin 53 is set to
// output, even if you don't use it:
pinMode(10, OUTPUT);//Uno 10, MEGA 53
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect))
{
Serial.println("Card failed");
// don't do anything more:011
return;
}
Serial.println("CARD OK");
delay(2000);
GetClock();
Serial.println("today's date for filename");
Serial.print(year);
Serial.print("/");
Serial.print(month);
Serial.print("/");
Serial.println(monthDay);
getFileName();
Serial.println("active filename");
Serial.println(filename);
delay(2000);
}
void loop() {
GetClock();
while (Serial.available())
{
delay(3);
char c = Serial.read();
readString += c;
}// end while
if (readString.length() >0)
{
getDump();
readString="";
} // end if
}
byte bcdToDec(byte val) {
// Convert binary coded decimal to normal decimal bers
return ( (val/16*10) + (val%16) );
}
void getFileName(){
sprintf(filename, "%04u%02u%02u.csv", year, month, monthDay);
}
void GetClock(){
// Reset the register pointer
Wire.beginTransmission(DS1307_ADDRESS);
byte zero = 0x00;
Wire.write(zero);
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDRESS, 7);
second = bcdToDec(Wire.read());
minute = bcdToDec(Wire.read());
hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
monthDay = bcdToDec(Wire.read());
month = bcdToDec(Wire.read());
year = bcdToDec(Wire.read());
year = year +2000;
}
void getDump() {
dtostrf(year,4, 0, strYr);
stringSix = strYr + readString + ".csv";
stringSix.toCharArray(charBuf, 15);
File dumpFile = SD.open(charBuf);
if (dumpFile)
{
Serial.println("DUMP FROM ");
Serial.println(charBuf);
delay(2000);
while (dumpFile.available())
{
Serial.write(dumpFile.read());
}
dumpFile.close();
}
else {
Serial.println("error opening file");
}
}
I started with the idea of checking the connection. So my android is sending some data which starts the process, and after that the arduino is sending data back. When this process stops, the board should print "Connection dropped" in the console. My purpose is to check when the bluetooth is no longer connected and to make this communication 2 ways(it's already 2 ways, but it's not working properly all the time). You asked me how it looks when is working properly: The arduino prints in the console some numbers(the numbers it has received), and sends back some strings which are displayed in a textView. For some reason after some time of sending data to the arduino board, those characters appear, it's like the encoder, or what is happening in there is not working properly. I tried to change the baud rate to 115200, but that is not working properly at all, even if I input commands in the AT, it won't convert them to actual letters.
I'm not home at the moment. I'll try them as soon as possible. As about what I'm printing is: "999214127555" "777234157234" and some other numbers that look like this sent like a string.
If you have any other idea of checking if the bluetooth has disconnected please share it. I don't need the arduino to send data that much,I mean it would be nice to work properly, but I just want the board to print "Connection Lost" as soon as it is no longer connected.
I have no idea what you mean by use of strings. And about your code, I haven't tried it. Which part do you want me to try ? What's wrong with mine to start with ..
iDaniel19:
What's wrong with mine to start with ..
I thought you said it doesn't work the way you want ?
I thought you were looking for suggestions to help you get working code.
My code uses strings (small s). A string (small s) is a char array terminated with a '\0' character. They work much better in the limited memory of the Arduino. Strings (big S) are more convenient but are not suited to limited memory.