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.

155 lines
3.9 KiB

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