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
1
28
#include < SparkFun_LTE_Shield_Arduino_Library.h>
2
29
30
+ // Create a SoftwareSerial object to pass to the LTE_Shield library
3
31
SoftwareSerial lteSerial (8 , 9 );
32
+ // Create a LTE_Shield object to use throughout the sketch
4
33
LTE_Shield lte;
5
34
35
+ // Hologram server constants, these shouldn't change:
6
36
const unsigned int HOLOGRAM_PORT = 9999 ;
7
37
const unsigned int HOLOGRAM_LISTEN_PORT = 4010 ;
8
38
39
+ int listeningSocket = -1 ;
40
+
41
+ // Callback to process data coming into the LTE module
9
42
void processSocketRead (int socket, String response) {
43
+ // Print message received, and the IP address it was received from.
44
+ // Also print socket received on.
10
45
Serial.println (" Read: " + response);
11
46
Serial.println (" Socket: " + String (socket));
12
47
Serial.print (" Remote IP: " );
13
48
Serial.println (lte.lastRemoteIP ());
14
49
}
15
50
51
+ // Callback to process when a socket closes
16
52
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
17
59
Serial.println (" Socket " + String (socket) + " closed" );
60
+ }
18
61
}
19
62
20
63
void setup () {
21
- Serial.begin (9600 );
64
+ Serial.begin (9600 );
22
65
23
66
if ( lte.begin (lteSerial, 9600 ) ) {
24
67
Serial.println (F (" LTE Shield connected!" ));
25
68
}
26
69
27
70
lte.setSocketReadCallback (&processSocketRead);
28
71
lte.setSocketCloseCallback (&processSocketClose);
29
-
30
- listenHologramMessage ();
31
72
}
32
73
33
74
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
+ }
36
81
}
37
82
38
83
void listenHologramMessage ()
39
84
{
40
85
int sock = -1 ;
41
86
LTE_Shield_error_t err;
42
87
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);
46
94
if (err == LTE_SHIELD_ERROR_SUCCESS) {
47
95
Serial.print (F (" Listening socket open: " ));
48
- Serial.println (sock );
96
+ Serial.println (listeningSocket );
49
97
}
50
98
else {
51
99
Serial.println (" Unable to listen on socket" );
@@ -54,15 +102,4 @@ void listenHologramMessage()
54
102
else {
55
103
Serial.println (" Unable to open socket" );
56
104
}
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
- }
68
105
}
0 commit comments