Skip to content

Commit aa069ff

Browse files
committed
Adding macros for serial monitor and lte shield port to better support more architectures.
1 parent c305807 commit aa069ff

File tree

1 file changed

+64
-51
lines changed

1 file changed

+64
-51
lines changed

examples/00_Register_Operator/00_Register_Operator.ino

Lines changed: 64 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,19 @@
3030
// library. Below creates a SoftwareSerial object on the standard LTE
3131
// Shield RX/TX pins:
3232
// 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)
3434
SoftwareSerial lteSerial(8, 9);
3535

3636
// Create a LTE_Shield object to be used throughout the sketch:
3737
LTE_Shield lte;
3838

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+
3946
// Network operator can be set to either:
4047
// MNO_SW_DEFAULT -- DEFAULT
4148
// MNO_ATT -- AT&T
@@ -64,86 +71,92 @@ void setup() {
6471
String currentOperator = "";
6572
bool newConnection = true;
6673

67-
Serial.begin(9600);
74+
SerialMonitor.begin(9600);
75+
while (!SerialMonitor) ; // For boards with built-in USB
6876

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+
7281
// Call lte.begin and pass it your Serial/SoftwareSerial object to
7382
// communicate with the LTE Shield.
7483
// 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) ;
7890
}
7991

8092
// First check to see if we're already connected to an operator:
8193
if (lte.getOperator(&currentOperator) == 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);
8496
// 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') {
88100
newConnection = false;
89101
}
102+
while (SerialMonitor.available()) SerialMonitor.read();
90103
}
91104

92105
if (newConnection) {
93106
// Set MNO to either Verizon, T-Mobile, AT&T, Telstra, etc.
94107
// 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"));
96109
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");
99112
} 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."));
101114
while (1) ;
102115
}
103116

104117
// Set the APN -- Access Point Name -- e.g. "hologram"
105-
Serial.println(F("Setting APN..."));
118+
SerialMonitor.println(F("Setting APN..."));
106119
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"));
108121
} 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."));
110123
while (1) ;
111124
}
112125

113126
// 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.."));
115128
serialWait();
116129

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"));
118131
// lte.getOperators takes in a operator_stats struct pointer and max number of
119132
// structs to scan for, then fills up those objects with operator names and numbers
120133
opsAvailable = lte.getOperators(ops, MAX_OPERATORS); // This will block for up to 3 minutes
121134

122135
if (opsAvailable > 0) {
123136
// Pretty-print operators we found:
124-
Serial.println("Found " + String(opsAvailable) + " operators:");
137+
SerialMonitor.println("Found " + String(opsAvailable) + " operators:");
125138
printOperators(ops, opsAvailable);
126139

127140
// 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.");
129142
char c = 0;
130143
bool selected = false;
131144
while (!selected) {
132-
while (!Serial.available()) ;
133-
c = Serial.read();
145+
while (!SerialMonitor.available()) ;
146+
c = SerialMonitor.read();
134147
int selection = c - '0';
135148
if ((selection >= 1) && (selection <= opsAvailable)) {
136149
selected = true;
137-
Serial.println("Connecting to option " + String(selection));
150+
SerialMonitor.println("Connecting to option " + String(selection));
138151
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");
140153
} 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."));
142155
}
143156
}
144157
}
145158
} 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."));
147160
while (1) ;
148161
}
149162
}
@@ -156,11 +169,11 @@ void loop() {
156169
// Loop won't do much besides provide a debugging interface.
157170
// Pass serial data from Arduino to shield and vice-versa
158171
#ifdef DEBUG_PASSTHROUGH_ENABLED
159-
if (Serial.available()) {
160-
lteSerial.write((char) Serial.read());
172+
if (LTEShieldSerial.available()) {
173+
SerialMonitor.write((char) LTEShieldSerial.read());
161174
}
162-
if (lteSerial.available()) {
163-
Serial.write((char) lteSerial.read());
175+
if (SerialMonitor.available()) {
176+
LTEShieldSerial.write((char) SerialMonitor.read());
164177
}
165178
#endif
166179
}
@@ -170,48 +183,48 @@ void printInfo(void) {
170183
IPAddress ip(0, 0, 0, 0);
171184
String currentOperator = "";
172185

173-
Serial.println(F("Connection info:"));
186+
SerialMonitor.println(F("Connection info:"));
174187
// APN Connection info: APN name and IP
175188
if (lte.getAPN(&currentApn, &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);
179192
}
180193

181194
// Operator name or number
182195
if (lte.getOperator(&currentOperator) == LTE_SHIELD_SUCCESS) {
183-
Serial.print(F("Operator: "));
184-
Serial.println(currentOperator);
196+
SerialMonitor.print(F("Operator: "));
197+
SerialMonitor.println(currentOperator);
185198
}
186199

187200
// Received signal strength
188-
Serial.println("RSSI: " + String(lte.rssi()));
189-
Serial.println();
201+
SerialMonitor.println("RSSI: " + String(lte.rssi()));
202+
SerialMonitor.println();
190203
}
191204

192205
void printOperators(struct operator_stats * ops, int operatorsAvailable) {
193206
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) + ") - ");
196209
switch (ops[i].stat) {
197210
case 0:
198-
Serial.println(F("UNKNOWN"));
211+
SerialMonitor.println(F("UNKNOWN"));
199212
break;
200213
case 1:
201-
Serial.println(F("AVAILABLE"));
214+
SerialMonitor.println(F("AVAILABLE"));
202215
break;
203216
case 2:
204-
Serial.println(F("CURRENT"));
217+
SerialMonitor.println(F("CURRENT"));
205218
break;
206219
case 3:
207-
Serial.println(F("FORBIDDEN"));
220+
SerialMonitor.println(F("FORBIDDEN"));
208221
break;
209222
}
210223
}
211-
Serial.println();
224+
SerialMonitor.println();
212225
}
213226

214227
void serialWait() {
215-
while (!Serial.available()) ;
216-
while (Serial.available()) Serial.read();
228+
while (!SerialMonitor.available()) ;
229+
while (SerialMonitor.available()) SerialMonitor.read();
217230
}

0 commit comments

Comments
 (0)