30
30
// library. Below creates a SoftwareSerial object on the standard LTE
31
31
// Shield RX/TX pins:
32
32
// Note: if you're using an Arduino board with a dedicated hardware
33
- // serial port, comment out the line below. (Also see note in setup. )
33
+ // serial port, comment out the line below. (Also see note in setup)
34
34
SoftwareSerial lteSerial (8 , 9 );
35
35
36
36
// Create a LTE_Shield object to be used throughout the sketch:
37
37
LTE_Shield lte;
38
38
39
+ // To support multiple architectures, serial ports are abstracted here.
40
+ // By default, they'll support AVR's like the Arduino Uno and Redboard
41
+ // For example, on a SAMD21 board SerialMonitor can be changed to SerialUSB
42
+ // and LTEShieldSerial can be set to Serial1 (hardware serial port on 0/1)
43
+ #define SerialMonitor Serial
44
+ #define LTEShieldSerial lteSerial
45
+
39
46
// Network operator can be set to either:
40
47
// MNO_SW_DEFAULT -- DEFAULT
41
48
// MNO_ATT -- AT&T
@@ -64,86 +71,92 @@ void setup() {
64
71
String currentOperator = " " ;
65
72
bool newConnection = true ;
66
73
67
- Serial.begin (9600 );
74
+ SerialMonitor.begin (9600 );
75
+ while (!SerialMonitor) ; // For boards with built-in USB
68
76
69
- Serial.println (F (" Initializing the LTE Shield..." ));
70
- Serial.println (F (" ...this may take ~25 seconds if the shield is off." ));
71
- Serial.println (F (" ...it may take ~5 seconds if it just turned on." ));
77
+ SerialMonitor.println (F (" Initializing the LTE Shield..." ));
78
+ SerialMonitor.println (F (" ...this may take ~25 seconds if the shield is off." ));
79
+ SerialMonitor.println (F (" ...it may take ~5 seconds if it just turned on." ));
80
+
72
81
// Call lte.begin and pass it your Serial/SoftwareSerial object to
73
82
// communicate with the LTE Shield.
74
83
// Note: If you're using an Arduino with a dedicated hardware serial
75
- // poert, you may instead slide "Serial" into this begin call.
76
- if ( lte.begin (lteSerial, 9600 ) ) {
77
- Serial.println (F (" LTE Shield connected!\r\n " ));
84
+ // port, you may instead slide "Serial" into this begin call.
85
+ if ( lte.begin (LTEShieldSerial, 9600 ) ) {
86
+ SerialMonitor.println (F (" LTE Shield connected!\r\n " ));
87
+ } else {
88
+ SerialMonitor.println (" Unable to initialize the shield." );
89
+ while (1 ) ;
78
90
}
79
91
80
92
// First check to see if we're already connected to an operator:
81
93
if (lte.getOperator (¤tOperator) == LTE_SHIELD_SUCCESS) {
82
- Serial .print (F (" Already connected to: " ));
83
- Serial .println (currentOperator);
94
+ SerialMonitor .print (F (" Already connected to: " ));
95
+ SerialMonitor .println (currentOperator);
84
96
// If already connected provide the option to type y to connect to new operator
85
- Serial .println (F (" Press y to connect to a new operator, or any other key to continue.\r\n " ));
86
- while (!Serial .available ()) ;
87
- if (Serial .read () != ' y' ) {
97
+ SerialMonitor .println (F (" Press y to connect to a new operator, or any other key to continue.\r\n " ));
98
+ while (!SerialMonitor .available ()) ;
99
+ if (SerialMonitor .read () != ' y' ) {
88
100
newConnection = false ;
89
101
}
102
+ while (SerialMonitor.available ()) SerialMonitor.read ();
90
103
}
91
104
92
105
if (newConnection) {
93
106
// Set MNO to either Verizon, T-Mobile, AT&T, Telstra, etc.
94
107
// This will narrow the operator options during our scan later
95
- Serial .println (F (" Setting mobile-network operator" ));
108
+ SerialMonitor .println (F (" Setting mobile-network operator" ));
96
109
if (lte.setNetwork (MOBILE_NETWORK_OPERATOR)) {
97
- Serial .print (F (" Set mobile network operator to " ));
98
- Serial .println (MOBILE_NETWORK_STRINGS[MOBILE_NETWORK_OPERATOR] + " \r\n " );
110
+ SerialMonitor .print (F (" Set mobile network operator to " ));
111
+ SerialMonitor .println (MOBILE_NETWORK_STRINGS[MOBILE_NETWORK_OPERATOR] + " \r\n " );
99
112
} else {
100
- Serial .println (F (" Error setting MNO. Try cycling power to the shield/Arduino." ));
113
+ SerialMonitor .println (F (" Error setting MNO. Try cycling power to the shield/Arduino." ));
101
114
while (1 ) ;
102
115
}
103
116
104
117
// Set the APN -- Access Point Name -- e.g. "hologram"
105
- Serial .println (F (" Setting APN..." ));
118
+ SerialMonitor .println (F (" Setting APN..." ));
106
119
if (lte.setAPN (APN) == LTE_SHIELD_SUCCESS) {
107
- Serial .println (F (" APN successfully set.\r\n " ));
120
+ SerialMonitor .println (F (" APN successfully set.\r\n " ));
108
121
} else {
109
- Serial .println (F (" Error setting APN. Try cycling power to the shield/Arduino." ));
122
+ SerialMonitor .println (F (" Error setting APN. Try cycling power to the shield/Arduino." ));
110
123
while (1 ) ;
111
124
}
112
125
113
126
// Wait for user to press button before initiating network scan.
114
- Serial .println (F (" Press any key scan for networks.." ));
127
+ SerialMonitor .println (F (" Press any key scan for networks.." ));
115
128
serialWait ();
116
129
117
- Serial .println (F (" Scanning for operators...this may take up to 3 minutes\r\n " ));
130
+ SerialMonitor .println (F (" Scanning for operators...this may take up to 3 minutes\r\n " ));
118
131
// lte.getOperators takes in a operator_stats struct pointer and max number of
119
132
// structs to scan for, then fills up those objects with operator names and numbers
120
133
opsAvailable = lte.getOperators (ops, MAX_OPERATORS); // This will block for up to 3 minutes
121
134
122
135
if (opsAvailable > 0 ) {
123
136
// Pretty-print operators we found:
124
- Serial .println (" Found " + String (opsAvailable) + " operators:" );
137
+ SerialMonitor .println (" Found " + String (opsAvailable) + " operators:" );
125
138
printOperators (ops, opsAvailable);
126
139
127
140
// Wait until the user presses a key to initiate an operator connection
128
- Serial .println (" Press 1-" + String (opsAvailable) + " to select an operator." );
141
+ SerialMonitor .println (" Press 1-" + String (opsAvailable) + " to select an operator." );
129
142
char c = 0 ;
130
143
bool selected = false ;
131
144
while (!selected) {
132
- while (!Serial .available ()) ;
133
- c = Serial .read ();
145
+ while (!SerialMonitor .available ()) ;
146
+ c = SerialMonitor .read ();
134
147
int selection = c - ' 0' ;
135
148
if ((selection >= 1 ) && (selection <= opsAvailable)) {
136
149
selected = true ;
137
- Serial .println (" Connecting to option " + String (selection));
150
+ SerialMonitor .println (" Connecting to option " + String (selection));
138
151
if (lte.registerOperator (ops[selection - 1 ]) == LTE_SHIELD_SUCCESS) {
139
- Serial .println (" Network " + ops[selection - 1 ].longOp + " registered\r\n " );
152
+ SerialMonitor .println (" Network " + ops[selection - 1 ].longOp + " registered\r\n " );
140
153
} else {
141
- Serial .println (F (" Error connecting to operator. Reset and try again, or try another network." ));
154
+ SerialMonitor .println (F (" Error connecting to operator. Reset and try again, or try another network." ));
142
155
}
143
156
}
144
157
}
145
158
} else {
146
- Serial .println (F (" Did not find an operator. Double-check SIM and antenna, reset and try again, or try another network." ));
159
+ SerialMonitor .println (F (" Did not find an operator. Double-check SIM and antenna, reset and try again, or try another network." ));
147
160
while (1 ) ;
148
161
}
149
162
}
@@ -156,11 +169,11 @@ void loop() {
156
169
// Loop won't do much besides provide a debugging interface.
157
170
// Pass serial data from Arduino to shield and vice-versa
158
171
#ifdef DEBUG_PASSTHROUGH_ENABLED
159
- if (Serial .available ()) {
160
- lteSerial .write ((char ) Serial .read ());
172
+ if (LTEShieldSerial .available ()) {
173
+ SerialMonitor .write ((char ) LTEShieldSerial .read ());
161
174
}
162
- if (lteSerial .available ()) {
163
- Serial .write ((char ) lteSerial .read ());
175
+ if (SerialMonitor .available ()) {
176
+ LTEShieldSerial .write ((char ) SerialMonitor .read ());
164
177
}
165
178
#endif
166
179
}
@@ -170,48 +183,48 @@ void printInfo(void) {
170
183
IPAddress ip (0 , 0 , 0 , 0 );
171
184
String currentOperator = " " ;
172
185
173
- Serial .println (F (" Connection info:" ));
186
+ SerialMonitor .println (F (" Connection info:" ));
174
187
// APN Connection info: APN name and IP
175
188
if (lte.getAPN (¤tApn, &ip) == LTE_SHIELD_SUCCESS) {
176
- Serial .println (" APN: " + String (currentApn));
177
- Serial .print (" IP: " );
178
- Serial .println (ip);
189
+ SerialMonitor .println (" APN: " + String (currentApn));
190
+ SerialMonitor .print (" IP: " );
191
+ SerialMonitor .println (ip);
179
192
}
180
193
181
194
// Operator name or number
182
195
if (lte.getOperator (¤tOperator) == LTE_SHIELD_SUCCESS) {
183
- Serial .print (F (" Operator: " ));
184
- Serial .println (currentOperator);
196
+ SerialMonitor .print (F (" Operator: " ));
197
+ SerialMonitor .println (currentOperator);
185
198
}
186
199
187
200
// Received signal strength
188
- Serial .println (" RSSI: " + String (lte.rssi ()));
189
- Serial .println ();
201
+ SerialMonitor .println (" RSSI: " + String (lte.rssi ()));
202
+ SerialMonitor .println ();
190
203
}
191
204
192
205
void printOperators (struct operator_stats * ops, int operatorsAvailable) {
193
206
for (int i = 0 ; i < operatorsAvailable; i++) {
194
- Serial .print (String (i + 1 ) + " : " );
195
- Serial .print (ops[i].longOp + " (" + String (ops[i].numOp ) + " ) - " );
207
+ SerialMonitor .print (String (i + 1 ) + " : " );
208
+ SerialMonitor .print (ops[i].longOp + " (" + String (ops[i].numOp ) + " ) - " );
196
209
switch (ops[i].stat ) {
197
210
case 0 :
198
- Serial .println (F (" UNKNOWN" ));
211
+ SerialMonitor .println (F (" UNKNOWN" ));
199
212
break ;
200
213
case 1 :
201
- Serial .println (F (" AVAILABLE" ));
214
+ SerialMonitor .println (F (" AVAILABLE" ));
202
215
break ;
203
216
case 2 :
204
- Serial .println (F (" CURRENT" ));
217
+ SerialMonitor .println (F (" CURRENT" ));
205
218
break ;
206
219
case 3 :
207
- Serial .println (F (" FORBIDDEN" ));
220
+ SerialMonitor .println (F (" FORBIDDEN" ));
208
221
break ;
209
222
}
210
223
}
211
- Serial .println ();
224
+ SerialMonitor .println ();
212
225
}
213
226
214
227
void serialWait () {
215
- while (!Serial .available ()) ;
216
- while (Serial .available ()) Serial .read ();
228
+ while (!SerialMonitor .available ()) ;
229
+ while (SerialMonitor .available ()) SerialMonitor .read ();
217
230
}
0 commit comments