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