Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int8_t sendATcommand(char* ATcommand, char* expected_answer, unsigned int timeout)
- {
- uint8_t x = 0, answer = 0;
- char response[100];
- unsigned long previous;
- memset(response, '\0', 100); // Initialice the string
- delay(100);
- while( Serial2.available() > 0) Serial2.read(); // Clean the input buffer
- Serial2.println(ATcommand); // Send the AT command
- x = 0;
- previous = millis();
- // this loop waits for the answer
- do
- {
- // if there are data in the UART input buffer, reads it and checks for the asnwer
- if(Serial2.available() != 0)
- {
- response[x] = Serial2.read();
- if (x < sizeof(response) - 1) x++;
- // check if the desired answer is in the response of the module
- if (strstr(response, expected_answer) != NULL)
- {
- answer = 1;
- }
- }
- // Waits for the asnwer with time out
- }
- while((answer == 0) && ((millis() - previous) < timeout));
- Serial.println(response);
- return answer;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement