Lora_with_GPS integration code - program hangs after a few transmissions

Dependencies:   mbed LoRaWAN-lib SingleFrequencyLora

Fork of simple-demo-76_revised_20171113 by Christopher De Bank

Committer:
Rishin
Date:
Mon Nov 20 12:03:22 2017 +0000
Revision:
12:7debb1c79a06
Sending GPS data every 5 seconds, but program is crashing - possibly due to  memory issues - try passing structs by reference

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Rishin 12:7debb1c79a06 1 #include "l86.hpp"
Rishin 12:7debb1c79a06 2 #include <string.h>
Rishin 12:7debb1c79a06 3 #include <stdio.h>
Rishin 12:7debb1c79a06 4 #include "mbed.h"
Rishin 12:7debb1c79a06 5 #include "LoRaMac.h"
Rishin 12:7debb1c79a06 6 #include "board.h"
Rishin 12:7debb1c79a06 7 #include "radio.h"
Rishin 12:7debb1c79a06 8
Rishin 12:7debb1c79a06 9 #ifdef DEBUGGER
Rishin 12:7debb1c79a06 10 RawSerial pc2(PC_TX, PC_RX); // USART2
Rishin 12:7debb1c79a06 11 #endif
Rishin 12:7debb1c79a06 12
Rishin 12:7debb1c79a06 13 /* Parse NMEA RMC sentence into RMC data struct */
Rishin 12:7debb1c79a06 14 RMC_data Parse_RMC_sentence(char RMC_sentence[MAX_NMEA_LENGTH]){
Rishin 12:7debb1c79a06 15 const char delimeter[2] = ",";
Rishin 12:7debb1c79a06 16 char *token = "";
Rishin 12:7debb1c79a06 17 int i = 0;
Rishin 12:7debb1c79a06 18 char temp[11][12]; /* [11][12]: 11 strings, of length 12 */
Rishin 12:7debb1c79a06 19 RMC_data RMC_parsed;
Rishin 12:7debb1c79a06 20
Rishin 12:7debb1c79a06 21 strcpy(RMC_parsed.Message, RMC_sentence);
Rishin 12:7debb1c79a06 22
Rishin 12:7debb1c79a06 23 //Seperated Message
Rishin 12:7debb1c79a06 24 /* get the first token */
Rishin 12:7debb1c79a06 25 token = strtok(RMC_sentence, delimeter);
Rishin 12:7debb1c79a06 26
Rishin 12:7debb1c79a06 27 /* walk through other tokens */
Rishin 12:7debb1c79a06 28 while( token != NULL )
Rishin 12:7debb1c79a06 29 {
Rishin 12:7debb1c79a06 30 strcpy(temp[i],token);
Rishin 12:7debb1c79a06 31 i++;
Rishin 12:7debb1c79a06 32 token = strtok(NULL, delimeter);
Rishin 12:7debb1c79a06 33 }
Rishin 12:7debb1c79a06 34
Rishin 12:7debb1c79a06 35 //Copy the message into its individual components
Rishin 12:7debb1c79a06 36 strcpy(RMC_parsed.Message_ID,temp[0]);
Rishin 12:7debb1c79a06 37 strcpy(RMC_parsed.UTC_Time,temp[1]);
Rishin 12:7debb1c79a06 38 strcpy(RMC_parsed.Status,temp[2]);
Rishin 12:7debb1c79a06 39 if(strcmp(RMC_parsed.Status,"A") == 0){
Rishin 12:7debb1c79a06 40 strcpy(RMC_parsed.Latitude,temp[3]);
Rishin 12:7debb1c79a06 41 strcpy(RMC_parsed.N_S_Indicator,temp[4]);
Rishin 12:7debb1c79a06 42 strcpy(RMC_parsed.Longitude,temp[5]);
Rishin 12:7debb1c79a06 43 strcpy(RMC_parsed.E_W_Indicator,temp[6]);
Rishin 12:7debb1c79a06 44 strcpy(RMC_parsed.Speed_Over_Ground,temp[7]);
Rishin 12:7debb1c79a06 45 strcpy(RMC_parsed.Course_Over_Ground,temp[8]);
Rishin 12:7debb1c79a06 46 strcpy(RMC_parsed.Date,temp[9]);
Rishin 12:7debb1c79a06 47 strcpy(RMC_parsed.Mode,temp[10]);
Rishin 12:7debb1c79a06 48 }
Rishin 12:7debb1c79a06 49 return RMC_parsed;
Rishin 12:7debb1c79a06 50 }
Rishin 12:7debb1c79a06 51
Rishin 12:7debb1c79a06 52 // Use GPS data parse function? // parses into data formats that will be sent over LoRa
Rishin 12:7debb1c79a06 53
Rishin 12:7debb1c79a06 54 /* Print RMC_data struct to PC USART for debugging */
Rishin 12:7debb1c79a06 55 void Print_RMC_data(RMC_data *RMC_data_print){
Rishin 12:7debb1c79a06 56 pc2.printf("RMC_Message: %s",RMC_data_print->Message);
Rishin 12:7debb1c79a06 57 pc2.printf("UTC_Time: %s\r\n",RMC_data_print->UTC_Time);
Rishin 12:7debb1c79a06 58 pc2.printf("Status: %s\r\n",RMC_data_print->Status);
Rishin 12:7debb1c79a06 59 if(strcmp(RMC_data_print->Status,"A") == 0){
Rishin 12:7debb1c79a06 60 pc2.printf("Latitude: %s\r\n",RMC_data_print->Latitude);
Rishin 12:7debb1c79a06 61 pc2.printf("N/S: %s\r\n",RMC_data_print->N_S_Indicator);
Rishin 12:7debb1c79a06 62 pc2.printf("Longitude: %s\r\n",RMC_data_print->Longitude);
Rishin 12:7debb1c79a06 63 pc2.printf("E/W: %s\r\n",RMC_data_print->E_W_Indicator);
Rishin 12:7debb1c79a06 64 pc2.printf("Speed: %s\r\n",RMC_data_print->Speed_Over_Ground);
Rishin 12:7debb1c79a06 65 pc2.printf("Course: %s\r\n",RMC_data_print->Course_Over_Ground);
Rishin 12:7debb1c79a06 66 pc2.printf("Date: %s\r\n",RMC_data_print->Date);
Rishin 12:7debb1c79a06 67 pc2.printf("Mode: %s\r\n",RMC_data_print->Mode);
Rishin 12:7debb1c79a06 68 }
Rishin 12:7debb1c79a06 69 }
Rishin 12:7debb1c79a06 70
Rishin 12:7debb1c79a06 71 /* Parse RMC_data struct into GPS data struct ready for sending over LoRa */
Rishin 12:7debb1c79a06 72 GPS_data Parse_RMC_data(RMC_data RMC_parsed){
Rishin 12:7debb1c79a06 73 GPS_data GPS_parsed;
Rishin 12:7debb1c79a06 74 if(strcmp(RMC_parsed.Status,"A") == 0){
Rishin 12:7debb1c79a06 75 strcpy(GPS_parsed.UTC_Time,RMC_parsed.UTC_Time);
Rishin 12:7debb1c79a06 76 if (strcmp(RMC_parsed.N_S_Indicator, "N") == 0){
Rishin 12:7debb1c79a06 77 strcpy(GPS_parsed.Latitude, strcat("+", RMC_parsed.Latitude));
Rishin 12:7debb1c79a06 78 }
Rishin 12:7debb1c79a06 79 else{
Rishin 12:7debb1c79a06 80 strcpy(GPS_parsed.Latitude, strcat("-", RMC_parsed.Latitude));
Rishin 12:7debb1c79a06 81 }
Rishin 12:7debb1c79a06 82 if (strcmp(RMC_parsed.E_W_Indicator, "E") == 0){
Rishin 12:7debb1c79a06 83 strcpy(GPS_parsed.Longitude, strcat("+", RMC_parsed.Longitude));
Rishin 12:7debb1c79a06 84 }
Rishin 12:7debb1c79a06 85 else{
Rishin 12:7debb1c79a06 86 strcpy(GPS_parsed.Longitude, strcat("-", RMC_parsed.Longitude));
Rishin 12:7debb1c79a06 87 }
Rishin 12:7debb1c79a06 88 strcpy(GPS_parsed.Speed_Over_Ground,RMC_parsed.Speed_Over_Ground);
Rishin 12:7debb1c79a06 89 strcpy(GPS_parsed.Course_Over_Ground,RMC_parsed.Course_Over_Ground);
Rishin 12:7debb1c79a06 90 strcpy(GPS_parsed.Date,RMC_parsed.Date);
Rishin 12:7debb1c79a06 91 strcpy(GPS_parsed.Valid,RMC_parsed.Status);
Rishin 12:7debb1c79a06 92 }
Rishin 12:7debb1c79a06 93 else {
Rishin 12:7debb1c79a06 94 strcpy(GPS_parsed.UTC_Time, "000000.000");
Rishin 12:7debb1c79a06 95 strcpy(GPS_parsed.Latitude,"+0000.0000");
Rishin 12:7debb1c79a06 96 strcpy(GPS_parsed.Longitude,"+00000.0000");
Rishin 12:7debb1c79a06 97 strcpy(GPS_parsed.Speed_Over_Ground,"0.00");
Rishin 12:7debb1c79a06 98 strcpy(GPS_parsed.Course_Over_Ground,"000.00");
Rishin 12:7debb1c79a06 99 strcpy(GPS_parsed.Date,"000000");
Rishin 12:7debb1c79a06 100 strcpy(GPS_parsed.Valid,"V");
Rishin 12:7debb1c79a06 101 }
Rishin 12:7debb1c79a06 102 return GPS_parsed;
Rishin 12:7debb1c79a06 103 }
Rishin 12:7debb1c79a06 104
Rishin 12:7debb1c79a06 105 /* Print GPS_data struct to PC USART for debugging */
Rishin 12:7debb1c79a06 106 void Print_GPS_data(GPS_data GPS_data_print){
Rishin 12:7debb1c79a06 107 pc2.printf("UTC_Time: %s\r\n",GPS_data_print.UTC_Time);
Rishin 12:7debb1c79a06 108 pc2.printf("Status: %s\r\n",GPS_data_print.Valid);
Rishin 12:7debb1c79a06 109 pc2.printf("Latitude: %s\r\n",GPS_data_print.Latitude);
Rishin 12:7debb1c79a06 110 pc2.printf("Longitude: %s\r\n",GPS_data_print.Longitude);
Rishin 12:7debb1c79a06 111 pc2.printf("Speed: %s\r\n",GPS_data_print.Speed_Over_Ground);
Rishin 12:7debb1c79a06 112 pc2.printf("Course: %s\r\n",GPS_data_print.Course_Over_Ground);
Rishin 12:7debb1c79a06 113 pc2.printf("Date: %s\r\n",GPS_data_print.Date);
Rishin 12:7debb1c79a06 114 }
Rishin 12:7debb1c79a06 115
Rishin 12:7debb1c79a06 116 /* Send GPS data using LoRa module */
Rishin 12:7debb1c79a06 117 void Send_GPS_data(GPS_data GPS_data_parsed){
Rishin 12:7debb1c79a06 118 __disable_irq();
Rishin 12:7debb1c79a06 119 char AppData[APPDATA_SIZE];
Rishin 12:7debb1c79a06 120
Rishin 12:7debb1c79a06 121 // Could do this using a pointer and pointing to the first address of the struct?
Rishin 12:7debb1c79a06 122 // fill AppData byte array with GPS_data struct
Rishin 12:7debb1c79a06 123 strcat(AppData, GPS_data_parsed.UTC_Time);
Rishin 12:7debb1c79a06 124 strcat(AppData, GPS_data_parsed.Latitude);
Rishin 12:7debb1c79a06 125 strcat(AppData, GPS_data_parsed.Longitude);
Rishin 12:7debb1c79a06 126 strcat(AppData, GPS_data_parsed.Speed_Over_Ground);
Rishin 12:7debb1c79a06 127 strcat(AppData, GPS_data_parsed.Course_Over_Ground);
Rishin 12:7debb1c79a06 128 strcat(AppData, GPS_data_parsed.Date);
Rishin 12:7debb1c79a06 129 strcat(AppData, GPS_data_parsed.Valid);
Rishin 12:7debb1c79a06 130
Rishin 12:7debb1c79a06 131 //pc2.printf(AppData);
Rishin 12:7debb1c79a06 132 //Sendframe
Rishin 12:7debb1c79a06 133
Rishin 12:7debb1c79a06 134 McpsReq_t mcpsReq;
Rishin 12:7debb1c79a06 135
Rishin 12:7debb1c79a06 136 uint8_t AppPort = 3;
Rishin 12:7debb1c79a06 137 mcpsReq.Type = MCPS_UNCONFIRMED;
Rishin 12:7debb1c79a06 138 mcpsReq.Req.Unconfirmed.fPort = AppPort;
Rishin 12:7debb1c79a06 139 mcpsReq.Req.Unconfirmed.fBuffer = AppData;
Rishin 12:7debb1c79a06 140 mcpsReq.Req.Unconfirmed.fBufferSize = APPDATA_SIZE;
Rishin 12:7debb1c79a06 141 mcpsReq.Req.Unconfirmed.Datarate = DR_5;
Rishin 12:7debb1c79a06 142
Rishin 12:7debb1c79a06 143 LoRaMacMcpsRequest( &mcpsReq );
Rishin 12:7debb1c79a06 144 __enable_irq();
Rishin 12:7debb1c79a06 145 }