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.

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