Skip to content

Instantly share code, notes, and snippets.

@oliexe
Created January 9, 2020 08:00
Show Gist options
  • Save oliexe/6741eb10763b5b8eb3e53167c7df6563 to your computer and use it in GitHub Desktop.
Save oliexe/6741eb10763b5b8eb3e53167c7df6563 to your computer and use it in GitHub Desktop.
AT Command Sener/Parser for ESP8266
bool modem_command(String command, String expected_result_excerpt , unsigned int timeout){
String response;
if (DEBUG) Serial.print("[MODEM] " + command);
//Send the AT command
mySerial.println(command);
while (mySerial.available() == 0); // wait for first char
//Wait and process repsonse
unsigned long lastRead = millis(); // last time a char was available
while (millis() - lastRead < timeout){
while (mySerial.available()){
response = mySerial.readString();
//Serial.println(response);
lastRead = millis(); // update the lastRead timestamp
}}
//Check if response was acceptable
if (response.indexOf(expected_result_excerpt) > 0) {
if(DEBUG) Serial.println(" [OK]");
return true;
}
else {
if(DEBUG) Serial.println(" [FAIL]");
return false;
}
}
void modem_command(String command, unsigned int timeout) {
String response;
if (DEBUG) Serial.println("[MODEM] " + command);
//Send the AT command
mySerial.println(command);
while (mySerial.available() == 0); // wait for first char
//Wait and process repsonse
unsigned long lastRead = millis(); // last time a char was available
while (millis() - lastRead < timeout) {
while (mySerial.available()) {
response = mySerial.readString();
//Serial.println(response);
lastRead = millis(); // update the lastRead timestamp
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment