Is my code still erroneous on those concepts??
Yes, it is. Look at the functions called in loop.
void loop()
{
Serial.write("===");
delay(100);
readprocess();
delay(2000);
Serial.write("LLL");
delay(100);
readprocess();
delay(2000);
Serial.write("\r\nAT+INQ\r\n");
delay(1000);
readprocess();
delay(30000);
readatinq();
delay(2000);
wdt_enable(WDTO_15MS);
wdt_reset();
}
You are still using Serial.write() to send ASCII data, even though you've been advised that that is not the correct function to use.
You write "===" to the Serial port, then call readprocess() to get whatever response arrives within the 1/10th second delay.
void readprocess()
{
while(Serial.available() > 0)
{
char inChar = Serial.read();
if(inChar == SOP)
{
index = 0;
inData[index] = '\0';
started = true;
ended = false;
}
else if(inChar == EOP)
{
ended = true;
break;
}
else
{
if(index < 199)
{
inData[index] = inChar;
Serial.print(inData[index]);
index++;
inData[index] = '\0';
}
}
}
Serial.println('\n');
// We are here either because all pending serial
// data has been read OR because an end of
// packet marker arrived. Which is it?
if(started && ended)
{
// The end of packet marker arrived. Process the packet
// Reset for the next packet
started = false;
ended = false;
index = 0;
inData[index] = '\0';
}
}
Data gets collected in an array. There is an area where processing should occur, but you have elected to do no processing in that area.
After the processing is complete, the data that was processed is discarded, and the code resets for the next time it is called.
Since no processing actually occurred, that data is simply discarded.
Notice, also that this code is reading from, and writing to the same port. Since that appears to be the one connected to the serial monitor, it seems quite unlikely that this code will receive any data from your phone. So, calling it was a waste of time.
So, now you diddle around some more, then write (not print) "LLL" to the serial monitor, diddle around some more, and then try to read whatever was sent from the serial monitor, discarding the data again without doing anything with it.
You diddle around some more, and then write (not print) "\r\nAT+INQ\r\n" to the serial monitor, diddle around some more, and then try to read whatever was sent from the serial monitor, discarding the data again without doing anything with it.
Finally, after a whole bunch of diddling around, you call readatinq().
void readatinq()
{
while(Serial.available() > 0)
{
char inChar = Serial.read();
if(inChar == SOP)
{
Serial.print("SOP detected");
index = 0;
inData[index] = '\0';
started = true;
ended = false;
}
else if(inChar == EOP)
{ Serial.print("EOP detected");
count=count+1;
if(count>=4)
{
ended = true;
break;
}}
else
{
if(index < 199)
{
inData[index] = inChar;
Serial.print(inData[index]);
index++;
inData[index] = '\0';
}
}
}
Serial.println('\n');
// We are here either because all pending serial
// data has been read OR because an end of
// packet marker arrived. Which is it?
if(started && ended)
{
// The end of packet marker arrived. Process the packet
// Reset for the next packet
started = false;
ended = false;
index = 0;
inData[index] = '\0';
}
}
Here again you read incoming data from the serial monitor, discarding the data without processing it. In this case, though, you expect one start of packet marker, and FOUR end of packet markers.
Now, I'm confused about several things. You have two very similar blocks of code - one that reasonably expects a single end of packet marker to match the start of packet marker sent. In this code, you discard with using any data received. Why?
The other block of code expects one start of packet marker and 4 end of packet markers. Since the phone is going to send one start of packet marker and one end of packet marker, why are you expecting 4 end of packet markers? In this code, you discard with using any data received. Why?
Now, the next thing that confuses me is whether the serial monitor or the phone is on the other end of the serial port. You can't have both of them there without confusing the hell out of one or the other. So, what is on the end of the serial port?
Now, for the serial monitor issue. Turn off the auto scrolling. When there is data present in the serial monitor, drag the mouse across an area of the monitor. When the characters highlight, if they do, press ctrl-c. Open notepad, and press ctrl-v. What happens? It certainly appears as though you are running windows, so, you should be able to highlight text in any text-based window and copy that text, and paste it anywhere else.
And, finally, why are you not printing some really simple diagnostic stuff, like:
void readatinq()
{
Serial.println("readatinq ==>");
// some code here
Serial.println("readatinq <==");
}
If you were to do so, we'd KNOW what code was being called to read what data, don't you think?