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.

150 lines
3.8 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_SIM900
  30. // #define TINY_GSM_MODEM_A6
  31. // #define TINY_GSM_MODEM_A7
  32. // #define TINY_GSM_MODEM_M590
  33. // #define TINY_GSM_MODEM_ESP8266
  34. // #define TINY_GSM_MODEM_XBEE
  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. // Unlock your SIM card with a PIN
  70. //modem.simUnlock("1234");
  71. Serial.print("Waiting for network...");
  72. if (!modem.waitForNetwork()) {
  73. Serial.println(" fail");
  74. while (true);
  75. }
  76. Serial.println(" OK");
  77. Serial.print("Connecting to ");
  78. Serial.print(apn);
  79. if (!modem.gprsConnect(apn, user, pass)) {
  80. Serial.println(" fail");
  81. while (true);
  82. }
  83. Serial.println(" OK");
  84. // MQTT Broker setup
  85. mqtt.setServer(broker, 1883);
  86. mqtt.setCallback(mqttCallback);
  87. }
  88. boolean mqttConnect() {
  89. Serial.print("Connecting to ");
  90. Serial.print(broker);
  91. if (!mqtt.connect("GsmClientTest")) {
  92. Serial.println(" fail");
  93. return false;
  94. }
  95. Serial.println(" OK");
  96. mqtt.publish(topicInit, "GsmClientTest started");
  97. mqtt.subscribe(topicLed);
  98. return mqtt.connected();
  99. }
  100. void loop() {
  101. if (mqtt.connected()) {
  102. mqtt.loop();
  103. } else {
  104. // Reconnect every 10 seconds
  105. unsigned long t = millis();
  106. if (t - lastReconnectAttempt > 10000L) {
  107. lastReconnectAttempt = t;
  108. if (mqttConnect()) {
  109. lastReconnectAttempt = 0;
  110. }
  111. }
  112. }
  113. }
  114. void mqttCallback(char* topic, byte* payload, unsigned int len) {
  115. Serial.print("Message arrived [");
  116. Serial.print(topic);
  117. Serial.print("]: ");
  118. Serial.write(payload, len);
  119. Serial.println();
  120. // Only proceed if incoming message's topic matches
  121. if (String(topic) == topicLed) {
  122. ledStatus = !ledStatus;
  123. digitalWrite(LED_PIN, ledStatus);
  124. mqtt.publish(topicLedStatus, ledStatus ? "1" : "0");
  125. }
  126. }