LTExBee_FirstConnection.ino example

Testing sketch to set up a never-previously-connected LTE XBee running in standard (transparent) mode.

Testing sketch to set up a never-previously-connected LTE XBee running in standard (transparent) mode.

1/** =========================================================================
2 * @example{lineno} LTExBee_FirstConnection.ino
3 * @brief Testing sketch to set up a never-previously-connected LTE XBee running
4 * in standard (transparent) mode.
5 *
6 * @m_examplenavigation{page_extra_helper_sketches,}
7 * ======================================================================= */
8
9#define TINY_GSM_MODEM_XBEE
10#define TINY_GSM_RX_BUFFER 64
11#define TINY_GSM_YIELD_MS 2
12#define TINY_GSM_DEBUG Serial
13
14
15#include <Arduino.h>
16#include <StreamDebugger.h>
17#include <TinyGsmClient.h>
18
19StreamDebugger debugger(Serial1, Serial);
20TinyGsm gsmModem(debugger);
21
22const char* apn = "hologram";
23
24void setup() {
25 // Set the reset pin HIGH to ensure the Bee does not continually reset
26 pinMode(20, OUTPUT);
27 digitalWrite(20, HIGH);
28
29 // Set the sleep_rq pin LOW to wake the Bee
30 pinMode(23, OUTPUT);
31 digitalWrite(23, LOW);
32
33 // Set the input pin mode
34 pinMode(19, INPUT);
35
36 // Set console baud rate
37 Serial.begin(115200);
38 delay(10);
39
40 // Set XBee module baud rate
41 Serial1.begin(9600);
42
43 // Wait for warm-up, probably overkill
44 delay(6000);
45}
46
47void loop() {
48 /** First run the TinyGSM init() function for the XBee. */
49 DBG(F("Initializing the XBee..."));
50 gsmModem.init();
51
52 /** Then enter command mode to set pin outputs. */
53 DBG(F("Putting XBee into command mode..."));
54 if (gsmModem.commandMode()) {
55 DBG(F("Setting I/O Pins..."));
56 /** Enable pin sleep functionality on `DIO9`.
57 * NOTE: Only the `DTR_N/SLEEP_RQ/DIO8` pin (9 on the bee socket) can be
58 * used for this pin sleep/wake. */
59 gsmModem.sendAT(GF("D8"), 1);
60 gsmModem.waitResponse();
61 /** Enable status indication on `DIO9` - it will be HIGH when the XBee
62 * is awake.
63 * NOTE: Only the `ON/SLEEP_N/DIO9` pin (13 on the bee socket) can be
64 * used for direct status indication. */
65 gsmModem.sendAT(GF("D9"), 1);
66 gsmModem.waitResponse();
67 /** Enable CTS on `DIO7` - it will be `LOW` when it is clear to send
68 * data to the XBee. This can be used as proxy for status indication if
69 * that pin is not readable.
70 * NOTE: Only the `CTS_N/DIO7` pin (12 on the bee socket) can be used
71 * for CTS. */
72 gsmModem.sendAT(GF("D7"), 1);
73 gsmModem.waitResponse();
74 /** Enable association indication on `DIO5` - this is should be directly
75 * attached to an LED if possible.
76 * - Solid light indicates no connection
77 * - Single blink indicates connection
78 * - double blink indicates connection but failed TCP link on last
79 * attempt
80 *
81 * NOTE: Only the `Associate/DIO5` pin (15 on the bee socket) can be
82 * used for this function. */
83 gsmModem.sendAT(GF("D5"), 1);
84 gsmModem.waitResponse();
85 /** Enable RSSI PWM output on `DIO10` - this should be directly attached
86 * to an LED if possible. A higher PWM duty cycle (and thus brighter
87 * LED) indicates better signal quality.
88 * NOTE: Only the `DIO10/PWM0` pin (6 on the bee socket) can be used for
89 * this function. */
90 gsmModem.sendAT(GF("P0"), 1);
91 gsmModem.waitResponse();
92 /** Enable pin sleep on the XBee. */
93 DBG(F("Setting Sleep Options..."));
94 gsmModem.sendAT(GF("SM"), 1);
95 gsmModem.waitResponse();
96 /** Disassociate from the network for the lowest power deep sleep. */
97 gsmModem.sendAT(GF("SO"), 0);
98 gsmModem.waitResponse();
99 DBG(F("Setting Other Options..."));
100 /** Disable remote manager, USB Direct, and LTE PSM
101 * NOTE: LTE-M's PSM (Power Save Mode) sounds good, but there's no easy
102 * way on the LTE-M Bee to wake the cell chip itself from PSM, so we'll
103 * use the Digi pin sleep instead. */
104 gsmModem.sendAT(GF("DO"), 0);
105 gsmModem.waitResponse();
106 /** Ask data to be "packetized" and sent out with every new line (0x0A)
107 * character. */
108 gsmModem.sendAT(GF("TD0A"));
109 gsmModem.waitResponse();
110 /* Make sure USB direct is NOT enabled on the XBee3 units. */
111 gsmModem.sendAT(GF("P1"), 0);
112 gsmModem.waitResponse();
113 /** Set the socket timeout to 10s (this is default). */
114 gsmModem.sendAT(GF("TM"), 64);
115 gsmModem.waitResponse();
116
117 DBG(F("Setting Cellular Carrier Options..."));
118 // Carrier Profile - 0 = Automatic selection
119 // - 1 = No profile/SIM ICCID selected
120 // - 2 = AT&T
121 // - 3 = Verizon
122 // NOTE: To select T-Mobile, you must enter bypass mode!
123 gsmModem.sendAT(GF("CP"), 2);
124 gsmModem.waitResponse();
125 // Cellular network technology - 0 = LTE-M with NB-IoT fallback
126 // - 1 = NB-IoT with LTE-M fallback
127 // - 2 = LTE-M only
128 // - 3 = NB-IoT only
129 gsmModem.sendAT(GF("N#"), 0);
130 gsmModem.waitResponse();
131
132 DBG(F("Setting the APN..."));
133 /** Save the network connection parameters. */
134 gsmModem.gprsConnect(apn);
135 DBG(F("Ensuring XBee is in transparent mode..."));
136 /* Make sure we're really in transparent mode. */
137 gsmModem.sendAT(GF("AP0"));
138 gsmModem.waitResponse();
139 /** Write all changes to flash and apply them. */
140 DBG(F("Applying changes..."));
141 gsmModem.writeChanges();
142 /** Finally, exit command mode. */
143 gsmModem.exitCommand();
144 /** Force restart the modem to make sure all settings take. */
145 DBG(F("Restarting XBee..."));
146 gsmModem.restart();
147 } else {
148 // wait a bit
149 delay(30000L);
150 // try again
151 return;
152 }
153
154 // Scan for networks - this is probably really slow
155 gsmModem.sendAT(GF("AS"));
156 gsmModem.waitResponse(180000L, GF("S"), GF("ERROR"));
157 while (Serial1.available()) {
158 Serial.println(Serial1.readStringUntil('\r'));
159 }
160
161 // Wait forever for a connection
162 DBG(F("Waiting for network registration"));
163 while (!gsmModem.isNetworkConnected()) {
164 int csq = gsmModem.getSignalQuality();
165 DBG("Signal quality:", csq);
166 delay(250);
167 }
168
169 // Scan for networks - this is probably really slow
170 gsmModem.sendAT(GF("AS"));
171 gsmModem.waitResponse(180000L, GF("S"), GF("ERROR"));
172 while (Serial1.available()) {
173 Serial.println(Serial1.readStringUntil('\r'));
174 }
175
176 // Print some stuff after connected
177 String ccid = gsmModem.getSimCCID();
178 DBG("CCID:", ccid);
179
180 String imei = gsmModem.getIMEI();
181 DBG("IMEI:", imei);
182
183 String imsi = gsmModem.getIMSI();
184 DBG("IMSI:", imsi);
185
186 String cop = gsmModem.getOperator();
187 DBG("Operator:", cop);
188
189 IPAddress local = gsmModem.localIP();
190 DBG("Local IP:", local);
191
192
193 // Shut down
194 gsmModem.poweroff();
195 DBG("Powering down.");
196
197 while (1) {
198 // And do nothing forever more.
199 }
200}