1
+ #include " SparkFun_u-blox_Cellular_Arduino_Library.h"
2
+
3
+ // Uncomment the line below that you need for Serial on your platform
4
+ #define mySerial Serial1
5
+ // SoftwareSerial mySerial(16, 17);
6
+
7
+ // Uncomment the module you're using. If your module is not listed below, then
8
+ // it's not supported for this example
9
+ SparkFun_ublox_Cellular myModule; // This example works with all modules, so the base class can be used
10
+ // SparkFun_ublox_SARA_R5 myModule; // Base SARA-R5 class
11
+ // SparkFun_ublox_SARA_R500S myModule;
12
+ // SparkFun_ublox_SARA_R500S_01B myModule;
13
+ // SparkFun_ublox_SARA_R500S_61B myModule;
14
+ // SparkFun_ublox_SARA_R510M8S_61B myModule;
15
+ // SparkFun_ublox_SARA_R510S myModule;
16
+ // SparkFun_ublox_LARA_R6 myModule; // Base LARA-R6 class
17
+ // SparkFun_ublox_LARA_R6001 myModule;
18
+ // SparkFun_ublox_LARA_R6001D myModule;
19
+ // SparkFun_ublox_LARA_R6401 myModule;
20
+ // SparkFun_ublox_LARA_R6401D myModule;
21
+ // SparkFun_ublox_LARA_R6801_00B myModule;
22
+ // SparkFun_ublox_LARA_R6801D myModule;
23
+
24
+ // You can change the Quality of Service (QoS) here if you want
25
+ int qos = 0 ;
26
+
27
+ // Topics that will be used for publishing and subscribing
28
+ String publishTopic;
29
+ String subscribeTopic;
30
+
31
+ // Whether we're connected to the MQTT broker
32
+ bool mqttConnected = false ;
33
+
34
+ // Callback function for handling MQTT responses from the module
35
+ void mqttCallback (int command, int result)
36
+ {
37
+ if (command == UBX_CELL_MQTT_COMMAND_LOGIN && result == 1 )
38
+ {
39
+ // Connected to broker
40
+ mqttConnected = true ;
41
+ Serial.println (F (" Connected to broker!" ));
42
+ }
43
+ else if (command == UBX_CELL_MQTT_COMMAND_LOGOUT && result == 1 )
44
+ {
45
+ // Disconnected from broker
46
+ mqttConnected = false ;
47
+ Serial.println (F (" Disconnected from broker!" ));
48
+ }
49
+ else if (command == UBX_CELL_MQTT_COMMAND_SUBSCRIBE && result == 1 )
50
+ {
51
+ // Topic subscription successful
52
+ Serial.println (F (" Subscribed to topic!" ));
53
+ Serial.println (F (" Enter any text to post to the topic" ));
54
+ Serial.println ();
55
+ }
56
+ else if (command == UBX_CELL_MQTT_COMMAND_READ)
57
+ {
58
+ // New message available
59
+ Serial.print (F (" A new message is available! Total messages to read: " ));
60
+ Serial.println (result);
61
+ Serial.println (F (" Enter a blank line to read the oldest message" ));
62
+ Serial.println ();
63
+ }
64
+ else
65
+ {
66
+ // Other response
67
+ Serial.print (F (" Unknown MQTT reponse! Command: " ));
68
+ Serial.print (command);
69
+ Serial.print (F (" Result: " ));
70
+ Serial.println (result);
71
+ }
72
+ }
73
+
74
+ void setup ()
75
+ {
76
+ String currentOperator = " " ;
77
+
78
+ Serial.begin (115200 ); // Start the serial console
79
+
80
+ // Wait for user to press key to begin
81
+ Serial.println (F (" u-blox Cellular Example 8 - MQTT" ));
82
+ Serial.println (F (" Press any key to begin" ));
83
+
84
+ while (!Serial.available ()) // Wait for the user to press a key (send any serial character)
85
+ ;
86
+ while (Serial.available ()) // Empty the serial RX buffer
87
+ Serial.read ();
88
+
89
+ Serial.println (F (" Beginning..." ));
90
+
91
+ // myModule.enableDebugging(); // Uncomment this line to enable helpful debug messages on Serial
92
+
93
+ // For the MicroMod Asset Tracker, we need to invert the power pin so it pulls high instead of low
94
+ // Uncomment the next line if required
95
+ // myModule.invertPowerPin(true);
96
+
97
+ // Initialize the module
98
+ if (myModule.begin (mySerial, UBX_CELL_DEFAULT_BAUD_RATE))
99
+ {
100
+ Serial.println (F (" Module connected!" ));
101
+ }
102
+ else
103
+ {
104
+ Serial.println (F (" Unable to communicate with the module." ));
105
+ Serial.println (F (" Manually power-on (hold the module's On button for 3 seconds) and try again." ));
106
+ while (1 )
107
+ ; // Loop forever on fail
108
+ }
109
+ Serial.println ();
110
+
111
+ // First check to see if we're connected to an operator:
112
+ if (myModule.getOperator (¤tOperator) == UBX_CELL_SUCCESS)
113
+ {
114
+ Serial.print (F (" Connected to: " ));
115
+ Serial.println (currentOperator);
116
+ }
117
+ else
118
+ {
119
+ Serial.print (F (" The module is not yet connected to an operator. Please use the previous examples to connect. "
120
+ " Or wait and retry. Freezing..." ));
121
+ while (1 )
122
+ ; // Do nothing more
123
+ }
124
+
125
+ Serial.println ();
126
+
127
+ // Make sure any previous MQTT connection is closed
128
+ myModule.disconnectMQTT ();
129
+
130
+ // Set callback for any MQTT responses from the module
131
+ myModule.setMQTTCommandCallback (mqttCallback);
132
+
133
+ Serial.println (F (" Enter the MQTT broker server name" ));
134
+
135
+ // Clear any previous input and wait for new input
136
+ while (Serial.available ())
137
+ Serial.read ();
138
+ while (!Serial.available ())
139
+ ;
140
+
141
+ // Get the broker server name
142
+ String serverName = Serial.readStringUntil (' \n ' );
143
+ Serial.print (F (" Server name: " ));
144
+ Serial.println (serverName);
145
+ Serial.println ();
146
+
147
+ Serial.println (F (" Enter the MQTT broker server port number" ));
148
+ Serial.println (F (" (or enter nothing for default port of 1883)" ));
149
+
150
+ // Clear any previous input and wait for new input
151
+ while (Serial.available ())
152
+ Serial.read ();
153
+ while (!Serial.available ())
154
+ ;
155
+
156
+ // Get the broker server port
157
+ String serverPort = Serial.readStringUntil (' \n ' );
158
+
159
+ // Attempt to parse the port number. If it fails, just set 1883
160
+ int port = serverPort.toInt ();
161
+ if (port == 0 )
162
+ port = 1883 ;
163
+
164
+ Serial.print (F (" Server port: " ));
165
+ Serial.println (port);
166
+ Serial.println ();
167
+
168
+ // Now set the MQTT server
169
+ myModule.setMQTTserver (serverName, port);
170
+
171
+ Serial.println (F (" Enter the client ID" ));
172
+
173
+ // Clear any previous input and wait for new input
174
+ while (Serial.available ())
175
+ Serial.read ();
176
+ while (!Serial.available ())
177
+ ;
178
+
179
+ // Get the client ID
180
+ String clientID = Serial.readStringUntil (' \n ' );
181
+ Serial.print (F (" Client ID: " ));
182
+ Serial.println (clientID);
183
+ Serial.println ();
184
+
185
+ // Set the client ID
186
+ myModule.setMQTTclientId (clientID);
187
+
188
+ Serial.println (F (" Connecting to MQTT broker..." ));
189
+ myModule.connectMQTT ();
190
+
191
+ // Wait for module to connect
192
+ while (!mqttConnected)
193
+ myModule.poll ();
194
+
195
+ Serial.println ();
196
+ Serial.println (F (" Enter a topic to publish to" ));
197
+
198
+ // Clear any previous input and wait for new input
199
+ while (Serial.available ())
200
+ Serial.read ();
201
+ while (!Serial.available ())
202
+ ;
203
+
204
+ // Get the topic name
205
+ publishTopic = Serial.readStringUntil (' \n ' );
206
+ Serial.print (F (" Publish topic: " ));
207
+ Serial.println (publishTopic);
208
+ Serial.println ();
209
+
210
+ Serial.println ();
211
+ Serial.println (F (" Enter a topic to subscribe to" ));
212
+
213
+ // Clear any previous input and wait for new input
214
+ while (Serial.available ())
215
+ Serial.read ();
216
+ while (!Serial.available ())
217
+ ;
218
+
219
+ // Get the topic name
220
+ subscribeTopic = Serial.readStringUntil (' \n ' );
221
+ Serial.print (F (" Subscribe topic: " ));
222
+ Serial.println (subscribeTopic);
223
+ Serial.println ();
224
+
225
+ // Subscribe to the topic
226
+ myModule.subscribeMQTTtopic (qos, subscribeTopic);
227
+ }
228
+
229
+ void loop ()
230
+ {
231
+ // Need to call poll() frequently to receive updates from the module.
232
+ myModule.poll ();
233
+
234
+ // Check for user input
235
+ if (Serial.available ())
236
+ {
237
+ // Get user's input
238
+ String inputString = Serial.readStringUntil (' \n ' );
239
+
240
+ // Clear any remaining input
241
+ while (Serial.available ())
242
+ Serial.read ();
243
+
244
+ // Check whether the user entered anything
245
+ if (inputString.length () > 0 )
246
+ {
247
+ // Publish the user's input to the topic
248
+ Serial.println (F (" Publishing message:" ));
249
+ Serial.println (inputString);
250
+ Serial.println ();
251
+ myModule.mqttPublishTextMsg (publishTopic, inputString.c_str ());
252
+ }
253
+ else
254
+ {
255
+ // Read next received message
256
+ uint8_t buffer[MAX_MQTT_DIRECT_MSG_LEN];
257
+ int bytesRead = 0 ;
258
+ myModule.readMQTT (&qos, &subscribeTopic, buffer, MAX_MQTT_DIRECT_MSG_LEN, &bytesRead);
259
+
260
+ // Print out message
261
+ Serial.println (F (" Received message:" ));
262
+ for (int i = 0 ; i < bytesRead; i++)
263
+ Serial.print ((char )buffer[i]);
264
+ Serial.println ();
265
+ Serial.println ();
266
+ }
267
+ }
268
+ }
0 commit comments