Skip to content

Commit 4d90c00

Browse files
thucarfallberg
authored andcommitted
MY_GATEWAY_TINYGSM support. GSM gateway using a modem module (#1097)
Added TinyGSM driver and GatewayGSMMQTTClient example
1 parent b5a2fc2 commit 4d90c00

20 files changed

+5862
-6
lines changed

.ci/arduino.groovy

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def buildMySensorsMicro(config, sketches, String key) {
4646
if (sketches[sketch].path != config.library_root+'examples/GatewayESP8266/GatewayESP8266.ino' &&
4747
sketches[sketch].path != config.library_root+'examples/GatewayESP8266MQTTClient/GatewayESP8266MQTTClient.ino' &&
4848
sketches[sketch].path != config.library_root+'examples/GatewayESP8266OTA/GatewayESP8266OTA.ino' &&
49+
sketches[sketch].path != config.library_root+'examples/GatewayGSMMQTTClient/GatewayGSMMQTTClient.ino' &&
4950
sketches[sketch].path != config.library_root+'examples/SensebenderGatewaySerial/SensebenderGatewaySerial.ino') {
5051
buildArduino(config, fqbn, sketches[sketch].path, key+'_MySensorsMicro')
5152
}
@@ -78,6 +79,7 @@ def buildMySensorsGw(config, sketches, String key) {
7879
sketches[sketch].path != config.library_root+'examples/GatewayESP8266/GatewayESP8266.ino' &&
7980
sketches[sketch].path != config.library_root+'examples/GatewayESP8266MQTTClient/GatewayESP8266MQTTClient.ino' &&
8081
sketches[sketch].path != config.library_root+'examples/GatewayESP8266OTA/GatewayESP8266OTA.ino' &&
82+
sketches[sketch].path != config.library_root+'examples/GatewayGSMMQTTClient/GatewayGSMMQTTClient.ino' &&
8183
sketches[sketch].path != config.library_root+'examples/GatewaySerialRS485/GatewaySerialRS485.ino' &&
8284
sketches[sketch].path != config.library_root+'examples/MotionSensorRS485/MotionSensorRS485.ino') {
8385
buildArduino(config, fqbn, sketches[sketch].path, key+'_MySensorsGw')
@@ -206,6 +208,7 @@ def buildEsp8266(config, sketches, String key) {
206208
sketches[sketch].path != config.library_root+'examples/GatewaySerialRS485/GatewaySerialRS485.ino' &&
207209
sketches[sketch].path != config.library_root+'examples/GatewayW5100/GatewayW5100.ino' &&
208210
sketches[sketch].path != config.library_root+'examples/GatewayW5100MQTTClient/GatewayW5100MQTTClient.ino' &&
211+
sketches[sketch].path != config.library_root+'examples/GatewayGSMMQTTClient/GatewayGSMMQTTClient.ino' &&
209212
sketches[sketch].path != config.library_root+'examples/MotionSensorRS485/MotionSensorRS485.ino' &&
210213
sketches[sketch].path != config.library_root+'examples/SensebenderGatewaySerial/SensebenderGatewaySerial.ino' &&
211214
sketches[sketch].path != config.library_root+'examples/SoilMoistSensor/SoilMoistSensor.ino') {
@@ -241,6 +244,7 @@ def buildnRF5(config, sketches, String key) {
241244
sketches[sketch].path != config.library_root+'examples/DustSensorDSM/DustSensorDSM.ino' &&
242245
sketches[sketch].path != config.library_root+'examples/GatewayESP8266/GatewayESP8266.ino' &&
243246
sketches[sketch].path != config.library_root+'examples/GatewayESP8266MQTTClient/GatewayESP8266MQTTClient.ino' &&
247+
sketches[sketch].path != config.library_root+'examples/GatewayGSMMQTTClient/GatewayGSMMQTTClient.ino' &&
244248
sketches[sketch].path != config.library_root+'examples/GatewayESP8266OTA/GatewayESP8266OTA.ino' &&
245249
sketches[sketch].path != config.library_root+'examples/GatewaySerialRS485/GatewaySerialRS485.ino' &&
246250
sketches[sketch].path != config.library_root+'examples/GatewayW5100/GatewayW5100.ino' &&

.ci/doxygen.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ def call(config) {
66
Documentation/doxygen.sh"""
77
warnings canComputeNew: false, canResolveRelativePaths: false,
88
defaultEncoding: '',
9-
excludePattern: '''.*/sha204_library\\.h,.*/drivers/Linux/.*,.*/cores/esp8266/.*,hardware/.*''',
9+
excludePattern: '''.*/sha204_library\\.h,.*/drivers/Linux/.*,.*/drivers/TinyGSM/.*,.*/cores/esp8266/.*,hardware/.*''',
1010
failedTotalAll: '', healthy: '', includePattern: '', messagesPattern: '',
1111
parserConfigurations: [[parserName: 'Doxygen', pattern: config.repository_root+'doxygen.log']],
1212
unHealthy: '', unstableTotalAll: '0'
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
ConfigurationNotChecked
22
unmatchedSuppression
33
// This suppression is because the problem is in an in-lined macro so in-line-suppressions does not appear to take effect
4-
unreadVariable:*/MyHwNRF5.cpp
4+
unreadVariable:*/MyHwNRF5.cpp
5+
uninitMemberVar:*/TinyGsmFifo.h
6+

MyConfig.h

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1344,12 +1344,15 @@
13441344
* @brief Define this for Ethernet GW based on the ESP8266.
13451345
* @def MY_GATEWAY_LINUX
13461346
* @brief Define this for Ethernet GW based on Linux.
1347+
* @def MY_GATEWAY_TINYGSM
1348+
* @brief Define this for Ethernet GW based on GSM modems supported by TinyGSM library.
13471349
*/
13481350
// The gateway options available
13491351
//#define MY_GATEWAY_W5100
13501352
//#define MY_GATEWAY_ENC28J60
13511353
//#define MY_GATEWAY_ESP8266
13521354
//#define MY_GATEWAY_LINUX
1355+
//#define MY_GATEWAY_TINYGSM
13531356

13541357

13551358
/**
@@ -1888,7 +1891,7 @@
18881891
* MY_IS_GATEWAY is true when @ref MY_GATEWAY_FEATURE is set.
18891892
* MY_NODE_TYPE contain a string describing the class of sketch/node (gateway/repeater/node).
18901893
*/
1891-
#if defined(MY_GATEWAY_SERIAL) || defined(MY_GATEWAY_W5100) || defined(MY_GATEWAY_ENC28J60) || defined(MY_GATEWAY_ESP8266) || defined(MY_GATEWAY_LINUX) || defined(MY_GATEWAY_MQTT_CLIENT)
1894+
#if defined(MY_GATEWAY_SERIAL) || defined(MY_GATEWAY_W5100) || defined(MY_GATEWAY_ENC28J60) || defined(MY_GATEWAY_ESP8266) || defined(MY_GATEWAY_LINUX) || defined(MY_GATEWAY_MQTT_CLIENT) || defined(MY_GATEWAY_TINYGSM)
18921895
#define MY_GATEWAY_FEATURE
18931896
#define MY_IS_GATEWAY (true)
18941897
#define MY_NODE_TYPE "GW"
@@ -2051,9 +2054,51 @@
20512054
#define MY_GATEWAY_ENC28J60
20522055
#define MY_GATEWAY_ESP8266
20532056
#define MY_GATEWAY_LINUX
2057+
#define MY_GATEWAY_TINYGSM
20542058
#define MY_IP_ADDRESS 192,168,178,66
20552059
#define MY_USE_UDP
20562060
#define MY_CONTROLLER_IP_ADDRESS 192,168,178,254
2061+
// TinyGSM
2062+
/**
2063+
* @def MY_GSM_APN
2064+
* @brief APN from your cell carrier / mobile provider. Example: 4g.tele2.se
2065+
*/
2066+
#define MY_GSM_APN
2067+
/**
2068+
* @def MY_GSM_BAUDRATE
2069+
* @brief Baudrate for your GSM modem. If left undefined, TinyGSM will try to auto detect the correct rate
2070+
*/
2071+
#define MY_GSM_BAUDRATE
2072+
/**
2073+
* @def MY_GSM_PIN
2074+
* @brief PIN code for your SIM card, if PIN lock is active.
2075+
*/
2076+
#define MY_GSM_PIN
2077+
/**
2078+
* @def MY_GSM_PSW
2079+
* @brief If using a GSM modem, this is the password supplied by your cell carrier / mobile provider. If using ESP8266 as a WiFi modem, this is your WiFi network password
2080+
*/
2081+
#define MY_GSM_PSW
2082+
/**
2083+
* @def MY_GSM_RX
2084+
* @brief If defined, uses softSerial using defined pins (must also define MY_GSM_TX)
2085+
*/
2086+
#define MY_GSM_RX
2087+
/**
2088+
* @def MY_GSM_SSID
2089+
* @brief If using ESP8266 as WiFi modem, this is your network SSID
2090+
*/
2091+
#define MY_GSM_SSID
2092+
/**
2093+
* @def MY_GSM_TX
2094+
* @brief If defined, uses softSerial using defined pins (must also define MY_GSM_RX)
2095+
*/
2096+
#define MY_GSM_TX
2097+
/**
2098+
* @def MY_GSM_USR
2099+
* @brief Supplied by your cell carrier / mobile operator. If not required, leave undefined.
2100+
*/
2101+
#define MY_GSM_USR
20572102
// LED
20582103
#define MY_DEFAULT_ERR_LED_PIN
20592104
#define MY_DEFAULT_TX_LED_PIN

MySensors.h

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,16 @@ MY_DEFAULT_RX_LED_PIN in your sketch instead to enable LEDs
152152
#endif
153153

154154
// GATEWAY - COMMON FUNCTIONS
155-
// We support MQTT Client using W5100, ESP8266 and Linux
156-
#if !defined(MY_GATEWAY_CLIENT_MODE)
155+
// We support MQTT Client using W5100, ESP8266, GSM modems supported by TinyGSM library and Linux
156+
#if !defined(MY_GATEWAY_CLIENT_MODE) && !defined(MY_GATEWAY_TINYGSM)
157157
#error You must specify MY_CONTROLLER_IP_ADDRESS or MY_CONTROLLER_URL_ADDRESS
158158
#endif
159159

160+
#if defined(MY_GATEWAY_TINYGSM) && !defined(MY_GATEWAY_MQTT_CLIENT)
161+
// TinyGSM currently only supports MQTTClient mode.
162+
#error MY_GATEWAY_TINYGSM only works with MY_GATEWAY_MQTT_CLIENT
163+
#endif
164+
160165
#if !defined(MY_MQTT_PUBLISH_TOPIC_PREFIX)
161166
#error You must specify a topic publish prefix MY_MQTT_PUBLISH_TOPIC_PREFIX for this MQTT client
162167
#endif
@@ -172,6 +177,10 @@ MY_DEFAULT_RX_LED_PIN in your sketch instead to enable LEDs
172177
#include "core/MyGatewayTransport.cpp"
173178
#include "core/MyProtocolMySensors.cpp"
174179

180+
#if defined(MY_GATEWAY_TINYGSM)
181+
#include "drivers/TinyGSM/TinyGsmClient.h"
182+
#endif
183+
175184
#if defined(MY_GATEWAY_LINUX)
176185
#include "drivers/Linux/EthernetClient.h"
177186
#include "drivers/Linux/EthernetServer.h"
@@ -192,7 +201,7 @@ MY_DEFAULT_RX_LED_PIN in your sketch instead to enable LEDs
192201
#endif
193202

194203
#if !defined(MY_PORT)
195-
#error You must define MY_PORT (controller or gatway port to open)
204+
#error You must define MY_PORT (controller or gateway port to open)
196205
#endif
197206
#if defined(MY_GATEWAY_ESP8266)
198207
// GATEWAY - ESP8266
@@ -307,6 +316,15 @@ MY_DEFAULT_RX_LED_PIN in your sketch instead to enable LEDs
307316
#include "drivers/AVR/DigitalIO/DigitalIO.h"
308317
#endif
309318

319+
// SOFTSERIAL
320+
#if defined(MY_GSM_TX) != defined(MY_GSM_RX)
321+
#error Both, MY_GSM_TX and MY_GSM_RX need to be defined when using SoftSerial
322+
#endif
323+
324+
#if defined(MY_GATEWAY_TINYGSM) && !defined(SerialAT) && (!defined(MY_GSM_TX) || !defined(MY_GSM_RX))
325+
#error You need to define either SerialAT or MY_GSM_RX and MY_GSM_TX pins
326+
#endif
327+
310328
// POWER PIN
311329
#ifndef DOXYGEN
312330
#if defined(MY_RF24_POWER_PIN) || defined(MY_RFM69_POWER_PIN) || defined(MY_RFM95_POWER_PIN) || defined(MY_RADIO_NRF5_ESB)

core/MyGatewayTransportMQTTClient.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,24 @@ IPAddress _subnetIp(255, 255, 255, 0);
5050
uint8_t _MQTT_clientMAC[] = { MY_MAC_ADDRESS };
5151
#endif /* End of MY_GATEWAY_ESP8266 */
5252

53+
#if defined(MY_GATEWAY_TINYGSM)
54+
#if defined(MY_GSM_RX) && defined(MY_GSM_TX)
55+
SoftwareSerial SerialAT(MY_GSM_RX, MY_GSM_TX);
56+
#endif
57+
static TinyGsm modem(SerialAT);
58+
static TinyGsmClient _MQTT_gsmClient(modem);
59+
static PubSubClient _MQTT_client(_MQTT_gsmClient);
60+
#if defined(MY_GSM_BAUDRATE)
61+
uint32_t rate = MY_GSM_BAUDRATE;
62+
#else /* Else part of MY_GSM_BAUDRATE */
63+
uint32_t rate = 0;
64+
#endif /* End of MY_GSM_BAUDRATE */
65+
#else /* Else part of MY_GATEWAY_TINYGSM */
5366
static EthernetClient _MQTT_ethClient;
5467
static PubSubClient _MQTT_client(_MQTT_ethClient);
68+
#endif /* End of MY_GATEWAY_TINYGSM */
69+
70+
5571
static bool _MQTT_connecting = true;
5672
static bool _MQTT_available = false;
5773
static MyMessage _MQTT_msg;
@@ -114,6 +130,8 @@ bool gatewayTransportConnect(void)
114130
#if defined(MY_IP_ADDRESS)
115131
_MQTT_ethClient.bind(_MQTT_clientIp);
116132
#endif /* End of MY_IP_ADDRESS */
133+
#elif defined(MY_GATEWAY_TINYGSM) /* Elif part of MY_GATEWAY_ESP8266 */
134+
GATEWAY_DEBUG(PSTR("GWT:TPC:IP=%s\n"), modem.getLocalIP().c_str());
117135
#else /* Else part of MY_GATEWAY_ESP8266 */
118136
#if defined(MY_IP_ADDRESS)
119137
Ethernet.begin(_MQTT_clientMAC, _MQTT_clientIp);
@@ -137,6 +155,46 @@ bool gatewayTransportConnect(void)
137155
bool gatewayTransportInit(void)
138156
{
139157
_MQTT_connecting = true;
158+
159+
#if defined(MY_GATEWAY_TINYGSM)
160+
161+
#if !defined(MY_GSM_BAUDRATE)
162+
rate = TinyGsmAutoBaud(SerialAT);
163+
#endif /* End of MY_GSM_BAUDRATE */
164+
165+
SerialAT.begin(rate);
166+
delay(3000);
167+
168+
modem.restart();
169+
170+
#if defined(MY_GSM_PIN) && !defined(TINY_GSM_MODEM_ESP8266)
171+
modem.simUnlock(MY_GSM_PIN);
172+
#endif /* End of MY_GSM_PIN */
173+
174+
#ifndef TINY_GSM_MODEM_ESP8266
175+
if (!modem.waitForNetwork()) {
176+
GATEWAY_DEBUG(PSTR("!GWT:TIN:ETH FAIL\n"));
177+
while (true);
178+
}
179+
GATEWAY_DEBUG(PSTR("GWT:TIN:ETH OK\n"));
180+
181+
if (!modem.gprsConnect(MY_GSM_APN, MY_GSM_USR, MY_GSM_PSW)) {
182+
GATEWAY_DEBUG(PSTR("!GWT:TIN:ETH FAIL\n"));
183+
while (true);
184+
}
185+
GATEWAY_DEBUG(PSTR("GWT:TIN:ETH OK\n"));
186+
delay(1000);
187+
#else /* Else part of TINY_GSM_MODEM_ESP8266 */
188+
if (!modem.networkConnect(MY_GSM_SSID, MY_GSM_PSW)) {
189+
GATEWAY_DEBUG(PSTR("!GWT:TIN:ETH FAIL\n"));
190+
while (true);
191+
}
192+
GATEWAY_DEBUG(PSTR("GWT:TIN:ETH OK\n"));
193+
delay(1000);
194+
#endif /* End of TINY_GSM_MODEM_ESP8266 */
195+
196+
#endif /* End of MY_GATEWAY_TINYGSM */
197+
140198
#if defined(MY_CONTROLLER_IP_ADDRESS)
141199
_MQTT_client.setServer(_brokerIp, MY_PORT);
142200
#else

0 commit comments

Comments
 (0)