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.

178 lines
4.6 KiB

6 years ago
8 years ago
6 years ago
6 years ago
8 years ago
8 years ago
8 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_SIM900
  32. // #define TINY_GSM_MODEM_SIM808
  33. // #define TINY_GSM_MODEM_SIM868
  34. // #define TINY_GSM_MODEM_SIM7000
  35. // #define TINY_GSM_MODEM_UBLOX
  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. #include <TinyGsmClient.h>
  46. #include <PubSubClient.h>
  47. // Set serial for debug console (to the Serial Monitor, default speed 115200)
  48. #define SerialMon Serial
  49. // Use Hardware Serial on Mega, Leonardo, Micro
  50. #define SerialAT Serial1
  51. // or Software Serial on Uno, Nano
  52. //#include <SoftwareSerial.h>
  53. //SoftwareSerial SerialAT(2, 3); // RX, TX
  54. // Your GPRS credentials
  55. // Leave empty, if missing user or pass
  56. const char apn[] = "YourAPN";
  57. const char user[] = "";
  58. const char pass[] = "";
  59. // MQTT details
  60. const char* broker = "test.mosquitto.org";
  61. const char* topicLed = "GsmClientTest/led";
  62. const char* topicInit = "GsmClientTest/init";
  63. const char* topicLedStatus = "GsmClientTest/ledStatus";
  64. TinyGsm modem(SerialAT);
  65. TinyGsmClient client(modem);
  66. PubSubClient mqtt(client);
  67. #define LED_PIN 13
  68. int ledStatus = LOW;
  69. long lastReconnectAttempt = 0;
  70. void setup() {
  71. pinMode(LED_PIN, OUTPUT);
  72. // Set console baud rate
  73. SerialMon.begin(115200);
  74. delay(10);
  75. // Set GSM module baud rate
  76. SerialAT.begin(115200);
  77. delay(3000);
  78. // Restart takes quite some time
  79. // To skip it, call init() instead of restart()
  80. SerialMon.println("Initializing modem...");
  81. modem.restart();
  82. String modemInfo = modem.getModemInfo();
  83. SerialMon.print("Modem: ");
  84. SerialMon.println(modemInfo);
  85. // Unlock your SIM card with a PIN
  86. //modem.simUnlock("1234");
  87. SerialMon.print("Waiting for network...");
  88. if (!modem.waitForNetwork()) {
  89. SerialMon.println(" fail");
  90. while (true);
  91. }
  92. SerialMon.println(" OK");
  93. SerialMon.print("Connecting to ");
  94. SerialMon.print(apn);
  95. if (!modem.gprsConnect(apn, user, pass)) {
  96. SerialMon.println(" fail");
  97. while (true);
  98. }
  99. SerialMon.println(" OK");
  100. // MQTT Broker setup
  101. mqtt.setServer(broker, 1883);
  102. mqtt.setCallback(mqttCallback);
  103. }
  104. boolean mqttConnect() {
  105. SerialMon.print("Connecting to ");
  106. SerialMon.print(broker);
  107. // Connect to MQTT Broker
  108. boolean status = mqtt.connect("GsmClientTest");
  109. // Or, if you want to authenticate MQTT:
  110. //boolean status = mqtt.connect("GsmClientName", "mqtt_user", "mqtt_pass");
  111. if (status == false) {
  112. SerialMon.println(" fail");
  113. return false;
  114. }
  115. SerialMon.println(" OK");
  116. mqtt.publish(topicInit, "GsmClientTest started");
  117. mqtt.subscribe(topicLed);
  118. return mqtt.connected();
  119. }
  120. void loop() {
  121. if (!mqtt.connected()) {
  122. SerialMon.println("=== MQTT NOT CONNECTED ===");
  123. // Reconnect every 10 seconds
  124. unsigned long t = millis();
  125. if (t - lastReconnectAttempt > 10000L) {
  126. lastReconnectAttempt = t;
  127. if (mqttConnect()) {
  128. lastReconnectAttempt = 0;
  129. }
  130. }
  131. delay(100);
  132. return;
  133. }
  134. mqtt.loop();
  135. }
  136. void mqttCallback(char* topic, byte* payload, unsigned int len) {
  137. SerialMon.print("Message arrived [");
  138. SerialMon.print(topic);
  139. SerialMon.print("]: ");
  140. SerialMon.write(payload, len);
  141. SerialMon.println();
  142. // Only proceed if incoming message's topic matches
  143. if (String(topic) == topicLed) {
  144. ledStatus = !ledStatus;
  145. digitalWrite(LED_PIN, ledStatus);
  146. mqtt.publish(topicLedStatus, ledStatus ? "1" : "0");
  147. }
  148. }