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.

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