Skip to content

Commit 1a7e1e9

Browse files
committed
Example cleanup, add serial passthrough.
1 parent 942614c commit 1a7e1e9

File tree

7 files changed

+158
-59
lines changed

7 files changed

+158
-59
lines changed

examples/00_Network_Connect/00_Network_Connect.ino

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ String registrationString[] = {
4848
"Registered, roaming, CSFB not prefered" // 9
4949
};
5050

51+
// Network operator can be set to either:
52+
// MNO_ATT -- AT&T
53+
// MNO_VERIZON -- Verizon
54+
// MNO_TELSTRA -- Telstra
55+
// MNO_TMO -- T-Mobile
56+
const mobile_network_operator_t MOBILE_NETWORK_OPERATOR = MNO_VERIZON;
57+
5158
void setup() {
5259
Serial.begin(9600);
5360

@@ -79,12 +86,7 @@ void setup() {
7986
Serial.println("ICCID: " + lte.ccid());
8087
Serial.println();
8188

82-
// Network operator can be set to either:
83-
// MNO_ATT -- AT&T
84-
// MNO_VERIZON -- Verizon
85-
// MNO_TELSTRA -- Telstra
86-
// MNO_TMO -- T-Mobile
87-
if (!lte.setNetwork(MNO_VERIZON)) {
89+
if (!lte.setNetwork(MOBILE_NETWORK_OPERATOR)) {
8890
Serial.println(F("Error setting network. Try cycling power on your Arduino/shield."));
8991
while (1) ;
9092
}

examples/01_SMS_Send/01_SMS_Send.ino

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ void loop() {
5454
// Read a message until a \n (newline) is received
5555
if (c == '\n') {
5656
// Once we receive a newline. send the text.
57-
Serial.println("Sending: " + String(message));
57+
Serial.println("Sending: \"" + String(message) + "\" to " +
58+
DESTINATION_NUMBER);
5859
// Call lte.sendSMS(String number, String message) to send an SMS
5960
// message.
6061
lte.sendSMS(DESTINATION_NUMBER, message);

examples/02_TCP_Send_Hologram/02_TCP_Send_Hologram.ino

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Send an SMS with the SparkFun LTE Cat M1/NB-IoT Shield
2+
Send a TCP message to Hologram
33
By: Jim Lindblom
44
SparkFun Electronics
55
Date: October 23, 2018
@@ -8,7 +8,7 @@
88
Feel like supporting our work? Buy a board from SparkFun!
99
https://www.sparkfun.com/products/14997
1010
11-
This example demonstrates how to send an SMS message with the LTE Shield.
11+
This example demonstrates how to send TCP messages to a Hologram server
1212
1313
Before beginning, you should have your shield connected on a MNO.
1414
See example 00 for help with that.
@@ -35,12 +35,11 @@ SoftwareSerial lteSerial(8, 9);
3535
LTE_Shield lte;
3636

3737
// Plug in your Hologram device key here:
38-
String HOLOGRAM_DEVICE_KEY = "LC2tG5cf";
38+
String HOLOGRAM_DEVICE_KEY = "Ab12CdE4";
3939

4040
// These values should remain the same:
4141
const char HOLOGRAM_URL[] = "cloudsocket.hologram.io";
4242
const unsigned int HOLOGRAM_PORT = 9999;
43-
const unsigned int HOLOGRAM_LISTEN_PORT = 4010;
4443

4544
void setup() {
4645
Serial.begin(9600);

examples/03_TCP_Receive_Hologram/03_TCP_Receive_Hologram.ino

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Send an SMS with the SparkFun LTE Cat M1/NB-IoT Shield
2+
Receive a TCP message from Hologram server
33
By: Jim Lindblom
44
SparkFun Electronics
55
Date: October 23, 2018
@@ -8,15 +8,16 @@
88
Feel like supporting our work? Buy a board from SparkFun!
99
https://www.sparkfun.com/products/14997
1010
11-
This example demonstrates how to send an SMS message with the LTE Shield.
11+
This example demonstrates how to to sset your shield/Arduino up as a
12+
TCP server and receive a message from a Hologram server.
1213
1314
Before beginning, you should have your shield connected on a MNO.
1415
See example 00 for help with that.
1516
1617
Once programmed, open the serial monitor, set the baud rate to 9600.
1718
Use the Hologram dashboard to send a message (via Cloud data) to your device.
1819
You should see the message relayed to your serial monitor.
19-
Make sure the port your sending to matches HOLOGRAM_LISTEN_PORT.
20+
Make sure the port you're sending to matches HOLOGRAM_LISTEN_PORT.
2021
2122
Hardware Connections:
2223
Attach the SparkFun LTE Cat M1/NB-IoT Shield to your Arduino

examples/05_Hologram_Data_Feed/05_Hologram_Data_Feed.ino

Lines changed: 91 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,126 @@
1-
2-
1+
/*
2+
Receive a message from Hologram, then send data on command
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 combines a bit of example 2 (TCP Send to Hologram) and
12+
example 3 (TCP receive from Hologram). On a specific message receipt from
13+
Hologram (read_analog), this sketch will send a JSON-encoded list of
14+
all six analog measurements.
15+
16+
After uploading the code, open your Hologram dashboard, select your
17+
device and send a message matching "read_analog" to the device.
18+
19+
Shortly after this the sketch should respond with a JSON-encoded list
20+
of all six analog measurements. This JSON string can be used for Hologram
21+
routes to send emails, text messages, or more. (See the LTE Shield hookup
22+
guide for more information.)
23+
24+
Hardware Connections:
25+
Attach the SparkFun LTE Cat M1/NB-IoT Shield to your Arduino
26+
Power the shield with your Arduino -- ensure the PWR_SEL switch is in
27+
the "ARDUINO" position.
28+
*/
29+
30+
//Click here to get the library: http://librarymanager/All#SparkFun_LTE_Shield_Arduino_Library
331
#include <SparkFun_LTE_Shield_Arduino_Library.h>
432

33+
// Create a SoftwareSerial object to pass to the LTE_Shield library
534
SoftwareSerial lteSerial(8, 9);
35+
// Create a LTE_Shield object to use throughout the sketch
636
LTE_Shield lte;
737

8-
#define SEND_INTERVAL 60000
9-
10-
String HOLOGRAM_DEVICE_KEY = "LC2tG5cf";
38+
// Hologram device key. Used to send messages:
39+
String HOLOGRAM_DEVICE_KEY = "Ab12CdE4";
40+
// Hologram message topic(s):
1141
String HOLOGRAM_ANALOG_TOPIC = "ANALOG";
1242

43+
// Hologram Server constants. Shouldn't have to change:
1344
const char HOLOGRAM_URL[] = "cloudsocket.hologram.io";
1445
const unsigned int HOLOGRAM_PORT = 9999;
1546
const unsigned int HOLOGRAM_LISTEN_PORT = 4010;
1647

48+
// Pair analog pins up with a JSON identifier string
1749
#define ANALOG_PINS_TO_READ 6
1850
int readPins[ANALOG_PINS_TO_READ] = {A0, A1, A2, A3, A4, A5};
1951
String pinNames[ANALOG_PINS_TO_READ] = {"A0", "A1", "A2", "A3", "A4", "A5"};
2052

21-
boolean doSendAnalog = false;
22-
boolean doServerListen = true;
23-
int listenSocket = -1;
53+
// loop flags to check:
54+
boolean doSendAnalog = false; // Send analog values
55+
boolean doServerListen = true; // Open listening server
56+
int listenSocket = -1; // Listen socket -1 is incactive, 0-5 for active socket
2457

58+
// Callback to process a data read from a socket
2559
void processSocketRead(int socket, String response) {
26-
if (response == "read_analog") {
27-
doSendAnalog = true;
28-
} else {
29-
Serial.println("Server read: " + String(response));
30-
}
60+
// Look for the specified string
61+
if (response == "read_analog") {
62+
doSendAnalog = true; // If found set a flag to send analog values
63+
} else {
64+
Serial.println("Server read: " + String(response));
65+
}
3166
}
3267

68+
// Callback to process a closed socket
3369
void processSocketClose(int socket) {
34-
Serial.println("Socket " + String(socket) + " closed");
35-
if (socket == listenSocket) {
36-
doServerListen = true;
37-
}
70+
Serial.println("Socket " + String(socket) + " closed");
71+
// If the socket was our listening socket, trigger flag to re-open
72+
if (socket == listenSocket) {
73+
doServerListen = true;
74+
}
3875
}
3976

4077
void setup() {
41-
Serial.begin(9600);
78+
Serial.begin(9600);
4279

43-
for (int i=0; i<ANALOG_PINS_TO_READ; i++) {
44-
pinMode(readPins[i], INPUT);
45-
}
80+
for (int i=0; i<ANALOG_PINS_TO_READ; i++) {
81+
pinMode(readPins[i], INPUT);
82+
}
4683

47-
if (!lte.begin(lteSerial)) {
48-
Serial.println("Could not initialize LTE Shield");
49-
while (1) ;
50-
}
84+
if (!lte.begin(lteSerial)) {
85+
Serial.println("Could not initialize LTE Shield");
86+
while (1) ;
87+
}
5188

89+
// Configure callbacks for data read and socket close
5290
lte.setSocketReadCallback(&processSocketRead);
5391
lte.setSocketCloseCallback(&processSocketClose);
5492
}
5593

5694
void loop() {
57-
lte.poll();
58-
59-
if (doSendAnalog) {
60-
Serial.println("Sending analog values");
61-
sendAnalogValues();
62-
doSendAnalog = false;
63-
}
95+
// Poll as often as possible. Try to keep other functions
96+
// in loop as short as possible.
97+
lte.poll();
98+
99+
// If sendAnalog flag is set, do it and clear flag
100+
if (doSendAnalog) {
101+
Serial.println("Sending analog values");
102+
sendAnalogValues();
103+
doSendAnalog = false;
104+
}
64105

65-
if (doServerListen) {
66-
listenHologramMessage();
67-
doServerListen = false;
68-
}
106+
// If serverlisten flag is set, set up listening server
107+
// then clear flag
108+
if (doServerListen) {
109+
listenHologramMessage();
110+
doServerListen = false;
111+
}
69112
}
70113

71114
void listenHologramMessage()
72115
{
73116
int sock = -1;
74117
LTE_Shield_error_t err;
75118

119+
// Open a new available socket
76120
sock = lte.socketOpen(LTE_SHIELD_TCP);
121+
// If a socket is available it should return a value between 0-5
77122
if (sock >= 0) {
123+
// Listen on the socket on the defined port
78124
err = lte.socketListen(sock, HOLOGRAM_LISTEN_PORT);
79125
if (err == LTE_SHIELD_ERROR_SUCCESS) {
80126
Serial.print(F("Listening socket open: "));
@@ -89,15 +135,16 @@ void listenHologramMessage()
89135
}
90136
}
91137

138+
// Compile analog reads into a JSON-encoded string, then send to Hologram
92139
void sendAnalogValues(void) {
93-
String toSend = "{";
94-
for (int i = 0; i < ANALOG_PINS_TO_READ; i++) {
95-
toSend += "\\\"" + pinNames[i] + "\\\": ";
96-
toSend += String(analogRead(readPins[i]));
97-
if (i < 5) toSend += ", ";
98-
}
99-
toSend += "}";
100-
sendHologramMessage(toSend);
140+
String toSend = "{";
141+
for (int i = 0; i < ANALOG_PINS_TO_READ; i++) {
142+
toSend += "\\\"" + pinNames[i] + "\\\": ";
143+
toSend += String(analogRead(readPins[i]));
144+
if (i < 5) toSend += ", ";
145+
}
146+
toSend += "}";
147+
sendHologramMessage(toSend);
101148
}
102149

103150
void sendHologramMessage(String message) {
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
Send AT commands to the SARA-R4 via the Serial Port
3+
By: Jim Lindblom
4+
SparkFun Electronics
5+
Date: October 26, 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 sketch provides a direct interface to the SARA-R4 module's
12+
AT command port. It will initialize the device, and then let you run free!
13+
Consult the SARA-R4 AT command set for a list of commands.
14+
15+
After uploading the code, open your Serial Monitor, set the baud rate to
16+
9600 baud. We also recommend setting the line-ending setting to
17+
"Carriage Return", so your AT commands will be read in by the SARA module.
18+
19+
Hardware Connections:
20+
Attach the SparkFun LTE Cat M1/NB-IoT Shield to your Arduino
21+
Power the shield with your Arduino -- ensure the PWR_SEL switch is in
22+
the "ARDUINO" position.
23+
*/
24+
25+
//Click here to get the library: http://librarymanager/All#SparkFun_LTE_Shield_Arduino_Library
26+
#include <SparkFun_LTE_Shield_Arduino_Library.h>
27+
28+
// Create a SoftwareSerial object to pass to the LTE_Shield library
29+
SoftwareSerial lteSerial(8, 9);
30+
// Create a LTE_Shield object to use throughout the sketch
31+
LTE_Shield lte;
32+
33+
void setup() {
34+
Serial.begin(9600);
35+
36+
if ( lte.begin(lteSerial, 9600) ) {
37+
Serial.println(F("LTE Shield connected!"));
38+
}
39+
Serial.println(F("Ready to passthrough!\r\n"));
40+
}
41+
42+
void loop() {
43+
if (Serial.available()) {
44+
lteSerial.write((char) Serial.read());
45+
}
46+
if (lteSerial.available()) {
47+
Serial.write((char) lteSerial.read());
48+
}
49+
}

src/SparkFun_LTE_Shield_Arduino_Library.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,11 @@ const char LTE_SHIELD_RESPONSE_OK[] = "OK\r\n";
7171
#define NUM_SUPPORTED_BAUD 6
7272
const unsigned long LTE_SHIELD_SUPPORTED_BAUD[NUM_SUPPORTED_BAUD] =
7373
{
74+
115200,
7475
9600,
7576
19200,
7677
38400,
7778
57600,
78-
115200,
7979
230400
8080
};
8181
#define LTE_SHIELD_DEFAULT_BAUD_RATE 115200

0 commit comments

Comments
 (0)