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