added other files
Dependencies: lib_gps lib_mpl3115a2 lmic_MOTE_L152RC mbed
Fork of lmic_NAmote_GPS_tjm by
main.cpp@0:a5e3347bad61, 2015-06-04 (annotated)
- Committer:
- dudmuck
- Date:
- Thu Jun 04 21:48:47 2015 +0000
- Revision:
- 0:a5e3347bad61
- Child:
- 2:a8f00db60fdb
LMIC transmit demo for NA-mote72 with GPS
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dudmuck | 0:a5e3347bad61 | 1 | /* |
dudmuck | 0:a5e3347bad61 | 2 | / _____) _ | | |
dudmuck | 0:a5e3347bad61 | 3 | ( (____ _____ ____ _| |_ _____ ____| |__ |
dudmuck | 0:a5e3347bad61 | 4 | \____ \| ___ | (_ _) ___ |/ ___) _ \ |
dudmuck | 0:a5e3347bad61 | 5 | _____) ) ____| | | || |_| ____( (___| | | | |
dudmuck | 0:a5e3347bad61 | 6 | (______/|_____)_|_|_| \__)_____)\____)_| |_| |
dudmuck | 0:a5e3347bad61 | 7 | (C)2013 Semtech |
dudmuck | 0:a5e3347bad61 | 8 | |
dudmuck | 0:a5e3347bad61 | 9 | Description: MBED example application |
dudmuck | 0:a5e3347bad61 | 10 | |
dudmuck | 0:a5e3347bad61 | 11 | License: Revised BSD License, see LICENSE.TXT file include in the project |
dudmuck | 0:a5e3347bad61 | 12 | |
dudmuck | 0:a5e3347bad61 | 13 | Maintainer: Miguel Luis and Gregory Cristian |
dudmuck | 0:a5e3347bad61 | 14 | */ |
dudmuck | 0:a5e3347bad61 | 15 | #include "mbed.h" |
dudmuck | 0:a5e3347bad61 | 16 | #include "lmic.h" |
dudmuck | 0:a5e3347bad61 | 17 | #include "mpl3115a2.h" |
dudmuck | 0:a5e3347bad61 | 18 | #include "gps.h" |
dudmuck | 0:a5e3347bad61 | 19 | #include "debug.h" |
dudmuck | 0:a5e3347bad61 | 20 | |
dudmuck | 0:a5e3347bad61 | 21 | #define SENET_F |
dudmuck | 0:a5e3347bad61 | 22 | //#define SMTC |
dudmuck | 0:a5e3347bad61 | 23 | |
dudmuck | 0:a5e3347bad61 | 24 | /* ****************************************** */ |
dudmuck | 0:a5e3347bad61 | 25 | /* ***** Basic App and Network Parameters *** */ |
dudmuck | 0:a5e3347bad61 | 26 | /* ****************************************** */ |
dudmuck | 0:a5e3347bad61 | 27 | // Hybrid Mode must be defined in lmic.h // |
dudmuck | 0:a5e3347bad61 | 28 | // DevEUI and Keys defined by Activation type // |
dudmuck | 0:a5e3347bad61 | 29 | #define APP_DATA_SIZE 11 |
dudmuck | 0:a5e3347bad61 | 30 | #define APP_ACK 0 |
dudmuck | 0:a5e3347bad61 | 31 | #define DELAY_NEXT_TX 10 |
dudmuck | 0:a5e3347bad61 | 32 | #ifdef SMTC |
dudmuck | 0:a5e3347bad61 | 33 | #define OVER_THE_AIR_ACTIVATION 0 |
dudmuck | 0:a5e3347bad61 | 34 | #else |
dudmuck | 0:a5e3347bad61 | 35 | #define OVER_THE_AIR_ACTIVATION 1 |
dudmuck | 0:a5e3347bad61 | 36 | #endif /* SMTC */ |
dudmuck | 0:a5e3347bad61 | 37 | |
dudmuck | 0:a5e3347bad61 | 38 | /* ***************************************** */ |
dudmuck | 0:a5e3347bad61 | 39 | |
dudmuck | 0:a5e3347bad61 | 40 | #define LED_RED PB_1 |
dudmuck | 0:a5e3347bad61 | 41 | #define LED_YEL PB_10 |
dudmuck | 0:a5e3347bad61 | 42 | |
dudmuck | 0:a5e3347bad61 | 43 | static DigitalOut led1(LED_RED); |
dudmuck | 0:a5e3347bad61 | 44 | static DigitalOut led2(LED_YEL); |
dudmuck | 0:a5e3347bad61 | 45 | |
dudmuck | 0:a5e3347bad61 | 46 | /* gps(tx, rx, en); */ |
dudmuck | 0:a5e3347bad61 | 47 | GPS gps(PB_6, PB_7, PB_11); |
dudmuck | 0:a5e3347bad61 | 48 | |
dudmuck | 0:a5e3347bad61 | 49 | I2C i2c(I2C_SDA, I2C_SCL); |
dudmuck | 0:a5e3347bad61 | 50 | DigitalIn i2c_int_pin(PB_4); |
dudmuck | 0:a5e3347bad61 | 51 | MPL3115A2 mpl3115a2(i2c, i2c_int_pin); |
dudmuck | 0:a5e3347bad61 | 52 | AnalogIn bat(PA_0); |
dudmuck | 0:a5e3347bad61 | 53 | |
dudmuck | 0:a5e3347bad61 | 54 | ////////////////////////////////////////////////// |
dudmuck | 0:a5e3347bad61 | 55 | // CONFIGURATION (FOR APPLICATION CALLBACKS BELOW) |
dudmuck | 0:a5e3347bad61 | 56 | ////////////////////////////////////////////////// |
dudmuck | 0:a5e3347bad61 | 57 | #ifdef SENET_F |
dudmuck | 0:a5e3347bad61 | 58 | // application router ID (LSBF) |
dudmuck | 0:a5e3347bad61 | 59 | static const u1_t APPEUI[8] = { 0x01, 0x00, 0x01, 0x00, 0x00, 0x0c, 0x25, 0x00 }; |
dudmuck | 0:a5e3347bad61 | 60 | |
dudmuck | 0:a5e3347bad61 | 61 | // unique device ID (LSBF) |
dudmuck | 0:a5e3347bad61 | 62 | static const u1_t DEVEUI[8] = { 0x0f, 0x00, 0x00, 0x00, 0x01, 0x0c, 0x25, 0x00 }; |
dudmuck | 0:a5e3347bad61 | 63 | |
dudmuck | 0:a5e3347bad61 | 64 | // device-specific AES key (derived from device EUI) |
dudmuck | 0:a5e3347bad61 | 65 | static const u1_t DEVKEY[16] = { 0xe4, 0x72, 0x71, 0xc5, 0xf5, 0x30, 0xa9, 0x9f, 0xcf, 0xc4, 0x0e, 0xab, 0xea, 0xd7, 0x19, 0x42, }; |
dudmuck | 0:a5e3347bad61 | 66 | // E4 -72 -71 -C5 -F5 -30 -A9 -9F -CF -C4 -0E -AB -EA -D7 -19 -42 |
dudmuck | 0:a5e3347bad61 | 67 | #endif /* SENET_F */ |
dudmuck | 0:a5e3347bad61 | 68 | |
dudmuck | 0:a5e3347bad61 | 69 | #ifdef SMTC |
dudmuck | 0:a5e3347bad61 | 70 | // Semtech Activation (v1.x server) |
dudmuck | 0:a5e3347bad61 | 71 | // application router ID (LSBF) |
dudmuck | 0:a5e3347bad61 | 72 | static const u1_t APPEUI[8] = { 0xAA, 0xCC, 0x11, 0x00, 0xCC, 0xEE, 0x77, 0xEE }; |
dudmuck | 0:a5e3347bad61 | 73 | |
dudmuck | 0:a5e3347bad61 | 74 | // unique device ID (LSBF) |
dudmuck | 0:a5e3347bad61 | 75 | static const u1_t DEVEUI[8] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF }; |
dudmuck | 0:a5e3347bad61 | 76 | |
dudmuck | 0:a5e3347bad61 | 77 | // device-specific AES key (derived from device EUI) |
dudmuck | 0:a5e3347bad61 | 78 | static const u1_t DEVKEY[16] = { 0xAB, 0x89, 0xEF, 0xCD, 0x23, 0x01, 0x67, 0x45, 0x54, 0x76, 0x10, 0x32, 0xDC, 0xFE, 0x98, 0xBA }; |
dudmuck | 0:a5e3347bad61 | 79 | |
dudmuck | 0:a5e3347bad61 | 80 | static uint8_t NwkSKey[] = |
dudmuck | 0:a5e3347bad61 | 81 | { |
dudmuck | 0:a5e3347bad61 | 82 | 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, |
dudmuck | 0:a5e3347bad61 | 83 | 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C |
dudmuck | 0:a5e3347bad61 | 84 | }; |
dudmuck | 0:a5e3347bad61 | 85 | |
dudmuck | 0:a5e3347bad61 | 86 | static uint8_t ArtSKey[] = |
dudmuck | 0:a5e3347bad61 | 87 | { |
dudmuck | 0:a5e3347bad61 | 88 | 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, |
dudmuck | 0:a5e3347bad61 | 89 | 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C |
dudmuck | 0:a5e3347bad61 | 90 | }; |
dudmuck | 0:a5e3347bad61 | 91 | |
dudmuck | 0:a5e3347bad61 | 92 | #endif /* SMTC */ |
dudmuck | 0:a5e3347bad61 | 93 | |
dudmuck | 0:a5e3347bad61 | 94 | ////////////////////////////////////////////////// |
dudmuck | 0:a5e3347bad61 | 95 | // APPLICATION CALLBACKS |
dudmuck | 0:a5e3347bad61 | 96 | ////////////////////////////////////////////////// |
dudmuck | 0:a5e3347bad61 | 97 | |
dudmuck | 0:a5e3347bad61 | 98 | // provide application router ID (8 bytes, LSBF) |
dudmuck | 0:a5e3347bad61 | 99 | void os_getArtEui (u1_t* buf) { |
dudmuck | 0:a5e3347bad61 | 100 | memcpy(buf, APPEUI, 8); |
dudmuck | 0:a5e3347bad61 | 101 | } |
dudmuck | 0:a5e3347bad61 | 102 | |
dudmuck | 0:a5e3347bad61 | 103 | // provide device ID (8 bytes, LSBF) |
dudmuck | 0:a5e3347bad61 | 104 | void os_getDevEui (u1_t* buf) { |
dudmuck | 0:a5e3347bad61 | 105 | memcpy(buf, DEVEUI, 8); |
dudmuck | 0:a5e3347bad61 | 106 | } |
dudmuck | 0:a5e3347bad61 | 107 | |
dudmuck | 0:a5e3347bad61 | 108 | // provide device key (16 bytes) |
dudmuck | 0:a5e3347bad61 | 109 | void os_getDevKey (u1_t* buf) { |
dudmuck | 0:a5e3347bad61 | 110 | memcpy(buf, DEVKEY, 16); |
dudmuck | 0:a5e3347bad61 | 111 | } |
dudmuck | 0:a5e3347bad61 | 112 | |
dudmuck | 0:a5e3347bad61 | 113 | ////////////////////////////////////////////////// |
dudmuck | 0:a5e3347bad61 | 114 | // MAIN - INITIALIZATION AND STARTUP |
dudmuck | 0:a5e3347bad61 | 115 | ////////////////////////////////////////////////// |
dudmuck | 0:a5e3347bad61 | 116 | |
dudmuck | 0:a5e3347bad61 | 117 | // initial job |
dudmuck | 0:a5e3347bad61 | 118 | static void initfunc (osjob_t* j) { |
dudmuck | 0:a5e3347bad61 | 119 | debug_str("B: INITFUNC\n"); |
dudmuck | 0:a5e3347bad61 | 120 | // reset MAC state |
dudmuck | 0:a5e3347bad61 | 121 | LMIC_reset(); |
dudmuck | 0:a5e3347bad61 | 122 | // start joining |
dudmuck | 0:a5e3347bad61 | 123 | #if( OVER_THE_AIR_ACTIVATION != 0 ) |
dudmuck | 0:a5e3347bad61 | 124 | LMIC_startJoining(); |
dudmuck | 0:a5e3347bad61 | 125 | #else |
dudmuck | 0:a5e3347bad61 | 126 | devaddr_t *serial_id = (devaddr_t *) 0x1FF800d0; // cat3 device stm32l152rc |
dudmuck | 0:a5e3347bad61 | 127 | LMIC_setSession( 0, *serial_id, NwkSKey, ArtSKey ); |
dudmuck | 0:a5e3347bad61 | 128 | debug_val("SN = ", *serial_id); |
dudmuck | 0:a5e3347bad61 | 129 | #endif |
dudmuck | 0:a5e3347bad61 | 130 | // init done - onEvent() callback will be invoked... |
dudmuck | 0:a5e3347bad61 | 131 | |
dudmuck | 0:a5e3347bad61 | 132 | //DEBUG_STR("E: INITFUNC"); |
dudmuck | 0:a5e3347bad61 | 133 | gps.init(); |
dudmuck | 0:a5e3347bad61 | 134 | gps.enable(1); |
dudmuck | 0:a5e3347bad61 | 135 | //gps.verbose = 1; |
dudmuck | 0:a5e3347bad61 | 136 | |
dudmuck | 0:a5e3347bad61 | 137 | mpl3115a2.init(); |
dudmuck | 0:a5e3347bad61 | 138 | } |
dudmuck | 0:a5e3347bad61 | 139 | |
dudmuck | 0:a5e3347bad61 | 140 | int main(void) |
dudmuck | 0:a5e3347bad61 | 141 | { |
dudmuck | 0:a5e3347bad61 | 142 | osjob_t initjob; |
dudmuck | 0:a5e3347bad61 | 143 | |
dudmuck | 0:a5e3347bad61 | 144 | // initialize runtime env |
dudmuck | 0:a5e3347bad61 | 145 | os_init(); |
dudmuck | 0:a5e3347bad61 | 146 | debug_init(); |
dudmuck | 0:a5e3347bad61 | 147 | // setup initial job |
dudmuck | 0:a5e3347bad61 | 148 | os_setCallback(&initjob, initfunc); |
dudmuck | 0:a5e3347bad61 | 149 | // execute scheduled jobs and events |
dudmuck | 0:a5e3347bad61 | 150 | os_runloop(); |
dudmuck | 0:a5e3347bad61 | 151 | // (not reached) |
dudmuck | 0:a5e3347bad61 | 152 | } |
dudmuck | 0:a5e3347bad61 | 153 | |
dudmuck | 0:a5e3347bad61 | 154 | ////////////////////////////////////////////////// |
dudmuck | 0:a5e3347bad61 | 155 | // LMIC EVENT CALLBACK |
dudmuck | 0:a5e3347bad61 | 156 | ////////////////////////////////////////////////// |
dudmuck | 0:a5e3347bad61 | 157 | osjob_t rxLedJob; |
dudmuck | 0:a5e3347bad61 | 158 | osjob_t txLedJob; |
dudmuck | 0:a5e3347bad61 | 159 | osjob_t sendFrameJob; |
dudmuck | 0:a5e3347bad61 | 160 | |
dudmuck | 0:a5e3347bad61 | 161 | static void onRxLed (osjob_t* j) { |
dudmuck | 0:a5e3347bad61 | 162 | debug_val("LED2 = ", 1 ); |
dudmuck | 0:a5e3347bad61 | 163 | led2 = 1; |
dudmuck | 0:a5e3347bad61 | 164 | } |
dudmuck | 0:a5e3347bad61 | 165 | |
dudmuck | 0:a5e3347bad61 | 166 | static void onTxLed (osjob_t* j) { |
dudmuck | 0:a5e3347bad61 | 167 | debug_val("LED1 = ", 1 ); |
dudmuck | 0:a5e3347bad61 | 168 | led1 = 1; |
dudmuck | 0:a5e3347bad61 | 169 | } |
dudmuck | 0:a5e3347bad61 | 170 | |
dudmuck | 0:a5e3347bad61 | 171 | static bool AppLedStateOn = false; |
dudmuck | 0:a5e3347bad61 | 172 | |
dudmuck | 0:a5e3347bad61 | 173 | static void PrepareDataFrame( void ) |
dudmuck | 0:a5e3347bad61 | 174 | { |
dudmuck | 0:a5e3347bad61 | 175 | uint16_t altitudeGps; |
dudmuck | 0:a5e3347bad61 | 176 | //int i; |
dudmuck | 0:a5e3347bad61 | 177 | |
dudmuck | 0:a5e3347bad61 | 178 | gps.service(); |
dudmuck | 0:a5e3347bad61 | 179 | printf("lat:%f long:%f\r\n", gps.Latitude, gps.Longitude); |
dudmuck | 0:a5e3347bad61 | 180 | // printf("lat:%d long:%d\r\n", gps.LatitudeBinary, gps.LongitudeBinary); |
dudmuck | 0:a5e3347bad61 | 181 | mpl3115a2.ReadTemperature(); |
dudmuck | 0:a5e3347bad61 | 182 | //printf("temp:%d\r\n", (int)mpl3115a2.Temperature); |
dudmuck | 0:a5e3347bad61 | 183 | |
dudmuck | 0:a5e3347bad61 | 184 | // immediately prepare next transmission |
dudmuck | 0:a5e3347bad61 | 185 | //LMIC.frame[0] = LMIC.rxq.snr; |
dudmuck | 0:a5e3347bad61 | 186 | LMIC.frame[0] = AppLedStateOn; // (bit 0 == 1) => LED on |
dudmuck | 0:a5e3347bad61 | 187 | LMIC.frame[1] = (int)mpl3115a2.Temperature; // Signed degrees Celcius in half degree units. So, +/-63 C |
dudmuck | 0:a5e3347bad61 | 188 | LMIC.frame[2] = (bat.read_u16() >> 8) + (bat.read_u16() >> 9) ; // per LoRaMAC spec; 0=Charging; 1...254 = level, 255 = N/A |
dudmuck | 0:a5e3347bad61 | 189 | LMIC.frame[3] = ( gps.LatitudeBinary >> 16 ) & 0xFF; |
dudmuck | 0:a5e3347bad61 | 190 | LMIC.frame[4] = ( gps.LatitudeBinary >> 8 ) & 0xFF; |
dudmuck | 0:a5e3347bad61 | 191 | LMIC.frame[5] = gps.LatitudeBinary & 0xFF; |
dudmuck | 0:a5e3347bad61 | 192 | LMIC.frame[6] = ( gps.LongitudeBinary >> 16 ) & 0xFF; |
dudmuck | 0:a5e3347bad61 | 193 | LMIC.frame[7] = ( gps.LongitudeBinary >> 8 ) & 0xFF; |
dudmuck | 0:a5e3347bad61 | 194 | LMIC.frame[8] = gps.LongitudeBinary & 0xFF; |
dudmuck | 0:a5e3347bad61 | 195 | |
dudmuck | 0:a5e3347bad61 | 196 | altitudeGps = atoi(gps.NmeaGpsData.NmeaAltitude); |
dudmuck | 0:a5e3347bad61 | 197 | printf("alt:%d\r\n", altitudeGps); |
dudmuck | 0:a5e3347bad61 | 198 | LMIC.frame[9] = ( altitudeGps >> 8 ) & 0xFF; |
dudmuck | 0:a5e3347bad61 | 199 | LMIC.frame[10] = altitudeGps & 0xFF; |
dudmuck | 0:a5e3347bad61 | 200 | } |
dudmuck | 0:a5e3347bad61 | 201 | |
dudmuck | 0:a5e3347bad61 | 202 | static void onSendFrame (osjob_t* j) { |
dudmuck | 0:a5e3347bad61 | 203 | PrepareDataFrame( ); |
dudmuck | 0:a5e3347bad61 | 204 | // schedule transmission (port 1, data[], datalen 1, ACK requested) |
dudmuck | 0:a5e3347bad61 | 205 | // (will be sent as soon as duty cycle permits) |
dudmuck | 0:a5e3347bad61 | 206 | LMIC_setTxData2(5, LMIC.frame, APP_DATA_SIZE, APP_ACK); |
dudmuck | 0:a5e3347bad61 | 207 | } |
dudmuck | 0:a5e3347bad61 | 208 | |
dudmuck | 0:a5e3347bad61 | 209 | void onEvent (ev_t ev) { |
dudmuck | 0:a5e3347bad61 | 210 | |
dudmuck | 0:a5e3347bad61 | 211 | debug_event(ev); |
dudmuck | 0:a5e3347bad61 | 212 | |
dudmuck | 0:a5e3347bad61 | 213 | gps.service(); |
dudmuck | 0:a5e3347bad61 | 214 | |
dudmuck | 0:a5e3347bad61 | 215 | switch(ev) |
dudmuck | 0:a5e3347bad61 | 216 | { |
dudmuck | 0:a5e3347bad61 | 217 | // network joined, session established |
dudmuck | 0:a5e3347bad61 | 218 | case EV_JOINED: |
dudmuck | 0:a5e3347bad61 | 219 | debug_val("Net ID = ", LMIC.netid); |
dudmuck | 0:a5e3347bad61 | 220 | goto tx; |
dudmuck | 0:a5e3347bad61 | 221 | // scheduled data sent (optionally data received) |
dudmuck | 0:a5e3347bad61 | 222 | case EV_TXCOMPLETE: |
dudmuck | 0:a5e3347bad61 | 223 | if(LMIC.dataLen) |
dudmuck | 0:a5e3347bad61 | 224 | { // data received in rx slot after tx |
dudmuck | 0:a5e3347bad61 | 225 | debug_buf(LMIC.frame+LMIC.dataBeg, LMIC.dataLen); |
dudmuck | 0:a5e3347bad61 | 226 | if(LMIC.dataLen == 1) { // set LED state if exactly one byte is received |
dudmuck | 0:a5e3347bad61 | 227 | AppLedStateOn = LMIC.frame[LMIC.dataBeg] & 0x01; |
dudmuck | 0:a5e3347bad61 | 228 | debug_val("LED3 = ", AppLedStateOn ? 0 : 1 ); |
dudmuck | 0:a5e3347bad61 | 229 | } |
dudmuck | 0:a5e3347bad61 | 230 | } |
dudmuck | 0:a5e3347bad61 | 231 | if((LMIC.txrxFlags & (TXRX_DNW1|TXRX_DNW2) )!= 0 ) |
dudmuck | 0:a5e3347bad61 | 232 | { |
dudmuck | 0:a5e3347bad61 | 233 | debug_val("LED2 = ", 0 ); |
dudmuck | 0:a5e3347bad61 | 234 | led2 = 0; |
dudmuck | 0:a5e3347bad61 | 235 | os_setTimedCallback( &rxLedJob, os_getTime() + ms2osticks(15), onRxLed ); |
dudmuck | 0:a5e3347bad61 | 236 | } |
dudmuck | 0:a5e3347bad61 | 237 | tx: |
dudmuck | 0:a5e3347bad61 | 238 | os_setTimedCallback( &sendFrameJob, os_getTime() + sec2osticks(DELAY_NEXT_TX), onSendFrame ); // Change the Tx periodicity |
dudmuck | 0:a5e3347bad61 | 239 | //onSendFrame(NULL); |
dudmuck | 0:a5e3347bad61 | 240 | |
dudmuck | 0:a5e3347bad61 | 241 | // Blink Tx LED |
dudmuck | 0:a5e3347bad61 | 242 | debug_val("LED1 = ", 0 ); |
dudmuck | 0:a5e3347bad61 | 243 | led1 = 0; |
dudmuck | 0:a5e3347bad61 | 244 | |
dudmuck | 0:a5e3347bad61 | 245 | os_setTimedCallback( &txLedJob, os_getTime() + ms2osticks(25), onTxLed ); |
dudmuck | 0:a5e3347bad61 | 246 | break; |
dudmuck | 0:a5e3347bad61 | 247 | default: |
dudmuck | 0:a5e3347bad61 | 248 | break; |
dudmuck | 0:a5e3347bad61 | 249 | } |
dudmuck | 0:a5e3347bad61 | 250 | } |