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.

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