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.

245 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_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. // Set your reset, enable, power pins here
  100. pinMode(LED_PIN, OUTPUT);
  101. pinMode(20, OUTPUT);
  102. digitalWrite(20, HIGH);
  103. pinMode(23, OUTPUT);
  104. digitalWrite(23, LOW);
  105. SerialMon.println("Wait...");
  106. // Set GSM module baud rate
  107. SerialAT.begin(115200);
  108. delay(3000);
  109. // Restart takes quite some time
  110. // To skip it, call init() instead of restart()
  111. SerialMon.println("Initializing modem...");
  112. modem.restart();
  113. // modem.init();
  114. String modemInfo = modem.getModemInfo();
  115. SerialMon.print("Modem: ");
  116. SerialMon.println(modemInfo);
  117. #if TINY_GSM_USE_GPRS
  118. // Unlock your SIM card with a PIN if needed
  119. if ( GSM_PIN && modem.getSimStatus() != 3 ) {
  120. modem.simUnlock(GSM_PIN);
  121. }
  122. #endif
  123. #if defined TINY_GSM_USE_WIFI && defined TINY_GSM_MODEM_HAS_WIFI
  124. SerialMon.print(F("Setting SSID/password..."));
  125. if (!modem.networkConnect(wifiSSID, wifiPass)) {
  126. SerialMon.println(" fail");
  127. delay(10000);
  128. return;
  129. }
  130. SerialMon.println(" success");
  131. #endif
  132. #if TINY_GSM_USE_GPRS && defined TINY_GSM_MODEM_XBEE
  133. // The XBee must run the gprsConnect function BEFORE waiting for network!
  134. modem.gprsConnect(apn, gprsUser, gprsPass);
  135. #endif
  136. SerialMon.print("Waiting for network...");
  137. if (!modem.waitForNetwork()) {
  138. SerialMon.println(" fail");
  139. delay(10000);
  140. return;
  141. }
  142. SerialMon.println(" success");
  143. if (modem.isNetworkConnected()) {
  144. SerialMon.println("Network connected");
  145. }
  146. #if TINY_GSM_USE_GPRS && defined TINY_GSM_MODEM_HAS_GPRS
  147. SerialMon.print(F("Connecting to "));
  148. SerialMon.print(apn);
  149. if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
  150. SerialMon.println(" fail");
  151. delay(10000);
  152. return;
  153. }
  154. SerialMon.println(" success");
  155. #endif
  156. // MQTT Broker setup
  157. mqtt.setServer(broker, 1883);
  158. mqtt.setCallback(mqttCallback);
  159. }
  160. boolean mqttConnect() {
  161. SerialMon.print("Connecting to ");
  162. SerialMon.print(broker);
  163. // Connect to MQTT Broker
  164. boolean status = mqtt.connect("GsmClientTest");
  165. // Or, if you want to authenticate MQTT:
  166. //boolean status = mqtt.connect("GsmClientName", "mqtt_user", "mqtt_pass");
  167. if (status == false) {
  168. SerialMon.println(" fail");
  169. return false;
  170. }
  171. SerialMon.println(" success");
  172. mqtt.publish(topicInit, "GsmClientTest started");
  173. mqtt.subscribe(topicLed);
  174. return mqtt.connected();
  175. }
  176. void loop() {
  177. if (!mqtt.connected()) {
  178. SerialMon.println("=== MQTT NOT CONNECTED ===");
  179. // Reconnect every 10 seconds
  180. unsigned long t = millis();
  181. if (t - lastReconnectAttempt > 10000L) {
  182. lastReconnectAttempt = t;
  183. if (mqttConnect()) {
  184. lastReconnectAttempt = 0;
  185. }
  186. }
  187. delay(100);
  188. return;
  189. }
  190. mqtt.loop();
  191. }
  192. void mqttCallback(char* topic, byte* payload, unsigned int len) {
  193. SerialMon.print("Message arrived [");
  194. SerialMon.print(topic);
  195. SerialMon.print("]: ");
  196. SerialMon.write(payload, len);
  197. SerialMon.println();
  198. // Only proceed if incoming message's topic matches
  199. if (String(topic) == topicLed) {
  200. ledStatus = !ledStatus;
  201. digitalWrite(LED_PIN, ledStatus);
  202. mqtt.publish(topicLedStatus, ledStatus ? "1" : "0");
  203. }
  204. }