Guest User

Untitled

a guest
Nov 30th, 2020
136
277 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #ifndef MQTT
  2. #define MQTT
  3.  
  4. #include <WiFi.h>
  5. #include <PubSubClient.h>
  6. #include "log.cpp"
  7. #include "utilities.cpp"
  8. #include "definitions.h"
  9.  
  10. class Mqtt {
  11.  
  12.     Log* rlog;
  13.     String log_prefix = "[MQTT] ";
  14.     PubSubClient client;
  15.     String server;
  16.     String user;
  17.     String password;
  18.     int port;
  19.     String baseTopic;
  20.  
  21.     // MQTT connect try
  22.     int lasttry = 10000;
  23.     int maxtry = 10;
  24.  
  25.     public:
  26.         Mqtt(Log &log) {
  27.             this -> rlog = &log;
  28.         }
  29.  
  30.         void setup(WiFiClient &wifiClient, String server, String user, String password, int port) {
  31.            
  32.             this -> user = user;
  33.             this -> password = password;
  34.             this -> port = port;
  35.             this -> server = server;
  36.             this -> baseTopic = baseTopic;
  37.            
  38.             const char* mqtt_s = const_cast<char*>(server.c_str());
  39.             PubSubClient client(wifiClient);
  40.  
  41.             this -> client.setServer(mqtt_s, port);
  42.            
  43.             // this -> client.setCallback(callback);
  44.             // Problem: argument of type "void (Mqtt::*)(char *topic, uint8_t *payload, unsigned int length)" is incompatible with parameter of type "void (*)(char *, uint8_t *, unsigned int)"C/C++(167)
  45.  
  46.             // this -> client.setCallback ( [this] (char* topic, uint8_t* payload, unsigned int length) { this->callback(topic, payload, length); });
  47.             // Problem: no suitable conversion function from "lambda []void (char *topic, uint8_t *payload, unsigned int length)->void" to "void (*)(char *, uint8_t *, unsigned int)" existsC/C++(413)
  48.              
  49.             // client.setCallback(std::bind(&Mqtt::callback));
  50.             // Problem no suitable conversion function from "std::_Binder<std::_Unforced, void (Mqtt::*)(char *topic, uint8_t *payload, unsigned int length)>" to "void (*)(char *, uint8_t *, unsigned int)" existsC/C++(413)
  51.         }
  52.  
  53.         void reconnect() {
  54.           // Loop until we're reconnected
  55.             if (!this->client.connected()) {
  56.                 // Wait 5 seconds before retrying
  57.                 if (millis() - lasttry > 5000 && maxtry > 0) {
  58.                     lasttry = millis();
  59.  
  60.                     rlog -> log(log_prefix, "Attempting MQTT connection... Server: " + server + "user:" + user);
  61.  
  62.                     const char* mqtt_u = const_cast<char*>(user.c_str());
  63.                     const char* mqtt_p = const_cast<char*>(password.c_str());
  64.  
  65.                     // Create a random client ID
  66.                     String clientId = BOARD_NAME;
  67.                     clientId += "-";
  68.                     clientId += String(random(0xffff), HEX);
  69.                     // Attempt to connect
  70.                     if (client.connect(clientId.c_str(), mqtt_u, mqtt_p)) {
  71.                         rlog -> log(log_prefix, "Connected.");
  72.                        
  73.                         // Once connected, publish an announcement...
  74.                         client.subscribe(MQTT_BASE_TOPIC);
  75.                        
  76.                         rlog -> log(log_prefix + "Subscribed topic: " + MQTT_BASE_TOPIC);
  77.                        
  78.                         maxtry = MQTT_MAX_TRY;
  79.  
  80.                     } else {
  81.                          rlog -> log(log_prefix + "Failed to connect. Try again in 5 seconds.");
  82.                         maxtry--;
  83.                     }
  84.                 }
  85.             }
  86.         }
  87.  
  88.     private:
  89.         // This function will be triggered if MQTT message arrived
  90.         void callback(char* topic, uint8_t *payload, unsigned int length) {
  91.  
  92.             String payload_str = "";
  93.  
  94.             for (int i = 0; i < length; i++) {            
  95.                 payload_str += (char)payload[i];
  96.             }
  97.  
  98.             //rlog -> log(this -> log_prefix + "Message arrived received. Topic: " + topic + " - payload: " + payload_str);
  99.  
  100.             // do something
  101.  
  102.            
  103.         }
  104. };
  105.        
  106.  
  107. #endif
RAW Paste Data