 
 
 
+/**************************************************************************/
+/*!
+
+*/
+/**************************************************************************/
+void cmdSdWrite(int arg_cnt, char **args)
+{
+    char data[1000];
+
+    // concatenate strings typed into the command line and send it to
+    // the specified address
+    strCat(data, 1, arg_cnt, args);
+
+    SdWriteBuf(data);
+}
+
+/**************************************************************************/
+/*!
+
+*/
+/**************************************************************************/
+void SdWriteBuf(char *data)
+{
+    if (myFile.open(FILENAME, O_RDWR | O_CREAT | O_AT_END))
+
+    {
+      myFile.write(data);
+      myFile.close();
+    }
+    else
+    {
+      printf("Error opening dataFile\n");
+    }
+}
+
+
+/**************************************************************************/
+/*!
+
+*/
+/**************************************************************************/
+void cmdSdRead()
+{
+    if (myFile.open(FILENAME, O_READ))
+
+    {
+      char data[101];
+      myFile.read(data, 100);
+      Serial.print((char *)data);
+      myFile.close();
+    }
+    else
+    {
+      printf("Error opening dataFile\n");
+    }
+}
+
+/**************************************************************************/
+/*!
+
+*/
+/**************************************************************************/
+void cmdSdClear()
+{
+    if (myFile.open(FILENAME, O_RDWR | O_CREAT | O_TRUNC))
+    {
+      myFile.close();
+    }
+    else
+    {
+      printf("Error opening dataFile\n");
+    }
+}

        }

        if ((micros() - t) > 40)
        { 
            bits[idx] |= mask;
        }
        mask >>= 1;

/**************************************************************************/
void loop()
{
  if(debug_mode==1){
    if(digitalRead(debugModePin)==HIGH){
      debug_mode = 0;
    }
    chibiCmdPoll();
    return;
  }

  digitalWrite(sonarAwakePin, HIGH);

#ifdef OneWireSensor
  get_temp_ow(main_packet.temperature.value);
#else
  get_temp(main_packet.temperature.value, main_packet.humidity.value);
#endif
  get_vbat(main_packet.vbat.value);
  get_vsol(main_packet.vsol.value);
  get_sonar(main_packet.distance_to_water_surface.value);
  delay(1000);
  get_sonar(main_packet.distance_to_water_surface.value);
  /*if(r.sonar.value>10){
    digitalWrite(sonarAwakePin, LOW);
  }
  else{
    digitalWrite(sonarAwakePin, HIGH);
  }*/
  main_packet.count++;
  get_timestamp(main_packet.timestamp);

  char sbuf[SBUF_SIZE];
  sprintf(sbuf, "Port: %d Bit: %d",digitalPinToPort(hgmPin), digitalPinToBitMask(hgmPin));
  Serial.println(sbuf);


  sprintf(sbuf, "Node_id: %d, count: %d, timestamp: %19s, id %d: %dC (temperature), id %d: %d (humidity), id %d: %dmV (battery), id %d: %dmV (solar), id %d: %d cm (water level)",
                (int) main_packet.node_id,
                (int) main_packet.count,
                (int) main_packet.timestamp,
                (int) main_packet.temperature.sensor_id, (int) main_packet.temperature.value,
                (int) main_packet.humidity.sensor_id, (int) main_packet.humidity.value,
                (int) main_packet.vbat.sensor_id, (int) main_packet.vbat.value,
                (int) main_packet.vsol.sensor_id, (int) main_packet.vsol.value,
                (int) main_packet.distance_to_water_surface.sensor_id, (int) main_packet.distance_to_water_surface.value);
  Serial.println(sbuf);
  //chibiTx(EDGE_ID, (unsigned char*)(&main_packet), sizeof(main_packet));


  sprintf(sbuf, "%s|%d|%d=%d;%d=%d;\n",
          (int)main_packet.timestamp,
          (int)main_packet.count,
          (int)main_packet.temperature.sensor_id, (int) main_packet.temperature.value,
          (int)main_packet.humidity.sensor_id, (int) main_packet.humidity.value,
          (int)main_packet.vbat.sensor_id, (int)main_packet.vbat.value,
          (int)main_packet.vsol.sensor_id, (int)main_packet.vsol.value);
  SdWriteBuf(sbuf);
  Serial.println(sbuf);

  free(sbuf);
  sleep_mcu();
}


void rtcInterrupt(){
  Serial.println("Interrupt");
