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.0 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
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 115200
  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 how you're planning to connect to the internet
  67. #define TINY_GSM_USE_GPRS false
  68. #define TINY_GSM_USE_WIFI true
  69. // set GSM PIN, if any
  70. #define GSM_PIN ""
  71. // Your GPRS credentials, if any
  72. const char apn[] = "YourAPN";
  73. const char gprsUser[] = "";
  74. const char gprsPass[] = "";
  75. // Your WiFi connection credentials, if applicable
  76. const char wifiSSID[] = "YourSSID";
  77. const char wifiPass[] = "YourWiFiPass";
  78. // MQTT details
  79. const char* broker = "broker.hivemq.com";
  80. const char* topicLed = "GsmClientTest/led";
  81. const char* topicInit = "GsmClientTest/init";
  82. const char* topicLedStatus = "GsmClientTest/ledStatus";
  83. #include <TinyGsmClient.h>
  84. #include <PubSubClient.h>
  85. // Just in case someone defined the wrong thing..
  86. #if TINY_GSM_USE_GPRS && not defined TINY_GSM_MODEM_HAS_GPRS
  87. #undef TINY_GSM_USE_GPRS
  88. #undef TINY_GSM_USE_WIFI
  89. #define TINY_GSM_USE_GPRS false
  90. #define TINY_GSM_USE_WIFI true
  91. #endif
  92. #if TINY_GSM_USE_WIFI && not defined TINY_GSM_MODEM_HAS_WIFI
  93. #undef TINY_GSM_USE_GPRS
  94. #undef TINY_GSM_USE_WIFI
  95. #define TINY_GSM_USE_GPRS true
  96. #define TINY_GSM_USE_WIFI false
  97. #endif
  98. #ifdef DUMP_AT_COMMANDS
  99. #include <StreamDebugger.h>
  100. StreamDebugger debugger(SerialAT, SerialMon);
  101. TinyGsm modem(debugger);
  102. #else
  103. TinyGsm modem(SerialAT);
  104. #endif
  105. TinyGsmClient client(modem);
  106. PubSubClient mqtt(client);
  107. #define LED_PIN 13
  108. int ledStatus = LOW;
  109. long lastReconnectAttempt = 0;
  110. void mqttCallback(char* topic, byte* payload, unsigned int len) {
  111. SerialMon.print("Message arrived [");
  112. SerialMon.print(topic);
  113. SerialMon.print("]: ");
  114. SerialMon.write(payload, len);
  115. SerialMon.println();
  116. // Only proceed if incoming message's topic matches
  117. if (String(topic) == topicLed) {
  118. ledStatus = !ledStatus;
  119. digitalWrite(LED_PIN, ledStatus);
  120. mqtt.publish(topicLedStatus, ledStatus ? "1" : "0");
  121. }
  122. }
  123. boolean mqttConnect() {
  124. SerialMon.print("Connecting to ");
  125. SerialMon.print(broker);
  126. // Connect to MQTT Broker
  127. boolean status = mqtt.connect("GsmClientTest");
  128. // Or, if you want to authenticate MQTT:
  129. //boolean status = mqtt.connect("GsmClientName", "mqtt_user", "mqtt_pass");
  130. if (status == false) {
  131. SerialMon.println(" fail");
  132. return false;
  133. }
  134. SerialMon.println(" success");
  135. mqtt.publish(topicInit, "GsmClientTest started");
  136. mqtt.subscribe(topicLed);
  137. return mqtt.connected();
  138. }
  139. void setup() {
  140. // Set console baud rate
  141. SerialMon.begin(115200);
  142. delay(10);
  143. pinMode(LED_PIN, OUTPUT);
  144. // !!!!!!!!!!!
  145. // Set your reset, enable, power pins here
  146. // !!!!!!!!!!!
  147. SerialMon.println("Wait...");
  148. // Set GSM module baud rate
  149. // TinyGsmAutoBaud(SerialAT,GSM_AUTOBAUD_MIN,GSM_AUTOBAUD_MAX);
  150. SerialAT.begin(9600);
  151. delay(3000);
  152. // Restart takes quite some time
  153. // To skip it, call init() instead of restart()
  154. SerialMon.println("Initializing modem...");
  155. modem.restart();
  156. // modem.init();
  157. String modemInfo = modem.getModemInfo();
  158. SerialMon.print("Modem Info: ");
  159. SerialMon.println(modemInfo);
  160. #if TINY_GSM_USE_GPRS
  161. // Unlock your SIM card with a PIN if needed
  162. if ( GSM_PIN && modem.getSimStatus() != 3 ) {
  163. modem.simUnlock(GSM_PIN);
  164. }
  165. #endif
  166. #if TINY_GSM_USE_WIFI
  167. // Wifi connection parameters must be set before waiting for the network
  168. SerialMon.print(F("Setting SSID/password..."));
  169. if (!modem.networkConnect(wifiSSID, wifiPass)) {
  170. SerialMon.println(" fail");
  171. delay(10000);
  172. return;
  173. }
  174. SerialMon.println(" success");
  175. #endif
  176. #if TINY_GSM_USE_GPRS && defined TINY_GSM_MODEM_XBEE
  177. // The XBee must run the gprsConnect function BEFORE waiting for network!
  178. modem.gprsConnect(apn, gprsUser, gprsPass);
  179. #endif
  180. SerialMon.print("Waiting for network...");
  181. if (!modem.waitForNetwork()) {
  182. SerialMon.println(" fail");
  183. delay(10000);
  184. return;
  185. }
  186. SerialMon.println(" success");
  187. if (modem.isNetworkConnected()) {
  188. SerialMon.println("Network connected");
  189. }
  190. #if TINY_GSM_USE_GPRS
  191. // GPRS connection parameters are usually set after network registration
  192. SerialMon.print(F("Connecting to "));
  193. SerialMon.print(apn);
  194. if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
  195. SerialMon.println(" fail");
  196. delay(10000);
  197. return;
  198. }
  199. SerialMon.println(" success");
  200. if (modem.isGprsConnected()) {
  201. SerialMon.println("GPRS connected");
  202. }
  203. #endif
  204. // MQTT Broker setup
  205. mqtt.setServer(broker, 1883);
  206. mqtt.setCallback(mqttCallback);
  207. }
  208. void loop() {
  209. if (!mqtt.connected()) {
  210. SerialMon.println("=== MQTT NOT CONNECTED ===");
  211. // Reconnect every 10 seconds
  212. unsigned long t = millis();
  213. if (t - lastReconnectAttempt > 10000L) {
  214. lastReconnectAttempt = t;
  215. if (mqttConnect()) {
  216. lastReconnectAttempt = 0;
  217. }
  218. }
  219. delay(100);
  220. return;
  221. }
  222. mqtt.loop();
  223. }