Skip to content

Commit 2737280

Browse files
authored
Merge pull request #23 from myDevicesIoT/feature/gsm-support
Feature/gsm support
2 parents 099fd57 + 98f9b85 commit 2737280

File tree

3 files changed

+195
-0
lines changed

3 files changed

+195
-0
lines changed

examples/Connections/GSM/GSM.ino

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
This example shows how to connect to Cayenne using a GSM device and send/receive sample data.
3+
4+
The CayenneMQTT Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.
5+
6+
Steps:
7+
1. Install the TinyGSM library (https://github.com/vshymanskyy/TinyGSM) from the Arduino Library Manager.
8+
2. Set the Cayenne authentication info to match the authentication info from the Dashboard.
9+
3. Uncomment the correct GSM modem type and set the GSM connection info, if needed.
10+
4. Compile and upload the sketch.
11+
5. A temporary widget will be automatically generated in the Cayenne Dashboard. To make the widget permanent click the plus sign on the widget.
12+
*/
13+
14+
//#define CAYENNE_DEBUG // Uncomment to show debug messages
15+
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
16+
17+
// Uncomment your modem type:
18+
#define TINY_GSM_MODEM_SIM800
19+
// #define TINY_GSM_MODEM_SIM808
20+
// #define TINY_GSM_MODEM_SIM900
21+
// #define TINY_GSM_MODEM_UBLOX
22+
// #define TINY_GSM_MODEM_BG96
23+
// #define TINY_GSM_MODEM_A6
24+
// #define TINY_GSM_MODEM_A7
25+
// #define TINY_GSM_MODEM_M590
26+
// #define TINY_GSM_MODEM_ESP8266
27+
// #define TINY_GSM_MODEM_XBEE
28+
29+
#include <CayenneMQTTGSM.h>
30+
31+
// This sketch uses a software serial connection.
32+
#include <SoftwareSerial.h>
33+
SoftwareSerial gsmSerial(2, 3); // RX, TX
34+
// If you are using a device that supports a hardware serial (Mega, Leonardo, etc.) and prefer to use
35+
// that you can comment out the above lines and uncomment the one below.
36+
//#define gsmSerial Serial1
37+
38+
// GSM connection info.
39+
char apn[] = ""; // Access point name. Leave empty if it is not needed.
40+
char gprsLogin[] = ""; // GPRS username. Leave empty if it is not needed.
41+
char gprsPassword[] = ""; // GPRS password. Leave empty if it is not needed.
42+
char pin[] = ""; // SIM pin number. Leave empty if it is not needed.
43+
44+
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
45+
char username[] = "MQTT_USERNAME";
46+
char password[] = "MQTT_PASSWORD";
47+
char clientID[] = "CLIENT_ID";
48+
49+
void setup() {
50+
Serial.begin(9600);
51+
// Auto-detect the GSM serial baud rate. You can manually set it instead if you want to save a bit of space.
52+
TinyGsmAutoBaud(gsmSerial);
53+
Cayenne.begin(username, password, clientID, gsmSerial, apn, gprsLogin, gprsPassword, pin);
54+
}
55+
56+
void loop() {
57+
Cayenne.loop();
58+
}
59+
60+
// Default function for sending sensor data at intervals to Cayenne.
61+
// You can also use functions for specific channels, e.g CAYENNE_OUT(1) for sending channel 1 data.
62+
CAYENNE_OUT_DEFAULT()
63+
{
64+
// Write data to Cayenne here. This example just sends the current uptime in milliseconds on virtual channel 0.
65+
Cayenne.virtualWrite(0, millis());
66+
// Some examples of other functions you can use to send data.
67+
//Cayenne.celsiusWrite(1, 22.0);
68+
//Cayenne.luxWrite(2, 700);
69+
//Cayenne.virtualWrite(3, 50, TYPE_PROXIMITY, UNIT_CENTIMETER);
70+
}
71+
72+
// Default function for processing actuator commands from the Cayenne Dashboard.
73+
// You can also use functions for specific channels, e.g CAYENNE_IN(1) for channel 1 commands.
74+
CAYENNE_IN_DEFAULT()
75+
{
76+
CAYENNE_LOG("Channel %u, value %s", request.channel, getValue.asString());
77+
//Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");
78+
}

src/CayenneMQTTGSM.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
The MIT License(MIT)
3+
4+
Cayenne Arduino Client Library
5+
Copyright (c) 2018 myDevices
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
8+
documentation files(the "Software"), to deal in the Software without restriction, including without limitation
9+
the rights to use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the Software,
10+
and to permit persons to whom the Software is furnished to do so, subject to the following conditions :
11+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
12+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
13+
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR
14+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
15+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16+
*/
17+
18+
#ifndef _CAYENNEMQTTGSM_h
19+
#define _CAYENNEMQTTGSM_h
20+
21+
#define INFO_CONNECTION "GSM"
22+
23+
#include <TinyGsmClient.h>
24+
#include "CayenneUtils/CayenneDefines.h"
25+
#include "CayenneMQTTGSMClient.h"
26+
27+
#endif

src/CayenneMQTTGSMClient.h

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
The MIT License(MIT)
3+
4+
Cayenne Arduino Client Library
5+
Copyright (c) 2018 myDevices
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
8+
documentation files(the "Software"), to deal in the Software without restriction, including without limitation
9+
the rights to use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the Software,
10+
and to permit persons to whom the Software is furnished to do so, subject to the following conditions :
11+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
12+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
13+
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR
14+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
15+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16+
*/
17+
18+
#ifndef _CAYENNEMQTTGSMCLIENT_h
19+
#define _CAYENNEMQTTGSMCLIENT_h
20+
21+
#include "CayenneArduinoMQTTClient.h"
22+
23+
24+
class CayenneMQTTGSMClient : public CayenneArduinoMQTTClient
25+
{
26+
public:
27+
/**
28+
* Begins Cayenne session and in the process establishes a GSM connection with the supplied ssid and WIFI password
29+
* @param username Cayenne username
30+
* @param password Cayenne password
31+
* @param clientID Cayennne client ID
32+
* @param serial Serial connection to GSM device
33+
* @param apn Access Point Name
34+
* @param gprsLogin GPRS login
35+
* @param gprsPassword GPRS password
36+
* @param pin SIM pin number
37+
*/
38+
void begin(const char* username, const char* password, const char* clientID, Stream& serial, const char* apn, const char* gprsLogin, const char* gprsPassword, const char* pin = "")
39+
{
40+
_modem = new TinyGsm(serial);
41+
_gsmClient.init(_modem);
42+
CAYENNE_LOG("Initializing modem");
43+
if (!_modem->begin()) {
44+
CAYENNE_LOG("Cannot init");
45+
}
46+
if (pin && pin[0] != 0) {
47+
CAYENNE_LOG("Unlocking SIM");
48+
_modem->simUnlock(pin);
49+
}
50+
CAYENNE_LOG("Waiting for network");
51+
while (!_modem->waitForNetwork()) {
52+
CAYENNE_LOG("Register failed");
53+
delay(500);
54+
}
55+
CAYENNE_LOG("Connecting to GPRS");
56+
while (!_modem->gprsConnect(apn, gprsLogin, gprsPassword)) {
57+
CAYENNE_LOG("Not connected");
58+
delay(500);
59+
}
60+
CAYENNE_LOG("Connected to GPRS");
61+
62+
// int csq = _modem->getSignalQuality();
63+
// CAYENNE_LOG("Signal quality: %d", csq);
64+
IPAddress local_ip = _modem->localIP();
65+
CAYENNE_LOG("IP: %d.%d.%d.%d", local_ip[0], local_ip[1], local_ip[2], local_ip[3]);
66+
CayenneArduinoMQTTClient::begin(_gsmClient, username, password, clientID);
67+
}
68+
69+
/**
70+
* Begins Cayenne session, assumes that the GSM is already up and running.
71+
* @param username Cayenne username
72+
* @param password Cayenne password
73+
* @param clientID Cayennne client ID
74+
*/
75+
void begin(const char* username, const char* password, const char* clientID)
76+
{
77+
IPAddress local_ip = _modem->localIP();
78+
CAYENNE_LOG("IP: %d.%d.%d.%d", local_ip[0], local_ip[1], local_ip[2], local_ip[3]);
79+
CayenneArduinoMQTTClient::begin(_gsmClient, username, password, clientID);
80+
}
81+
82+
83+
private:
84+
TinyGsmClient _gsmClient;
85+
TinyGsm* _modem;
86+
};
87+
88+
CayenneMQTTGSMClient Cayenne;
89+
90+
#endif

0 commit comments

Comments
 (0)