This repository has been archived by the owner on Jan 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathGSM_MQTT_Medium.ino
401 lines (321 loc) · 11.1 KB
/
GSM_MQTT_Medium.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/****************************************************************************************************************************
GSM_MQTT_Medium.ino - Dead simple MQTT Client for GSM/GPRS shields
For ESP8266, ESP32, SAMD21/SAMD51, nRF52, SAM DUE, Teensy and STM32 with GSM modules
GSM_Generic is a library for the ESP8266, ESP32, SAMD21/SAMD51, nRF52, SAM DUE, Teensy and STM32 with GSM modules
Based on and modified from MKRGSM Library (https://github.com/arduino-libraries/MKRGSM)
Built by Khoi Hoang https://github.com/khoih-prog/GSM_Generic
Licensed under GNU Lesser General Public License
Copyright (C) 2017 Arduino AG (http://www.arduino.cc/)
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License
as published by the Free Software Foundation, either version 2.1 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program.
If not, see <https://www.gnu.org/licenses/>.
**********************************************************************************************************************************/
/****************************************************************************************************************************
You have to modify file ./libraries/Adafruit_MQTT_Library/Adafruit_MQTT.cpp as follows to avoid dtostrf error, if exists
#ifdef __cplusplus
extern "C" {
#endif
extern char* itoa(int value, char *string, int radix);
extern char* ltoa(long value, char *string, int radix);
extern char* utoa(unsigned value, char *string, int radix);
extern char* ultoa(unsigned long value, char *string, int radix);
#ifdef __cplusplus
} // extern "C"
#endif
//#if defined(ARDUINO_SAMD_ZERO) || defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_ARCH_SAMD)
#if !( ESP32 || ESP8266 || defined(CORE_TEENSY) || defined(STM32F1) || defined(STM32F2) || defined(STM32F3) || defined(STM32F4) || defined(STM32F7) || \
( defined(ARDUINO_ARCH_RP2040) && !defined(ARDUINO_ARCH_MBED) ) || ARDUINO_ARCH_SEEED_SAMD || ( defined(SEEED_WIO_TERMINAL) || defined(SEEED_XIAO_M0) || \
defined(SEEED_FEMTO_M0) || defined(Wio_Lite_MG126) || defined(WIO_GPS_BOARD) || defined(SEEEDUINO_ZERO) || defined(SEEEDUINO_LORAWAN) || defined(WIO_LTE_CAT) || \
defined(SEEED_GROVE_UI_WIRELESS) ) )
static char *dtostrf(double val, signed char width, unsigned char prec, char *sout)
{
char fmt[20];
sprintf(fmt, "%%%d.%df", width, prec);
sprintf(sout, fmt, val);
return sout;
}
#endif
*****************************************************************************************************************************/
#include "defines.h"
#define LOCAL_DEBUG true //false
//////////////////////////////////////////////////////
// Please enter your sensitive data in the Secret tab or arduino_secrets.h
// PIN Number
const char PINNUMBER[] = SECRET_PINNUMBER;
// APN data
const char GPRS_APN[] = SECRET_GPRS_APN;
const char GPRS_LOGIN[] = SECRET_GPRS_LOGIN;
const char GPRS_PASSWORD[] = SECRET_GPRS_PASSWORD;
//////////////////////////////////////////////////////
#include "Adafruit_MQTT.h" //https://github.com/adafruit/Adafruit_MQTT_Library
#include "Adafruit_MQTT_Client.h" //https://github.com/adafruit/Adafruit_MQTT_Library
#define AIO_SERVER_LEN 20
#define AIO_SERVERPORT_LEN 6
#define AIO_USERNAME_LEN 20
#define AIO_KEY_LEN 40
//
#define AIO_PUB_TOPIC_LEN 40
#define AIO_SUB_TOPIC_LEN 40
char AIO_SERVER [AIO_SERVER_LEN + 1] = "io.adafruit.com";
char AIO_SERVERPORT [AIO_SERVERPORT_LEN + 1] = "1883"; //1883, or 8883 for SSL
char AIO_USERNAME [AIO_USERNAME_LEN + 1] = "private";
char AIO_KEY [AIO_KEY_LEN + 1] = "private";
char AIO_PUB_TOPIC [AIO_PUB_TOPIC_LEN + 1] = "/feeds/Temperature";
char AIO_SUB_TOPIC [AIO_SUB_TOPIC_LEN + 1] = "/feeds/LED_Control";
//////////////////////////////////////////////////////
// initialize the library instance
GSMClient *client = NULL;
GPRS gprs;
GSM gsmAccess;
// connection state
bool connected = false;
// BaudRate to communicate to GSM/GPRS modem. If be limit to max 115200 inside modem
unsigned long baudRateSerialGSM = 115200;
//////////////////////////////////////////////////////
Adafruit_MQTT_Client *mqtt = NULL;
Adafruit_MQTT_Publish *Temperature = NULL;
Adafruit_MQTT_Subscribe *LED_Control = NULL;
// You have to get from a sensor. Here is just an example
uint32_t measuredTemp = 5;
#ifdef LED_BUILTIN
#define LED_PIN LED_BUILTIN
#else
#define LED_PIN 13
#endif
//////////////////////////////////////////////////////
void heartBeatPrint()
{
static int num = 1;
if (connected)
Serial.print("G"); // W means connected to GSM/GPRS
else
Serial.print("N"); // N means not connected to GSM
if (num == 40)
{
Serial.println();
num = 1;
}
else if (num++ % 5 == 0)
{
Serial.print(" ");
}
}
void publishMQTT()
{
MQTT_connect();
if (Temperature->publish(measuredTemp))
{
Serial.print(F("T")); // T means publishing OK
}
else
{
Serial.print(F("F")); // F means publishing failure
}
}
void subscribeMQTT()
{
Adafruit_MQTT_Subscribe *subscription;
MQTT_connect();
while ((subscription = mqtt->readSubscription(5000)))
{
if (subscription == LED_Control)
{
Serial.print(F("\nGot: "));
Serial.println((char *)LED_Control->lastread);
if (!strcmp((char*) LED_Control->lastread, "ON"))
{
digitalWrite(LED_PIN, HIGH);
}
else
{
digitalWrite(LED_PIN, LOW);
}
}
}
}
void check_status()
{
static unsigned long checkstatus_timeout = 0;
//KH
#define HEARTBEAT_INTERVAL 60000L
// Print GSM hearbeat, Publish MQTT Topic every HEARTBEAT_INTERVAL (60) seconds.
if ((millis() > checkstatus_timeout) || (checkstatus_timeout == 0))
{
if (connected)
{
// MQTT related jobs
publishMQTT();
subscribeMQTT();
}
heartBeatPrint();
checkstatus_timeout = millis() + HEARTBEAT_INTERVAL;
}
}
void deleteOldInstances()
{
// Delete previous instances
if (mqtt)
{
delete mqtt;
mqtt = NULL;
Serial.println(F("Deleting old MQTT object"));
}
if (Temperature)
{
delete Temperature;
Temperature = NULL;
Serial.println(F("Deleting old Temperature object"));
}
}
#define USE_GLOBAL_TOPIC true
#if USE_GLOBAL_TOPIC
String completePubTopic;
String completeSubTopic;
#endif
void createNewInstances()
{
if (!client)
{
client = new GSMClient;
if (client)
{
Serial.println(F("\nCreating new GSMClient client object OK"));
}
else
Serial.println(F("\nCreating new GSMClient client object failed"));
}
// Create new instances from new data
if (!mqtt)
{
// Setup the MQTT client class by passing in the GSM client and MQTT server and login details.
mqtt = new Adafruit_MQTT_Client(client, AIO_SERVER, atoi(AIO_SERVERPORT), AIO_USERNAME, AIO_KEY);
if (mqtt)
{
Serial.println(F("Creating new MQTT object OK"));
Serial.print(F("AIO_SERVER = ")); Serial.print(AIO_SERVER); Serial.print(F(", AIO_SERVERPORT = ")); Serial.print(AIO_SERVERPORT);
Serial.print(F("AIO_USERNAME = ")); Serial.print(AIO_USERNAME); Serial.print(F(", AIO_KEY = ")); Serial.print(AIO_KEY);
}
else
Serial.println(F("Creating new MQTT object failed"));
}
if (!Temperature)
{
#if USE_GLOBAL_TOPIC
completePubTopic = String(AIO_USERNAME) + String(AIO_PUB_TOPIC);
#else
// Must be static or global
static String completePubTopic = String(AIO_USERNAME) + String(AIO_PUB_TOPIC);
#endif
Temperature = new Adafruit_MQTT_Publish(mqtt, completePubTopic.c_str());
Serial.print(F("Creating new MQTT_Pub_Topic, Temperature = ")); Serial.println(completePubTopic);
if (Temperature)
{
Serial.println(F("Creating new Temperature object OK"));
Serial.print(F("Temperature MQTT_Pub_Topic = ")); Serial.println(completePubTopic);
}
else
Serial.println(F("Creating new Temperature object failed"));
}
if (!LED_Control)
{
#if USE_GLOBAL_TOPIC
completeSubTopic = String(AIO_USERNAME) + String(AIO_SUB_TOPIC);
#else
// Must be static or global
static String completeSubTopic = String(AIO_USERNAME) + String(AIO_SUB_TOPIC);
#endif
LED_Control = new Adafruit_MQTT_Subscribe(mqtt, completeSubTopic.c_str());
Serial.print(F("Creating new AIO_SUB_TOPIC, LED_Control = ")); Serial.println(completeSubTopic);
if (LED_Control)
{
Serial.println(F("Creating new LED_Control object OK"));
Serial.print(F("LED_Control AIO_SUB_TOPIC = ")); Serial.println(completeSubTopic);
mqtt->subscribe(LED_Control);
}
else
Serial.println(F("Creating new LED_Control object failed"));
}
}
void MQTT_connect()
{
int8_t ret;
createNewInstances();
// Return if already connected
if (mqtt->connected())
{
return;
}
#if LOCAL_DEBUG
Serial.println(F("\nConnecting to GSM MQTT (3 attempts)..."));
#endif
uint8_t attempt = 3;
while ( (ret = mqtt->connect()) )
{
// connect will return 0 for connected
Serial.println(mqtt->connectErrorString(ret));
#if LOCAL_DEBUG
Serial.println(F("Another attemtpt to connect to MQTT in 5 seconds..."));
#endif
mqtt->disconnect();
delay(5000); // wait 5 seconds
attempt--;
if (attempt == 0)
{
Serial.println(F("GSM MQTT connection failed. Continuing with program..."));
return;
}
}
#if LOCAL_DEBUG
Serial.println(F("GSM MQTT connection successful!"));
#endif
}
void connectToGPRS()
{
#define NUMBER_OF_CONNECTION_TRIES 10
uint8_t tryNumber = 1;
// After starting the modem with GSM.begin()
// attach the shield to the GPRS network with the APN, login and password
while ((gsmAccess.begin(baudRateSerialGSM, PINNUMBER) != GSM_READY) ||
(gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) != GPRS_READY))
{
// failed, retry
Serial.print("Connect GPRS failed, try = "); Serial.println(tryNumber);
if (++tryNumber > NUMBER_OF_CONNECTION_TRIES)
{
Serial.println("\nCan't connected to GPRS.");
connected = false;
return;
}
delay(1000);
}
connected = true;
//Get IP.
IPAddress LocalIP = gprs.getIPAddress();
Serial.print("\nConnected to GPRS. IP address = ");
Serial.println(LocalIP);
}
void setup()
{
// Debug console
Serial.begin(115200);
while (!Serial);
pinMode(LED_PIN, OUTPUT);
delay(200);
Serial.print(F("\nStart GSM_MQTT_Medium on ")); Serial.println(BOARD_NAME);
Serial.println(GSM_GENERIC_VERSION);
#if ( defined(DEBUG_GSM_GENERIC_PORT) && (_GSM_GENERIC_LOGLEVEL_ > 4) )
MODEM.debug(DEBUG_GSM_GENERIC_PORT);
#endif
connectToGPRS();
}
void loop()
{
if (gsmAccess.status() != GSM_READY || gprs.status() != GPRS_READY)
{
connectToGPRS();
}
check_status();
}