You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

235 lines
5.9 KiB

6 years ago
8 years ago
6 years ago
6 years ago
8 years ago
8 years ago
8 years ago
6 years ago
6 years ago
6 years ago
  1. /**************************************************************
  2. *
  3. * For this example, you need to install PubSubClient library:
  4. * https://github.com/knolleary/pubsubclient
  5. * or from http://librarymanager/all#PubSubClient
  6. *
  7. * TinyGSM Getting Started guide:
  8. * https://tiny.cc/tinygsm-readme
  9. *
  10. * For more MQTT examples, see PubSubClient library
  11. *
  12. **************************************************************
  13. * Use Mosquitto client tools to work with MQTT
  14. * Ubuntu/Linux: sudo apt-get install mosquitto-clients
  15. * Windows: https://mosquitto.org/download/
  16. *
  17. * Subscribe for messages:
  18. * mosquitto_sub -h test.mosquitto.org -t GsmClientTest/init -t GsmClientTest/ledStatus -q 1
  19. * Toggle led:
  20. * mosquitto_pub -h test.mosquitto.org -t GsmClientTest/led -q 1 -m "toggle"
  21. *
  22. * You can use Node-RED for wiring together MQTT-enabled devices
  23. * https://nodered.org/
  24. * Also, take a look at these additional Node-RED modules:
  25. * node-red-contrib-blynk-ws
  26. * node-red-dashboard
  27. *
  28. **************************************************************/
  29. // Select your modem:
  30. #define TINY_GSM_MODEM_SIM800
  31. // #define TINY_GSM_MODEM_SIM808
  32. // #define TINY_GSM_MODEM_SIM868
  33. // #define TINY_GSM_MODEM_SIM900
  34. // #define TINY_GSM_MODEM_SIM7000
  35. // #define TINY_GSM_MODEM_UBLOX
  36. // #define TINY_GSM_MODEM_SARAR4
  37. // #define TINY_GSM_MODEM_M95
  38. // #define TINY_GSM_MODEM_BG96
  39. // #define TINY_GSM_MODEM_A6
  40. // #define TINY_GSM_MODEM_A7
  41. // #define TINY_GSM_MODEM_M590
  42. // #define TINY_GSM_MODEM_MC60
  43. // #define TINY_GSM_MODEM_MC60E
  44. // #define TINY_GSM_MODEM_ESP8266
  45. // #define TINY_GSM_MODEM_XBEE
  46. // #define TINY_GSM_MODEM_SEQUANS_MONARCH
  47. // See all AT commands, if wanted
  48. // #define DUMP_AT_COMMANDS
  49. // See the debugging, if wanted
  50. #define TINY_GSM_DEBUG SerialMon
  51. // Range to attempt to autobaud
  52. #define GSM_AUTOBAUD_MIN 9600
  53. #define GSM_AUTOBAUD_MAX 38400
  54. // Add a reception delay, if needed
  55. #define TINY_GSM_YIELD() { delay(2); }
  56. // Set serial for debug console (to the Serial Monitor, default speed 115200)
  57. #define SerialMon Serial
  58. // Set serial for AT commands (to the module)
  59. // Use Hardware Serial on Mega, Leonardo, Micro
  60. #define SerialAT Serial1
  61. // or Software Serial on Uno, Nano
  62. //#include <SoftwareSerial.h>
  63. //SoftwareSerial SerialAT(2, 3); // RX, TX
  64. // Your GPRS credentials
  65. // Leave empty, if missing user or pass
  66. const char apn[] = "YourAPN";
  67. const char user[] = "";
  68. const char pass[] = "";
  69. // MQTT details
  70. const char* broker = "test.mosquitto.org";
  71. const char* topicLed = "GsmClientTest/led";
  72. const char* topicInit = "GsmClientTest/init";
  73. const char* topicLedStatus = "GsmClientTest/ledStatus";
  74. #include <TinyGsmClient.h>
  75. #include <PubSubClient.h>
  76. #ifdef DUMP_AT_COMMANDS
  77. #include <StreamDebugger.h>
  78. StreamDebugger debugger(SerialAT, SerialMon);
  79. TinyGsm modem(debugger);
  80. #else
  81. TinyGsm modem(SerialAT);
  82. #endif
  83. TinyGsmClient client(modem);
  84. PubSubClient mqtt(client);
  85. #define LED_PIN 13
  86. int ledStatus = LOW;
  87. long lastReconnectAttempt = 0;
  88. void setup() {
  89. // Set console baud rate
  90. SerialMon.begin(115200);
  91. delay(10);
  92. // Set your reset, enable, power pins here
  93. pinMode(LED_PIN, OUTPUT);
  94. pinMode(20, OUTPUT);
  95. digitalWrite(20, HIGH);
  96. pinMode(23, OUTPUT);
  97. digitalWrite(23, LOW);
  98. SerialMon.println("Wait...");
  99. // Set GSM module baud rate
  100. SerialAT.begin(115200);
  101. delay(3000);
  102. // Restart takes quite some time
  103. // To skip it, call init() instead of restart()
  104. SerialMon.println("Initializing modem...");
  105. modem.restart();
  106. // modem.init();
  107. String modemInfo = modem.getModemInfo();
  108. SerialMon.print("Modem: ");
  109. SerialMon.println(modemInfo);
  110. // Unlock your SIM card with a PIN
  111. //modem.simUnlock("1234");
  112. #if TINY_GSM_USE_WIFI
  113. SerialMon.print(F("Setting SSID/password..."));
  114. if (!modem.networkConnect(wifiSSID, wifiPass)) {
  115. SerialMon.println(" fail");
  116. delay(10000);
  117. return;
  118. }
  119. SerialMon.println(" OK");
  120. #endif
  121. #if TINY_GSM_USE_GPRS && defined TINY_GSM_MODEM_XBEE
  122. // The XBee must run the gprsConnect function BEFORE waiting for network!
  123. modem.gprsConnect(apn, gprsUser, gprsPass);
  124. #endif
  125. SerialMon.print("Waiting for network...");
  126. if (!modem.waitForNetwork(240000L)) {
  127. SerialMon.println(" fail");
  128. delay(10000);
  129. return;
  130. }
  131. SerialMon.println(" OK");
  132. if (modem.isNetworkConnected()) {
  133. SerialMon.println("Network connected");
  134. }
  135. #if TINY_GSM_USE_GPRS && defined TINY_GSM_MODEM_HAS_GPRS
  136. SerialMon.print(F("Connecting to "));
  137. SerialMon.print(apn);
  138. if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
  139. SerialMon.println(" fail");
  140. delay(10000);
  141. return;
  142. }
  143. SerialMon.println(" OK");
  144. #endif
  145. // MQTT Broker setup
  146. mqtt.setServer(broker, 1883);
  147. mqtt.setCallback(mqttCallback);
  148. }
  149. boolean mqttConnect() {
  150. SerialMon.print("Connecting to ");
  151. SerialMon.print(broker);
  152. // Connect to MQTT Broker
  153. boolean status = mqtt.connect("GsmClientTest");
  154. // Or, if you want to authenticate MQTT:
  155. //boolean status = mqtt.connect("GsmClientName", "mqtt_user", "mqtt_pass");
  156. if (status == false) {
  157. SerialMon.println(" fail");
  158. return false;
  159. }
  160. SerialMon.println(" OK");
  161. mqtt.publish(topicInit, "GsmClientTest started");
  162. mqtt.subscribe(topicLed);
  163. return mqtt.connected();
  164. }
  165. void loop() {
  166. if (!mqtt.connected()) {
  167. SerialMon.println("=== MQTT NOT CONNECTED ===");
  168. // Reconnect every 10 seconds
  169. unsigned long t = millis();
  170. if (t - lastReconnectAttempt > 10000L) {
  171. lastReconnectAttempt = t;
  172. if (mqttConnect()) {
  173. lastReconnectAttempt = 0;
  174. }
  175. }
  176. delay(100);
  177. return;
  178. }
  179. mqtt.loop();
  180. }
  181. void mqttCallback(char* topic, byte* payload, unsigned int len) {
  182. SerialMon.print("Message arrived [");
  183. SerialMon.print(topic);
  184. SerialMon.print("]: ");
  185. SerialMon.write(payload, len);
  186. SerialMon.println();
  187. // Only proceed if incoming message's topic matches
  188. if (String(topic) == topicLed) {
  189. ledStatus = !ledStatus;
  190. digitalWrite(LED_PIN, ledStatus);
  191. mqtt.publish(topicLedStatus, ledStatus ? "1" : "0");
  192. }
  193. }