Skip to content

Commit 942614c

Browse files
committed
Adding comments, cleaning up example 3.
1 parent 3ce0290 commit 942614c

File tree

2 files changed

+58
-20
lines changed

2 files changed

+58
-20
lines changed
Lines changed: 57 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,99 @@
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+
Once programmed, open the serial monitor, set the baud rate to 9600.
17+
Use the Hologram dashboard to send a message (via Cloud data) to your device.
18+
You should see the message relayed to your serial monitor.
19+
Make sure the port your sending to matches HOLOGRAM_LISTEN_PORT.
20+
21+
Hardware Connections:
22+
Attach the SparkFun LTE Cat M1/NB-IoT Shield to your Arduino
23+
Power the shield with your Arduino -- ensure the PWR_SEL switch is in
24+
the "ARDUINO" position.
25+
*/
26+
27+
//Click here to get the library: http://librarymanager/All#SparkFun_LTE_Shield_Arduino_Library
128
#include <SparkFun_LTE_Shield_Arduino_Library.h>
229

30+
// Create a SoftwareSerial object to pass to the LTE_Shield library
331
SoftwareSerial lteSerial(8, 9);
32+
// Create a LTE_Shield object to use throughout the sketch
433
LTE_Shield lte;
534

35+
// Hologram server constants, these shouldn't change:
636
const unsigned int HOLOGRAM_PORT = 9999;
737
const unsigned int HOLOGRAM_LISTEN_PORT = 4010;
838

39+
int listeningSocket = -1;
40+
41+
// Callback to process data coming into the LTE module
942
void processSocketRead(int socket, String response) {
43+
// Print message received, and the IP address it was received from.
44+
// Also print socket received on.
1045
Serial.println("Read: " + response);
1146
Serial.println("Socket: " + String(socket));
1247
Serial.print("Remote IP: ");
1348
Serial.println(lte.lastRemoteIP());
1449
}
1550

51+
// Callback to process when a socket closes
1652
void processSocketClose(int socket) {
53+
// If the closed socket is the one we're listening on.
54+
// Set a flag to re-open the listening socket.
55+
if (socket == listeningSocket) {
56+
listeningSocket = -1;
57+
} else {
58+
// Otherwise print the closed socket
1759
Serial.println("Socket " + String(socket) + " closed");
60+
}
1861
}
1962

2063
void setup() {
21-
Serial.begin(9600);
64+
Serial.begin(9600);
2265

2366
if ( lte.begin(lteSerial, 9600) ) {
2467
Serial.println(F("LTE Shield connected!"));
2568
}
2669

2770
lte.setSocketReadCallback(&processSocketRead);
2871
lte.setSocketCloseCallback(&processSocketClose);
29-
30-
listenHologramMessage();
3172
}
3273

3374
void loop() {
34-
//passthrough();
35-
lte.poll();
75+
lte.poll();
76+
77+
// If a listening socket is not open. Set up a new one.
78+
if (listeningSocket < 0) {
79+
listenHologramMessage();
80+
}
3681
}
3782

3883
void listenHologramMessage()
3984
{
4085
int sock = -1;
4186
LTE_Shield_error_t err;
4287

43-
sock = lte.socketOpen(LTE_SHIELD_TCP);
44-
if (sock >= 0) {
45-
err = lte.socketListen(sock, HOLOGRAM_LISTEN_PORT);
88+
// Open a new available socket
89+
listeningSocket = lte.socketOpen(LTE_SHIELD_TCP);
90+
// If a socket is available it should return a value between 0-5
91+
if (listeningSocket >= 0) {
92+
// Listen on the socket on the defined port
93+
err = lte.socketListen(listeningSocket, HOLOGRAM_LISTEN_PORT);
4694
if (err == LTE_SHIELD_ERROR_SUCCESS) {
4795
Serial.print(F("Listening socket open: "));
48-
Serial.println(sock);
96+
Serial.println(listeningSocket);
4997
}
5098
else {
5199
Serial.println("Unable to listen on socket");
@@ -54,15 +102,4 @@ void listenHologramMessage()
54102
else {
55103
Serial.println("Unable to open socket");
56104
}
57-
}
58-
59-
void passthrough() {
60-
61-
if (Serial.available()) {
62-
char c = Serial.read();
63-
lteSerial.write(c);
64-
}
65-
if (lteSerial.available()) {
66-
Serial.write(lteSerial.read());
67-
}
68105
}

src/SparkFun_LTE_Shield_Arduino_Library.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ typedef enum {
6565
LTE_SHIELD_ERROR_UNEXPECTED_RESPONSE,
6666
LTE_SHIELD_ERROR_NO_RESPONSE
6767
} LTE_Shield_error_t;
68+
#define LTE_SHIELD_SUCCESS LTE_SHIELD_ERROR_SUCCESS
6869

6970
typedef enum {
7071
LTE_SHIELD_REGISTRATION_INVALID = -1,

0 commit comments

Comments
 (0)