This is used for sending Data to receiving mDot

Dependencies:   libmDot-dev-mbed5-deprecated sd-driver ISL29011

Fork of mdot-examples by 3mdeb

Revision:
28:29702434319d
Parent:
27:5db200a4d496
Child:
29:0f0a059f023e
--- a/send_main.cpp	Sun Apr 01 21:32:06 2018 +0000
+++ b/send_main.cpp	Tue Apr 10 23:51:44 2018 +0000
@@ -7,18 +7,39 @@
 #include <iostream>
 #include <string.h>
 #include <mbed.h>
-#include "mDot.h"
 #include "dot_util.h"
 #include "RadioEvent.h"
 #include "itoa.h"
+#include <sstream>
+
+// For time Keeping
+#include <ctime>
+
+#include <errno.h>  // For SD card error checking
+#include "platform/FilePath.h"  // For SD card struct stat
+
+// Headers for the SD card
+#include "SDBlockDevice.h"
+#include "FATFileSystem.h"
 
 #define BUFFER_SIZE 10
 
+//------------------------------SD CARD------------------------------------//
+
+SDBlockDevice sd(D11, D12, D13, D10); // mosi, miso, sclk, cs
+FATFileSystem fs("sd"); 
+
+FILE *fp;
+
+// Error checking SD stuff
+int err;
+
 //note: added GPS functions , variables below (will organize better later ski
 //and gps code in main is noted
 
 
 
+
 ///-----------------FOR GPS------BELOW----------------------------------
 //
 
@@ -380,18 +401,6 @@
  /////FOR GPS-----------------ABOVE-------------------------------------
 
 
-
-
-
-
-
-
-
-
-
-
-
- 
 /////////////////////////////////////////////////////////////////////////////
 // -------------------- DOT LIBRARY REQUIRED ------------------------------//
 // * Because these example programs can be used for both mDot and xDot     //
@@ -418,8 +427,8 @@
 lora::ChannelPlan* plan = NULL;
 //--------------End of MDOT variables-------------------//
 
+// PC serial communication
 Serial pc(USBTX, USBRX);
-Ticker Periodic;
 
 // ADXL372 Slave I2C
 I2C ADXL372(I2C_SDA, I2C_SCL);  // (D14,D15) (MISO, CS)
@@ -442,11 +451,8 @@
 const int ADXL372_Address_7bit = 0x1D;      // Address for the I2C if MISO pulled low, 0x53 if pulled high
 const int ADXL372_Address_8bit = ADXL372_Address_7bit << 1; // Same
 
-const int DS7505s_Address_7bit = 0x48;  // A0 set LOR, A1 set LOW, A2 set LOW
-const int DS7505s_Address_8bit = DS7505s_Address_7bit << 1; // Same
-
-
-int regAddress; // Used by all sensors
+// Used by all sensors to pass register address of device
+int regAddress;
 
 /*
  *  Variables used for ADT7410 Temperature
@@ -460,8 +466,6 @@
 /*
  *  Variables used for mDot
  */
-
-
 uint32_t tx_frequency;
 uint8_t tx_datarate;
 uint8_t tx_power;
@@ -501,6 +505,7 @@
  *  Prototype functions
  */
  
+std::string convertInt(int number);
 char twosComplementConversion(char value);
 
 void ADXL372Initialize(void);
@@ -525,6 +530,7 @@
     takeAccelerometer = true;   // Take accelerometer because something happened
 }
 
+// This is where all the reading and sending takes place
 void takePeriodicReading(std::vector<uint8_t> tx_data){
         pc.printf("Regular periodic reading \n\r");
         /*
@@ -640,7 +646,17 @@
         tx_data.push_back(ZData & 0xFF);
         logInfo("Temperautre: %lu [0x%04X]", convertedTempValue, convertedTempValue);
         send_data(tx_data);
-        periodicReadingTrigger = false; // Flip back to no trigger
+        
+        
+        // Save data
+//        fprintf(fp,"%d,%d,%d,%d,%f,%f,%d/%d/%d,%d:%d:%d:%d\n",
+//                    convertedTempValue,
+//                    XData, YData, ZData,
+//                    myGPS.longitude, myGPS.latitude,
+//                    myGPS.month, myGPS.day, myGPS.year,
+//                    myGPS.hour, myGPS.minute, myGPS.seconds, myGPS.milliseconds);
+//        periodicReadingTrigger = false; // Flip back to no trigger
+        
 }
 
 void takePeriodicReadingTicker(void){
@@ -657,9 +673,11 @@
  *//////////////////////////////////////////////////////////////////////////////
 int main(void)
 {
+    
     // Custom event handler for automatically displaying RX data
     //interruptEverything.attach(&interruptReadTemperature, 7.0);
     RadioEvent events;
+    
     // Change baud rate in serial terminal to this value
     pc.baud(115200);
     ADXL372.frequency(300000);  // I2C devices are connected to the same clock
@@ -672,6 +690,20 @@
     myGPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);
     myGPS.sendCommand(PGCMD_ANTENNA);
     
+    // current date/time based on current system
+    std::time_t now = time(0);
+    
+    // convert now to string format
+    char* dt = ctime(&now);
+    // To access individual times
+    tm *ltm = localtime(&now);
+    
+    std::string file_Date = convertInt(ltm->tm_mon) + "-" + 
+                            convertInt(ltm->tm_mday) + "-" + 
+                            convertInt(ltm->tm_year + 1900) + ".txt";
+    std::string directory = "sd/mytest/";
+    std::string fileName =  directory + file_Date;
+    
     
     mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);
     
