Skip to content

Commit

Permalink
MY_GATEWAY_TINYGSM support. GSM gateway using a modem module (#1097)
Browse files Browse the repository at this point in the history
Added TinyGSM driver and GatewayGSMMQTTClient example
  • Loading branch information
thucar authored and fallberg committed Apr 4, 2018
1 parent b5a2fc2 commit 4d90c00
Show file tree
Hide file tree
Showing 20 changed files with 5,862 additions and 6 deletions.
4 changes: 4 additions & 0 deletions .ci/arduino.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def buildMySensorsMicro(config, sketches, String key) {
if (sketches[sketch].path != config.library_root+'examples/GatewayESP8266/GatewayESP8266.ino' &&
sketches[sketch].path != config.library_root+'examples/GatewayESP8266MQTTClient/GatewayESP8266MQTTClient.ino' &&
sketches[sketch].path != config.library_root+'examples/GatewayESP8266OTA/GatewayESP8266OTA.ino' &&
sketches[sketch].path != config.library_root+'examples/GatewayGSMMQTTClient/GatewayGSMMQTTClient.ino' &&
sketches[sketch].path != config.library_root+'examples/SensebenderGatewaySerial/SensebenderGatewaySerial.ino') {
buildArduino(config, fqbn, sketches[sketch].path, key+'_MySensorsMicro')
}
Expand Down Expand Up @@ -78,6 +79,7 @@ def buildMySensorsGw(config, sketches, String key) {
sketches[sketch].path != config.library_root+'examples/GatewayESP8266/GatewayESP8266.ino' &&
sketches[sketch].path != config.library_root+'examples/GatewayESP8266MQTTClient/GatewayESP8266MQTTClient.ino' &&
sketches[sketch].path != config.library_root+'examples/GatewayESP8266OTA/GatewayESP8266OTA.ino' &&
sketches[sketch].path != config.library_root+'examples/GatewayGSMMQTTClient/GatewayGSMMQTTClient.ino' &&
sketches[sketch].path != config.library_root+'examples/GatewaySerialRS485/GatewaySerialRS485.ino' &&
sketches[sketch].path != config.library_root+'examples/MotionSensorRS485/MotionSensorRS485.ino') {
buildArduino(config, fqbn, sketches[sketch].path, key+'_MySensorsGw')
Expand Down Expand Up @@ -206,6 +208,7 @@ def buildEsp8266(config, sketches, String key) {
sketches[sketch].path != config.library_root+'examples/GatewaySerialRS485/GatewaySerialRS485.ino' &&
sketches[sketch].path != config.library_root+'examples/GatewayW5100/GatewayW5100.ino' &&
sketches[sketch].path != config.library_root+'examples/GatewayW5100MQTTClient/GatewayW5100MQTTClient.ino' &&
sketches[sketch].path != config.library_root+'examples/GatewayGSMMQTTClient/GatewayGSMMQTTClient.ino' &&
sketches[sketch].path != config.library_root+'examples/MotionSensorRS485/MotionSensorRS485.ino' &&
sketches[sketch].path != config.library_root+'examples/SensebenderGatewaySerial/SensebenderGatewaySerial.ino' &&
sketches[sketch].path != config.library_root+'examples/SoilMoistSensor/SoilMoistSensor.ino') {
Expand Down Expand Up @@ -241,6 +244,7 @@ def buildnRF5(config, sketches, String key) {
sketches[sketch].path != config.library_root+'examples/DustSensorDSM/DustSensorDSM.ino' &&
sketches[sketch].path != config.library_root+'examples/GatewayESP8266/GatewayESP8266.ino' &&
sketches[sketch].path != config.library_root+'examples/GatewayESP8266MQTTClient/GatewayESP8266MQTTClient.ino' &&
sketches[sketch].path != config.library_root+'examples/GatewayGSMMQTTClient/GatewayGSMMQTTClient.ino' &&
sketches[sketch].path != config.library_root+'examples/GatewayESP8266OTA/GatewayESP8266OTA.ino' &&
sketches[sketch].path != config.library_root+'examples/GatewaySerialRS485/GatewaySerialRS485.ino' &&
sketches[sketch].path != config.library_root+'examples/GatewayW5100/GatewayW5100.ino' &&
Expand Down
2 changes: 1 addition & 1 deletion .ci/doxygen.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def call(config) {
Documentation/doxygen.sh"""
warnings canComputeNew: false, canResolveRelativePaths: false,
defaultEncoding: '',
excludePattern: '''.*/sha204_library\\.h,.*/drivers/Linux/.*,.*/cores/esp8266/.*,hardware/.*''',
excludePattern: '''.*/sha204_library\\.h,.*/drivers/Linux/.*,.*/drivers/TinyGSM/.*,.*/cores/esp8266/.*,hardware/.*''',
failedTotalAll: '', healthy: '', includePattern: '', messagesPattern: '',
parserConfigurations: [[parserName: 'Doxygen', pattern: config.repository_root+'doxygen.log']],
unHealthy: '', unstableTotalAll: '0'
Expand Down
4 changes: 3 additions & 1 deletion .mystools/cppcheck/config/suppressions.cfg
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
ConfigurationNotChecked
unmatchedSuppression
// This suppression is because the problem is in an in-lined macro so in-line-suppressions does not appear to take effect
unreadVariable:*/MyHwNRF5.cpp
unreadVariable:*/MyHwNRF5.cpp
uninitMemberVar:*/TinyGsmFifo.h

47 changes: 46 additions & 1 deletion MyConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -1344,12 +1344,15 @@
* @brief Define this for Ethernet GW based on the ESP8266.
* @def MY_GATEWAY_LINUX
* @brief Define this for Ethernet GW based on Linux.
* @def MY_GATEWAY_TINYGSM
* @brief Define this for Ethernet GW based on GSM modems supported by TinyGSM library.
*/
// The gateway options available
//#define MY_GATEWAY_W5100
//#define MY_GATEWAY_ENC28J60
//#define MY_GATEWAY_ESP8266
//#define MY_GATEWAY_LINUX
//#define MY_GATEWAY_TINYGSM


/**
Expand Down Expand Up @@ -1888,7 +1891,7 @@
* MY_IS_GATEWAY is true when @ref MY_GATEWAY_FEATURE is set.
* MY_NODE_TYPE contain a string describing the class of sketch/node (gateway/repeater/node).
*/
#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)
#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)
#define MY_GATEWAY_FEATURE
#define MY_IS_GATEWAY (true)
#define MY_NODE_TYPE "GW"
Expand Down Expand Up @@ -2051,9 +2054,51 @@
#define MY_GATEWAY_ENC28J60
#define MY_GATEWAY_ESP8266
#define MY_GATEWAY_LINUX
#define MY_GATEWAY_TINYGSM
#define MY_IP_ADDRESS 192,168,178,66
#define MY_USE_UDP
#define MY_CONTROLLER_IP_ADDRESS 192,168,178,254
// TinyGSM
/**
* @def MY_GSM_APN
* @brief APN from your cell carrier / mobile provider. Example: 4g.tele2.se
*/
#define MY_GSM_APN
/**
* @def MY_GSM_BAUDRATE
* @brief Baudrate for your GSM modem. If left undefined, TinyGSM will try to auto detect the correct rate
*/
#define MY_GSM_BAUDRATE
/**
* @def MY_GSM_PIN
* @brief PIN code for your SIM card, if PIN lock is active.
*/
#define MY_GSM_PIN
/**
* @def MY_GSM_PSW
* @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
*/
#define MY_GSM_PSW
/**
* @def MY_GSM_RX
* @brief If defined, uses softSerial using defined pins (must also define MY_GSM_TX)
*/
#define MY_GSM_RX
/**
* @def MY_GSM_SSID
* @brief If using ESP8266 as WiFi modem, this is your network SSID
*/
#define MY_GSM_SSID
/**
* @def MY_GSM_TX
* @brief If defined, uses softSerial using defined pins (must also define MY_GSM_RX)
*/
#define MY_GSM_TX
/**
* @def MY_GSM_USR
* @brief Supplied by your cell carrier / mobile operator. If not required, leave undefined.
*/
#define MY_GSM_USR
// LED
#define MY_DEFAULT_ERR_LED_PIN
#define MY_DEFAULT_TX_LED_PIN
Expand Down
24 changes: 21 additions & 3 deletions MySensors.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,16 @@ MY_DEFAULT_RX_LED_PIN in your sketch instead to enable LEDs
#endif

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

#if defined(MY_GATEWAY_TINYGSM) && !defined(MY_GATEWAY_MQTT_CLIENT)
// TinyGSM currently only supports MQTTClient mode.
#error MY_GATEWAY_TINYGSM only works with MY_GATEWAY_MQTT_CLIENT
#endif

#if !defined(MY_MQTT_PUBLISH_TOPIC_PREFIX)
#error You must specify a topic publish prefix MY_MQTT_PUBLISH_TOPIC_PREFIX for this MQTT client
#endif
Expand All @@ -172,6 +177,10 @@ MY_DEFAULT_RX_LED_PIN in your sketch instead to enable LEDs
#include "core/MyGatewayTransport.cpp"
#include "core/MyProtocolMySensors.cpp"

#if defined(MY_GATEWAY_TINYGSM)
#include "drivers/TinyGSM/TinyGsmClient.h"
#endif

#if defined(MY_GATEWAY_LINUX)
#include "drivers/Linux/EthernetClient.h"
#include "drivers/Linux/EthernetServer.h"
Expand All @@ -192,7 +201,7 @@ MY_DEFAULT_RX_LED_PIN in your sketch instead to enable LEDs
#endif

#if !defined(MY_PORT)
#error You must define MY_PORT (controller or gatway port to open)
#error You must define MY_PORT (controller or gateway port to open)
#endif
#if defined(MY_GATEWAY_ESP8266)
// GATEWAY - ESP8266
Expand Down Expand Up @@ -307,6 +316,15 @@ MY_DEFAULT_RX_LED_PIN in your sketch instead to enable LEDs
#include "drivers/AVR/DigitalIO/DigitalIO.h"
#endif

// SOFTSERIAL
#if defined(MY_GSM_TX) != defined(MY_GSM_RX)
#error Both, MY_GSM_TX and MY_GSM_RX need to be defined when using SoftSerial
#endif

#if defined(MY_GATEWAY_TINYGSM) && !defined(SerialAT) && (!defined(MY_GSM_TX) || !defined(MY_GSM_RX))
#error You need to define either SerialAT or MY_GSM_RX and MY_GSM_TX pins
#endif

// POWER PIN
#ifndef DOXYGEN
#if defined(MY_RF24_POWER_PIN) || defined(MY_RFM69_POWER_PIN) || defined(MY_RFM95_POWER_PIN) || defined(MY_RADIO_NRF5_ESB)
Expand Down
58 changes: 58 additions & 0 deletions core/MyGatewayTransportMQTTClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,24 @@ IPAddress _subnetIp(255, 255, 255, 0);
uint8_t _MQTT_clientMAC[] = { MY_MAC_ADDRESS };
#endif /* End of MY_GATEWAY_ESP8266 */

#if defined(MY_GATEWAY_TINYGSM)
#if defined(MY_GSM_RX) && defined(MY_GSM_TX)
SoftwareSerial SerialAT(MY_GSM_RX, MY_GSM_TX);
#endif
static TinyGsm modem(SerialAT);
static TinyGsmClient _MQTT_gsmClient(modem);
static PubSubClient _MQTT_client(_MQTT_gsmClient);
#if defined(MY_GSM_BAUDRATE)
uint32_t rate = MY_GSM_BAUDRATE;
#else /* Else part of MY_GSM_BAUDRATE */
uint32_t rate = 0;
#endif /* End of MY_GSM_BAUDRATE */
#else /* Else part of MY_GATEWAY_TINYGSM */
static EthernetClient _MQTT_ethClient;
static PubSubClient _MQTT_client(_MQTT_ethClient);
#endif /* End of MY_GATEWAY_TINYGSM */


static bool _MQTT_connecting = true;
static bool _MQTT_available = false;
static MyMessage _MQTT_msg;
Expand Down Expand Up @@ -114,6 +130,8 @@ bool gatewayTransportConnect(void)
#if defined(MY_IP_ADDRESS)
_MQTT_ethClient.bind(_MQTT_clientIp);
#endif /* End of MY_IP_ADDRESS */
#elif defined(MY_GATEWAY_TINYGSM) /* Elif part of MY_GATEWAY_ESP8266 */
GATEWAY_DEBUG(PSTR("GWT:TPC:IP=%s\n"), modem.getLocalIP().c_str());
#else /* Else part of MY_GATEWAY_ESP8266 */
#if defined(MY_IP_ADDRESS)
Ethernet.begin(_MQTT_clientMAC, _MQTT_clientIp);
Expand All @@ -137,6 +155,46 @@ bool gatewayTransportConnect(void)
bool gatewayTransportInit(void)
{
_MQTT_connecting = true;

#if defined(MY_GATEWAY_TINYGSM)

#if !defined(MY_GSM_BAUDRATE)
rate = TinyGsmAutoBaud(SerialAT);
#endif /* End of MY_GSM_BAUDRATE */

SerialAT.begin(rate);
delay(3000);

modem.restart();

#if defined(MY_GSM_PIN) && !defined(TINY_GSM_MODEM_ESP8266)
modem.simUnlock(MY_GSM_PIN);
#endif /* End of MY_GSM_PIN */

#ifndef TINY_GSM_MODEM_ESP8266
if (!modem.waitForNetwork()) {
GATEWAY_DEBUG(PSTR("!GWT:TIN:ETH FAIL\n"));
while (true);
}
GATEWAY_DEBUG(PSTR("GWT:TIN:ETH OK\n"));

if (!modem.gprsConnect(MY_GSM_APN, MY_GSM_USR, MY_GSM_PSW)) {
GATEWAY_DEBUG(PSTR("!GWT:TIN:ETH FAIL\n"));
while (true);
}
GATEWAY_DEBUG(PSTR("GWT:TIN:ETH OK\n"));
delay(1000);
#else /* Else part of TINY_GSM_MODEM_ESP8266 */
if (!modem.networkConnect(MY_GSM_SSID, MY_GSM_PSW)) {
GATEWAY_DEBUG(PSTR("!GWT:TIN:ETH FAIL\n"));
while (true);
}
GATEWAY_DEBUG(PSTR("GWT:TIN:ETH OK\n"));
delay(1000);
#endif /* End of TINY_GSM_MODEM_ESP8266 */

#endif /* End of MY_GATEWAY_TINYGSM */

#if defined(MY_CONTROLLER_IP_ADDRESS)
_MQTT_client.setServer(_brokerIp, MY_PORT);
#else
Expand Down
Loading

0 comments on commit 4d90c00

Please sign in to comment.