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
3
31
#include < SparkFun_LTE_Shield_Arduino_Library.h>
4
32
33
+ // Create a SoftwareSerial object to pass to the LTE_Shield library
5
34
SoftwareSerial lteSerial (8 , 9 );
35
+ // Create a LTE_Shield object to use throughout the sketch
6
36
LTE_Shield lte;
7
37
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):
11
41
String HOLOGRAM_ANALOG_TOPIC = " ANALOG" ;
12
42
43
+ // Hologram Server constants. Shouldn't have to change:
13
44
const char HOLOGRAM_URL[] = " cloudsocket.hologram.io" ;
14
45
const unsigned int HOLOGRAM_PORT = 9999 ;
15
46
const unsigned int HOLOGRAM_LISTEN_PORT = 4010 ;
16
47
48
+ // Pair analog pins up with a JSON identifier string
17
49
#define ANALOG_PINS_TO_READ 6
18
50
int readPins[ANALOG_PINS_TO_READ] = {A0, A1, A2, A3, A4, A5};
19
51
String pinNames[ANALOG_PINS_TO_READ] = {" A0" , " A1" , " A2" , " A3" , " A4" , " A5" };
20
52
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
24
57
58
+ // Callback to process a data read from a socket
25
59
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
+ }
31
66
}
32
67
68
+ // Callback to process a closed socket
33
69
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
+ }
38
75
}
39
76
40
77
void setup () {
41
- Serial.begin (9600 );
78
+ Serial.begin (9600 );
42
79
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
+ }
46
83
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
+ }
51
88
89
+ // Configure callbacks for data read and socket close
52
90
lte.setSocketReadCallback (&processSocketRead);
53
91
lte.setSocketCloseCallback (&processSocketClose);
54
92
}
55
93
56
94
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
+ }
64
105
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
+ }
69
112
}
70
113
71
114
void listenHologramMessage ()
72
115
{
73
116
int sock = -1 ;
74
117
LTE_Shield_error_t err;
75
118
119
+ // Open a new available socket
76
120
sock = lte.socketOpen (LTE_SHIELD_TCP);
121
+ // If a socket is available it should return a value between 0-5
77
122
if (sock >= 0 ) {
123
+ // Listen on the socket on the defined port
78
124
err = lte.socketListen (sock, HOLOGRAM_LISTEN_PORT);
79
125
if (err == LTE_SHIELD_ERROR_SUCCESS) {
80
126
Serial.print (F (" Listening socket open: " ));
@@ -89,15 +135,16 @@ void listenHologramMessage()
89
135
}
90
136
}
91
137
138
+ // Compile analog reads into a JSON-encoded string, then send to Hologram
92
139
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);
101
148
}
102
149
103
150
void sendHologramMessage (String message) {
0 commit comments