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.

130 lines
3.2 KiB

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