I am working on the code below. Requires to compare the incoming message from serial with the expected response. please note that the echo mode is on, so i cannot just compare it directly, i have to somehow check with the string received if there is any expected response present.
There is a difference between String (capital S) and string (lower case s, null terminated character arrays). The str functions don't work on String but on string.
This is the code. What i am trying to do here is that i want to take incoming_string and compare it with the expected_response. If any part of the expected_response is present in the incoming_string, it returns TRUE.
#include <SoftwareSerial.h>
#define FONA_PWRKEY 6
#define FONA_RST 7
SoftwareSerial mySerial(10, 11); // RX, TX
void setup() {
pinMode(FONA_RST , OUTPUT);
digitalWrite(FONA_RST, HIGH);
pinMode(FONA_PWRKEY, OUTPUT);
digitalWrite(FONA_PWRKEY,LOW);
delay(100);
digitalWrite(FONA_PWRKEY, HIGH);
Serial.begin(9600);
Serial.println(F("FONA basic test"));
mySerial.begin(115200); // Default SIM7000 shield baud rate
Serial.println(F("Configuring to 9600 baud"));
mySerial.println("AT+IPR=9600"); // Set baud rate
delay(100); // Short pause to let the command run
mySerial.begin(9600);
Serial.println(F("FONA is OK"));
systeminit();
}
void loop() { // run over and over
}
int sendCommand(char[500] ATcommand, char[500] expected_response) {
delay(100);
String incoming_message;
int answer = 0;
String Trimmed_message;
mySerial.println(ATcommand);
do{
if (mySerial.available() > 0) {
incoming_message = mySerial.readString();
Serial.println(incoming_message);
}
Trimmed_message = strstr(incoming_message, expected_response);
if (Trimmed_message == expected_response) {
answer = 1;
}
}while (answer == 0);
Serial.println(incoming_message);
return answer;
}
void systeminit() {
sendCommand("AT+CNMP=38", "OK");
}