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