@@ -782,16 +814,66 @@
     // It's gonna output a lot of information onto the Serial Terminal
     display_config();
 
-
+    
+    
 
 
-
-//below is acc,temp sensors
+    //below is acc,temp sensors
     ADT7410Initialize();
     ADXL372Initialize();
     
-    //Periodic.attach(&takePeriodicReadingTicker,5);
+    pc.printf("%s\n", fileName);
     
+//    // Initialize the SD card
+//    if(sd.init() == 0)
+//    {
+//        pc.printf("Init success \n\r");
+//    }
+//    else pc.printf("Init failed \n\r");
+//    err = fs.mount(&sd);
+//    pc.printf("%s\r\n", (err ? "Failed :(\r\n" : "OK\r\n"));
+//    if (err)
+//        return err;
+//    pc.printf("Error for mounting was %d\n\r", err);
+//    
+//    pc.printf("Opening file %s... ", fileName);
+// 
+//    // Open your file
+//    fp = fopen(fileName.c_str(), "w+");
+//    pc.printf("%s\r\n", (!fp ? "Failed :(\r\n" : "OK\r\n"));
+//    
+//    if (!fp)
+//    {
+//        // Check whether directory '/sd/mytest' exists.
+//        pc.printf("\r\nChecking directory '%s'...\r\n", fileName);
+//        struct stat info;
+//        err = stat(directory.c_str(), &info);
+//        if (err)
+//        {
+//            pc.printf("Directory '%s' does not exist.\r\n", directory);
+//            pc.printf("Trying to create it...");
+//            err = mkdir(directory.c_str(), 0777);
+//            pc.printf("%s\r\n", (err ? "Failed :(\r\n" : "OK\r\n"));
+//            if (err)
+//              return err;
+//        }
+//                
+//        // Create a new 'sdtest.txt' file.
+//        pc.printf("File not found, creating a new one...\r\n");
+//        fp = fopen(fileName.c_str(), "w+");
+//        pc.printf("%s\r\n", (!fp ? "Failed :(" : "OK"));
+//        if (!fp)
+//        {
+//            error("error: %s (%d)\r\n", strerror(errno), -errno);
+//            return errno;
+//        }
+//    }
+//    
+//    fprintf(fp,"Temperature,X Acceleration,Y Acceleration,Z Acceleration,\
+//                    Longitude,Latitude,Date,Time\n");
+    
+    
+    // Continous loop
     while(1){
         // Create a vector of uint8_t elements to be sent later
         
@@ -810,9 +892,6 @@
             
             takePeriodicReading(tx_data);
             
-            
-            
-            
             takeAccelerometer = false;  // Flip back to no trigger
             takeTemperature = false;    // Flip back to no trigger
             
@@ -822,7 +901,7 @@
         
         // Go to sleep for 5 seconds and wake up after 5 seconds or wakeup from interrupt
         
-        if(dot->sleep(5,mDot::RTC_ALARM_OR_INTERRUPT, false) == mDot::MDOT_OK)
+        if(dot->sleep(1,mDot::RTC_ALARM_OR_INTERRUPT, false) == mDot::MDOT_OK)
         {
            pc.printf("Sleep well\n\r");
         }
@@ -832,6 +911,15 @@
     return 0;
 }
 
+
+std::string convertInt(int number)
+{
+    std::stringstream ss;
+    ss << number;
+    return ss.str();    
+    
+}
+
 /*******************************************************************************
  *  Not really used at the moment
  *  Not really needed. But keep just in case because I don't want to rewrite it
@@ -861,9 +949,20 @@
 void ADXL372Initialize(void){
     ADXL372Reset();
     pc.printf("Initializing ADXL372 accelerometer\n\r");
-    accelerometerI2CWrite(0x3F, 0x0F);  // Enable I2C highspeed,Low Pass, High pass and full bandwidth measurement mode
+     accelerometerI2CWrite(0x20, 0x0F);   // X offset
+     accelerometerI2CWrite(0x21, 0x02);   // Y offset
+     accelerometerI2CWrite(0x22, 0x05);   // Z offset
+    // accelerometerI2CWrite(0x33, 0x01);   // X axis used
+    // accelerometerI2CWrite(0x35, 0x01);   // Y axis used
+    // accelerometerI2CWrite(0x37, 0x01);   // Z axis used
+
+    //accelerometerI2CWrite(0x3A, 0x00);    // FIFO takes X,Y,Z
+    //accelerometerI2CWrite(0x3D, 0x03);    // ODR 3200 Hz
+    accelerometerI2CWrite(0x3E, 0x03);    // Bandwidth 1600
+//  accelerometerI2CWrite(0x3F, 0x2F);  // High instant on threshold, LPF&HPF disabled, Instant on Mode
+    accelerometerI2CWrite(0x3F, 0x3E);  // Enable I2C highspeed,Low Pass, High pass and full bandwidth measurement mode
     accelerometerI2CWrite(0x38, 0x01);  // Enable the High pass filter corner 1 at register 0x38
-    accelerometerI2CWrite(0x3E, 0x44);  // AutoSleep is enabled with LINKLOOP at DEFAULTMODE , Bandwidth at default, 
+    //accelerometerI2CWrite(0x3E, 0x44);  // AutoSleep is enabled with LINKLOOP at DEFAULTMODE , Bandwidth at default, 
     
 /*     accelerometerI2CWrite(0x24, 0x01);  // X used for activity threshold
     accelerometerI2CWrite(0x26, 0x01);  // Y used for activity threshold