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.

167 lines
4.2 KiB

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