Anish updated the code to work with L152. RTC seems to causing issue. Will have to disable LSE.

Dependencies:   mbed LoRaWAN-lib SX1276Lib

Revision:
16:e1d692816864
Parent:
15:f4805eb13d73
Child:
17:23caa53e6ec1
--- a/app/main.cpp	Mon Jan 21 19:53:37 2019 +0000
+++ b/app/main.cpp	Mon Jan 21 23:31:31 2019 +0000
@@ -164,31 +164,22 @@
 time_t next_ltest;
 
 uint8_t hb_data[5];
-uint8_t short_tdata[14];
-uint8_t long_tdata[41];
-
-uint8_t received_data[28];
+uint8_t tdata[10];
 
 uint8_t *data;
 uint8_t data_len;
 
 bool running_test;
 bool joining;
-bool received_downlink;
+// bool received_downlink;
 
 time_t current_time;
 
-unsigned long last_measurement;
 unsigned long last_hb;
 unsigned long test_start;
 
-int measureCount;
-uint8_t s_measurements[4];
-uint8_t l_measurements[22];
-uint8_t *measurements;
-int available_slots = 9;
+uint8_t measurements[5];
 
-unsigned long measurement_interval;
 unsigned long test_duration;
 
 /*!
@@ -360,20 +351,16 @@
     memcpy(hb_data+1, current_date, 4);
 }
 
-void short_test_result(uint8_t *short_tdata, uint8_t *test_start_date, uint8_t *next_test_date, uint8_t *measurements) {
-    short_tdata[0] = 0xD7;
-    memcpy(short_tdata+1, test_start_date, 4);
-    memcpy(short_tdata+5, next_test_date, 4);
-    short_tdata[9] = (uint8_t) 2;
-    memcpy(short_tdata+10, measurements, 4);
+void short_test_result(uint8_t *tdata, uint8_t *test_start_date, uint8_t *measurements) {
+    tdata[0] = 0xD7;
+    memcpy(tdata+1, test_start_date, 4);
+    memcpy(tdata+5, measurements, 5);
 }
 
-void long_test_result(uint8_t *long_tdata, uint8_t *test_start_date, uint8_t *next_test_date, uint8_t *measurements) {
-    long_tdata[0] = 0xD8;
-    memcpy(long_tdata+1, test_start_date, 4);
-    memcpy(long_tdata+5, next_test_date, 4);
-    long_tdata[9] = (uint8_t) 2;
-    memcpy(long_tdata+10, measurements, 31);
+void long_test_result(uint8_t *tdata, uint8_t *test_start_date, uint8_t *measurements) {
+    tdata[0] = 0xD8;
+    memcpy(tdata+1, test_start_date, 4);
+    memcpy(tdata+5, measurements, 5);
 }
 
 /*!
@@ -395,14 +382,14 @@
                 }
                 case MESSAGE_TYPE_SHORT_TEST:
                 {
-                    AppDataSize = 14;
-                    short_test_result(AppData, (uint8_t *)&current_time, (uint8_t *)&next_stest, s_measurements);
+                    AppDataSize = 10;
+                    short_test_result(AppData, (uint8_t *)&current_time, measurements);
                     break;
                 }
                 case MESSAGE_TYPE_LONG_TEST:
                 {
-                    AppDataSize = 32;
-                    long_test_result(AppData, (uint8_t *)&current_time, (uint8_t *)&next_ltest, l_measurements);
+                    AppDataSize = 10;
+                    long_test_result(AppData, (uint8_t *)&current_time, measurements);
                     break;
                 }
                 default:
@@ -880,36 +867,16 @@
 
 // ****************************    TEST METHODS   ********************************   //
 
-int takeMeasurement(uint8_t *measurements, int count) {
-  // This method should only run for long tests
-  int battery_reading = BAT_PIN.read()*1000;
-  int vce_reading = VCE_PIN.read()*1000;
-  int light_1_reading = LIGHT_1_PIN.read()*1000 - vce_reading;
-  int light_2_reading = LIGHT_2_PIN.read()*1000 - vce_reading;
-
-  int index = count;
-  
-  measurements[(uint8_t)index] = (uint8_t) (battery_reading/10);
-  measurements[(uint8_t)index+10] = (uint8_t) light_1_reading;
-  measurements[(uint8_t)index+20] = (uint8_t) light_2_reading;
-  return index+1;
-}
-
 void startTest(uint8_t *measurements) {
     
-    // Determine test length & measurement interval
+    // Determine test length
     switch (MessageType)
     {
         case MESSAGE_TYPE_SHORT_TEST:
             test_duration = 30;
-            // set interval to twice the test length so that it never happens
-            measurement_interval = 2*test_duration;
             break;
         case MESSAGE_TYPE_LONG_TEST:
             test_duration = 5400;
-            // interval is divided by two longer than available measurement 
-            // slots so that it completes before the end of the test
-            measurement_interval = test_duration/(available_slots + 2);
             break;
         default:
             test_duration = 0;
@@ -918,9 +885,6 @@
     
     // Measure voltage preval
     measurements[0] = (uint8_t) (BAT_PIN.read()*1000)/10;
-
-    // Set measurement count to 1
-    measureCount = 1;
     
     // Start test
     relayPin = 1;
@@ -935,19 +899,10 @@
     int light_1_reading = LIGHT_1_PIN.read()*1000 - vce_reading;
     int light_2_reading = LIGHT_2_PIN.read()*1000 - vce_reading;
     
-    switch (MessageType)
-    {
-        case MESSAGE_TYPE_SHORT_TEST:
-            measurements[1] = (uint8_t) (battery_reading/10);
-            measurements[2] = (uint8_t) light_1_reading;
-            measurements[3] = (uint8_t) light_2_reading;
-            break;
-        default:
-            measurements[10] = (uint8_t) (battery_reading/10);
-            measurements[20] = (uint8_t) light_1_reading;
-            measurements[30] = (uint8_t) light_2_reading;
-            break;
-    }
+    measurements[1] = (uint8_t) (battery_reading/10);
+    measurements[2] = (uint8_t) light_1_reading;
+    measurements[3] = (uint8_t) light_2_reading;
+    measurements[4] = (uint8_t) 0; // TODO: Update for RH
     
     // End test
     relayPin = 0;
@@ -960,24 +915,20 @@
     if (press_count >= 3) {
         // Run long test
         MessageType = MESSAGE_TYPE_LONG_TEST;
-        measurements = l_measurements;
         
         startTest(measurements);
                     
         running_test = true;
         test_start = current_time;
-        last_measurement = current_time;
     }
     if (press_count == 2) {
         // Run short test
         MessageType = MESSAGE_TYPE_SHORT_TEST;
-        measurements = s_measurements;
         
         startTest(measurements);
                     
         running_test = true;
         test_start = current_time;
-        last_measurement = current_time;
     }
     
     // Reset count
@@ -1062,7 +1013,7 @@
     pc.printf("mbed-os-rev: %d.%d.%d   lib-rev: %d\r\n", \
             MBED_MAJOR_VERSION, MBED_MINOR_VERSION,MBED_PATCH_VERSION,MBED_LIBRARY_VERSION);
     pc.printf("BUILD= %s, SysClock= %d, RCC= %0X\r\n", __TIME__, SystemCoreClock, RCC->CR);   
-    my_patch();
+    // my_patch();
     pc.printf("NEW SysClock= %d, NEW RCC= %0X\r\n", SystemCoreClock, RCC->CR);
     wait(3);
     LoRaMacPrimitives_t LoRaMacPrimitives;
@@ -1089,11 +1040,10 @@
     
     running_test = false;
     joining = true;
-    received_downlink = false;
+    //received_downlink = false;
     time_t start_time = time(NULL);
   
     last_hb = start_time;
-    last_measurement = start_time;
     
     //button1.mode(PullUp); // Activate pull-up
     button1.fall(callback(button1_onpressed_cb)); // Attach ISR to handle button press event
@@ -1274,13 +1224,6 @@
                 // Is a test is running
                 if (running_test) {
                   // If it is 
-                  //check if it's time to take the next interval measurement
-                  if (current_time - last_measurement >= measurement_interval) {
-                    if (measureCount <= available_slots) {
-                      measureCount = takeMeasurement(measurements, measureCount);
-                    }
-                    last_measurement = current_time;
-                  }
               
                   //check if it's time to end the test
                   if (current_time - test_start >= test_duration) {
@@ -1333,17 +1276,14 @@
                     // Check what kind of test to run
                     if (current_time >= next_ltest) {
                       MessageType = MESSAGE_TYPE_LONG_TEST;
-                      measurements = l_measurements;
                     } else {
                       MessageType = MESSAGE_TYPE_SHORT_TEST;
-                      measurements = s_measurements;
                     }
             
                     startTest(measurements);
                     
                     running_test = true;
                     test_start = current_time;
-                    last_measurement = current_time;
               
                     
                   //check if it's time for a heartbeat