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.

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