 
     // convert cmd line string to integer with specified base
     addr = chibiCmdStr2Num(args[1], 16);
-    
+
     // concatenate strings typed into the command line and send it to
     // the specified address
-    len = strCat((char *)data, 2, arg_cnt, args);    
+    len = strCat((char *)data, 2, arg_cnt, args);
     chibiTx(addr, data,len);
+}
+
+/**************************************************************************/
+/*!
+    Reads the configuration of the node's ids in the eeprom and prints it.
+    Each id is a 32 unsigned integer, taking 4 bytes.
+    Usage: rconf
+*/
+/**************************************************************************/
+void cmdReadConf(int arg_cnt, char** args)
+{
+  uint16_t addr = EEPROM_CONF_ADDR;
+  chb_eeprom_read(addr, (U8 *)(&(main_packet.node_id)), 4); addr+=4;
+  chb_eeprom_read(addr, (U8 *)(&(main_packet.vsol.sensor_id)), 4); addr+=4;
+  chb_eeprom_read(addr, (U8 *)(&(main_packet.vbat.sensor_id)), 4); addr+=4;
+  chb_eeprom_read(addr, (U8 *)(&(main_packet.temperature.sensor_id)), 4); addr+=4;
+  chb_eeprom_read(addr, (U8 *)(&(main_packet.humidity.sensor_id)), 4); addr+=4;
+  chb_eeprom_read(addr, (U8 *)(&(main_packet.distance_to_water_surface.sensor_id)), 4); addr+=4;
+  char sbuf[SBUF_SIZE];
+  sprintf(sbuf, "Node_id: %d\n\r  vsol_id: %d\n\r  vbat_id: %d\n\r  temperature_id: %d\n\r  humidity_id: %d\n\r  sonar_id: %d",
+                (int) main_packet.node_id,
+                (int) main_packet.vsol.sensor_id,
+                (int) main_packet.vbat.sensor_id,
+                (int) main_packet.temperature.sensor_id,
+                (int) main_packet.humidity.sensor_id,
+                (int) main_packet.distance_to_water_surface.sensor_id);
+  Serial.println(sbuf);
+}
+
+/**************************************************************************/
+/*!
+    Writes the configuration of the node's ids in the eeprom and prints it.
+    Each id is a 32 unsigned integer, taking 4 bytes.
+    Usage: wconf node_id vsol_id vbat_id temperature_id humidity_id sonar_id
+*/
+/**************************************************************************/
+void cmdWriteConf(int arg_cnt, char** args)
+{
+  main_packet.node_id =           strtol(args[1], NULL, 10);
+  main_packet.vsol.sensor_id =    strtol(args[2], NULL, 10);
+  main_packet.vbat.sensor_id =    strtol(args[3], NULL, 10);
+  main_packet.temperature.sensor_id =               strtol(args[4], NULL, 10);
+  main_packet.humidity.sensor_id =                  strtol(args[5], NULL, 10);
+  main_packet.distance_to_water_surface.sensor_id = strtol(args[6], NULL, 10);
+  uint16_t addr = EEPROM_CONF_ADDR;
+  chb_eeprom_write(addr, (U8 *)(&(main_packet.node_id)), 4); addr+=4;
+  chb_eeprom_write(addr, (U8 *)(&(main_packet.vsol.sensor_id)), 4); addr+=4;
+  chb_eeprom_write(addr, (U8 *)(&(main_packet.vbat.sensor_id)), 4); addr+=4;
+  chb_eeprom_write(addr, (U8 *)(&(main_packet.temperature.sensor_id)), 4); addr+=4;
+  chb_eeprom_write(addr, (U8 *)(&(main_packet.humidity.sensor_id)), 4); addr+=4;
+  chb_eeprom_write(addr, (U8 *)(&(main_packet.distance_to_water_surface.sensor_id)), 4); addr+=4;
 }
 
 /**************************************************************************/

/**************************************************************************/
void loop()
{
  /*chibiCmdPoll();
  return;*/
  
  static int count = 0;
  
  // This function checks the command line to see if anything new was typed.
//  chibiCmdPoll();

  get_temp(r.temperature.value, r.humidity.value);
  get_vbat(r.battery.value);
  get_vsol(r.solar.value);
  r.count++;
  get_timestamp(r.timestamp);

  char sbuf[200];
  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),", 
                (int) r.node_id,
                (int) r.count, 
                (int) r.timestamp,
                (int) r.temperature.sensor_id, (int) r.temperature.value,
                (int) r.humidity.sensor_id, (int) r.humidity.value,
                (int) r.battery.sensor_id, (int) r.battery.value,
                (int) r.solar.sensor_id, (int) r.solar.value);
  Serial.println(sbuf);

  sprintf(sbuf, "%s|%d=%d;%d=%d;\n",
          r.timestamp,
          r.battery.sensor_id, r.battery.value,
          r.solar.sensor_id, r.solar.value);
  cmdSdWrite(1, (char**)(&sbuf));


  //chibiTx(EDGE_ID, (unsigned char*)(&r), sizeof(r));


  
  sleep_mcu();
}

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

#include <SdFat.h>
#include <pcf2127.h>
#include <stdint.h>
#include <OneWire.h>

#include <src/chb_eeprom.h>

typedef struct{
  int32_t sensor_id;
  int32_t value;
} reading_t;



typedef struct{
  reading_t vsol;
  reading_t vbat;
  reading_t temperature;
  reading_t humidity;
  reading_t distance_to_water_surface;
  int32_t count;
  int32_t signal_strength;
  char timestamp[19];
  int32_t node_id;
} awarenode_packet_t;



awarenode_packet_t main_packet;



#define EDGE_ID BROADCAST_ADDR

#define RTC_CLOCK_SOURCE 0b11 // Selects the clock source. 0b11 selects 1/60Hz clock.
#define RTC_SLEEP 30 // Number of timer clock cycles before interrupt is generated.

#define SBUF_SIZE 200 //Note that due to inefficient implementation of sd_write() the memory footprint is actually twice of this value.

#define EEPROM_CONF_ADDR 0x12

#define DATECODE "14-08-2016"
#define TITLE "SABOTEN 900 MHz Long Range\nDATALOGGER\n"
#define ADCREFVOLTAGE 3.3
#define FILENAME "AWANODE.TXT"

#define OneWireSensor 1

int hgmPin = 22;
int sdCsPin = 15;
int rtcCsPin = 28;
int ledPin = 18;
int sdDetectPin = 19;
int vbatPin = 31;
int vsolPin = 29;
int sensorPin = 8;
int sonarTriggerPin = 5;
int sonarEchoPin = 7;
int sonarAwakePin = 10;
int debugModePin = 4;
int burstModePin = 3;


int debug_mode = 0;

// Pins that will not interfer with the SPI: 2 to 5, 7 to 10 + 14 and 15

unsigned char buf[100];

// this is for printf
static FILE uartout = {0};

PCF2127 pcf(0, 0, 0, rtcCsPin);
SdFat sd;
SdFile myFile;

#ifdef OneWireSensor
OneWire OW_temperature_probe(sensorPin);
#endif



