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.

141 lines
3.5 KiB

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