GPS working with LoRa code - can't transmit faster that once every 6 seconds
Dependencies: mbed LoRaWAN-lib_gps_lora SingleFrequencyLora
app/main.cpp@13:66d854ad31d8, 2017-11-24 (annotated)
- Committer:
- Rishin
- Date:
- Fri Nov 24 15:20:12 2017 +0000
- Revision:
- 13:66d854ad31d8
- Child:
- 14:9e41438308eb
Valid GPS data sent over LoRa for roughly 7 mins with no crash
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Rishin | 13:66d854ad31d8 | 1 | /* |
Rishin | 13:66d854ad31d8 | 2 | / _____) _ | | |
Rishin | 13:66d854ad31d8 | 3 | ( (____ _____ ____ _| |_ _____ ____| |__ |
Rishin | 13:66d854ad31d8 | 4 | \____ \| ___ | (_ _) ___ |/ ___) _ \ |
Rishin | 13:66d854ad31d8 | 5 | _____) ) ____| | | || |_| ____( (___| | | | |
Rishin | 13:66d854ad31d8 | 6 | (______/|_____)_|_|_| \__)_____)\____)_| |_| |
Rishin | 13:66d854ad31d8 | 7 | (C)2015 Semtech |
Rishin | 13:66d854ad31d8 | 8 | |
Rishin | 13:66d854ad31d8 | 9 | Description: LoRaMac classA device implementation |
Rishin | 13:66d854ad31d8 | 10 | |
Rishin | 13:66d854ad31d8 | 11 | License: Revised BSD License, see LICENSE.TXT file include in the project |
Rishin | 13:66d854ad31d8 | 12 | |
Rishin | 13:66d854ad31d8 | 13 | Maintainer: Miguel Luis and Gregory Cristian |
Rishin | 13:66d854ad31d8 | 14 | */ |
Rishin | 13:66d854ad31d8 | 15 | #include "mbed.h" |
Rishin | 13:66d854ad31d8 | 16 | #include "board.h" |
Rishin | 13:66d854ad31d8 | 17 | #include "radio.h" |
Rishin | 13:66d854ad31d8 | 18 | #include "LoRaMac.h" |
Rishin | 13:66d854ad31d8 | 19 | |
Rishin | 13:66d854ad31d8 | 20 | |
Rishin | 13:66d854ad31d8 | 21 | #define APPDATA_SIZE 54 |
Rishin | 13:66d854ad31d8 | 22 | #define LORAWAN_DEFAULT_DATARATE DR_0 |
Rishin | 13:66d854ad31d8 | 23 | #define LORAWAN_DEVICE_ADDRESS ( uint32_t )0x01234567 |
Rishin | 13:66d854ad31d8 | 24 | #define LORAWAN_NWKSKEY { 0x11, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 } |
Rishin | 13:66d854ad31d8 | 25 | #define LORAWAN_APPSKEY { 0x11, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 } |
Rishin | 13:66d854ad31d8 | 26 | |
Rishin | 13:66d854ad31d8 | 27 | uint8_t AppData[APPDATA_SIZE]; |
Rishin | 13:66d854ad31d8 | 28 | static uint8_t NwkSKey[] = LORAWAN_NWKSKEY; |
Rishin | 13:66d854ad31d8 | 29 | static uint8_t AppSKey[] = LORAWAN_APPSKEY; |
Rishin | 13:66d854ad31d8 | 30 | |
Rishin | 13:66d854ad31d8 | 31 | |
Rishin | 13:66d854ad31d8 | 32 | #include "mbed.h" |
Rishin | 13:66d854ad31d8 | 33 | #include "l86.hpp" |
Rishin | 13:66d854ad31d8 | 34 | |
Rishin | 13:66d854ad31d8 | 35 | |
Rishin | 13:66d854ad31d8 | 36 | // #ifdef DEBUGGER |
Rishin | 13:66d854ad31d8 | 37 | // RawSerial pc(PC_TX, PC_RX); // USART2 |
Rishin | 13:66d854ad31d8 | 38 | // #endif |
Rishin | 13:66d854ad31d8 | 39 | |
Rishin | 13:66d854ad31d8 | 40 | RawSerial gps(GPS_TX, GPS_RX); // USART1 |
Rishin | 13:66d854ad31d8 | 41 | |
Rishin | 13:66d854ad31d8 | 42 | // DigitalOut Reset(RESET_PIN); |
Rishin | 13:66d854ad31d8 | 43 | // DigitalOut Force_on(FORCE_ON_PIN); |
Rishin | 13:66d854ad31d8 | 44 | DigitalOut GPS_status(GPS_STATUS_LED); |
Rishin | 13:66d854ad31d8 | 45 | // DigitalIn OnePPS(ONEPPS_PIN); |
Rishin | 13:66d854ad31d8 | 46 | |
Rishin | 13:66d854ad31d8 | 47 | RMC_data RMC; |
Rishin | 13:66d854ad31d8 | 48 | GPS_data GPS_parse; |
Rishin | 13:66d854ad31d8 | 49 | volatile int Char_index = 0; // index for char array |
Rishin | 13:66d854ad31d8 | 50 | char Rx_data[MAX_NMEA_LENGTH] = "0"; // char array to store received bytes |
Rishin | 13:66d854ad31d8 | 51 | char tx_line[82]; |
Rishin | 13:66d854ad31d8 | 52 | uint8_t valid = 0; |
Rishin | 13:66d854ad31d8 | 53 | volatile uint8_t GPS_data_ready = 0; |
Rishin | 13:66d854ad31d8 | 54 | volatile uint8_t sending = 0; |
Rishin | 13:66d854ad31d8 | 55 | |
Rishin | 13:66d854ad31d8 | 56 | Ticker LoRaSend_timer; |
Rishin | 13:66d854ad31d8 | 57 | |
Rishin | 13:66d854ad31d8 | 58 | void gps_receive(void) { |
Rishin | 13:66d854ad31d8 | 59 | // Note: you need to actually read from the serial to clear the RX interrupt |
Rishin | 13:66d854ad31d8 | 60 | while(gps.readable()) { |
Rishin | 13:66d854ad31d8 | 61 | char incoming = gps.getc(); |
Rishin | 13:66d854ad31d8 | 62 | // #ifdef DEBUGGER |
Rishin | 13:66d854ad31d8 | 63 | // pc.putc(incoming); |
Rishin | 13:66d854ad31d8 | 64 | // #endif |
Rishin | 13:66d854ad31d8 | 65 | Rx_data[Char_index] = incoming; |
Rishin | 13:66d854ad31d8 | 66 | |
Rishin | 13:66d854ad31d8 | 67 | if((Rx_data[Char_index] == '\n')){ // Received the end of an NMEA scentence |
Rishin | 13:66d854ad31d8 | 68 | if((strncmp(GPRMC_Tag,Rx_data,(sizeof(GPRMC_Tag)-1)) == 0) || (strncmp(GNRMC_Tag,Rx_data,(sizeof(GNRMC_Tag)-1)) == 0)){ |
Rishin | 13:66d854ad31d8 | 69 | RMC = Parse_RMC_sentence(Rx_data); |
Rishin | 13:66d854ad31d8 | 70 | if(strcmp(RMC.Status, "A") == 0){ |
Rishin | 13:66d854ad31d8 | 71 | // GPS_status = 0; // Turn status LED off |
Rishin | 13:66d854ad31d8 | 72 | valid = 1; |
Rishin | 13:66d854ad31d8 | 73 | // GPS_parse = Parse_RMC_data(RMC); // Parse into format suitable for Tom (Dinghy_RaceTrak software) |
Rishin | 13:66d854ad31d8 | 74 | // Call function to send using LoRa module |
Rishin | 13:66d854ad31d8 | 75 | } |
Rishin | 13:66d854ad31d8 | 76 | else{ |
Rishin | 13:66d854ad31d8 | 77 | // GPS_status = 1; |
Rishin | 13:66d854ad31d8 | 78 | // Send "No GPS fix" over LoRa? |
Rishin | 13:66d854ad31d8 | 79 | } |
Rishin | 13:66d854ad31d8 | 80 | |
Rishin | 13:66d854ad31d8 | 81 | // delete these lines |
Rishin | 13:66d854ad31d8 | 82 | if (sending){ |
Rishin | 13:66d854ad31d8 | 83 | Char_index = 0; |
Rishin | 13:66d854ad31d8 | 84 | memset(Rx_data,0,sizeof(Rx_data)); |
Rishin | 13:66d854ad31d8 | 85 | return; |
Rishin | 13:66d854ad31d8 | 86 | } |
Rishin | 13:66d854ad31d8 | 87 | GPS_data_ready = 0; |
Rishin | 13:66d854ad31d8 | 88 | GPS_parse = Parse_RMC_data(RMC); // Parse into format suitable for Tom (Dinghy_RaceTrak software) |
Rishin | 13:66d854ad31d8 | 89 | GPS_data_ready = 1; |
Rishin | 13:66d854ad31d8 | 90 | |
Rishin | 13:66d854ad31d8 | 91 | |
Rishin | 13:66d854ad31d8 | 92 | } |
Rishin | 13:66d854ad31d8 | 93 | Char_index = 0; |
Rishin | 13:66d854ad31d8 | 94 | memset(Rx_data,0,sizeof(Rx_data)); |
Rishin | 13:66d854ad31d8 | 95 | } |
Rishin | 13:66d854ad31d8 | 96 | else |
Rishin | 13:66d854ad31d8 | 97 | Char_index++; |
Rishin | 13:66d854ad31d8 | 98 | } |
Rishin | 13:66d854ad31d8 | 99 | return; |
Rishin | 13:66d854ad31d8 | 100 | // store chars into a string then process into LAT, LONG, SOG, COG & DATETIME, VALID/INVLAID |
Rishin | 13:66d854ad31d8 | 101 | } |
Rishin | 13:66d854ad31d8 | 102 | |
Rishin | 13:66d854ad31d8 | 103 | void LoRaSend(){ |
Rishin | 13:66d854ad31d8 | 104 | if(GPS_data_ready == 1){ |
Rishin | 13:66d854ad31d8 | 105 | sending = 1; |
Rishin | 13:66d854ad31d8 | 106 | char AppDatas[APPDATA_SIZE+2]="h"; |
Rishin | 13:66d854ad31d8 | 107 | strcat(AppDatas, GPS_parse.Latitude); |
Rishin | 13:66d854ad31d8 | 108 | strcat(AppDatas, ","); |
Rishin | 13:66d854ad31d8 | 109 | strcat(AppDatas, GPS_parse.Longitude); |
Rishin | 13:66d854ad31d8 | 110 | strcat(AppDatas, ","); |
Rishin | 13:66d854ad31d8 | 111 | strcat(AppDatas, GPS_parse.Course_Over_Ground); |
Rishin | 13:66d854ad31d8 | 112 | strcat(AppDatas, ","); |
Rishin | 13:66d854ad31d8 | 113 | strcat(AppDatas, GPS_parse.Speed_Over_Ground); |
Rishin | 13:66d854ad31d8 | 114 | strcat(AppDatas, ","); |
Rishin | 13:66d854ad31d8 | 115 | strcat(AppDatas, GPS_parse.Date); |
Rishin | 13:66d854ad31d8 | 116 | strcat(AppDatas, ","); |
Rishin | 13:66d854ad31d8 | 117 | strcat(AppDatas, GPS_parse.UTC_Time); |
Rishin | 13:66d854ad31d8 | 118 | strcat(AppDatas, ","); |
Rishin | 13:66d854ad31d8 | 119 | strcat(AppDatas, GPS_parse.Valid); |
Rishin | 13:66d854ad31d8 | 120 | |
Rishin | 13:66d854ad31d8 | 121 | McpsReq_t mcpsReq; |
Rishin | 13:66d854ad31d8 | 122 | |
Rishin | 13:66d854ad31d8 | 123 | uint8_t AppPort = 3; |
Rishin | 13:66d854ad31d8 | 124 | mcpsReq.Type = MCPS_UNCONFIRMED; |
Rishin | 13:66d854ad31d8 | 125 | mcpsReq.Req.Unconfirmed.fPort = AppPort; |
Rishin | 13:66d854ad31d8 | 126 | mcpsReq.Req.Unconfirmed.fBuffer = AppDatas; |
Rishin | 13:66d854ad31d8 | 127 | mcpsReq.Req.Unconfirmed.fBufferSize = APPDATA_SIZE+2; |
Rishin | 13:66d854ad31d8 | 128 | mcpsReq.Req.Unconfirmed.Datarate = DR_5; |
Rishin | 13:66d854ad31d8 | 129 | // status = !status; |
Rishin | 13:66d854ad31d8 | 130 | GPS_status = !GPS_status; |
Rishin | 13:66d854ad31d8 | 131 | LoRaMacMcpsRequest( &mcpsReq ); |
Rishin | 13:66d854ad31d8 | 132 | GPS_data_ready = 0; |
Rishin | 13:66d854ad31d8 | 133 | // Ready_for_more = 1; |
Rishin | 13:66d854ad31d8 | 134 | } |
Rishin | 13:66d854ad31d8 | 135 | sending = 0; |
Rishin | 13:66d854ad31d8 | 136 | } |
Rishin | 13:66d854ad31d8 | 137 | |
Rishin | 13:66d854ad31d8 | 138 | void McpsConfirm( McpsConfirm_t *mcpsConfirm ) |
Rishin | 13:66d854ad31d8 | 139 | { |
Rishin | 13:66d854ad31d8 | 140 | return; |
Rishin | 13:66d854ad31d8 | 141 | } |
Rishin | 13:66d854ad31d8 | 142 | |
Rishin | 13:66d854ad31d8 | 143 | void McpsIndication( McpsIndication_t *mcpsIndication ) |
Rishin | 13:66d854ad31d8 | 144 | { |
Rishin | 13:66d854ad31d8 | 145 | return; |
Rishin | 13:66d854ad31d8 | 146 | } |
Rishin | 13:66d854ad31d8 | 147 | |
Rishin | 13:66d854ad31d8 | 148 | |
Rishin | 13:66d854ad31d8 | 149 | int main( void ) |
Rishin | 13:66d854ad31d8 | 150 | { |
Rishin | 13:66d854ad31d8 | 151 | //Initialise firmware |
Rishin | 13:66d854ad31d8 | 152 | |
Rishin | 13:66d854ad31d8 | 153 | LoRaMacPrimitives_t LoRaMacPrimitives; |
Rishin | 13:66d854ad31d8 | 154 | LoRaMacCallback_t LoRaMacCallbacks; |
Rishin | 13:66d854ad31d8 | 155 | MibRequestConfirm_t mibReq; |
Rishin | 13:66d854ad31d8 | 156 | |
Rishin | 13:66d854ad31d8 | 157 | LoRaMacPrimitives.MacMcpsConfirm = McpsConfirm; |
Rishin | 13:66d854ad31d8 | 158 | LoRaMacPrimitives.MacMcpsIndication = McpsIndication; |
Rishin | 13:66d854ad31d8 | 159 | // LoRaMacPrimitives.MacMlmeConfirm = McpsConfirm; |
Rishin | 13:66d854ad31d8 | 160 | LoRaMacInitialization( &LoRaMacPrimitives, &LoRaMacCallbacks ); |
Rishin | 13:66d854ad31d8 | 161 | |
Rishin | 13:66d854ad31d8 | 162 | LoRaMacChannelAdd( 3, ( ChannelParams_t ){ 869400000, { ( ( DR_5 << 4 ) | DR_5 ) }, 3 } ); |
Rishin | 13:66d854ad31d8 | 163 | //LoRaMacChannelAdd( 4, ( ChannelParams_t ){ 867300000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 } ); |
Rishin | 13:66d854ad31d8 | 164 | //LoRaMacChannelAdd( 5, ( ChannelParams_t ){ 867500000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 } ); |
Rishin | 13:66d854ad31d8 | 165 | //LoRaMacChannelAdd( 6, ( ChannelParams_t ){ 867700000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 } ); |
Rishin | 13:66d854ad31d8 | 166 | //LoRaMacChannelAdd( 7, ( ChannelParams_t ){ 867900000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 } ); |
Rishin | 13:66d854ad31d8 | 167 | //LoRaMacChannelAdd( 8, ( ChannelParams_t ){ 868800000, { ( ( DR_7 << 4 ) | DR_7 ) }, 2 } ); |
Rishin | 13:66d854ad31d8 | 168 | //LoRaMacChannelAdd( 9, ( ChannelParams_t ){ 868300000, { ( ( DR_6 << 4 ) | DR_6 ) }, 1 } ); |
Rishin | 13:66d854ad31d8 | 169 | |
Rishin | 13:66d854ad31d8 | 170 | //Join ABP |
Rishin | 13:66d854ad31d8 | 171 | |
Rishin | 13:66d854ad31d8 | 172 | mibReq.Type = MIB_NET_ID; |
Rishin | 13:66d854ad31d8 | 173 | mibReq.Param.NetID = 0; |
Rishin | 13:66d854ad31d8 | 174 | LoRaMacMibSetRequestConfirm( &mibReq ); |
Rishin | 13:66d854ad31d8 | 175 | |
Rishin | 13:66d854ad31d8 | 176 | mibReq.Type = MIB_DEV_ADDR; |
Rishin | 13:66d854ad31d8 | 177 | mibReq.Param.DevAddr = LORAWAN_DEVICE_ADDRESS; |
Rishin | 13:66d854ad31d8 | 178 | LoRaMacMibSetRequestConfirm( &mibReq ); |
Rishin | 13:66d854ad31d8 | 179 | |
Rishin | 13:66d854ad31d8 | 180 | mibReq.Type = MIB_NWK_SKEY; |
Rishin | 13:66d854ad31d8 | 181 | mibReq.Param.NwkSKey = NwkSKey; |
Rishin | 13:66d854ad31d8 | 182 | LoRaMacMibSetRequestConfirm( &mibReq ); |
Rishin | 13:66d854ad31d8 | 183 | |
Rishin | 13:66d854ad31d8 | 184 | mibReq.Type = MIB_APP_SKEY; |
Rishin | 13:66d854ad31d8 | 185 | mibReq.Param.AppSKey = AppSKey; |
Rishin | 13:66d854ad31d8 | 186 | LoRaMacMibSetRequestConfirm( &mibReq ); |
Rishin | 13:66d854ad31d8 | 187 | |
Rishin | 13:66d854ad31d8 | 188 | mibReq.Type = MIB_NETWORK_JOINED; |
Rishin | 13:66d854ad31d8 | 189 | mibReq.Param.IsNetworkJoined = true; |
Rishin | 13:66d854ad31d8 | 190 | LoRaMacMibSetRequestConfirm( &mibReq ); |
Rishin | 13:66d854ad31d8 | 191 | |
Rishin | 13:66d854ad31d8 | 192 | mibReq.Type = MIB_CHANNELS_TX_POWER; |
Rishin | 13:66d854ad31d8 | 193 | mibReq.Param.ChannelsTxPower = LORAMAC_MAX_TX_POWER; |
Rishin | 13:66d854ad31d8 | 194 | LoRaMacMibSetRequestConfirm( &mibReq ); |
Rishin | 13:66d854ad31d8 | 195 | |
Rishin | 13:66d854ad31d8 | 196 | mibReq.Type = MIB_CHANNELS_DEFAULT_TX_POWER; |
Rishin | 13:66d854ad31d8 | 197 | mibReq.Param.ChannelsDefaultTxPower = LORAMAC_MAX_TX_POWER; |
Rishin | 13:66d854ad31d8 | 198 | LoRaMacMibSetRequestConfirm( &mibReq ); |
Rishin | 13:66d854ad31d8 | 199 | |
Rishin | 13:66d854ad31d8 | 200 | /*//Prepareframe |
Rishin | 13:66d854ad31d8 | 201 | |
Rishin | 13:66d854ad31d8 | 202 | AppData[0] = 0x43; |
Rishin | 13:66d854ad31d8 | 203 | AppData[1] = 0x68; |
Rishin | 13:66d854ad31d8 | 204 | AppData[2] = 0x72; |
Rishin | 13:66d854ad31d8 | 205 | AppData[3] = 0x69; |
Rishin | 13:66d854ad31d8 | 206 | AppData[4] = 0x73; |
Rishin | 13:66d854ad31d8 | 207 | AppData[5] = 0x21; |
Rishin | 13:66d854ad31d8 | 208 | AppData[6] = 0x21; |
Rishin | 13:66d854ad31d8 | 209 | |
Rishin | 13:66d854ad31d8 | 210 | //Sendframe |
Rishin | 13:66d854ad31d8 | 211 | |
Rishin | 13:66d854ad31d8 | 212 | McpsReq_t mcpsReq; |
Rishin | 13:66d854ad31d8 | 213 | |
Rishin | 13:66d854ad31d8 | 214 | uint8_t AppPort = 3; |
Rishin | 13:66d854ad31d8 | 215 | mcpsReq.Type = MCPS_UNCONFIRMED; |
Rishin | 13:66d854ad31d8 | 216 | mcpsReq.Req.Unconfirmed.fPort = AppPort; |
Rishin | 13:66d854ad31d8 | 217 | mcpsReq.Req.Unconfirmed.fBuffer = AppData; |
Rishin | 13:66d854ad31d8 | 218 | mcpsReq.Req.Unconfirmed.fBufferSize = APPDATA_SIZE; |
Rishin | 13:66d854ad31d8 | 219 | mcpsReq.Req.Unconfirmed.Datarate = DR_5; |
Rishin | 13:66d854ad31d8 | 220 | |
Rishin | 13:66d854ad31d8 | 221 | LoRaMacMcpsRequest( &mcpsReq );*/ |
Rishin | 13:66d854ad31d8 | 222 | |
Rishin | 13:66d854ad31d8 | 223 | GPS_status = 1; |
Rishin | 13:66d854ad31d8 | 224 | gps.attach(&gps_receive, Serial::RxIrq); |
Rishin | 13:66d854ad31d8 | 225 | gps.printf(PMTK_SET_NMEA_OUTPUT_RMC); // Only output RMC and GPTXT |
Rishin | 13:66d854ad31d8 | 226 | gps.printf(PMTK_SET_UPDATE_F_2HZ); // Set update Frequency to 2Hz |
Rishin | 13:66d854ad31d8 | 227 | LoRaSend_timer.attach(&LoRaSend, 1.0); |
Rishin | 13:66d854ad31d8 | 228 | while(1){ |
Rishin | 13:66d854ad31d8 | 229 | } |
Rishin | 13:66d854ad31d8 | 230 | } |