Advertisement
babyyoda_

MqrrClient Example

Jun 8th, 2021
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.  * This example connects to HiveMQ's showcase broker.
  14.  *
  15.  * You can quickly test sending and receiving messages from the HiveMQ webclient
  16.  * available at http://www.hivemq.com/demos/websocket-client/.
  17.  *
  18.  * Subscribe to the topic GsmClientTest/ledStatus
  19.  * Publish "toggle" to the topic GsmClientTest/led and the LED on your board
  20.  * should toggle and you should see a new message published to
  21.  * GsmClientTest/ledStatus with the newest LED status.
  22.  *
  23.  **************************************************************/
  24.  
  25. // Select your modem:
  26. //#define TINY_GSM_MODEM_SIM800
  27. // #define TINY_GSM_MODEM_SIM808
  28. // #define TINY_GSM_MODEM_SIM868
  29.  #define TINY_GSM_MODEM_SIM900
  30. // #define TINY_GSM_MODEM_SIM7000
  31. // #define TINY_GSM_MODEM_SIM7000SSL
  32. // #define TINY_GSM_MODEM_SIM7080
  33. // #define TINY_GSM_MODEM_SIM5360
  34. // #define TINY_GSM_MODEM_SIM7600
  35. // #define TINY_GSM_MODEM_UBLOX
  36. // #define TINY_GSM_MODEM_SARAR4
  37. // #define TINY_GSM_MODEM_M95
  38. // #define TINY_GSM_MODEM_BG96
  39. // #define TINY_GSM_MODEM_A6
  40. // #define TINY_GSM_MODEM_A7
  41. // #define TINY_GSM_MODEM_M590
  42. // #define TINY_GSM_MODEM_MC60
  43. // #define TINY_GSM_MODEM_MC60E
  44. // #define TINY_GSM_MODEM_ESP8266
  45. // #define TINY_GSM_MODEM_XBEE
  46. // #define TINY_GSM_MODEM_SEQUANS_MONARCH
  47.  
  48. // Set serial for debug console (to the Serial Monitor, default speed 115200)
  49. #define SerialMon Serial
  50.  
  51. // Set serial for AT commands (to the module)
  52. // Use Hardware Serial on Mega, Leonardo, Micro
  53. #ifndef __AVR_ATmega328P__
  54. #define SerialAT Serial1
  55.  
  56. // or Software Serial on Uno, Nano
  57. #else
  58. #include <SoftwareSerial.h>
  59. SoftwareSerial SerialAT(9, 10);  // RX, TX
  60. #endif
  61.  
  62. // See all AT commands, if wanted
  63. // #define DUMP_AT_COMMANDS
  64.  
  65. // Define the serial console for debug prints, if needed
  66. #define TINY_GSM_DEBUG SerialMon
  67.  
  68. // Range to attempt to autobaud
  69. // NOTE:  DO NOT AUTOBAUD in production code.  Once you've established
  70. // communication, set a fixed baud rate using modem.setBaud(#).
  71. #define GSM_AUTOBAUD_MIN 9600
  72. #define GSM_AUTOBAUD_MAX 115200
  73.  
  74. // Add a reception delay, if needed.
  75. // This may be needed for a fast processor at a slow baud rate.
  76.  #define TINY_GSM_YIELD() { delay(2); }
  77.  
  78. // Define how you're planning to connect to the internet.
  79. // This is only needed for this example, not in other code.
  80. #define TINY_GSM_USE_GPRS true
  81. #define TINY_GSM_USE_WIFI false
  82.  
  83. // set GSM PIN, if any
  84. #define GSM_PIN ""
  85.  
  86. // Your GPRS credentials, if any
  87. const char apn[]      = "airtelgprs.com";
  88. const char gprsUser[] = "";
  89. const char gprsPass[] = "";
  90.  
  91. // Your WiFi connection credentials, if applicable
  92. const char wifiSSID[] = "";
  93. const char wifiPass[] = "";
  94.  
  95. // MQTT details
  96. const char* broker = "broker.mqttdashboard.com";
  97.  
  98. const char* topicLed       = "clientId223/led";
  99. const char* topicInit      = "clientId223/init";
  100. const char* topicLedStatus = "clientId223/ledStatus";
  101.  
  102. #include <TinyGsmClient.h>
  103. #include <PubSubClient.h>
  104.  
  105. // Just in case someone defined the wrong thing..
  106. #if TINY_GSM_USE_GPRS && not defined TINY_GSM_MODEM_HAS_GPRS
  107. #undef TINY_GSM_USE_GPRS
  108. #undef TINY_GSM_USE_WIFI
  109. #define TINY_GSM_USE_GPRS false
  110. #define TINY_GSM_USE_WIFI true
  111. #endif
  112. #if TINY_GSM_USE_WIFI && not defined TINY_GSM_MODEM_HAS_WIFI
  113. #undef TINY_GSM_USE_GPRS
  114. #undef TINY_GSM_USE_WIFI
  115. #define TINY_GSM_USE_GPRS true
  116. #define TINY_GSM_USE_WIFI false
  117. #endif
  118.  
  119. #ifdef DUMP_AT_COMMANDS
  120. #include <StreamDebugger.h>
  121. StreamDebugger debugger(SerialAT, SerialMon);
  122. TinyGsm        modem(debugger);
  123. #else
  124. TinyGsm        modem(SerialAT);
  125. #endif
  126. TinyGsmClient client(modem);
  127. PubSubClient  mqtt(client);
  128.  
  129. #define LED_PIN 13
  130. int ledStatus = LOW;
  131.  
  132. uint32_t lastReconnectAttempt = 0;
  133.  
  134. void mqttCallback(char* topic, byte* payload, unsigned int len) {
  135.   SerialMon.print("Message arrived [");
  136.   SerialMon.print(topic);
  137.   SerialMon.print("]: ");
  138.   SerialMon.write(payload, len);
  139.   SerialMon.println();
  140.  
  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. }
  148.  
  149. boolean mqttConnect() {
  150.   SerialMon.print("Connecting to ");
  151.   SerialMon.print(broker);
  152.  
  153.   // Connect to MQTT Broker
  154.   boolean status = mqtt.connect("GsmClientTest");
  155.  
  156.   // Or, if you want to authenticate MQTT:
  157.   // boolean status = mqtt.connect("GsmClientName", "mqtt_user", "mqtt_pass");
  158.  
  159.   if (status == false) {
  160.     SerialMon.println(" fail");
  161.     return false;
  162.   }
  163.   SerialMon.println(" success");
  164.   mqtt.publish(topicInit, "GsmClientTest started");
  165.   mqtt.subscribe(topicLed);
  166.   return mqtt.connected();
  167. }
  168.  
  169.  
  170. void setup() {
  171.   // Set console baud rate
  172.   SerialMon.begin(115200);
  173.   delay(10);
  174.  
  175.   pinMode(LED_PIN, OUTPUT);
  176.  
  177.   // !!!!!!!!!!!
  178.   // Set your reset, enable, power pins here
  179.   // !!!!!!!!!!!
  180.  
  181.   SerialMon.println("Wait...");
  182.  
  183.   // Set GSM module baud rate
  184.   TinyGsmAutoBaud(SerialAT, GSM_AUTOBAUD_MIN, GSM_AUTOBAUD_MAX);
  185.   // SerialAT.begin(9600);
  186.   delay(6000);
  187.  
  188.   // Restart takes quite some time
  189.   // To skip it, call init() instead of restart()
  190.   SerialMon.println("Initializing modem...");
  191.   modem.restart();
  192.   // modem.init();
  193.  
  194.   String modemInfo = modem.getModemInfo();
  195.   SerialMon.print("Modem Info: ");
  196.   SerialMon.println(modemInfo);
  197.  
  198. #if TINY_GSM_USE_GPRS
  199.   // Unlock your SIM card with a PIN if needed
  200.   if (GSM_PIN && modem.getSimStatus() != 3) { modem.simUnlock(GSM_PIN); }
  201. #endif
  202.  
  203. #if TINY_GSM_USE_WIFI
  204.   // Wifi connection parameters must be set before waiting for the network
  205.   SerialMon.print(F("Setting SSID/password..."));
  206.   if (!modem.networkConnect(wifiSSID, wifiPass)) {
  207.     SerialMon.println(" fail");
  208.     delay(10000);
  209.     return;
  210.   }
  211.   SerialMon.println(" success");
  212. #endif
  213.  
  214. #if TINY_GSM_USE_GPRS && defined TINY_GSM_MODEM_XBEE
  215.   // The XBee must run the gprsConnect function BEFORE waiting for network!
  216.   modem.gprsConnect(apn, gprsUser, gprsPass);
  217. #endif
  218.  
  219.   SerialMon.print("Waiting for network...");
  220.   if (!modem.waitForNetwork()) {
  221.     SerialMon.println(" fail");
  222.     delay(10000);
  223.     return;
  224.   }
  225.   SerialMon.println(" success");
  226.  
  227.   if (modem.isNetworkConnected()) { SerialMon.println("Network connected"); }
  228.  
  229. #if TINY_GSM_USE_GPRS
  230.   // GPRS connection parameters are usually set after network registration
  231.   SerialMon.print(F("Connecting to "));
  232.   SerialMon.print(apn);
  233.   if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
  234.     SerialMon.println(" fail");
  235.     delay(10000);
  236.     return;
  237.   }
  238.   SerialMon.println(" success");
  239.  
  240.   if (modem.isGprsConnected()) { SerialMon.println("GPRS connected"); }
  241. #endif
  242.  
  243.   // MQTT Broker setup
  244.   mqtt.setServer(broker, 1883);
  245.   mqtt.setCallback(mqttCallback);
  246. }
  247.  
  248. void loop() {
  249.   // Make sure we're still registered on the network
  250.   if (!modem.isNetworkConnected()) {
  251.     SerialMon.println("Network disconnected");
  252.     if (!modem.waitForNetwork(180000L, true)) {
  253.       SerialMon.println(" fail");
  254.       delay(10000);
  255.       return;
  256.     }
  257.     if (modem.isNetworkConnected()) {
  258.       SerialMon.println("Network re-connected");
  259.     }
  260.  
  261. #if TINY_GSM_USE_GPRS
  262.     // and make sure GPRS/EPS is still connected
  263.     if (!modem.isGprsConnected()) {
  264.       SerialMon.println("GPRS disconnected!");
  265.       SerialMon.print(F("Connecting to "));
  266.       SerialMon.print(apn);
  267.       if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
  268.         SerialMon.println(" fail");
  269.         delay(10000);
  270.         return;
  271.       }
  272.       if (modem.isGprsConnected()) { SerialMon.println("GPRS reconnected"); }
  273.     }
  274. #endif
  275.   }
  276.  
  277.   if (!mqtt.connected()) {
  278.     SerialMon.println("=== MQTT NOT CONNECTED ===");
  279.     // Reconnect every 10 seconds
  280.     uint32_t t = millis();
  281.     if (t - lastReconnectAttempt > 10000L) {
  282.       lastReconnectAttempt = t;
  283.       if (mqttConnect()) { lastReconnectAttempt = 0; }
  284.     }
  285.     delay(100);
  286.     return;
  287.   }
  288.  
  289.   mqtt.loop();
  290. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement