|
| 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