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.
6 * @m_examplenavigation{page_extra_helper_sketches,}
7 * ======================================================================= */
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
16#include <StreamDebugger.h>
17#include <TinyGsmClient.h>
19StreamDebugger debugger(Serial1, Serial);
20TinyGsm gsmModem(debugger);
22const char* apn = "hologram";
25 // Set the reset pin HIGH to ensure the Bee does not continually reset
27 digitalWrite(20, HIGH);
29 // Set the sleep_rq pin LOW to wake the Bee
31 digitalWrite(23, LOW);
33 // Set the input pin mode
36 // Set console baud rate
40 // Set XBee module baud rate
43 // Wait for warm-up, probably overkill
48 /** First run the TinyGSM init() function for the XBee. */
49 DBG(F("Initializing the XBee..."));
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
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
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
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
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)
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();
117 DBG(F("Setting Cellular Carrier Options..."));
118 // Carrier Profile - 0 = Automatic selection
119 // - 1 = No profile/SIM ICCID selected
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
129 gsmModem.sendAT(GF("N#"), 0);
130 gsmModem.waitResponse();
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..."));
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'));
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);
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'));
176 // Print some stuff after connected
177 String ccid = gsmModem.getSimCCID();
180 String imei = gsmModem.getIMEI();
183 String imsi = gsmModem.getIMSI();
186 String cop = gsmModem.getOperator();
187 DBG("Operator:", cop);
189 IPAddress local = gsmModem.localIP();
190 DBG("Local IP:", local);
195 DBG("Powering down.");
198 // And do nothing forever more.