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.

131 lines
3.3 KiB

  1. /**************************************************************
  2. *
  3. * For this example, you need to install PubSubClient library:
  4. * https://github.com/knolleary/pubsubclient/releases/latest
  5. * or from http://librarymanager/all#PubSubClient
  6. *
  7. * TinyGSM Getting Started guide:
  8. * http://tiny.cc/tiny-gsm-readme
  9. *
  10. **************************************************************
  11. * Use Mosquitto client tools to work with MQTT
  12. * Ubuntu/Linux: sudo apt-get install mosquitto-clients
  13. * Windows: https://mosquitto.org/download/
  14. *
  15. * Subscribe for messages:
  16. * mosquitto_sub -h test.mosquitto.org -t GsmClientTest/init -t GsmClientTest/ledStatus -q 1
  17. * Toggle led:
  18. * mosquitto_pub -h test.mosquitto.org -t GsmClientTest/led -q 1 -m "toggle"
  19. *
  20. * You can use Node-RED for wiring together MQTT-enabled devices
  21. * https://nodered.org/
  22. * Also, take a look at these additional Node-RED modules:
  23. * node-red-contrib-blynk-websockets
  24. * node-red-dashboard
  25. *
  26. **************************************************************/
  27. #include <TinyGsmClient.h>
  28. #include <PubSubClient.h>
  29. // Your GPRS credentials
  30. // Leave empty, if missing user or pass
  31. char apn[] = "YourAPN";
  32. char user[] = "";
  33. char pass[] = "";
  34. // Use Hardware Serial on Mega, Leonardo, Micro
  35. #define GsmSerial Serial1
  36. // or Software Serial on Uno, Nano
  37. //#include <SoftwareSerial.h>
  38. //SoftwareSerial GsmSerial(2, 3); // RX, TX
  39. TinyGsmClient gsm(GsmSerial);
  40. PubSubClient mqtt(gsm);
  41. const char* broker = "test.mosquitto.org";
  42. const char* topicLed = "GsmClientTest/led";
  43. const char* topicInit = "GsmClientTest/init";
  44. const char* topicLedStatus = "GsmClientTest/ledStatus";
  45. #define LED_PIN 13
  46. int ledStatus = LOW;
  47. long lastReconnectAttempt = 0;
  48. void setup() {
  49. pinMode(LED_PIN, OUTPUT);
  50. // Set console baud rate
  51. Serial.begin(115200);
  52. delay(10);
  53. // Set GSM module baud rate
  54. GsmSerial.begin(115200);
  55. delay(3000);
  56. // Restart takes quite some time
  57. // You can skip it in many cases
  58. Serial.println("Restarting modem...");
  59. gsm.restart();
  60. Serial.print("Connecting to ");
  61. Serial.print(apn);
  62. if (!gsm.networkConnect(apn, user, pass)) {
  63. Serial.println(" failed");
  64. while (true);
  65. }
  66. Serial.println(" OK");
  67. // MQTT Broker setup
  68. mqtt.setServer(broker, 1883);
  69. mqtt.setCallback(mqttCallback);
  70. }
  71. boolean mqttConnect() {
  72. Serial.print("Connecting to ");
  73. Serial.print(broker);
  74. if (!mqtt.connect("GsmClientTest")) {
  75. Serial.println(" failed");
  76. return false;
  77. }
  78. Serial.println(" OK");
  79. mqtt.publish(topicInit, "GsmClientTest started");
  80. mqtt.subscribe(topicLed);
  81. return mqtt.connected();
  82. }
  83. void loop() {
  84. if (mqtt.connected()) {
  85. mqtt.loop();
  86. } else {
  87. // Reconnect every 10 seconds
  88. unsigned long t = millis();
  89. if (t - lastReconnectAttempt > 10000L) {
  90. lastReconnectAttempt = t;
  91. if (mqttConnect()) {
  92. lastReconnectAttempt = 0;
  93. }
  94. }
  95. }
  96. }
  97. void mqttCallback(char* topic, byte* payload, unsigned int len) {
  98. Serial.print("Message arrived [");
  99. Serial.print(topic);
  100. Serial.print("]: ");
  101. Serial.write(payload, len);
  102. Serial.println();
  103. // Only proceed if incoming message's topic matches
  104. if (String(topic) == topicLed) {
  105. ledStatus = !ledStatus;
  106. digitalWrite(LED_PIN, ledStatus);
  107. mqtt.publish(topicLedStatus, ledStatus ? "1" : "0");
  108. }
  109. }