-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserial_modem_test.ino
117 lines (97 loc) · 2.74 KB
/
serial_modem_test.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <Arduino.h>
#define MEGA_BAUD_RATE (9600)
#define GSM_BAUD_RATE (9600)
#define HORIZONTAL_LINE ("================================")
#define AT_COMMAND_TIMEOUT (2000)
char temp;
int8_t totalTests = 2;
int8_t countTests = 0;
int8_t answer = 0;
void setup()
{
Serial.begin(MEGA_BAUD_RATE);
Serial.println(HORIZONTAL_LINE);
Serial.println(F(" GSM Modem Test "));
Serial.println(HORIZONTAL_LINE);
Serial.println(HORIZONTAL_LINE);
Serial.println(F("| Type \'t\' to start the test |"));
Serial.println(HORIZONTAL_LINE);
Serial1.begin(GSM_BAUD_RATE);
}
void loop()
{
if(Serial.read() == 't')
{
countTests = 0;
while(countTests < totalTests)
{
countTests = 0;
Serial.print(F("\n***** Test: "));
Serial.print(F("Send \'AT\'"));
Serial.println(F(" *****"));
if(sendATcommand("AT", "OK", AT_COMMAND_TIMEOUT))
{
Serial.println(F("Response: OK"));
countTests++;
}
else
{
Serial.println(F("Response: No response"));
Serial.println(F("Error: Test failed!"));
}
Serial.print(F("\n***** Test: "));
Serial.print(F("Send \'AT+CREG?\'"));
Serial.println(F(" *****"));
if(sendATcommand("AT+CREG?", "+CREG: 0,1", AT_COMMAND_TIMEOUT))
{
Serial.println(F("Response: +CREG: 0,1"));
Serial.println(F("Info: Registered on the home network"));
countTests++;
}
else
{
Serial.println(F("Response: Unexpected response"));
Serial.println(F("Info: Not registered to a network"));
}
while(Serial1.available() > 0)
{
temp = Serial1.read();
Serial.write(temp);
}
delay(1000);
}
Serial.println(HORIZONTAL_LINE);
Serial.println("| Test has ended |");
Serial.println(HORIZONTAL_LINE);
}
}
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); // Initialize the string
delay(100);
while( Serial1.available() > 0) Serial1.read(); // Clean the input buffer
Serial1.print(ATcommand); // Send the AT command
Serial1.write(13); // Send carriage return char
x = 0;
previous = millis();
// this loop waits for the answer
do{
if(Serial1.available() != 0){
response[x] = Serial1.read();
x++;
// check if the desired answer is in the response of the module
if (strstr(response, expected_answer) != NULL)
{
answer = 1;
}
}
// Serial.println(response);
// Waits for the answer with time out
}
while((answer == 0) && ((millis() - previous) < timeout));
return answer;
}