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.

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