Skip to content

Commit 3ce0290

Browse files
committed
Adding comments to first three examples.
1 parent 689a76a commit 3ce0290

File tree

3 files changed

+172
-34
lines changed

3 files changed

+172
-34
lines changed
Lines changed: 58 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,70 @@
1+
/*
2+
Register your LTE Shield/SIM combo on a mobile network operator
3+
By: Jim Lindblom
4+
SparkFun Electronics
5+
Date: October 23, 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 83. See comments above that line to help select either
16+
Verizon, T-Mobile, AT&T or others.
17+
18+
Hardware Connections:
19+
Attach the SparkFun LTE Cat M1/NB-IoT Shield to your Arduino
20+
Power the shield with your Arduino -- ensure the PWR_SEL switch is in
21+
the "ARDUINO" position.
22+
*/
23+
24+
//Click here to get the library: http://librarymanager/All#SparkFun_LTE_Shield_Arduino_Library
125
#include <SparkFun_LTE_Shield_Arduino_Library.h>
226

27+
// We need to pass a Serial or SoftwareSerial object to the LTE Shield
28+
// library. Below creates a SoftwareSerial object on the standard LTE
29+
// Shield RX/TX pins:
30+
// Note: if you're using an Arduino board with a dedicated hardware
31+
// serial port, comment out the line below. (Also see note in setup.)
332
SoftwareSerial lteSerial(8, 9);
33+
34+
// Create a LTE_Shield object to be used throughout the sketch:
435
LTE_Shield lte;
536

637
// Map registration status messages to more readable strings
738
String registrationString[] = {
8-
"Not registered", // 0
9-
"Registered, home", // 1
10-
"Searching for operator", // 2
11-
"Registration denied", // 3
12-
"Registration unknown", // 4
13-
"Registrered, roaming", // 5
14-
"Registered, home (SMS only)", // 6
15-
"Registered, roaming (SMS only)", // 7
16-
"Registered, home, CSFB not preferred", // 8
17-
"Registered, roaming, CSFB not prefered" // 9
39+
"Not registered", // 0
40+
"Registered, home", // 1
41+
"Searching for operator", // 2
42+
"Registration denied", // 3
43+
"Registration unknown", // 4
44+
"Registrered, roaming", // 5
45+
"Registered, home (SMS only)", // 6
46+
"Registered, roaming (SMS only)", // 7
47+
"Registered, home, CSFB not preferred", // 8
48+
"Registered, roaming, CSFB not prefered" // 9
1849
};
1950

