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.

163 lines
4.1 KiB

8 years ago
8 years ago
8 years ago
8 years ago
7 years ago
  1. /**************************************************************
  2. *
  3. * For this example, you need to install PubSubClient library:
  4. * https://github.com/knolleary/pubsubclient
  5. * or from http://librarymanager/all#PubSubClient
  6. *
  7. * TinyGSM Getting Started guide:
  8. * http://tiny.cc/tiny-gsm-readme
  9. *
  10. * For more MQTT examples, see PubSubClient library
  11. *
  12. **************************************************************
  13. * Use Mosquitto client tools to work with MQTT
  14. * Ubuntu/Linux: sudo apt-get install mosquitto-clients
  15. * Windows: https://mosquitto.org/download/
  16. *
  17. * Subscribe for messages:
  18. * mosquitto_sub -h test.mosquitto.org -t GsmClientTest/init -t GsmClientTest/ledStatus -q 1
  19. * Toggle led:
  20. * mosquitto_pub -h test.mosquitto.org -t GsmClientTest/led -q 1 -m "toggle"
  21. *
  22. * You can use Node-RED for wiring together MQTT-enabled devices
  23. * https://nodered.org/
  24. * Also, take a look at these additional Node-RED modules:
  25. * node-red-contrib-blynk-websockets
  26. * node-red-dashboard
  27. *
  28. **************************************************************/
  29. // Select your modem:
  30. #define TINY_GSM_MODEM_SIM800
  31. // #define TINY_GSM_MODEM_SIM808
  32. // #define TINY_GSM_MODEM_SIM900
  33. // #define TINY_GSM_MODEM_A6
  34. // #define TINY_GSM_MODEM_A7
  35. // #define TINY_GSM_MODEM_M590
  36. // #define TINY_GSM_MODEM_ESP8266
  37. // #define TINY_GSM_MODEM_XBEE
  38. #include <TinyGsmClient.h>
  39. #include <PubSubClient.h>
  40. // Set serial for debug console (to the Serial Monitor, default speed 115200)
  41. #define SerialMon Serial
  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. // Your GPRS credentials
  48. // Leave empty, if missing user or pass
  49. const char apn[] = "YourAPN";
  50. const char user[] = "";
  51. const char pass[] = "";
  52. // MQTT details
  53. const char* broker = "test.mosquitto.org";
  54. const char* topicLed = "GsmClientTest/led";
  55. const char* topicInit = "GsmClientTest/init";
  56. const char* topicLedStatus = "GsmClientTest/ledStatus";
  57. TinyGsm modem(SerialAT);
  58. TinyGsmClient client(modem);
  59. PubSubClient mqtt(client);
  60. #define LED_PIN 13
  61. int ledStatus = LOW;
  62. long lastReconnectAttempt = 0;
  63. void setup() {
  64. pinMode(LED_PIN, OUTPUT);
  65. // Set console baud rate
  66. SerialMon.begin(115200);
  67. delay(10);
  68. // Set GSM module baud rate
  69. SerialAT.begin(115200);
  70. delay(3000);
  71. // Restart takes quite some time
  72. // To skip it, call init() instead of restart()
  73. SerialMon.println("Initializing modem...");
  74. modem.restart();
  75. String modemInfo = modem.getModemInfo();
  76. SerialMon.print("Modem: ");
  77. SerialMon.println(modemInfo);
  78. // Unlock your SIM card with a PIN
  79. //modem.simUnlock("1234");
  80. SerialMon.print("Waiting for network...");
  81. if (!modem.waitForNetwork()) {
  82. SerialMon.println(" fail");
  83. while (true);
  84. }
  85. SerialMon.println(" OK");
  86. SerialMon.print("Connecting to ");
  87. SerialMon.print(apn);
  88. if (!modem.gprsConnect(apn, user, pass)) {
  89. SerialMon.println(" fail");
  90. while (true);
  91. }
  92. SerialMon.println(" OK");
  93. // MQTT Broker setup
  94. mqtt.setServer(broker, 1883);
  95. mqtt.setCallback(mqttCallback);
  96. }
  97. boolean mqttConnect() {
  98. SerialMon.print("Connecting to ");
  99. SerialMon.print(broker);
  100. if (!mqtt.connect("GsmClientTest")) {
  101. SerialMon.println(" fail");
  102. return false;
  103. }
  104. SerialMon.println(" OK");
  105. mqtt.publish(topicInit, "GsmClientTest started");
  106. mqtt.subscribe(topicLed);
  107. return mqtt.connected();
  108. }
  109. void loop() {
  110. if (mqtt.connected()) {
  111. mqtt.loop();
  112. } else {
  113. // Reconnect every 10 seconds
  114. unsigned long t = millis();
  115. if (t - lastReconnectAttempt > 10000L) {
  116. lastReconnectAttempt = t;
  117. if (mqttConnect()) {
  118. lastReconnectAttempt = 0;
  119. }
  120. }
  121. }
  122. }
  123. void mqttCallback(char* topic, byte* payload, unsigned int len) {
  124. SerialMon.print("Message arrived [");
  125. SerialMon.print(topic);
  126. SerialMon.print("]: ");
  127. SerialMon.write(payload, len);
  128. SerialMon.println();
  129. // Only proceed if incoming message's topic matches
  130. if (String(topic) == topicLed) {
  131. ledStatus = !ledStatus;
  132. digitalWrite(LED_PIN, ledStatus);
  133. mqtt.publish(topicLedStatus, ledStatus ? "1" : "0");
  134. }
  135. }