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.

138 lines
3.4 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 SerialAT Serial1
  36. // or Software Serial on Uno, Nano
  37. //#include <SoftwareSerial.h>
  38. //SoftwareSerial SerialAT(2, 3); // RX, TX
  39. TinyGsm modem(SerialAT);
  40. TinyGsmClient client(modem);
  41. PubSubClient mqtt(client);
  42. const char* broker = "test.mosquitto.org";
  43. const char* topicLed = "GsmClientTest/led";
  44. const char* topicInit = "GsmClientTest/init";
  45. const char* topicLedStatus = "GsmClientTest/ledStatus";
  46. #define LED_PIN 13
  47. int ledStatus = LOW;
  48. long lastReconnectAttempt = 0;
  49. void setup() {
  50. pinMode(LED_PIN, OUTPUT);
  51. // Set console baud rate
  52. Serial.begin(115200);
  53. delay(10);
  54. // Set GSM module baud rate
  55. SerialAT.begin(115200);
  56. delay(3000);
  57. // Restart takes quite some time
  58. // You can skip it in many cases
  59. modem.restart();
  60. Serial.print("Waiting for network...");
  61. if (!modem.waitForNetwork()) {
  62. Serial.println(" fail");
  63. while (true);
  64. }
  65. Serial.println(" OK");
  66. Serial.print("Connecting to ");
  67. Serial.print(apn);
  68. if (!modem.gprsConnect(apn, user, pass)) {
  69. Serial.println(" fail");
  70. while (true);
  71. }
  72. Serial.println(" OK");
  73. // MQTT Broker setup
  74. mqtt.setServer(broker, 1883);
  75. mqtt.setCallback(mqttCallback);
  76. }
  77. boolean mqttConnect() {
  78. Serial.print("Connecting to ");
  79. Serial.print(broker);
  80. if (!mqtt.connect("GsmClientTest")) {
  81. Serial.println(" fail");
  82. return false;
  83. }
  84. Serial.println(" OK");
  85. mqtt.publish(topicInit, "GsmClientTest started");
  86. mqtt.subscribe(topicLed);
  87. return mqtt.connected();
  88. }
  89. void loop() {
  90. if (mqtt.connected()) {
  91. mqtt.loop();
  92. } else {
  93. // Reconnect every 10 seconds
  94. unsigned long t = millis();
  95. if (t - lastReconnectAttempt > 10000L) {
  96. lastReconnectAttempt = t;
  97. if (mqttConnect()) {
  98. lastReconnectAttempt = 0;
  99. }
  100. }
  101. }
  102. }
  103. void mqttCallback(char* topic, byte* payload, unsigned int len) {
  104. Serial.print("Message arrived [");
  105. Serial.print(topic);
  106. Serial.print("]: ");
  107. Serial.write(payload, len);
  108. Serial.println();
  109. // Only proceed if incoming message's topic matches
  110. if (String(topic) == topicLed) {
  111. ledStatus = !ledStatus;
  112. digitalWrite(LED_PIN, ledStatus);
  113. mqtt.publish(topicLedStatus, ledStatus ? "1" : "0");
  114. }
  115. }