 
 void setup ()
 {
-    // start-up the Yun bridge
-    Bridge.begin();
-    delay(2000);
+  // start-up the Yun bridge
+  Bridge.begin();
+  delay(2000);
 
-    Serial.begin(57600);
-    Serial.println("***");
-    Serial.println("*** 150726 YunHub 1.05");
-    Serial.println("***");
+  Serial.begin(57600);
+  Serial.println("***");
+  Serial.println("*** 150726 YunHub 1.05");
+  Serial.println("***");
 
-    // initialize digital pin 13 as an output.
-    pinMode(13, OUTPUT);
+  // initialize digital pin 13 as an output.
+  pinMode(13, OUTPUT);
 
-    rf12_initialize(1, RF12_868MHZ, 0); // group id = 0 to receive from all groups
+  rf12_initialize(1, RF12_868MHZ, 0); // group id = 0 to receive from all groups
+
+  // Connect to MQTT server if not available
+  client.connect("arduinoYunClient");
+  client.publish("ConnectMessage", "YunChen connected.");
 }
 
 
 void loop ()
 {
+  client.loop();
 
-    if (rf12_recvDone() && rf12_crc == 0)
-    {
+  if (rf12_recvDone() && rf12_crc == 0)
+  {
 
-        digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
+    digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
 
-        // Connect to MQTT server if not available
-        if (!client.connected())
-        {
-            client.connect("arduinoYunClient");
-            client.publish("ConnectMessage", "YunChen connected.");
-        }
-
-        // identify sender via group & nodeid
-        // node id is in the first 5 bits of rf12_hdr --> & RF12_HDR_MASK
-        rf12_group = rf12_grp;
-        rf12_nodeid = rf12_hdr & RF12_HDR_MASK;
-        timeStamp = getTimeStamp();
+    // identify sender via group & nodeid
+    // node id is in the first 5 bits of rf12_hdr --> & RF12_HDR_MASK
+    rf12_group = rf12_grp;
+    rf12_nodeid = rf12_hdr & RF12_HDR_MASK;
+    timeStamp = getTimeStamp();
 
 
-        ////////////////////////////////////
-        //  Raw Data (group ALL, id ALL)  //
-        ////////////////////////////////////
-        rawdataStr = timeStamp + ","
-                     + String(rf12_group) + ","
-                     + String(rf12_nodeid);
-        for (byte i = 0; i < rf12_len; ++i)
-        {
-            rawdataStr += ",";
-            rawdataStr += rf12_data[i];
-        }
+    ////////////////////////////////////
+    //  Raw Data (group ALL, id ALL)  //
+    ////////////////////////////////////
+    rawdataStr = timeStamp + ","
+                 + String(rf12_group) + ","
+                 + String(rf12_nodeid);
+    for (byte i = 0; i < rf12_len; ++i)
+    {
+      rawdataStr += ",";
+      rawdataStr += rf12_data[i];
+    }
 
-        // publish to MQTT server
-        rawdataStr.toCharArray(message_buff, 128);
-        client.publish("RF12_RawData", message_buff);
+    // publish to MQTT server
+    char charBuf[rawDataStr.length()];
+    rawDataStr.toCharArray(charBuf, rawDataStr.length());
+    client.publish("RF12_RawData", charBuf);
 
 
-        ///////////////////////////////
-        //  TNode (group 33, id 22)  //
-        ///////////////////////////////
-        if (rf12_group == 33 and rf12_nodeid == 22)
-        {
-            TNodeData.rf12_group = rf12_group;
-            TNodeData.rf12_nodeid = rf12_nodeid;
-            TNodeData.timeStamp = timeStamp;
-            TNodeData.Vcc = int(word(rf12_data[1], rf12_data[0])) / 1000.0;
-            TNodeData.T1 = int(word(rf12_data[3], rf12_data[2])) / 100.0;
-            TNodeData.T2 = int(word(rf12_data[5], rf12_data[4])) / 100.0;
- 
-           dataStr = TNodeData.timeStamp + ","
-                      + String(TNodeData.rf12_group) + ","
-                      + String(TNodeData.rf12_nodeid) + ","
-                      + String(TNodeData.Vcc) + ","
-                      + String(TNodeData.T1) + ","
-                      + String(TNodeData.T2);
+    ///////////////////////////////
+    //  TNode (group 33, id 22)  //
+    ///////////////////////////////
+    if (rf12_group == 33 and rf12_nodeid == 22)
+    {
+      TNodeData.rf12_group = rf12_group;
+      TNodeData.rf12_nodeid = rf12_nodeid;
+      TNodeData.timeStamp = timeStamp;
+      TNodeData.Vcc = int(word(rf12_data[1], rf12_data[0])) / 1000.0;
+      TNodeData.T1 = int(word(rf12_data[3], rf12_data[2])) / 100.0;
+      TNodeData.T2 = int(word(rf12_data[5], rf12_data[4])) / 100.0;
 
-            // publish interpreted TNode data to MQTT server
-            dataStr.toCharArray(message_buff, 128);
-            client.publish("TNode", message_buff);
-        }
+      dataStr = TNodeData.timeStamp + ","
+                + String(TNodeData.rf12_group) + ","
+                + String(TNodeData.rf12_nodeid) + ","
+                + String(TNodeData.Vcc) + ","
+                + String(TNodeData.T1) + ","
+                + String(TNodeData.T2);
 
-        ////////////////////////////////////
-        //  RadioBlip2 (group 33, id 17)  //
-        ////////////////////////////////////
-        if (rf12_group == 33 and rf12_nodeid == 17)
-        {
-            RadioBlip2Data.rf12_group = rf12_group;
-            RadioBlip2Data.rf12_nodeid = rf12_nodeid;
-            RadioBlip2Data.timeStamp = timeStamp;
-            RadioBlip2Data.Vcc = 1.0 + rf12_data[5] * 0.02;
- 
-           dataStr = RadioBlip2Data.timeStamp + ","
-                      + String(RadioBlip2Data.rf12_group) + ","
-                      + String(RadioBlip2Data.rf12_nodeid) + ","
-                      + String(RadioBlip2Data.Vcc);
-                      
-            // publish interpreted TNode data to MQTT server
-            dataStr.toCharArray(message_buff, 128);
-            client.publish("RadioBlip2", message_buff);
-        }
+      // publish interpreted TNode data to MQTT server
+      dataStr.toCharArray(message_buff, 100);
+      client.publish("TNode", message_buff);
+    }
 
+    ////////////////////////////////////
+    //  RadioBlip2 (group 33, id 17)  //
+    ////////////////////////////////////
+    if (rf12_group == 33 and rf12_nodeid == 17)
+    {
+      RadioBlip2Data.rf12_group = rf12_group;
+      RadioBlip2Data.rf12_nodeid = rf12_nodeid;
+      RadioBlip2Data.timeStamp = timeStamp;
+      RadioBlip2Data.Vcc = 1.0 + rf12_data[5] * 0.02;
+
+      dataStr = RadioBlip2Data.timeStamp + ","
+                + String(RadioBlip2Data.rf12_group) + ","
+                + String(RadioBlip2Data.rf12_nodeid) + ","
+                + String(RadioBlip2Data.Vcc);
+
+      // publish interpreted TNode data to MQTT server
+      dataStr.toCharArray(message_buff, 100);
+      client.publish("RadioBlip2", message_buff);
     }
     digitalWrite(13, LOW);   // turn the LED off (HIGH is the voltage level)
-    client.loop();
+  }
 }
 
 


byte rf12_group, rf12_nodeid;
String timeStamp, dataStr, rawdataStr;
char message_buff[128];

// Update these with values suitable for your network.
byte server[] = { 192, 168, 178, 27 };

// ===========================================================
void callback(char *topic, byte *payload, unsigned int length)
{
  // counter
  int i = 0;
  // buffer for payload-to-string conversion
  char message_buff[100];

  // copy payload to buffer, append \0
  for (i = 0; i < length; i++)
  {
    message_buff[i] = payload[i];
  }
  message_buff[i] = '\0';
}

YunClient yun;
