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.

242 lines
6.2 KiB

6 years ago
8 years ago
5 years ago
6 years ago
6 years ago
8 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
8 years ago
5 years ago
8 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 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_SIM5360
  36. // #define TINY_GSM_MODEM_UBLOX
  37. // #define TINY_GSM_MODEM_SARAR4
  38. // #define TINY_GSM_MODEM_M95
  39. // #define TINY_GSM_MODEM_BG96
  40. // #define TINY_GSM_MODEM_A6
  41. // #define TINY_GSM_MODEM_A7
  42. // #define TINY_GSM_MODEM_M590
  43. // #define TINY_GSM_MODEM_MC60
  44. // #define TINY_GSM_MODEM_MC60E
  45. // #define TINY_GSM_MODEM_ESP8266
  46. // #define TINY_GSM_MODEM_XBEE
  47. // #define TINY_GSM_MODEM_SEQUANS_MONARCH
  48. // Set serial for debug console (to the Serial Monitor, default speed 115200)
  49. #define SerialMon Serial
  50. // Set serial for AT commands (to the module)
  51. // Use Hardware Serial on Mega, Leonardo, Micro
  52. #define SerialAT Serial1
  53. // or Software Serial on Uno, Nano
  54. //#include <SoftwareSerial.h>
  55. //SoftwareSerial SerialAT(2, 3); // RX, TX
  56. // See all AT commands, if wanted
  57. //#define DUMP_AT_COMMANDS
  58. // Define the serial console for debug prints, if needed
  59. #define TINY_GSM_DEBUG SerialMon
  60. // Range to attempt to autobaud
  61. #define GSM_AUTOBAUD_MIN 9600
  62. #define GSM_AUTOBAUD_MAX 38400
  63. // Add a reception delay, if needed
  64. //#define TINY_GSM_YIELD() { delay(2); }
  65. #define TINY_GSM_USE_GPRS true
  66. #define TINY_GSM_USE_WIFI false
  67. // set GSM PIN, if any
  68. #define GSM_PIN ""
  69. // Your GPRS credentials
  70. // Leave empty, if missing user or pass
  71. const char apn[] = "YourAPN";
  72. const char gprsUser[] = "";
  73. const char gprsPass[] = "";
  74. const char wifiSSID[] = "YourSSID";
  75. const char wifiPass[] = "YourWiFiPass";
  76. // MQTT details
  77. const char* broker = "test.mosquitto.org";
  78. const char* topicLed = "GsmClientTest/led";
  79. const char* topicInit = "GsmClientTest/init";
  80. const char* topicLedStatus = "GsmClientTest/ledStatus";
  81. #include <TinyGsmClient.h>
  82. #include <PubSubClient.h>
  83. #ifdef DUMP_AT_COMMANDS
  84. #include <StreamDebugger.h>
  85. StreamDebugger debugger(SerialAT, SerialMon);
  86. TinyGsm modem(debugger);
  87. #else
  88. TinyGsm modem(SerialAT);
  89. #endif
  90. TinyGsmClient client(modem);
  91. PubSubClient mqtt(client);
  92. #define LED_PIN 13
  93. int ledStatus = LOW;
  94. long lastReconnectAttempt = 0;
  95. void setup() {
  96. // Set console baud rate
  97. SerialMon.begin(115200);
  98. delay(10);
  99. pinMode(LED_PIN, OUTPUT);
  100. // !!!!!!!!!!!
  101. // Set your reset, enable, power pins here
  102. // !!!!!!!!!!!
  103. SerialMon.println("Wait...");
  104. // Set GSM module baud rate
  105. SerialAT.begin(115200);
  106. delay(3000);
  107. // Restart takes quite some time
  108. // To skip it, call init() instead of restart()
  109. SerialMon.println("Initializing modem...");
  110. modem.restart();
  111. // modem.init();
  112. String modemInfo = modem.getModemInfo();
  113. SerialMon.print("Modem: ");
  114. SerialMon.println(modemInfo);
  115. #if TINY_GSM_USE_GPRS
  116. // Unlock your SIM card with a PIN if needed
  117. if ( GSM_PIN && modem.getSimStatus() != 3 ) {
  118. modem.simUnlock(GSM_PIN);
  119. }
  120. #endif
  121. #if defined TINY_GSM_USE_WIFI && defined TINY_GSM_MODEM_HAS_WIFI
  122. SerialMon.print(F("Setting SSID/password..."));
  123. if (!modem.networkConnect(wifiSSID, wifiPass)) {
  124. SerialMon.println(" fail");
  125. delay(10000);
  126. return;
  127. }
  128. SerialMon.println(" success");
  129. #endif
  130. #if TINY_GSM_USE_GPRS && defined TINY_GSM_MODEM_XBEE
  131. // The XBee must run the gprsConnect function BEFORE waiting for network!
  132. modem.gprsConnect(apn, gprsUser, gprsPass);
  133. #endif
  134. SerialMon.print("Waiting for network...");
  135. if (!modem.waitForNetwork()) {
  136. SerialMon.println(" fail");
  137. delay(10000);
  138. return;
  139. }
  140. SerialMon.println(" success");
  141. if (modem.isNetworkConnected()) {
  142. SerialMon.println("Network connected");
  143. }
  144. #if TINY_GSM_USE_GPRS && defined TINY_GSM_MODEM_HAS_GPRS
  145. SerialMon.print(F("Connecting to "));
  146. SerialMon.print(apn);
  147. if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
  148. SerialMon.println(" fail");
  149. delay(10000);
  150. return;
  151. }
  152. SerialMon.println(" success");
  153. #endif
  154. // MQTT Broker setup
  155. mqtt.setServer(broker, 1883);
  156. mqtt.setCallback(mqttCallback);
  157. }
  158. boolean mqttConnect() {
  159. SerialMon.print("Connecting to ");
  160. SerialMon.print(broker);
  161. // Connect to MQTT Broker
  162. boolean status = mqtt.connect("GsmClientTest");
  163. // Or, if you want to authenticate MQTT:
  164. //boolean status = mqtt.connect("GsmClientName", "mqtt_user", "mqtt_pass");
  165. if (status == false) {
  166. SerialMon.println(" fail");
  167. return false;
  168. }
  169. SerialMon.println(" success");
  170. mqtt.publish(topicInit, "GsmClientTest started");
  171. mqtt.subscribe(topicLed);
  172. return mqtt.connected();
  173. }
  174. void loop() {
  175. if (!mqtt.connected()) {
  176. SerialMon.println("=== MQTT NOT CONNECTED ===");
  177. // Reconnect every 10 seconds
  178. unsigned long t = millis();
  179. if (t - lastReconnectAttempt > 10000L) {
  180. lastReconnectAttempt = t;
  181. if (mqttConnect()) {
  182. lastReconnectAttempt = 0;
  183. }
  184. }
  185. delay(100);
  186. return;
  187. }
  188. mqtt.loop();
  189. }
  190. void mqttCallback(char* topic, byte* payload, unsigned int len) {
  191. SerialMon.print("Message arrived [");
  192. SerialMon.print(topic);
  193. SerialMon.print("]: ");
  194. SerialMon.write(payload, len);
  195. SerialMon.println();
  196. // Only proceed if incoming message's topic matches
  197. if (String(topic) == topicLed) {
  198. ledStatus = !ledStatus;
  199. digitalWrite(LED_PIN, ledStatus);
  200. mqtt.publish(topicLedStatus, ledStatus ? "1" : "0");
  201. }
  202. }