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.

244 lines
6.3 KiB

6 years ago
8 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
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. // Set serial for debug console (to the Serial Monitor, default speed 115200)
  48. #define SerialMon Serial
  49. // Set serial for AT commands (to the module)
  50. // Use Hardware Serial on Mega, Leonardo, Micro
  51. #define SerialAT Serial1
  52. // or Software Serial on Uno, Nano
  53. //#include <SoftwareSerial.h>
  54. //SoftwareSerial SerialAT(2, 3); // RX, TX
  55. // See all AT commands, if wanted
  56. //#define DUMP_AT_COMMANDS
  57. // Define the serial console for debug prints, if needed
  58. #define TINY_GSM_DEBUG SerialMon
  59. // Range to attempt to autobaud
  60. #define GSM_AUTOBAUD_MIN 9600
  61. #define GSM_AUTOBAUD_MAX 38400
  62. // Add a reception delay, if needed
  63. //#define TINY_GSM_YIELD() { delay(2); }
  64. #define TINY_GSM_USE_GPRS true
  65. #define TINY_GSM_USE_WIFI false
  66. // set GSM PIN, if any
  67. #define GSM_PIN ""
  68. // Your GPRS credentials
  69. // Leave empty, if missing user or pass
  70. const char apn[] = "YourAPN";
  71. const char gprsUser[] = "";
  72. const char gprsPass[] = "";
  73. const char wifiSSID[] = "YourSSID";
  74. const char wifiPass[] = "YourWiFiPass";
  75. // MQTT details
  76. const char* broker = "test.mosquitto.org";
  77. const char* topicLed = "GsmClientTest/led";
  78. const char* topicInit = "GsmClientTest/init";
  79. const char* topicLedStatus = "GsmClientTest/ledStatus";
  80. #include <TinyGsmClient.h>
  81. #include <PubSubClient.h>
  82. #ifdef DUMP_AT_COMMANDS
  83. #include <StreamDebugger.h>
  84. StreamDebugger debugger(SerialAT, SerialMon);
  85. TinyGsm modem(debugger);
  86. #else
  87. TinyGsm modem(SerialAT);
  88. #endif
  89. TinyGsmClient client(modem);
  90. PubSubClient mqtt(client);
  91. #define LED_PIN 13
  92. int ledStatus = LOW;
  93. long lastReconnectAttempt = 0;
  94. void setup() {
  95. // Set console baud rate
  96. SerialMon.begin(115200);
  97. delay(10);
  98. // Set your reset, enable, power pins here
  99. pinMode(LED_PIN, OUTPUT);
  100. pinMode(20, OUTPUT);
  101. digitalWrite(20, HIGH);
  102. pinMode(23, OUTPUT);
  103. digitalWrite(23, LOW);
  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(" OK");
  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. }