1
+ /*
2
+ Register your LTE Shield/SIM combo on a mobile network operator
3
+ By: Jim Lindblom
4
+ SparkFun Electronics
5
+ Date: November 19, 2018
6
+ License: This code is public domain but you buy me a beer if you use this
7
+ and we meet someday (Beerware license).
8
+ Feel like supporting our work? Buy a board from SparkFun!
9
+ https://www.sparkfun.com/products/14997
10
+
11
+ This example demonstrates how to initialize your Cat M1/NB-IoT shield, and
12
+ connect it to a mobile network operator (Verizon, AT&T, T-Mobile, etc.).
13
+
14
+ Before beginning, you may need to adjust the mobile network operator (MNO)
15
+ setting on line 45. See comments above that line to help select either
16
+ Verizon, T-Mobile, AT&T or others.
17
+
18
+ You may also need to set an APN on line 51 -- e.g. "hologram"
19
+
20
+ Hardware Connections:
21
+ Attach the SparkFun LTE Cat M1/NB-IoT Shield to your Arduino
22
+ Power the shield with your Arduino -- ensure the PWR_SEL switch is in
23
+ the "ARDUINO" position.
24
+ */
25
+
26
+ // Click here to get the library: http://librarymanager/All#SparkFun_LTE_Shield_Arduino_Library
27
+ #include < SparkFun_LTE_Shield_Arduino_Library.h>
28
+
29
+ // We need to pass a Serial or SoftwareSerial object to the LTE Shield
30
+ // library. Below creates a SoftwareSerial object on the standard LTE
31
+ // Shield RX/TX pins:
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.)
34
+ SoftwareSerial lteSerial (8 , 9 );
35
+
36
+ // Create a LTE_Shield object to be used throughout the sketch:
37
+ LTE_Shield lte;
38
+
39
+ // Network operator can be set to either:
40
+ // MNO_SW_DEFAULT -- DEFAULT
41
+ // MNO_ATT -- AT&T
42
+ // MNO_VERIZON -- Verizon
43
+ // MNO_TELSTRA -- Telstra
44
+ // MNO_TMO -- T-Mobile
45
+ const mobile_network_operator_t MOBILE_NETWORK_OPERATOR = MNO_SW_DEFAULT;
46
+ const String MOBILE_NETWORK_STRINGS[] = {" Default" , " SIM_ICCD" , " AT&T" , " VERIZON" ,
47
+ " TELSTRA" , " T-Mobile" , " CT" };
48
+
49
+ // APN -- Access Point Name. Gateway between GPRS MNO
50
+ // and another computer network. E.g. "hologram
51
+ const String APN = " hologram" ;
52
+
53
+ // This defines the size of the ops struct array. Be careful making
54
+ // this much bigger than ~5 on an Arduino Uno. To narrow the operator
55
+ // list, set MOBILE_NETWORK_OPERATOR to AT&T, Verizeon etc. instead
56
+ // of MNO_SW_DEFAULT.
57
+ #define MAX_OPERATORS 5
58
+
59
+ #define DEBUG_PASSTHROUGH_ENABLED
60
+
61
+ void setup () {
62
+ int opsAvailable;
63
+ struct operator_stats ops[MAX_OPERATORS];
64
+ String currentOperator = " " ;
65
+ bool newConnection = true ;
66
+
67
+ Serial.begin (9600 );
68
+
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." ));
72
+ // Call lte.begin and pass it your Serial/SoftwareSerial object to
73
+ // communicate with the LTE Shield.
74
+ // 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 " ));
78
+ }
79
+
80
+ // First check to see if we're already connected to an operator:
81
+ if (lte.getOperator (¤tOperator) == LTE_SHIELD_SUCCESS) {
82
+ Serial.print (F (" Already connected to: " ));
83
+ Serial.println (currentOperator);
84
+ // 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' ) {
88
+ newConnection = false ;
89
+ }
90
+ }
91
+
92
+ if (newConnection) {
93
+ // Set MNO to either Verizon, T-Mobile, AT&T, Telstra, etc.
94
+ // This will narrow the operator options during our scan later
95
+ Serial.println (F (" Setting mobile-network operator" ));
96
+ 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 " );
99
+ } else {
100
+ Serial.println (F (" Error setting MNO. Try cycling power to the shield/Arduino." ));
101
+ while (1 ) ;
102
+ }
103
+
104
+ // Set the APN -- Access Point Name -- e.g. "hologram"
105
+ Serial.println (F (" Setting APN..." ));
106
+ if (lte.setAPN (APN) == LTE_SHIELD_SUCCESS) {
107
+ Serial.println (F (" APN successfully set.\r\n " ));
108
+ } else {
109
+ Serial.println (F (" Error setting APN. Try cycling power to the shield/Arduino." ));
110
+ while (1 ) ;
111
+ }
112
+
113
+ // Wait for user to press button before initiating network scan.
114
+ Serial.println (F (" Press any key scan for networks.." ));
115
+ serialWait ();
116
+
117
+ Serial.println (F (" Scanning for operators...this may take up to 3 minutes\r\n " ));
118
+ // lte.getOperators takes in a operator_stats struct pointer and max number of
119
+ // structs to scan for, then fills up those objects with operator names and numbers
120
+ opsAvailable = lte.getOperators (ops, MAX_OPERATORS); // This will block for up to 3 minutes
121
+
122
+ if (opsAvailable > 0 ) {
123
+ // Pretty-print operators we found:
124
+ Serial.println (" Found " + String (opsAvailable) + " operators:" );
125
+ printOperators (ops, opsAvailable);
126
+
127
+ // Wait until the user presses a key to initiate an operator connection
128
+ Serial.println (" Press 1-" + String (opsAvailable) + " to select an operator." );
129
+ char c = 0 ;
130
+ bool selected = false ;
131
+ while (!selected) {
132
+ while (!Serial.available ()) ;
133
+ c = Serial.read ();
134
+ int selection = c - ' 0' ;
135
+ if ((selection >= 1 ) && (selection <= opsAvailable)) {
136
+ selected = true ;
137
+ Serial.println (" Connecting to option " + String (selection));
138
+ if (lte.registerOperator (ops[selection - 1 ]) == LTE_SHIELD_SUCCESS) {
139
+ Serial.println (" Network " + ops[selection - 1 ].longOp + " registered\r\n " );
140
+ } else {
141
+ Serial.println (F (" Error connecting to operator. Reset and try again, or try another network." ));
142
+ }
143
+ }
144
+ }
145
+ } else {
146
+ Serial.println (F (" Did not find an operator. Double-check SIM and antenna, reset and try again, or try another network." ));
147
+ while (1 ) ;
148
+ }
149
+ }
150
+
151
+ // At the very end print connection information
152
+ printInfo ();
153
+ }
154
+
155
+ void loop () {
156
+ // Loop won't do much besides provide a debugging interface.
157
+ // Pass serial data from Arduino to shield and vice-versa
158
+ #ifdef DEBUG_PASSTHROUGH_ENABLED
159
+ if (Serial.available ()) {
160
+ lteSerial.write ((char ) Serial.read ());
161
+ }
162
+ if (lteSerial.available ()) {
163
+ Serial.write ((char ) lteSerial.read ());
164
+ }
165
+ #endif
166
+ }
167
+
168
+ void printInfo (void ) {
169
+ String currentApn = " " ;
170
+ IPAddress ip (0 , 0 , 0 , 0 );
171
+ String currentOperator = " " ;
172
+
173
+ Serial.println (F (" Connection info:" ));
174
+ // APN Connection info: APN name and IP
175
+ if (lte.getAPN (¤tApn, &ip) == LTE_SHIELD_SUCCESS) {
176
+ Serial.println (" APN: " + String (currentApn));
177
+ Serial.print (" IP: " );
178
+ Serial.println (ip);
179
+ }
180
+
181
+ // Operator name or number
182
+ if (lte.getOperator (¤tOperator) == LTE_SHIELD_SUCCESS) {
183
+ Serial.print (F (" Operator: " ));
184
+ Serial.println (currentOperator);
185
+ }
186
+
187
+ // Received signal strength
188
+ Serial.println (" RSSI: " + String (lte.rssi ()));
189
+ Serial.println ();
190
+ }
191
+
192
+ void printOperators (struct operator_stats * ops, int operatorsAvailable) {
193
+ for (int i = 0 ; i < operatorsAvailable; i++) {
194
+ Serial.print (String (i + 1 ) + " : " );
195
+ Serial.print (ops[i].longOp + " (" + String (ops[i].numOp ) + " ) - " );
196
+ switch (ops[i].stat ) {
197
+ case 0 :
198
+ Serial.println (F (" UNKNOWN" ));
199
+ break ;
200
+ case 1 :
201
+ Serial.println (F (" AVAILABLE" ));
202
+ break ;
203
+ case 2 :
204
+ Serial.println (F (" CURRENT" ));
205
+ break ;
206
+ case 3 :
207
+ Serial.println (F (" FORBIDDEN" ));
208
+ break ;
209
+ }
210
+ }
211
+ Serial.println ();
212
+ }
213
+
214
+ void serialWait () {
215
+ while (!Serial.available ()) ;
216
+ while (Serial.available ()) Serial.read ();
217
+ }
0 commit comments