 
 
 void setup()
-{    
-  uint8_t i, sdDetect;
+{
   // fill in the UART file descriptor with pointer to writer.
   fdev_setup_stream (&uartout, uart_putchar, NULL, _FDEV_SETUP_WRITE);
-  
+
   // The uart is the standard output device STDOUT.
   stdout = &uartout ;
-  
-  old[0] = 3;
-  
+
   // set up high gain mode pin
   pinMode(hgmPin, OUTPUT);
   digitalWrite(hgmPin, LOW);
-  
+
   // set up rtc chip select
   pinMode(rtcCsPin, OUTPUT);
   digitalWrite(rtcCsPin, HIGH);
-  
+
   pinMode(sdCsPin, OUTPUT);
   digitalWrite(sdCsPin, HIGH);
-  
+
   // set up sd card detect
   pinMode(sdDetectPin, INPUT);
   digitalWrite(sdDetectPin, HIGH);
-  
+
   // set up battery monitoring
   pinMode(vbatPin, INPUT);
-  
+
   // set up LED
   pinMode(ledPin, OUTPUT);
   digitalWrite(ledPin, LOW);
   //delay(300);
   //digitalWrite(ledPin, LOW);
-  
+
+  // set up the sonar
+  pinMode(sonarTriggerPin, OUTPUT);
+  pinMode(sonarEchoPin, INPUT);
+  pinMode(sonarAwakePin, OUTPUT);
+  digitalWrite(sonarAwakePin, HIGH);
+
+  // set up the burst and debug mode pin
+  pinMode(debugModePin, INPUT_PULLUP);
+  pinMode(burstModePin, INPUT_PULLUP);
+
+
   // Initialize the chibi command line and set the speed to 57600 bps
   chibiCmdInit(57600);
-  
-
-  pcf.enableMinuteInterrupt();
-  pcf.setInterruptToPulse();
+
+
+//  pcf.enableMinuteInterrupt();
+//  pcf.enableSecondInterrupt();
+//  pcf.setInterruptToPulse();
   attachInterrupt(2, rtcInterrupt, FALLING);
-  
+
   // Initialize the chibi wireless stack
   chibiInit();
-  
+
+  // Reads the node and sensors ID from the EEPROM
+  cmdReadConf(0,0);
+
   // set up chibi regs for external P/A
   //chibiRegWrite(0x4, 0xA0);
-    
+
   // datecode version
   printf(TITLE);
-  printf("Datecode: %s\n", DATECODE);
-
-
-  chibiSetShortAddr(NODE_ID);
-
-  Serial.print("NODE_ID: ");
-  Serial.println(r.node_id);
+  printf("Datecode: %s\n\r", DATECODE);
+
+  pcf.writeDate(16, 1, 29, 4);
+  pcf.writeTime(10, 15, 0);
+
+  chibiSetShortAddr(main_packet.node_id);
 
   // check for SD and init
+  uint8_t sdDetect;
   sdDetect = digitalRead(sdDetectPin);
   if (sdDetect == 0)
   {
     // init the SD card
-    if (!sd.begin(sdCsPin)) 
-    {
-      
+    if (!sd.begin(sdCsPin))
+    {
+
       Serial.println("Card failed, or not present");
-      sd.initErrorHalt();
-      return;
+      sd.initErrorPrint();
     }
     printf("SD Card is initialized.\n");
   }

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

/*
This struct will be common for all nodes
*/
typedef struct{
  int32_t sensor_id;
  int32_t value;
} reading_t;


/*
This struct depends on the note type
*/
typedef struct{
  reading_t temperature;
  reading_t humidity;
  reading_t battery;
  reading_t solar;
  int32_t count;
  int32_t signal_strength;
  char timestamp[19];
  int32_t node_id;
} techrice_packet_t;

/*
These values will be provided by the API
*/
#define NODE_ID 1
#define EDGE_ID 3
#define TEMPERATURE_SENSOR_ID 3
#define HUMIDITY_SENSOR_ID 4
#define BATTERY_SENSOR_ID 2
#define SOLAR_SENSOR_ID 1
#define SONAR_SENSOR_ID 5

techrice_packet_t r = {
  {TEMPERATURE_SENSOR_ID,0}, 
  {HUMIDITY_SENSOR_ID,0},
  {BATTERY_SENSOR_ID,0}, 
  {SOLAR_SENSOR_ID,0},
  0,
  0,
  "",
  NODE_ID
};


SdFat sd;
SdFile myFile;

#define DATECODE "07-29-2017"
#define TITLE "SABOTEN 900 MHz Long Range\nDATALOGER\n"
#define FILENAME "AWANODE.TXT"
#define ADCREFVOLTAGE 3.3

int hgmPin = 14;
int sdCsPin = 15;
int rtcCsPin = 28; 
int ledPin = 18;
int sdDetectPin = 19;
int vbatPin = 31;
int vsolPin = 29;

int dupe_cnt = 0;
unsigned char old[100];

unsigned char buf[100];

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

PCF2127 pcf(0, 0, 0, rtcCsPin);









  }



  // This is where you declare the commands for the command line.
  // The first argument is the alias you type in the command line. The second
  // argument is the name of the function that the command will jump to.

  chibiCmdAdd("getsaddr", cmdGetShortAddr);  // set the short address of the node
  chibiCmdAdd("setsaddr", cmdSetShortAddr);  // get the short address of the node
  chibiCmdAdd("send", cmdSend);   // send the string typed into the command line
  chibiCmdAdd("send2", cmd_tx2);
  chibiCmdAdd("rd", cmd_reg_read);   // send the string typed into the command line
  chibiCmdAdd("wr", cmd_reg_write);   // send the string typed into the command line
  chibiCmdAdd("slr", cmdSleepRadio);
  chibiCmdAdd("slm", cmdSleepMcu);
  chibiCmdAdd("time", cmdWriteTime);
  chibiCmdAdd("date", cmdWriteDate);
  chibiCmdAdd("rdt", cmdReadDateTime);
  chibiCmdAdd("bat", cmdVbatRead);
  chibiCmdAdd("sol", cmdVsolRead);
  chibiCmdAdd("tmp", cmdReadTemp);
  chibiCmdAdd("start", cmdStartCycle);
  chibiCmdAdd("rconf", cmdReadConf);
  chibiCmdAdd("wconf", cmdWriteConf);
  chibiCmdAdd("sdw", cmdSdWrite);
  chibiCmdAdd("sdr", cmdSdRead);
  chibiCmdAdd("sdc", cmdSdClear);


  // high gain mode
  digitalWrite(hgmPin, HIGH);
  if(digitalRead(debugModePin)==LOW){
    debug_mode = 1;
    digitalWrite(sonarAwakePin, HIGH);
  }

}

/**************************************************************************/
