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.

290 lines
8.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
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. * This example connects to HiveMQ's showcase broker.
  14. *
  15. * You can quickly test sending and receiving messages from the HiveMQ webclient
  16. * available at http://www.hivemq.com/demos/websocket-client/.
  17. *
  18. * Subscribe to the topic GsmClientTest/ledStatus
  19. * Publish "toggle" to the topic GsmClientTest/led and the LED on your board
  20. * should toggle and you should see a new message published to
  21. * GsmClientTest/ledStatus with the newest LED status.
  22. *
  23. **************************************************************/
  24. // Select your modem:
  25. #define TINY_GSM_MODEM_SIM800
  26. // #define TINY_GSM_MODEM_SIM808
  27. // #define TINY_GSM_MODEM_SIM868
  28. // #define TINY_GSM_MODEM_SIM900
  29. // #define TINY_GSM_MODEM_SIM7000
  30. // #define TINY_GSM_MODEM_SIM7000SSL
  31. // #define TINY_GSM_MODEM_SIM7080
  32. // #define TINY_GSM_MODEM_SIM5360
  33. // #define TINY_GSM_MODEM_SIM7600
  34. // #define TINY_GSM_MODEM_UBLOX
  35. // #define TINY_GSM_MODEM_SARAR4
  36. // #define TINY_GSM_MODEM_M95
  37. // #define TINY_GSM_MODEM_BG96
  38. // #define TINY_GSM_MODEM_A6
  39. // #define TINY_GSM_MODEM_A7
  40. // #define TINY_GSM_MODEM_M590
  41. // #define TINY_GSM_MODEM_MC60
  42. // #define TINY_GSM_MODEM_MC60E
  43. // #define TINY_GSM_MODEM_ESP8266
  44. // #define TINY_GSM_MODEM_XBEE
  45. // #define TINY_GSM_MODEM_SEQUANS_MONARCH
  46. // Set serial for debug console (to the Serial Monitor, default speed 115200)
  47. #define SerialMon Serial
  48. // Set serial for AT commands (to the module)
  49. // Use Hardware Serial on Mega, Leonardo, Micro
  50. #ifndef __AVR_ATmega328P__
  51. #define SerialAT Serial1
  52. // or Software Serial on Uno, Nano
  53. #else
  54. #include <SoftwareSerial.h>
  55. SoftwareSerial SerialAT(2, 3); // RX, TX
  56. #endif
  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. // NOTE: DO NOT AUTOBAUD in production code. Once you've established
  63. // communication, set a fixed baud rate using modem.setBaud(#).
  64. #define GSM_AUTOBAUD_MIN 9600
  65. #define GSM_AUTOBAUD_MAX 115200
  66. // Add a reception delay, if needed.
  67. // This 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. // This is only needed for this example, not in other code.
  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) { modem.simUnlock(GSM_PIN); }
  167. #endif
  168. #if TINY_GSM_USE_WIFI
  169. // Wifi connection parameters must be set before waiting for the network
  170. SerialMon.print(F("Setting SSID/password..."));
  171. if (!modem.networkConnect(wifiSSID, wifiPass)) {
  172. SerialMon.println(" fail");
  173. delay(10000);
  174. return;
  175. }
  176. SerialMon.println(" success");
  177. #endif
  178. #if TINY_GSM_USE_GPRS && defined TINY_GSM_MODEM_XBEE
  179. // The XBee must run the gprsConnect function BEFORE waiting for network!
  180. modem.gprsConnect(apn, gprsUser, gprsPass);
  181. #endif
  182. SerialMon.print("Waiting for network...");
  183. if (!modem.waitForNetwork()) {
  184. SerialMon.println(" fail");
  185. delay(10000);
  186. return;
  187. }
  188. SerialMon.println(" success");
  189. if (modem.isNetworkConnected()) { SerialMon.println("Network connected"); }
  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()) { SerialMon.println("GPRS connected"); }
  201. #endif
  202. // MQTT Broker setup
  203. mqtt.setServer(broker, 1883);
  204. mqtt.setCallback(mqttCallback);
  205. }
  206. void loop() {
  207. // Make sure we're still registered on the network
  208. if (!modem.isNetworkConnected()) {
  209. SerialMon.println("Network disconnected");
  210. if (!modem.waitForNetwork(180000L, true)) {
  211. SerialMon.println(" fail");
  212. delay(10000);
  213. return;
  214. }
  215. if (modem.isNetworkConnected()) {
  216. SerialMon.println("Network re-connected");
  217. }
  218. #if TINY_GSM_USE_GPRS
  219. // and make sure GPRS/EPS is still connected
  220. if (!modem.isGprsConnected()) {
  221. SerialMon.println("GPRS disconnected!");
  222. SerialMon.print(F("Connecting to "));
  223. SerialMon.print(apn);
  224. if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
  225. SerialMon.println(" fail");
  226. delay(10000);
  227. return;
  228. }
  229. if (modem.isGprsConnected()) { SerialMon.println("GPRS reconnected"); }
  230. }
  231. #endif
  232. }
  233. if (!mqtt.connected()) {
  234. SerialMon.println("=== MQTT NOT CONNECTED ===");
  235. // Reconnect every 10 seconds
  236. uint32_t t = millis();
  237. if (t - lastReconnectAttempt > 10000L) {
  238. lastReconnectAttempt = t;
  239. if (mqttConnect()) { lastReconnectAttempt = 0; }
  240. }
  241. delay(100);
  242. return;
  243. }
  244. mqtt.loop();
  245. }