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.

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