added other files

Dependencies:   lib_gps lib_mpl3115a2 lmic_MOTE_L152RC mbed

Fork of lmic_NAmote_GPS_tjm by Timothy Mulrooney

Committer:
tmulrooney
Date:
Tue Feb 09 00:39:29 2016 +0000
Revision:
8:1069ed1d4464
Parent:
6:0b2b2d196ddd
Child:
9:f4d52e9625f7
hello world

Who changed what in which revision?

UserRevisionLine numberNew 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 6:0b2b2d196ddd 21 #define SENET_F
dudmuck 6:0b2b2d196ddd 22 //#define SMTC
dudmuck 0:a5e3347bad61 23
dudmuck 2:a8f00db60fdb 24 typedef enum {
dudmuck 2:a8f00db60fdb 25 MOTE_NONE = 0,
dudmuck 2:a8f00db60fdb 26 MOTE_V2,
dudmuck 2:a8f00db60fdb 27 MOTE_V3
dudmuck 2:a8f00db60fdb 28 } mote_version_e;
dudmuck 2:a8f00db60fdb 29 mote_version_e mote_version = MOTE_NONE;
dudmuck 2:a8f00db60fdb 30
tmulrooney 8:1069ed1d4464 31 //DigitalOut pc_7(PTC1); /* *** TODO *** */
tmulrooney 8:1069ed1d4464 32 //DigitalIn pc_1(PTC1); /* *** TODO *** */
dudmuck 2:a8f00db60fdb 33
dudmuck 2:a8f00db60fdb 34
dudmuck 0:a5e3347bad61 35 /* ****************************************** */
dudmuck 0:a5e3347bad61 36 /* ***** Basic App and Network Parameters *** */
dudmuck 0:a5e3347bad61 37 /* ****************************************** */
dudmuck 0:a5e3347bad61 38 // Hybrid Mode must be defined in lmic.h //
dudmuck 0:a5e3347bad61 39 // DevEUI and Keys defined by Activation type //
dudmuck 0:a5e3347bad61 40 #define APP_DATA_SIZE 11
dudmuck 0:a5e3347bad61 41 #define APP_ACK 0
dudmuck 4:f9aaf0f82c00 42 #define MS_DELAY_NEXT_TX 400
dudmuck 0:a5e3347bad61 43 #ifdef SMTC
dudmuck 4:f9aaf0f82c00 44 #define OVER_THE_AIR_ACTIVATION 1 //0
dudmuck 0:a5e3347bad61 45 #else
dudmuck 0:a5e3347bad61 46 #define OVER_THE_AIR_ACTIVATION 1
dudmuck 0:a5e3347bad61 47 #endif /* SMTC */
dudmuck 0:a5e3347bad61 48
dudmuck 0:a5e3347bad61 49 /* ***************************************** */
dudmuck 0:a5e3347bad61 50
tmulrooney 8:1069ed1d4464 51 #define LED_RED PTA1
tmulrooney 8:1069ed1d4464 52 #define LED_YEL PTA2
dudmuck 0:a5e3347bad61 53
dudmuck 0:a5e3347bad61 54 static DigitalOut led1(LED_RED);
dudmuck 0:a5e3347bad61 55 static DigitalOut led2(LED_YEL);
dudmuck 0:a5e3347bad61 56
dudmuck 0:a5e3347bad61 57 /* gps(tx, rx, en); */
tmulrooney 8:1069ed1d4464 58 //GPS gps(PTC1, PTC1, PTC1); /* *** TODO *** */
dudmuck 0:a5e3347bad61 59
tmulrooney 8:1069ed1d4464 60 I2C i2c(PTB3,PTB2);
tmulrooney 8:1069ed1d4464 61 //DigitalIn i2c_int_pin(PTC1);
tmulrooney 8:1069ed1d4464 62 //MPL3115A2 mpl3115a2(i2c, i2c_int_pin); /* *** TODO *** */
dudmuck 3:d81e4a63fb2e 63 AnalogIn *bat;
dudmuck 3:d81e4a63fb2e 64 #define LOW_BAT_THRESHOLD 3.45
dudmuck 0:a5e3347bad61 65
dudmuck 0:a5e3347bad61 66 //////////////////////////////////////////////////
dudmuck 0:a5e3347bad61 67 // CONFIGURATION (FOR APPLICATION CALLBACKS BELOW)
dudmuck 0:a5e3347bad61 68 //////////////////////////////////////////////////
dudmuck 0:a5e3347bad61 69 #ifdef SENET_F
dudmuck 0:a5e3347bad61 70 // application router ID (LSBF)
dudmuck 6:0b2b2d196ddd 71 //static const u1_t APPEUI[8] = { 0x01, 0x00, 0x01, 0x00, 0x00, 0x0c, 0x25, 0x00 };
dudmuck 6:0b2b2d196ddd 72 static const u1_t reverse_APPEUI[8] = { 0x00, 0x25, 0x0C, 0x00, 0x00, 0x01, 0x00, 0x01 };
dudmuck 0:a5e3347bad61 73
dudmuck 0:a5e3347bad61 74 // unique device ID (LSBF)
dudmuck 6:0b2b2d196ddd 75 //static const u1_t DEVEUI[8] = { 0x0f, 0x00, 0x00, 0x00, 0x01, 0x0c, 0x25, 0x00 };
dudmuck 6:0b2b2d196ddd 76 static const u1_t reverse_DEVEUI[8] = { 0x00, 0x25, 0x0C, 0x01, 0x00, 0x00, 0x00, 0x0F };
dudmuck 0:a5e3347bad61 77
dudmuck 0:a5e3347bad61 78 // device-specific AES key (derived from device EUI)
dudmuck 0:a5e3347bad61 79 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 80 // E4 -72 -71 -C5 -F5 -30 -A9 -9F -CF -C4 -0E -AB -EA -D7 -19 -42
dudmuck 0:a5e3347bad61 81 #endif /* SENET_F */
dudmuck 0:a5e3347bad61 82
dudmuck 0:a5e3347bad61 83 #ifdef SMTC
dudmuck 0:a5e3347bad61 84 // Semtech Activation (v1.x server)
dudmuck 0:a5e3347bad61 85 // application router ID (LSBF)
dudmuck 6:0b2b2d196ddd 86 static const u1_t reverse_APPEUI[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
dudmuck 6:0b2b2d196ddd 87 //AA-AA-AA-AA-FF-FF-FF-FF
dudmuck 6:0b2b2d196ddd 88 //static const u1_t APPEUI[8] = { 0xff, 0xff, 0xff, 0xff, 0xaa, 0xaa, 0xaa, 0xaa };
dudmuck 6:0b2b2d196ddd 89 //static const u1_t reverse_APPEUI[8] = { 0xaa, 0xaa, 0xaa, 0xaa, 0xff, 0xff, 0xff, 0xff };
dudmuck 0:a5e3347bad61 90
dudmuck 0:a5e3347bad61 91 // unique device ID (LSBF)
dudmuck 6:0b2b2d196ddd 92 //static const u1_t DEVEUI[8] = { 0x21, 0x00, 0x00, 0x00, 0x01, 0x0c, 0x25, 0x00 };
dudmuck 6:0b2b2d196ddd 93 static const u1_t reverse_DEVEUI[8] = { 0x00, 0x25, 0x0C, 0x01, 0x00, 0x00, 0x00, 0x20 };
dudmuck 6:0b2b2d196ddd 94 //static const u1_t DEVEUI[8] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
dudmuck 6:0b2b2d196ddd 95 //static const u1_t reverse_DEVEUI[8] = { 0xEF, 0xCD, 0xAB, 0x89, 0x67, 0x45, 0x23, 0x01 };
dudmuck 0:a5e3347bad61 96
dudmuck 0:a5e3347bad61 97 // device-specific AES key (derived from device EUI)
dudmuck 4:f9aaf0f82c00 98 static const u1_t DEVKEY[16] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff };
dudmuck 6:0b2b2d196ddd 99 //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 100
dudmuck 4:f9aaf0f82c00 101 /*static uint8_t NwkSKey[] =
dudmuck 0:a5e3347bad61 102 {
dudmuck 0:a5e3347bad61 103 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
dudmuck 0:a5e3347bad61 104 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
dudmuck 0:a5e3347bad61 105 };
dudmuck 0:a5e3347bad61 106
dudmuck 0:a5e3347bad61 107 static uint8_t ArtSKey[] =
dudmuck 0:a5e3347bad61 108 {
dudmuck 0:a5e3347bad61 109 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
dudmuck 0:a5e3347bad61 110 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
dudmuck 4:f9aaf0f82c00 111 };*/
dudmuck 0:a5e3347bad61 112
dudmuck 0:a5e3347bad61 113 #endif /* SMTC */
dudmuck 0:a5e3347bad61 114
dudmuck 0:a5e3347bad61 115 //////////////////////////////////////////////////
dudmuck 0:a5e3347bad61 116 // APPLICATION CALLBACKS
dudmuck 0:a5e3347bad61 117 //////////////////////////////////////////////////
dudmuck 0:a5e3347bad61 118
dudmuck 0:a5e3347bad61 119 // provide application router ID (8 bytes, LSBF)
tmulrooney 8:1069ed1d4464 120 void os_getArtEui (u1_t* buf)
tmulrooney 8:1069ed1d4464 121 {
tmulrooney 8:1069ed1d4464 122 debug("os_getArtEui Enter\r\n");
tmulrooney 8:1069ed1d4464 123 for(int i=0; i < 8; i++)
tmulrooney 8:1069ed1d4464 124 debug("%02X ",reverse_APPEUI[i]);
tmulrooney 8:1069ed1d4464 125 debug("\r\n");
dudmuck 6:0b2b2d196ddd 126 //memcpy(buf, APPEUI, 8);
dudmuck 6:0b2b2d196ddd 127 LMIC_reverse_memcpy(buf, reverse_APPEUI, 8);
dudmuck 0:a5e3347bad61 128 }
dudmuck 0:a5e3347bad61 129
dudmuck 0:a5e3347bad61 130 // provide device ID (8 bytes, LSBF)
dudmuck 0:a5e3347bad61 131 void os_getDevEui (u1_t* buf) {
tmulrooney 8:1069ed1d4464 132 debug("os_getDevEui Enter\r\n");
tmulrooney 8:1069ed1d4464 133 for(int i=0; i < 8; i++)
tmulrooney 8:1069ed1d4464 134 debug("%02X ",reverse_DEVEUI[i]);
tmulrooney 8:1069ed1d4464 135 debug("\r\n");
dudmuck 6:0b2b2d196ddd 136 //memcpy(buf, DEVEUI, 8);
dudmuck 6:0b2b2d196ddd 137 LMIC_reverse_memcpy(buf, reverse_DEVEUI, 8);
dudmuck 0:a5e3347bad61 138 }
dudmuck 0:a5e3347bad61 139
dudmuck 0:a5e3347bad61 140 // provide device key (16 bytes)
dudmuck 0:a5e3347bad61 141 void os_getDevKey (u1_t* buf) {
tmulrooney 8:1069ed1d4464 142 debug("os_getDevKey Enter\r\n");
tmulrooney 8:1069ed1d4464 143 for(int i=0; i < 16; i++)
tmulrooney 8:1069ed1d4464 144 debug("%02X ",DEVKEY[i]);
tmulrooney 8:1069ed1d4464 145 debug("\r\n");
tmulrooney 8:1069ed1d4464 146 for(int i=0; i < 16; i++)
tmulrooney 8:1069ed1d4464 147 debug("%c ",DEVKEY[i]);
tmulrooney 8:1069ed1d4464 148 debug("\r\n");
tmulrooney 8:1069ed1d4464 149 memcpy(buf, DEVKEY, 16);
dudmuck 0:a5e3347bad61 150 }
dudmuck 0:a5e3347bad61 151
dudmuck 0:a5e3347bad61 152 //////////////////////////////////////////////////
dudmuck 0:a5e3347bad61 153 // MAIN - INITIALIZATION AND STARTUP
dudmuck 0:a5e3347bad61 154 //////////////////////////////////////////////////
dudmuck 2:a8f00db60fdb 155 void get_mote_version()
dudmuck 2:a8f00db60fdb 156 {
tmulrooney 8:1069ed1d4464 157 // char first;
dudmuck 2:a8f00db60fdb 158
tmulrooney 8:1069ed1d4464 159 // pc_7 = 1;
tmulrooney 8:1069ed1d4464 160 // first = pc_1;
tmulrooney 8:1069ed1d4464 161 // pc_7 = 0;
tmulrooney 8:1069ed1d4464 162 // if (first && !pc_1) {
tmulrooney 8:1069ed1d4464 163 // mote_version = MOTE_V2;
tmulrooney 8:1069ed1d4464 164 // printf("v2\r\n");
tmulrooney 8:1069ed1d4464 165 // bat = new AnalogIn(PA_0);
tmulrooney 8:1069ed1d4464 166 // } else {
tmulrooney 8:1069ed1d4464 167 // mote_version = MOTE_V3;
tmulrooney 8:1069ed1d4464 168 // printf("v3\r\n");
tmulrooney 8:1069ed1d4464 169 // bat = new AnalogIn(PA_1);
tmulrooney 8:1069ed1d4464 170 // }
dudmuck 2:a8f00db60fdb 171 }
dudmuck 0:a5e3347bad61 172
tmulrooney 8:1069ed1d4464 173 #if 0
dudmuck 0:a5e3347bad61 174 // initial job
dudmuck 0:a5e3347bad61 175 static void initfunc (osjob_t* j) {
dudmuck 0:a5e3347bad61 176 debug_str("B: INITFUNC\n");
dudmuck 0:a5e3347bad61 177 // reset MAC state
dudmuck 0:a5e3347bad61 178 LMIC_reset();
dudmuck 0:a5e3347bad61 179 // start joining
dudmuck 0:a5e3347bad61 180 #if( OVER_THE_AIR_ACTIVATION != 0 )
dudmuck 0:a5e3347bad61 181 LMIC_startJoining();
dudmuck 0:a5e3347bad61 182 #else
dudmuck 0:a5e3347bad61 183 devaddr_t *serial_id = (devaddr_t *) 0x1FF800d0; // cat3 device stm32l152rc
dudmuck 0:a5e3347bad61 184 LMIC_setSession( 0, *serial_id, NwkSKey, ArtSKey );
dudmuck 0:a5e3347bad61 185 debug_val("SN = ", *serial_id);
dudmuck 0:a5e3347bad61 186 #endif
tmulrooney 8:1069ed1d4464 187 //init done - onEvent() callback will be invoked...
dudmuck 0:a5e3347bad61 188
dudmuck 0:a5e3347bad61 189 //DEBUG_STR("E: INITFUNC");
dudmuck 2:a8f00db60fdb 190 get_mote_version();
tmulrooney 8:1069ed1d4464 191 // if (mote_version == MOTE_V3)
tmulrooney 8:1069ed1d4464 192 // gps.en_invert = false;
tmulrooney 8:1069ed1d4464 193 // else
tmulrooney 8:1069ed1d4464 194 // gps.en_invert = true;
dudmuck 2:a8f00db60fdb 195
tmulrooney 8:1069ed1d4464 196 // gps.init();
tmulrooney 8:1069ed1d4464 197 // gps.enable(1);
dudmuck 0:a5e3347bad61 198 //gps.verbose = 1;
dudmuck 0:a5e3347bad61 199
tmulrooney 8:1069ed1d4464 200 // mpl3115a2.init();
dudmuck 0:a5e3347bad61 201 }
tmulrooney 8:1069ed1d4464 202 #endif
tmulrooney 8:1069ed1d4464 203
tmulrooney 8:1069ed1d4464 204 #if 1
tmulrooney 8:1069ed1d4464 205 // counter static
tmulrooney 8:1069ed1d4464 206 int cnt = 0;
tmulrooney 8:1069ed1d4464 207 // log text to USART and toggle LED
tmulrooney 8:1069ed1d4464 208 static void initfunc (osjob_t* job)
tmulrooney 8:1069ed1d4464 209 {
tmulrooney 8:1069ed1d4464 210 // say hello
tmulrooney 8:1069ed1d4464 211 debug_str("Hello World!\r\n");
tmulrooney 8:1069ed1d4464 212 // log counter
tmulrooney 8:1069ed1d4464 213 debug_val("cnt = ", cnt);
tmulrooney 8:1069ed1d4464 214 // toggle LED
tmulrooney 8:1069ed1d4464 215 debug_led(++cnt & 1);
tmulrooney 8:1069ed1d4464 216 // reschedule job every second
tmulrooney 8:1069ed1d4464 217 os_setTimedCallback(job, os_getTime()+sec2osticks(1), initfunc);
tmulrooney 8:1069ed1d4464 218 }
tmulrooney 8:1069ed1d4464 219 #endif
tmulrooney 8:1069ed1d4464 220
dudmuck 0:a5e3347bad61 221
dudmuck 0:a5e3347bad61 222 int main(void)
dudmuck 0:a5e3347bad61 223 {
dudmuck 0:a5e3347bad61 224 osjob_t initjob;
tmulrooney 8:1069ed1d4464 225
dudmuck 0:a5e3347bad61 226 // initialize runtime env
tmulrooney 8:1069ed1d4464 227 debug_init();
dudmuck 0:a5e3347bad61 228 os_init();
dudmuck 0:a5e3347bad61 229 // setup initial job
dudmuck 0:a5e3347bad61 230 os_setCallback(&initjob, initfunc);
dudmuck 0:a5e3347bad61 231 // execute scheduled jobs and events
dudmuck 0:a5e3347bad61 232 os_runloop();
dudmuck 0:a5e3347bad61 233 // (not reached)
dudmuck 0:a5e3347bad61 234 }
dudmuck 0:a5e3347bad61 235
dudmuck 0:a5e3347bad61 236 //////////////////////////////////////////////////
dudmuck 0:a5e3347bad61 237 // LMIC EVENT CALLBACK
dudmuck 0:a5e3347bad61 238 //////////////////////////////////////////////////
dudmuck 0:a5e3347bad61 239 osjob_t rxLedJob;
dudmuck 0:a5e3347bad61 240 osjob_t txLedJob;
dudmuck 0:a5e3347bad61 241 osjob_t sendFrameJob;
dudmuck 0:a5e3347bad61 242
dudmuck 0:a5e3347bad61 243 static void onRxLed (osjob_t* j) {
dudmuck 0:a5e3347bad61 244 debug_val("LED2 = ", 1 );
dudmuck 0:a5e3347bad61 245 led2 = 1;
dudmuck 0:a5e3347bad61 246 }
dudmuck 0:a5e3347bad61 247
dudmuck 0:a5e3347bad61 248 static void onTxLed (osjob_t* j) {
dudmuck 0:a5e3347bad61 249 debug_val("LED1 = ", 1 );
dudmuck 0:a5e3347bad61 250 led1 = 1;
dudmuck 0:a5e3347bad61 251 }
dudmuck 0:a5e3347bad61 252
dudmuck 3:d81e4a63fb2e 253 static void
dudmuck 3:d81e4a63fb2e 254 restore_hsi()
dudmuck 3:d81e4a63fb2e 255 {
tmulrooney 8:1069ed1d4464 256 // RCC_OscInitTypeDef osc_init;
dudmuck 3:d81e4a63fb2e 257 /* if HSI was shut off in deep sleep (needed for AnalogIn) */
tmulrooney 8:1069ed1d4464 258 // HAL_RCC_GetOscConfig(&osc_init);
tmulrooney 8:1069ed1d4464 259 // if (osc_init.HSIState != RCC_HSI_ON) {
tmulrooney 8:1069ed1d4464 260 // // Enable the HSI (to clock the ADC)
tmulrooney 8:1069ed1d4464 261 // osc_init.OscillatorType = RCC_OSCILLATORTYPE_HSI;
tmulrooney 8:1069ed1d4464 262 // osc_init.HSIState = RCC_HSI_ON;
tmulrooney 8:1069ed1d4464 263 // osc_init.PLL.PLLState = RCC_PLL_NONE;
tmulrooney 8:1069ed1d4464 264 // HAL_RCC_OscConfig(&osc_init);
tmulrooney 8:1069ed1d4464 265 // }
dudmuck 3:d81e4a63fb2e 266 }
dudmuck 3:d81e4a63fb2e 267
dudmuck 0:a5e3347bad61 268 static bool AppLedStateOn = false;
dudmuck 0:a5e3347bad61 269
dudmuck 3:d81e4a63fb2e 270 #define AIN_VREF 3.3 // stm32 internal refernce
dudmuck 3:d81e4a63fb2e 271 #define AIN_VBAT_DIV 2 // resistor divider
dudmuck 0:a5e3347bad61 272 static void PrepareDataFrame( void )
dudmuck 0:a5e3347bad61 273 {
dudmuck 0:a5e3347bad61 274 uint16_t altitudeGps;
dudmuck 3:d81e4a63fb2e 275
dudmuck 3:d81e4a63fb2e 276 restore_hsi();
dudmuck 0:a5e3347bad61 277
tmulrooney 8:1069ed1d4464 278 // gps.service();
dudmuck 6:0b2b2d196ddd 279 //printf("lat:%f long:%f\r\n", gps.Latitude, gps.Longitude);
tmulrooney 8:1069ed1d4464 280 // mpl3115a2.ReadTemperature();
dudmuck 0:a5e3347bad61 281
dudmuck 0:a5e3347bad61 282 // immediately prepare next transmission
dudmuck 0:a5e3347bad61 283 //LMIC.frame[0] = LMIC.rxq.snr;
dudmuck 0:a5e3347bad61 284 LMIC.frame[0] = AppLedStateOn; // (bit 0 == 1) => LED on
tmulrooney 8:1069ed1d4464 285 // LMIC.frame[1] = (int)mpl3115a2.Temperature; // Signed degrees Celcius in half degree units. So, +/-63 C
dudmuck 3:d81e4a63fb2e 286 LMIC.frame[2] = (bat->read_u16() >> 8) + (bat->read_u16() >> 9) ; // per LoRaMAC spec; 0=Charging; 1...254 = level, 255 = N/A
dudmuck 3:d81e4a63fb2e 287
tmulrooney 8:1069ed1d4464 288 // LMIC.frame[3] = ( gps.LatitudeBinary >> 16 ) & 0xFF;
tmulrooney 8:1069ed1d4464 289 // LMIC.frame[4] = ( gps.LatitudeBinary >> 8 ) & 0xFF;
tmulrooney 8:1069ed1d4464 290 // LMIC.frame[5] = gps.LatitudeBinary & 0xFF;
tmulrooney 8:1069ed1d4464 291 // LMIC.frame[6] = ( gps.LongitudeBinary >> 16 ) & 0xFF;
tmulrooney 8:1069ed1d4464 292 // LMIC.frame[7] = ( gps.LongitudeBinary >> 8 ) & 0xFF;
tmulrooney 8:1069ed1d4464 293 // LMIC.frame[8] = gps.LongitudeBinary & 0xFF;
dudmuck 0:a5e3347bad61 294
tmulrooney 8:1069ed1d4464 295 // altitudeGps = atoi(gps.NmeaGpsData.NmeaAltitude);
dudmuck 3:d81e4a63fb2e 296 //printf("alt:%d\r\n", altitudeGps);
dudmuck 0:a5e3347bad61 297 LMIC.frame[9] = ( altitudeGps >> 8 ) & 0xFF;
dudmuck 0:a5e3347bad61 298 LMIC.frame[10] = altitudeGps & 0xFF;
dudmuck 3:d81e4a63fb2e 299
dudmuck 6:0b2b2d196ddd 300
dudmuck 6:0b2b2d196ddd 301 //printf("bat:%.2f\r\n", volts);
dudmuck 6:0b2b2d196ddd 302 #ifndef CHNL_HYBRID
dudmuck 6:0b2b2d196ddd 303 float volts = bat->read()*AIN_VREF*AIN_VBAT_DIV;
dudmuck 3:d81e4a63fb2e 304 if (volts < LOW_BAT_THRESHOLD)
dudmuck 3:d81e4a63fb2e 305 LMIC.txpow_limit = 20;
dudmuck 3:d81e4a63fb2e 306 else
dudmuck 3:d81e4a63fb2e 307 LMIC.txpow_limit = 30;
dudmuck 6:0b2b2d196ddd 308 #endif /* !CHNL_HYBRID */
dudmuck 0:a5e3347bad61 309 }
dudmuck 0:a5e3347bad61 310
dudmuck 6:0b2b2d196ddd 311 static void onSendFrame (osjob_t* j)
dudmuck 6:0b2b2d196ddd 312 {
dudmuck 6:0b2b2d196ddd 313 if (LMIC.opmode & OP_TXRXPEND)
dudmuck 6:0b2b2d196ddd 314 return;
dudmuck 6:0b2b2d196ddd 315
dudmuck 0:a5e3347bad61 316 PrepareDataFrame( );
dudmuck 6:0b2b2d196ddd 317 // schedule transmission (port 1, data[], datalen 1, ACK requested)
dudmuck 6:0b2b2d196ddd 318 // (will be sent as soon as duty cycle permits)
dudmuck 6:0b2b2d196ddd 319
dudmuck 0:a5e3347bad61 320 LMIC_setTxData2(5, LMIC.frame, APP_DATA_SIZE, APP_ACK);
dudmuck 0:a5e3347bad61 321 }
dudmuck 0:a5e3347bad61 322
dudmuck 0:a5e3347bad61 323 void onEvent (ev_t ev) {
dudmuck 0:a5e3347bad61 324
dudmuck 0:a5e3347bad61 325 debug_event(ev);
dudmuck 0:a5e3347bad61 326
tmulrooney 8:1069ed1d4464 327 // gps.service();
dudmuck 0:a5e3347bad61 328
dudmuck 0:a5e3347bad61 329 switch(ev)
dudmuck 0:a5e3347bad61 330 {
dudmuck 0:a5e3347bad61 331 // network joined, session established
dudmuck 0:a5e3347bad61 332 case EV_JOINED:
dudmuck 0:a5e3347bad61 333 debug_val("Net ID = ", LMIC.netid);
dudmuck 0:a5e3347bad61 334 goto tx;
dudmuck 0:a5e3347bad61 335 // scheduled data sent (optionally data received)
dudmuck 0:a5e3347bad61 336 case EV_TXCOMPLETE:
dudmuck 0:a5e3347bad61 337 if(LMIC.dataLen)
dudmuck 0:a5e3347bad61 338 { // data received in rx slot after tx
dudmuck 0:a5e3347bad61 339 debug_buf(LMIC.frame+LMIC.dataBeg, LMIC.dataLen);
dudmuck 0:a5e3347bad61 340 if(LMIC.dataLen == 1) { // set LED state if exactly one byte is received
dudmuck 0:a5e3347bad61 341 AppLedStateOn = LMIC.frame[LMIC.dataBeg] & 0x01;
dudmuck 0:a5e3347bad61 342 debug_val("LED3 = ", AppLedStateOn ? 0 : 1 );
dudmuck 0:a5e3347bad61 343 }
dudmuck 0:a5e3347bad61 344 }
dudmuck 0:a5e3347bad61 345 if((LMIC.txrxFlags & (TXRX_DNW1|TXRX_DNW2) )!= 0 )
dudmuck 0:a5e3347bad61 346 {
dudmuck 0:a5e3347bad61 347 debug_val("LED2 = ", 0 );
dudmuck 0:a5e3347bad61 348 led2 = 0;
dudmuck 0:a5e3347bad61 349 os_setTimedCallback( &rxLedJob, os_getTime() + ms2osticks(15), onRxLed );
dudmuck 0:a5e3347bad61 350 }
dudmuck 0:a5e3347bad61 351 tx:
dudmuck 4:f9aaf0f82c00 352 os_setTimedCallback( &sendFrameJob, os_getTime() + ms2osticks(MS_DELAY_NEXT_TX), onSendFrame ); // Change the Tx periodicity
dudmuck 0:a5e3347bad61 353 //onSendFrame(NULL);
dudmuck 0:a5e3347bad61 354
dudmuck 0:a5e3347bad61 355 // Blink Tx LED
dudmuck 0:a5e3347bad61 356 debug_val("LED1 = ", 0 );
dudmuck 0:a5e3347bad61 357 led1 = 0;
dudmuck 0:a5e3347bad61 358
dudmuck 0:a5e3347bad61 359 os_setTimedCallback( &txLedJob, os_getTime() + ms2osticks(25), onTxLed );
dudmuck 0:a5e3347bad61 360 break;
dudmuck 0:a5e3347bad61 361 default:
dudmuck 0:a5e3347bad61 362 break;
dudmuck 0:a5e3347bad61 363 }
dudmuck 0:a5e3347bad61 364 }