Hi there,
i am currently working on a project, that requires reading the rssi values from specific AP's so that i can later issue an HTTP GET request. For this reason i use an ESP8266 module connected through serial with Arduino Uno. Connection works well, module is responding to AT commands, but when i send the "AT+CWLAP" command problems start.
I use the serialEvent function to capture each line the "AT+CWLAP" command returns.
Serial.println("AT+CWLAP");
In order to get the whole line i use the serialEvent function :
void serialEvent() {
inputString = "";
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
stringComplete = true;
}
}
}
As far i understand the serialEvent function, it reads each byte and adds it to a string. When a newline character is read, the loop() function proceeds the string. It seems preety simple, but unfortunattely is not working for me.
In fact serialEvent returns rubbish, previous outpout etc like that, unitl it hangs :
AT+CWLAP
AT+CWDHCP=1,0
OK
AT+CWJAP="*******",AT+CWLAP
busy p...
AT+CWDHCP=1,0
OK
AT+CWJAP="******",AT+CWLAP
busy p...
AT+CWDHCP=1,0
OK
AT+CWJAP="******",AT+CWLAP
busy p...
ERROR
AT+CWDHCP=1,0
busy
p...
OK
Any ideas why is this happening?Am i missing something?
Thanks
Below is the test code :
#define ARRAY_SIZE 4
String inputString = ""; // a string to hold incoming data
String curAT; // current AT command
boolean stringComplete = false; // whether the string is complete
int ap[ARRAY_SIZE];
int i=0;
int rssi;
void setup() {
inputString.reserve(200);
Serial.begin(115200);
//testing
for(i=0;i<ARRAY_SIZE+1;i++){
ap[i] = 0;
}
Serial.println("AT+CWLAP");curAT="AT+CWLAP";//Serial.println(curAT);
delay(4000);
}
void loop() {
if ((stringComplete)) {
if (inputString.startsWith("+CWLAP:")) {
int firstComma = inputString.indexOf(',')+1;
if (inputString.substring(firstComma+1,firstComma+6) == "ESPAP") {
int secondComma = inputString.indexOf(',',firstComma+1)+1;
//load rssi to array
String rssi = inputString.substring(secondComma+1,secondComma+3);
ap[i]=rssi.toInt();
inputString ="";
rssi = "";
i+=1;
}
// clear the string:
inputString = "";
stringComplete = false;
}
else if ((inputString.startsWith("OK"))&& (curAT=="AT+CWLAP")){
//Serial.println(htmlGet());
inputString = "";
i=0;
} //else if
} //if (stringComplete)
}
void serialEvent() {
inputString = "";
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
stringComplete = true;
}
}
}