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.

174 lines
4.4 KiB

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