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.

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