   bool topicParsed = false;
   int parserPos = 0;
   int i = 0;
-  while(!topicParsed){
-    int startPos = parserPos+1;
+  while (!topicParsed)
+  {
+    int startPos = parserPos + 1;
     //Serial.print("indexOf: "); Serial.println(topic_str.indexOf("/", startPos));
     int endPos = topic_str.indexOf("/", startPos);
-    if (endPos==-1){
+    if (endPos == -1)
+    {
       endPos = topic_str.length();
       topicParsed = true;
     }

SoftwareSerial swSer(RX, TX, false, 255);

// Network settings
const char* ssid              = "VM146928-2G";
const char* password          = "gbwnhajc";

// MQTT connect settings
const char* mqtt_server       = "192.168.1.3";
const char* mqtt_username     = "device"; 
const char* mqtt_password     = "didmsiskf0wdo";

//// Network settings
//const char* ssid              = "some-SSID";
//const char* password          = "some-PSK";
//
//// MQTT connect settings
//const char* mqtt_server       = "some-MQTT-server";
//const char* mqtt_username     = "some-MQTT-username"; 
//const char* mqtt_password     = "some-MQTT-password";
const char* mqtt_client_id    = "Mesquitto Gateway"; //String("Mesquitto Gateway "+ ESP.getChipId()).c_str();
//const char* mqtt_will_topic   = ("/1/gateways/"+getMac()+"/disconnected").c_str();
const char* mqtt_will_payload = "1";
const int   mqtt_will_qos     = 1;
const bool  mqtt_will_retain  = true; 

// WiFi and MQTT client instantiation
WiFiClientSecure espClient;
PubSubClient client(espClient);

// Buffers to store messages
CustomList<String> mqttMessageBuffer;         // Stores list of all messages queued to be sent to MQTT broker
CustomList<String> meshMessageBuffer;         // Stores list of all messages queued to be forwarded to Mesh gateway

// Timestamp to store last time a message was sent to Mesh
long lastMsg = 0;

// Global flags used for control
volatile bool _sending                 = false;                // Flag set when sending to Mesh
volatile bool _receiving               = false;              // Flag set when receiving from mesh
bool _empty_mqtt_buffer_irq   = false;  // Flag set when 

//String TX_FSM_STATE = TX_FSM_READY;
//Ticker RX_Interrupt_Ticker;


  // Format and push message to buffer
  String mesh_topic;
  for (int i = 3; i < subtopicNo; i++)
  {
    mesh_topic += "/";
    mesh_topic += subtopics[i];
  }
  meshMessageBuffer.push_back(jsonMqttMessage(mesh_topic, value));
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

/*************************************************************************/
/* MQTT reconnect funtion: Get called when a connection is lost or first */
/* started.                                                              */
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
void reconnect()
{
  // Loop until we're reconnected
  while (!client.connected())
  {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect(mqtt_client_id, mqtt_username, mqtt_password, mqtt_will_topic, mqtt_will_qos, mqtt_will_retain, mqtt_will_payload))
    {
      Serial.println("connected");
      // Once connected, publish an announcement...
      client.publish(String("/1/gateways/" + getMac() + "/disconnected").c_str(), String("0").c_str());
      // ... and resubscribe
      client.subscribe(String("/1/gateways/" + getMac() + "/+/sensors/+/input/value").c_str());
    }
    else
    {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
