22
22
#include < ChallengerLTE.h>
23
23
24
24
Challenger2040LTEClass::Challenger2040LTEClass () {
25
- pinMode (PIN_SARA_ON, OUTPUT);
26
- digitalWrite (PIN_SARA_ON, LOW); // Output register must always be low
27
- pinMode (PIN_SARA_ON, INPUT_PULLUP);
25
+ pinMode (PIN_SARA_ON, OUTPUT);
26
+ digitalWrite (PIN_SARA_ON, LOW); // Output register must always be low
27
+ pinMode (PIN_SARA_ON, INPUT_PULLUP);
28
28
29
- pinMode (PIN_SARA_RST, INPUT_PULLUP); // Keep as input for now
29
+ pinMode (PIN_SARA_RST, INPUT_PULLUP); // Keep as input for now
30
30
31
- pinMode (PIN_SARA_PWR, OUTPUT);
32
- digitalWrite (PIN_SARA_PWR, LOW); // No power to SARA yet
33
- serialPortConfigured = false ;
31
+ pinMode (PIN_SARA_PWR, OUTPUT);
32
+ digitalWrite (PIN_SARA_PWR, LOW); // No power to SARA yet
33
+ serialPortConfigured = false ;
34
34
}
35
35
36
36
// Do a HW reset by applying a low pulse to the reset line for 1mSec
37
37
bool Challenger2040LTEClass::doPowerOn () {
38
- bool ret;
39
- digitalWrite (PIN_SARA_PWR, HIGH); // Make sure LDO is on
40
- delay (100 ); // let the power stabilize
41
- pinMode (PIN_SARA_ON, OUTPUT); // Pull power on control low
42
- delay (150 ); // For 150mS
43
- pinMode (PIN_SARA_ON, INPUT_PULLUP); // before releasing it again.
44
- delay (1000 ); // Now wait for 1 second
45
- SARA_SERIAL_PORT.begin (DEFAULT_SARA_BAUDRATE);
46
- serialPortConfigured = true ;
47
- ret = isAlive (); // Makie sure the modem is
48
- // up and running
49
-
50
- delay (250 ); // Allow for any extra characters
51
- // before flushing the input buffer
52
- while (SARA_SERIAL_PORT.available ()) SARA_SERIAL_PORT.read ();
53
-
54
- return ret;
38
+ bool ret;
39
+ digitalWrite (PIN_SARA_PWR, HIGH); // Make sure LDO is on
40
+ delay (100 ); // let the power stabilize
41
+ pinMode (PIN_SARA_ON, OUTPUT); // Pull power on control low
42
+ delay (150 ); // For 150mS
43
+ pinMode (PIN_SARA_ON, INPUT_PULLUP); // before releasing it again.
44
+ delay (1000 ); // Now wait for 1 second
45
+ SARA_SERIAL_PORT.begin (DEFAULT_SARA_BAUDRATE);
46
+ serialPortConfigured = true ;
47
+ ret = isAlive (); // Makie sure the modem is
48
+ // up and running
49
+
50
+ delay (250 ); // Allow for any extra characters
51
+ // before flushing the input buffer
52
+ while (SARA_SERIAL_PORT.available ()) {
53
+ SARA_SERIAL_PORT.read ();
54
+ }
55
+
56
+ return ret;
55
57
}
56
58
57
59
// Checks to see if the modem responds to the "AT" poll command.
58
60
bool Challenger2040LTEClass::isAlive (uint32_t timeout) {
59
- SARA_SERIAL_PORT.setTimeout (100 );
60
- SARA_SERIAL_PORT.println (F (" AT" ));
61
- String rdy = SARA_SERIAL_PORT.readStringUntil (' \n ' );
62
- while (!rdy.startsWith (F (" OK" )) && --timeout) {
61
+ SARA_SERIAL_PORT.setTimeout (100 );
63
62
SARA_SERIAL_PORT.println (F (" AT" ));
64
- rdy = SARA_SERIAL_PORT.readStringUntil (' \n ' );
65
- // Serial.println(rdy);
66
- }
67
- SARA_SERIAL_PORT.setTimeout (1000 ); // Restore serial timeout
68
- if (timeout)
69
- return true ;
70
- return false ;
63
+ String rdy = SARA_SERIAL_PORT.readStringUntil (' \n ' );
64
+ while (!rdy.startsWith (F (" OK" )) && --timeout) {
65
+ SARA_SERIAL_PORT.println (F (" AT" ));
66
+ rdy = SARA_SERIAL_PORT.readStringUntil (' \n ' );
67
+ // Serial.println(rdy);
68
+ }
69
+ SARA_SERIAL_PORT.setTimeout (1000 ); // Restore serial timeout
70
+ if (timeout) {
71
+ return true ;
72
+ }
73
+ return false ;
71
74
}
72
75
73
76
// Return the current MNO profile
74
77
// Returns -1 if the serial port is not yet setup or the number of the current
75
78
// MNO profile setting from the modem.
76
79
int Challenger2040LTEClass::getMNOProfile () {
77
- if (!serialPortConfigured)
78
- return -1 ;
79
- SARA_SERIAL_PORT.println (F (" AT+UMNOPROF?" ));
80
- String resp = getResponse ();
81
- return resp.substring (resp.indexOf (" +UMNOPROF: " ) + 11 ).toInt ();
80
+ if (!serialPortConfigured) {
81
+ return -1 ;
82
+ }
83
+ SARA_SERIAL_PORT.println (F (" AT+UMNOPROF?" ));
84
+ String resp = getResponse ();
85
+ return resp.substring (resp.indexOf (" +UMNOPROF: " ) + 11 ).toInt ();
82
86
}
83
87
84
88
// Set a new MNO profile
85
89
// Returns false if the serial port is not yet setup
86
90
bool Challenger2040LTEClass::setMNOProfile (int profile) {
87
- if (!serialPortConfigured)
88
- return false ;
89
- String cmd = " AT+UMNOPROF=" + String (profile) + " ,1" ;
90
- SARA_SERIAL_PORT.println (cmd);
91
-
92
- if (!getResponse ().endsWith (" OK" )) {
93
- return false ;
94
- }
95
- return true ;
91
+ if (!serialPortConfigured) {
92
+ return false ;
93
+ }
94
+ String cmd = " AT+UMNOPROF=" + String (profile) + " ,1" ;
95
+ SARA_SERIAL_PORT.println (cmd);
96
+
97
+ if (!getResponse ().endsWith (" OK" )) {
98
+ return false ;
99
+ }
100
+ return true ;
96
101
}
97
102
98
103
// Disable power save features
99
104
bool Challenger2040LTEClass::enablePS (bool enable) {
100
- if (!serialPortConfigured)
101
- return false ;
102
- if (enable)
103
- SARA_SERIAL_PORT.println (F (" AT+CPSMS=1" ));
104
- else
105
- SARA_SERIAL_PORT.println (F (" AT+CPSMS=0" ));
106
-
107
- if (!getResponse ().endsWith (" OK" )) {
108
- return false ;
109
- }
110
- return true ;
105
+ if (!serialPortConfigured) {
106
+ return false ;
107
+ }
108
+ if (enable) {
109
+ SARA_SERIAL_PORT.println (F (" AT+CPSMS=1" ));
110
+ } else {
111
+ SARA_SERIAL_PORT.println (F (" AT+CPSMS=0" ));
112
+ }
113
+
114
+ if (!getResponse ().endsWith (" OK" )) {
115
+ return false ;
116
+ }
117
+ return true ;
111
118
}
112
119
113
120
// Get a response from SARA
@@ -116,18 +123,19 @@ bool Challenger2040LTEClass::enablePS(bool enable) {
116
123
// from control characters and appended with a tab character as a separator.
117
124
//
118
125
String Challenger2040LTEClass::getResponse (int timeout) {
119
- SARA_SERIAL_PORT.setTimeout (2000 ); // allow for really slow responses
126
+ SARA_SERIAL_PORT.setTimeout (2000 ); // allow for really slow responses
120
127
121
- String resp = SARA_SERIAL_PORT.readStringUntil (' \n ' );
122
- resp.trim ();
123
- String acc = resp;
124
- while (resp.indexOf (" OK" ) == -1 && resp.indexOf (" ERROR" ) == -1 && --timeout) {
125
- resp = SARA_SERIAL_PORT.readStringUntil (' \n ' );
128
+ String resp = SARA_SERIAL_PORT.readStringUntil (' \n ' );
126
129
resp.trim ();
127
- if (resp.length ())
128
- acc += " \t " + resp;
129
- }
130
- return acc;
130
+ String acc = resp;
131
+ while (resp.indexOf (" OK" ) == -1 && resp.indexOf (" ERROR" ) == -1 && --timeout) {
132
+ resp = SARA_SERIAL_PORT.readStringUntil (' \n ' );
133
+ resp.trim ();
134
+ if (resp.length ()) {
135
+ acc += " \t " + resp;
136
+ }
137
+ }
138
+ return acc;
131
139
}
132
140
133
141
Challenger2040LTEClass Challenger2040LTE;
0 commit comments