2051
void setup() {
21-
Serial.begin(9600);
52+
Serial.begin(9600);
2253

23-
Serial.println(F("Initializing the LTE Shield..."));
24-
Serial.println(F("...this may take ~25 seconds if the shield is off."));
25-
Serial.println(F("...it may take ~5 seconds if it just turned on."));
54+
Serial.println(F("Initializing the LTE Shield..."));
55+
Serial.println(F("...this may take ~25 seconds if the shield is off."));
56+
Serial.println(F("...it may take ~5 seconds if it just turned on."));
2657

27-
if ( lte.begin(lteSerial, 9600) ) {
58+
// Call lte.begin and pass it your Serial/SoftwareSerial object to
59+
// communicate with the LTE Shield.
60+
// Note: If you're using an Arduino with a dedicated hardware serial
61+
// poert, you may instead slide "Serial" into this begin call.
62+
if ( lte.begin(lteSerial) ) {
2863
Serial.println(F("LTE Shield connected!"));
64+
} else {
65+
Serial.println(F("Unable to communicate with the shield."));
66+
Serial.println(F("Manually power-on (hold POWER for 3 seconds) on and try again."));
67+
while (1) ; // Loop forever on fail
2968
}
3069
Serial.println();
3170

@@ -46,8 +85,8 @@ void setup() {
4685
// MNO_TELSTRA -- Telstra
4786
// MNO_TMO -- T-Mobile
4887
if (!lte.setNetwork(MNO_VERIZON)) {
49-
Serial.println(F("Error setting network. Try cycling power on your Arduino/shield."));
50-
while (1) ;
88+
Serial.println(F("Error setting network. Try cycling power on your Arduino/shield."));
89+
while (1) ;
5190
}
5291

5392
Serial.println("Network set. Ready to go!");
@@ -59,9 +98,10 @@ void setup() {
5998
Serial.println("Network registration: " + registrationString[regStatus]);
6099
}
61100
if (regStatus > 0) {
62-
Serial.println("All set. Go to the next example!");
101+
Serial.println("All set. Go to the next example!");
63102
}
64103
}
65104

66105
void loop() {
106+
// Do nothing. Now that we're registered move on to the next example.
67107
}

examples/01_SMS_Send/01_SMS_Send.ino

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,41 @@
1+
/*
2+
Send an SMS with the SparkFun LTE Cat M1/NB-IoT Shield
3+
By: Jim Lindblom
4+
SparkFun Electronics
5+
Date: October 23, 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 send an SMS message with the LTE Shield.
12+
13+
Before beginning, you should have your shield connected on a MNO.
14+
See example 00 for help with that.
15+
16+
Before uploading, set the DESTINATION_NUMBER constant to the
17+
phone number you want to send a text message to.
18+
(Don't forget to add an internation code (e.g. 1 for US)).
19+
20+
Once programmed, open the serial monitor, set the baud rate to 9600,
21+
and type a message to be sent via SMS.
22+
23+
Hardware Connections:
24+
Attach the SparkFun LTE Cat M1/NB-IoT Shield to your Arduino
25+
Power the shield with your Arduino -- ensure the PWR_SEL switch is in
26+
the "ARDUINO" position.
27+
*/
28+
29+
//Click here to get the library: http://librarymanager/All#SparkFun_LTE_Shield_Arduino_Library
130
#include <SparkFun_LTE_Shield_Arduino_Library.h>
231

32+
// Create a SoftwareSerial object to pass to the LTE_Shield library
333
SoftwareSerial lteSerial(8, 9);
34+
// Create a LTE_Shield object to use throughout the sketch
435
LTE_Shield lte;
536

6-
String destinationNumber = "11234567890";
37+
// Set the cell phone number to be texted
38+
String DESTINATION_NUMBER = "11234567890";
739

840
void setup() {
941
Serial.begin(9600);
@@ -15,16 +47,20 @@ void setup() {
1547
}
1648

1749
void loop() {
18-
static String message = "";
50+
static String message = "";
1951
if (Serial.available())
2052
{
2153
char c = Serial.read();
54+
// Read a message until a \n (newline) is received
2255
if (c == '\n') {
23-
Serial.println("Sending: " + String(message));
24-
lte.sendSMS(destinationNumber, message);
25-
message = "";
56+
// Once we receive a newline. send the text.
57+
Serial.println("Sending: " + String(message));
58+
// Call lte.sendSMS(String number, String message) to send an SMS
59+
// message.
60+
lte.sendSMS(DESTINATION_NUMBER, message);
61+
message = ""; // Clear message string
2662
} else {
27-
message += c;
63+
message += c; // Add last character to message
2864
}
2965
}
3066
}

examples/02_TCP_Send_Hologram/02_TCP_Send_Hologram.ino

Lines changed: 72 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,49 @@
1+
/*
2+
Send an SMS with the SparkFun LTE Cat M1/NB-IoT Shield
3+
By: Jim Lindblom
4+
SparkFun Electronics
5+
Date: October 23, 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 send an SMS message with the LTE Shield.
12+
13+
Before beginning, you should have your shield connected on a MNO.
14+
See example 00 for help with that.
15+
16+
Before uploading, set your HOLOGRAM_DEVICE_KEY. This string can be found
17+
in your device's Hologram dashboard.
18+
19+
Once programmed, open the serial monitor, set the baud rate to 9600,
20+
and type a message to be sent via TCP to the Hologram message service.
21+
Make sure your serial monitor's end-of-line setting is set to "newline".
22+
23+
Hardware Connections:
24+
Attach the SparkFun LTE Cat M1/NB-IoT Shield to your Arduino
25+
Power the shield with your Arduino -- ensure the PWR_SEL switch is in
26+
the "ARDUINO" position.
27+
*/
28+
29+
//Click here to get the library: http://librarymanager/All#SparkFun_LTE_Shield_Arduino_Library
130
#include <SparkFun_LTE_Shield_Arduino_Library.h>
231

32+
// Create a SoftwareSerial object to pass to the LTE_Shield library
333
SoftwareSerial lteSerial(8, 9);
34+
// Create a LTE_Shield object to use throughout the sketch
435
LTE_Shield lte;
536

6-
String hologramDeviceKey = "LC2tG5cf";
37+
// Plug in your Hologram device key here:
38+
String HOLOGRAM_DEVICE_KEY = "LC2tG5cf";
739

40+
// These values should remain the same:
841
const char HOLOGRAM_URL[] = "cloudsocket.hologram.io";
942
const unsigned int HOLOGRAM_PORT = 9999;
1043
const unsigned int HOLOGRAM_LISTEN_PORT = 4010;
1144

1245
void setup() {
13-
Serial.begin(9600);
46+
Serial.begin(9600);
1447

1548
if ( lte.begin(lteSerial, 9600) ) {
1649
Serial.println(F("LTE Shield connected!"));
@@ -20,26 +53,55 @@ void setup() {
2053
}
2154

2255
void loop() {
23-
lte.poll();
56+
static String message = "";
57+
if (Serial.available())
58+
{
59+
char c = Serial.read();
60+
// Read a message until a \n (newline) is received
61+
if (c == '\n') {
62+
// Once we receive a newline. send the text.
63+
Serial.println("Sending: " + String(message));
64+
// Call lte.sendSMS(String number, String message) to send an SMS
65+
// message.
66+
sendHologramMessage(message);
67+
message = ""; // Clear message string
68+
} else {
69+
message += c; // Add last character to message
70+
}
71+
}
72+
lte.poll();
2473
}
2574

2675
void sendHologramMessage(String message)
2776
{
2877
int socket = -1;
2978
String hologramMessage;
3079

31-
hologramMessage = "{\"k\":\"" + hologramDeviceKey + "\",\"d\":\"" +
32-
message + "\"}";
80+
// New lines are not handled well
81+
message.replace('\r', ' ');
82+
message.replace('\n', ' ');
3383

84+
// Construct a JSON-encoded Hologram message string:
85+
hologramMessage = "{\"k\":\"" + HOLOGRAM_DEVICE_KEY + "\",\"d\":\"" +
86+
message + "\"}";
87+
88+
// Open a socket
3489
socket = lte.socketOpen(LTE_SHIELD_TCP);
90+
// On success, socketOpen will return a value between 0-5. On fail -1.
3591
if (socket >= 0) {
36-
Serial.println("Connecting to socket: " + String(socket));
37-
if (lte.socketConnect(socket, HOLOGRAM_URL, HOLOGRAM_PORT) == LTE_SHIELD_ERROR_SUCCESS) {
38-
Serial.println("Connected to Hologram.io");
92+
// Use the socket to connec to the Hologram server
93+
Serial.println("Connecting to socket: " + String(socket));
94+
if (lte.socketConnect(socket, HOLOGRAM_URL, HOLOGRAM_PORT) == LTE_SHIELD_SUCCESS) {
95+
// Send our message to the server:
3996
Serial.println("Sending: " + String(hologramMessage));
40-
if (lte.socketWrite(socket, hologramMessage) == LTE_SHIELD_ERROR_SUCCESS)
97+
if (lte.socketWrite(socket, hologramMessage) == LTE_SHIELD_SUCCESS)
4198
{
42-
lte.socketClose(socket);
99+
// On succesful write, close the socket.
100+
if (lte.socketClose(socket) == LTE_SHIELD_SUCCESS) {
101+
Serial.println("Socket " + String(socket) + " closed");
102+
}
103+
} else {
104+
Serial.println(F("Failed to write"));
43105
}
44106
}
45107
}

0 commit comments

Comments
 (0)