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.

177 lines
4.5 KiB

6 years ago
8 years ago
6 years ago
6 years ago
8 years ago
8 years ago
8 years ago
6 years ago
6 years ago
6 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. * https://tiny.cc/tinygsm-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-ws
  26. * node-red-dashboard
  27. *
  28. **************************************************************/
  29. // Select your modem:
  30. #define TINY_GSM_MODEM_SIM800
  31. // #define TINY_GSM_MODEM_SIM900
  32. // #define TINY_GSM_MODEM_SIM808
  33. // #define TINY_GSM_MODEM_SIM868
  34. // #define TINY_GSM_MODEM_UBLOX
  35. // #define TINY_GSM_MODEM_M95
  36. // #define TINY_GSM_MODEM_BG96
  37. // #define TINY_GSM_MODEM_A6
  38. // #define TINY_GSM_MODEM_A7
  39. // #define TINY_GSM_MODEM_M590
  40. // #define TINY_GSM_MODEM_MC60
  41. // #define TINY_GSM_MODEM_MC60E
  42. // #define TINY_GSM_MODEM_ESP8266
  43. // #define TINY_GSM_MODEM_XBEE
  44. #include <TinyGsmClient.h>
  45. #include <PubSubClient.h>
  46. // Set serial for debug console (to the Serial Monitor, default speed 115200)
  47. #define SerialMon Serial
  48. // Use Hardware Serial on Mega, Leonardo, Micro
  49. #define SerialAT Serial1
  50. // or Software Serial on Uno, Nano
  51. //#include <SoftwareSerial.h>
  52. //SoftwareSerial SerialAT(2, 3); // RX, TX
  53. // Your GPRS credentials
  54. // Leave empty, if missing user or pass
  55. const char apn[] = "YourAPN";
  56. const char user[] = "";
  57. const char pass[] = "";
  58. // MQTT details
  59. const char* broker = "test.mosquitto.org";
  60. const char* topicLed = "GsmClientTest/led";
  61. const char* topicInit = "GsmClientTest/init";
  62. const char* topicLedStatus = "GsmClientTest/ledStatus";
  63. TinyGsm modem(SerialAT);
  64. TinyGsmClient client(modem);
  65. PubSubClient mqtt(client);
  66. #define LED_PIN 13
  67. int ledStatus = LOW;
  68. long lastReconnectAttempt = 0;
  69. void setup() {
  70. pinMode(LED_PIN, OUTPUT);
  71. // Set console baud rate
  72. SerialMon.begin(115200);
  73. delay(10);
  74. // Set GSM module baud rate
  75. SerialAT.begin(115200);
  76. delay(3000);
  77. // Restart takes quite some time
  78. // To skip it, call init() instead of restart()
  79. SerialMon.println("Initializing modem...");
  80. modem.restart();
  81. String modemInfo = modem.getModemInfo();
  82. SerialMon.print("Modem: ");
  83. SerialMon.println(modemInfo);
  84. // Unlock your SIM card with a PIN
  85. //modem.simUnlock("1234");
  86. SerialMon.print("Waiting for network...");
  87. if (!modem.waitForNetwork()) {
  88. SerialMon.println(" fail");
  89. while (true);
  90. }
  91. SerialMon.println(" OK");
  92. SerialMon.print("Connecting to ");
  93. SerialMon.print(apn);
  94. if (!modem.gprsConnect(apn, user, pass)) {
  95. SerialMon.println(" fail");
  96. while (true);
  97. }
  98. SerialMon.println(" OK");
  99. // MQTT Broker setup
  100. mqtt.setServer(broker, 1883);
  101. mqtt.setCallback(mqttCallback);
  102. }
  103. boolean mqttConnect() {
  104. SerialMon.print("Connecting to ");
  105. SerialMon.print(broker);
  106. // Connect to MQTT Broker
  107. boolean status = mqtt.connect("GsmClientTest");
  108. // Or, if you want to authenticate MQTT:
  109. //boolean status = mqtt.connect("GsmClientName", "mqtt_user", "mqtt_pass");
  110. if (status == false) {
  111. SerialMon.println(" fail");
  112. return false;
  113. }
  114. SerialMon.println(" OK");
  115. mqtt.publish(topicInit, "GsmClientTest started");
  116. mqtt.subscribe(topicLed);
  117. return mqtt.connected();
  118. }
  119. void loop() {
  120. if (!mqtt.connected()) {
  121. SerialMon.println("=== MQTT NOT CONNECTED ===");
  122. // Reconnect every 10 seconds
  123. unsigned long t = millis();
  124. if (t - lastReconnectAttempt > 10000L) {
  125. lastReconnectAttempt = t;
  126. if (mqttConnect()) {
  127. lastReconnectAttempt = 0;
  128. }
  129. }
  130. delay(100);
  131. return;
  132. }
  133. mqtt.loop();
  134. }
  135. void mqttCallback(char* topic, byte* payload, unsigned int len) {
  136. SerialMon.print("Message arrived [");
  137. SerialMon.print(topic);
  138. SerialMon.print("]: ");
  139. SerialMon.write(payload, len);
  140. SerialMon.println();
  141. // Only proceed if incoming message's topic matches
  142. if (String(topic) == topicLed) {
  143. ledStatus = !ledStatus;
  144. digitalWrite(LED_PIN, ledStatus);
  145. mqtt.publish(topicLedStatus, ledStatus ? "1" : "0");
  146. }
  147. }