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.

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