SAIT ARIS / LRAT-example-lorawan-REFACTOR-and-CLEAN-Branch

Dependencies:   Custom_LSM303 Custom_UBloxGPS LRAT-mbed-os USBDevice mbed-lora-radio-drv stm32EEPROM

Fork of LRAT-example-lorawan by SAIT ARIS

Revision:
30:ff249a9156dc
Parent:
26:f07f5febf97f
Child:
31:f03c183e2bf6
--- a/main.cpp	Mon Jul 23 12:00:22 2018 +0100
+++ b/main.cpp	Thu Aug 02 23:19:00 2018 +0000
@@ -15,6 +15,7 @@
  * limitations under the License.
  */
 #include <stdio.h>
+#include "mbed.h"
 
 #include "lorawan/LoRaWANInterface.h"
 #include "lorawan/system/lorawan_data_structures.h"
@@ -25,6 +26,9 @@
 #include "trace_helper.h"
 #include "lora_radio_helper.h"
 
+#include "mbed-trace/mbed_trace.h"
+#define TRACE_GROUP "MAIN"
+
 using namespace events;
 
 // Max payload size can be LORAMAC_PHY_MAXPAYLOAD.
@@ -48,7 +52,7 @@
 /**
  * Maximum number of retries for CONFIRMED messages before giving up
  */
-#define CONFIRMED_MSG_RETRY_COUNTER     3
+#define CONFIRMED_MSG_RETRY_COUNTER     15
 
 /**
  * Dummy pin for dummy sensor
@@ -87,6 +91,17 @@
  */
 static lorawan_app_callbacks_t callbacks;
 
+int mytime;
+int mybatt;
+double mylat;
+double mylon;
+
+#define NEOM8M_ADR_GPS (0x42 >> 1);
+#define LSM303_ADR_ACC (0x32 >> 1);
+#define LSM303_ADR_MAG (0x3C >> 1);
+
+I2C i2c(PB_9, PB_8);
+
 /**
  * Entry point for application
  */
@@ -98,6 +113,13 @@
     // stores the status of a call to LoRaWAN protocol
     lorawan_status_t retcode;
 
+    printf("\r\n- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\r\n");
+    
+    mytime = 0;
+    mybatt = 15;
+    mylat = 51.06509;
+    mylon = -114.08895;
+
     // Initialize LoRaWAN stack
     if (lorawan.initialize(&ev_queue) != LORAWAN_STATUS_OK) {
         printf("\r\n LoRa initialization failed! \r\n");
@@ -105,6 +127,7 @@
     }
 
     printf("\r\n Mbed LoRaWANStack initialized \r\n");
+    //printf("MBED_CONF_LORA_APP_PORT: %d", MBED_CONF_LORA_APP_PORT);
 
     // prepare application callbacks
     callbacks.events = mbed::callback(lora_event_handler);
@@ -129,7 +152,9 @@
     printf("\r\n Adaptive data  rate (ADR) - Enabled \r\n");
 
     retcode = lorawan.connect();
-
+    /*if (retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) {
+        printf("Retcode = Connecting.");
+    }*/
     if (retcode == LORAWAN_STATUS_OK ||
         retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) {
     } else {
@@ -164,9 +189,37 @@
         return;
     }
 
-    packet_len = sprintf((char*) tx_buffer, "Dummy Sensor Value is %3.1f",
-                    sensor_value);
-
+    //packet_len = sprintf((char*) tx_buffer, "Dummy Sensor Value is %3.1f",
+    //                sensor_value);
+    //packet_len = sprintf((char*) tx_buffer, "VAL=%3.1f", sensor_value);
+    //packet_len = sprintf((char*) tx_buffer, "%d,%d,%f,%f", mytime, mybatt, mylat, mylon);
+    //printf("BUF: |%s|", tx_buffer);
+    time_t tNow = time(NULL);
+    printf("Clock: %d\r\n", tNow);
+    mytime = tNow;
+    char[1024] buf;
+    i2c.read(NEOM8M_ADR_GPS, buf, 1024);
+    printf(buf);
+    int ilat = (int)(mylat * 100000);
+    int ilon = (int)(mylon * 100000);
+    packet_len = 11;
+    tx_buffer[0] = (mytime >> 24) & 0xFF;
+    tx_buffer[1] = (mytime >> 16) & 0xFF;
+    tx_buffer[2] = (mytime >> 8) & 0xFF;
+    tx_buffer[3] = (mytime >> 0) & 0xFF;
+    tx_buffer[4] = ((mybatt << 4) & 0xF0) | ((ilat >> 22) & 0x0F);
+    tx_buffer[5] = (ilat >> 14) & 0xFF;
+    tx_buffer[6] = (ilat >> 6) & 0xFF;
+    tx_buffer[7] = ((ilat << 2) & 0xFC) | ((ilon >> 24) & 0x03);
+    tx_buffer[8] = (ilon >> 16) & 0xFF;
+    tx_buffer[9] = (ilon >> 8) & 0xFF;
+    tx_buffer[10] = (ilon >> 0) & 0xFF;
+    printf("\r\nBUF: |");
+    int i;
+    for (i = 0; i < packet_len; i++) { printf("%02x", tx_buffer[i]); }
+    printf("|\r\n");
+    mytime++;
+    if (mybatt == 0) { mybatt = 15; } else { mybatt--; }
     retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, tx_buffer, packet_len,
                            MSG_CONFIRMED_FLAG);
 
@@ -218,6 +271,7 @@
  */
 static void lora_event_handler(lorawan_event_t event)
 {
+    tr_debug("In lora_event_handler(%d)...", event);
     switch (event) {
         case CONNECTED:
             printf("\r\n Connection - Successful \r\n");