 // Temperature node data structure. Timestamp created locally, at time of receival.
 struct
 {
-    String timeStamp;   // "+%Y-%m-%d %T": format "YYYY-MM-DD hh:mm:ss"
-    byte rf12_group;    // RF12 group ID
-    byte rf12_nodeid;   // RF12 node ID
-    float Vcc;          // Supply voltage
-    float T1;            // Temperature reading
-    float T2;            // Temperature reading
+  String timeStamp;   // "+%Y-%m-%d %T": format "YYYY-MM-DD hh:mm:ss"
+  byte rf12_group;    // RF12 group ID
+  byte rf12_nodeid;   // RF12 node ID
+  float Vcc;          // Supply voltage
+  float T1;            // Temperature reading
+  float T2;            // Temperature reading
 } TNodeData;
 
 
 // RadioBlip2 data structure. Timestamp created locally, at time of receival.
 struct
 {
-    String timeStamp;   // "+%Y-%m-%d %T": format "YYYY-MM-DD hh:mm:ss"
-    byte rf12_group;    // RF12 group ID
-    byte rf12_nodeid;   // RF12 node ID
-    float Vcc;          // Supply voltage
+  String timeStamp;   // "+%Y-%m-%d %T": format "YYYY-MM-DD hh:mm:ss"
+  byte rf12_group;    // RF12 group ID
+  byte rf12_nodeid;   // RF12 node ID
+  float Vcc;          // Supply voltage
 } RadioBlip2Data;
 
 

// This function returns a string with the time stamp
String getTimeStamp()
{
    String result;
    Process time;
    // date is a command line utility to get the date and the time
    // in different formats depending on the additional parameter
    time.begin("date");
    time.addParameter("+%Y-%m-%d %T");  // parameters: D for the complete date mm/dd/yy, T for the time hh:mm:ss
    time.run();  // run the command

    // read the output of the command
    while (time.available() > 0)
    {
        char c = time.read();
        if (c != '\n')
            result += c;
    }

    return result;
}



void setup ()
{
  // start-up the Yun bridge
  Bridge.begin();
  delay(2000);

  Serial.begin(57600);
  Serial.println("***");
  Serial.println("*** 150726 YunHub 1.05");
  Serial.println("***");

  // initialize digital pin 13 as an output.
  pinMode(13, OUTPUT);

  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)
  {

    digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)

    // 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];
    }

    // 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);

      // 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)
  }
}


