Advertisement
babyyoda_

GSM_MQTT_TTCAL

Jul 15th, 2021
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. // Select your modem:
  4. #define TINY_GSM_MODEM_SIM900 // Modem is SIM800L
  5.  
  6. // Set serial for debug console (to the Serial Monitor, default speed 115200)
  7. #define SerialMon Serial
  8. // Set serial for AT commands
  9. #define SerialAT Serial1
  10.  
  11. // Define the serial console for debug prints, if needed
  12. #define TINY_GSM_DEBUG SerialMon
  13.  
  14. // set GSM PIN, if any
  15. #define GSM_PIN ""
  16.  
  17. // Your GPRS credentials, if any
  18. const char apn[] = "airtelgprs.com"; // APN (example: internet.vodafone.pt) use https://wiki.apnchanger.org
  19. const char gprsUser[] = "";
  20. const char gprsPass[] = "";
  21.  
  22. // SIM card PIN (leave empty, if not defined)
  23. const char simPIN[]   = "";
  24.  
  25. // MQTT details
  26. const char* broker = "142.93.214.20";                    // Public IP address or domain name
  27. const char* mqttUsername = "";  // MQTT username
  28. const char* mqttPassword = "";  // MQTT password
  29.  
  30. const char* topicOutput1 = "RCU00001";
  31. const char* topicOutput2 = "RCU00002";
  32. const char* topicTemperature = "RCU00003";
  33. const char* topicHumidity = "RCU00004";
  34.  
  35. // Define the serial console for debug prints, if needed
  36. //#define DUMP_AT_COMMANDS
  37.  
  38. #include <Wire.h>
  39. #include <TinyGsmClient.h>
  40.  
  41. //-------------------------------------------------------------------//
  42. #ifdef DUMP_AT_COMMANDS
  43.   #include <StreamDebugger.h>
  44.   StreamDebugger debugger(SerialAT, SerialMon);
  45.   TinyGsm modem(debugger);
  46. #else
  47.   TinyGsm modem(SerialAT);
  48. #endif
  49. //-------------------------------------------------------------------//
  50.  
  51. #include <PubSubClient.h>
  52.  
  53. TinyGsmClient client(modem);
  54. PubSubClient mqtt(client);
  55.  
  56.  
  57. #define MODEM_TX             33   //27
  58. #define MODEM_RX             26   //26
  59.  
  60. #define OUTPUT_1             21
  61. #define OUTPUT_2             22
  62.  
  63. uint32_t lastReconnectAttempt = 0;
  64.  
  65. float temperature = 0;
  66. float humidity = 0;
  67. long lastMsg = 0;
  68.  
  69. void mqttCallback(char* topic, byte* message, unsigned int len) {
  70.   Serial.print("Message arrived on topic: ");
  71.   Serial.print(topic);
  72.   Serial.print(". Message: ");
  73.   String messageTemp;
  74.  
  75.   for (int i = 0; i < len; i++) {
  76.     Serial.print((char)message[i]);
  77.     messageTemp += (char)message[i];
  78.   }
  79.   Serial.println();
  80.  
  81.   // Feel free to add more if statements to control more GPIOs with MQTT
  82.  
  83.   // If a message is received on the topic esp/output1, you check if the message is either "true" or "false".
  84.   // Changes the output state according to the message
  85.   if (String(topic) == "RCU00001") {
  86.     Serial.print("Changing output to ");
  87.     if(messageTemp == "true"){
  88.       Serial.println("true");
  89.       digitalWrite(OUTPUT_1, HIGH);
  90.     }
  91.     else if(messageTemp == "false"){
  92.       Serial.println("false");
  93.       digitalWrite(OUTPUT_1, LOW);
  94.     }
  95.   }
  96.   else if (String(topic) == "RCU00002") {
  97.     Serial.print("Changing output to ");
  98.     if(messageTemp == "true"){
  99.       Serial.println("true");
  100.       digitalWrite(OUTPUT_2, HIGH);
  101.     }
  102.     else if(messageTemp == "false"){
  103.       Serial.println("false");
  104.       digitalWrite(OUTPUT_2, LOW);
  105.     }
  106.   }
  107. }
  108.  
  109. boolean mqttConnect() {
  110.   SerialMon.print("Connecting to ");
  111.   SerialMon.print(broker);
  112.  
  113.   // Connect to MQTT Broker without username and password
  114.   //boolean status = mqtt.connect("GsmClientN");
  115.  
  116.   // Or, if you want to authenticate MQTT:
  117.   boolean status = mqtt.connect("GsmClientN", mqttUsername, mqttPassword);
  118.  
  119.   if (status == false) {
  120.     SerialMon.println(" fail");
  121.     ESP.restart();
  122.     return false;
  123.   }
  124.   SerialMon.println(" success");
  125.   mqtt.subscribe(topicOutput1);
  126.   mqtt.subscribe(topicOutput2);
  127.  
  128.   return mqtt.connected();
  129. }
  130.  
  131.  
  132. void setup() {
  133.   // Set console baud rate
  134.   SerialMon.begin(115200);
  135.   delay(10);
  136.  
  137.   pinMode(OUTPUT_1, OUTPUT);
  138.   pinMode(OUTPUT_2, OUTPUT);
  139.  
  140.   digitalWrite(OUTPUT_1, HIGH);   //alive
  141.   digitalWrite(OUTPUT_2, HIGH);
  142.   delay(250);
  143.   digitalWrite(OUTPUT_1, LOW);
  144.   digitalWrite(OUTPUT_2, LOW);
  145.  
  146.   SerialMon.println("Wait...");
  147.  
  148.   // Set GSM module baud rate and UART pins
  149.   SerialAT.begin(115200, SERIAL_8N1, MODEM_RX, MODEM_TX);
  150.   delay(6000);
  151.  
  152.   // Restart takes quite some time
  153.   // To skip it, call init() instead of restart()
  154.   SerialMon.println("Initializing modem...");
  155.   modem.restart();
  156.   // modem.init();
  157.  
  158.   String modemInfo = modem.getModemInfo();
  159.   SerialMon.print("Modem Info: ");
  160.   SerialMon.println(modemInfo);
  161.  
  162.   // Unlock your SIM card with a PIN if needed
  163.   if ( GSM_PIN && modem.getSimStatus() != 3 ) {
  164.     modem.simUnlock(GSM_PIN);
  165.   }
  166.  
  167.  
  168.   SerialMon.print("Connecting to APN: ");
  169.   SerialMon.print(apn);
  170.   if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
  171.     SerialMon.println(" fail");
  172.     ESP.restart();
  173.   }
  174.   else {
  175.     SerialMon.println(" OK");
  176.   }
  177.  
  178.   if (modem.isGprsConnected()) {
  179.     SerialMon.println("GPRS connected");
  180.   }
  181.  
  182.   // MQTT Broker setup
  183.   mqtt.setServer(broker, 1883);
  184.   mqtt.setCallback(mqttCallback);
  185. }
  186.  
  187. void loop() {
  188.   if (!mqtt.connected()) {
  189.     SerialMon.println("=== MQTT NOT CONNECTED ===");
  190.     // Reconnect every 10 seconds
  191.     uint32_t t = millis();
  192.     if (t - lastReconnectAttempt > 10000L) {
  193.       lastReconnectAttempt = t;
  194.       if (mqttConnect()) {
  195.         lastReconnectAttempt = 0;
  196.       }
  197.     }
  198.     delay(100);
  199.     return;
  200.   }
  201.   mqtt.loop();
  202. }
  203.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement