Temperature reading using NUCLEO-L152RE microcontroller and Grove – Temperature&Humidity Sensor Pro.
Dependencies: DHT LMiC SX1276Lib mbed
Fork of LoRaWAN_send_text by
main.cpp@5:1f8829bd11ed, 2015-09-13 (annotated)
- Committer:
- tamberg
- Date:
- Sun Sep 13 13:51:32 2015 +0000
- Revision:
- 5:1f8829bd11ed
- Parent:
- 4:f83ad3eee79d
- Child:
- 6:fbfc95b5c979
Cloned and reformatted LoRaWAN-lmic-app
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tamberg | 5:1f8829bd11ed | 1 | /* License: Revised BSD License, see LICENSE.TXT, (c)2015 Semtech */ |
mluis | 0:a2929fa6e4f0 | 2 | |
mluis | 1:60184eda0066 | 3 | #include "mbed.h" |
mluis | 0:a2929fa6e4f0 | 4 | #include "lmic.h" |
mluis | 1:60184eda0066 | 5 | #include "debug.h" |
mluis | 0:a2929fa6e4f0 | 6 | |
mluis | 0:a2929fa6e4f0 | 7 | #define OVER_THE_AIR_ACTIVATION 0 |
mluis | 1:60184eda0066 | 8 | #define LORAWAN_NET_ID ( uint32_t )0x00000000 |
tamberg | 4:f83ad3eee79d | 9 | #define LORAWAN_DEV_ADDR ( uint32_t )0x00001056//0x12345678 |
mluis | 1:60184eda0066 | 10 | #define APP_TX_DUTYCYCLE 5000 // 5 [s] value in ms |
mluis | 1:60184eda0066 | 11 | #define APP_TX_DUTYCYCLE_RND 1000 // 1 [s] value in ms |
mluis | 1:60184eda0066 | 12 | #define LORAWAN_ADR_ON 1 |
mluis | 1:60184eda0066 | 13 | #define LORAWAN_CONFIRMED_MSG_ON 1 |
mluis | 1:60184eda0066 | 14 | #define LORAWAN_APP_PORT 15 |
mluis | 1:60184eda0066 | 15 | #define LORAWAN_APP_DATA_SIZE 6 |
mluis | 1:60184eda0066 | 16 | |
tamberg | 5:1f8829bd11ed | 17 | static const uint8_t AppEui[8] = { |
mluis | 1:60184eda0066 | 18 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
mluis | 0:a2929fa6e4f0 | 19 | }; |
mluis | 0:a2929fa6e4f0 | 20 | |
tamberg | 5:1f8829bd11ed | 21 | static const u1_t DevEui[8] = { |
tamberg | 4:f83ad3eee79d | 22 | 0xF0, 0x3D, 0x29, 0x10, 0x00, 0x00, 0x10, 0x56 |
mluis | 0:a2929fa6e4f0 | 23 | }; |
mluis | 0:a2929fa6e4f0 | 24 | |
tamberg | 5:1f8829bd11ed | 25 | static const uint8_t DevKey[16] = { |
mluis | 1:60184eda0066 | 26 | 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, |
mluis | 1:60184eda0066 | 27 | 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C |
mluis | 0:a2929fa6e4f0 | 28 | }; |
mluis | 0:a2929fa6e4f0 | 29 | |
tamberg | 5:1f8829bd11ed | 30 | static uint8_t NwkSKey[] = { |
tamberg | 4:f83ad3eee79d | 31 | 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, |
tamberg | 4:f83ad3eee79d | 32 | 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF |
mluis | 0:a2929fa6e4f0 | 33 | }; |
mluis | 0:a2929fa6e4f0 | 34 | |
tamberg | 5:1f8829bd11ed | 35 | static uint8_t ArtSKey[] = { |
tamberg | 4:f83ad3eee79d | 36 | 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, |
tamberg | 4:f83ad3eee79d | 37 | 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF |
mluis | 0:a2929fa6e4f0 | 38 | }; |
mluis | 0:a2929fa6e4f0 | 39 | |
mluis | 0:a2929fa6e4f0 | 40 | osjob_t rxLedJob; |
mluis | 0:a2929fa6e4f0 | 41 | osjob_t txLedJob; |
mluis | 0:a2929fa6e4f0 | 42 | osjob_t sendFrameJob; |
mluis | 0:a2929fa6e4f0 | 43 | |
mluis | 0:a2929fa6e4f0 | 44 | static bool AppLedStateOn = false; |
mluis | 0:a2929fa6e4f0 | 45 | |
tamberg | 5:1f8829bd11ed | 46 | int32_t randr (int32_t min, int32_t max) { |
tamberg | 5:1f8829bd11ed | 47 | return (int32_t) rand() % (max - min + 1) + min; |
tamberg | 5:1f8829bd11ed | 48 | } |
tamberg | 5:1f8829bd11ed | 49 | |
tamberg | 5:1f8829bd11ed | 50 | void os_getArtEui (uint8_t *buf) { |
tamberg | 5:1f8829bd11ed | 51 | debug_str("os_getArtEui\r\n"); |
tamberg | 5:1f8829bd11ed | 52 | memcpy(buf, AppEui, 8); |
mluis | 1:60184eda0066 | 53 | } |
mluis | 1:60184eda0066 | 54 | |
tamberg | 5:1f8829bd11ed | 55 | void os_getDevEui (uint8_t *buf) { |
tamberg | 5:1f8829bd11ed | 56 | debug_str("os_getDevEui\r\n"); |
tamberg | 5:1f8829bd11ed | 57 | memcpy(buf, DevEui, 8); |
mluis | 0:a2929fa6e4f0 | 58 | } |
mluis | 0:a2929fa6e4f0 | 59 | |
tamberg | 5:1f8829bd11ed | 60 | void os_getDevKey (uint8_t *buf) { |
tamberg | 5:1f8829bd11ed | 61 | debug_str("os_getDevKey\r\n"); |
tamberg | 5:1f8829bd11ed | 62 | memcpy(buf, DevKey, 16); |
mluis | 0:a2929fa6e4f0 | 63 | } |
mluis | 0:a2929fa6e4f0 | 64 | |
tamberg | 5:1f8829bd11ed | 65 | static void onRxLed (osjob_t* j) { |
mluis | 1:60184eda0066 | 66 | debug_val("LED2 = ", 0 ); |
mluis | 0:a2929fa6e4f0 | 67 | } |
mluis | 0:a2929fa6e4f0 | 68 | |
tamberg | 5:1f8829bd11ed | 69 | static void onTxLed (osjob_t* j) { |
mluis | 1:60184eda0066 | 70 | debug_val("LED1 = ", 0 ); |
mluis | 0:a2929fa6e4f0 | 71 | } |
mluis | 0:a2929fa6e4f0 | 72 | |
tamberg | 5:1f8829bd11ed | 73 | static void prepareTxFrame (void) { |
tamberg | 4:f83ad3eee79d | 74 | debug_str("prepareTxFrame\r\n"); |
mluis | 0:a2929fa6e4f0 | 75 | LMIC.frame[0] = AppLedStateOn; |
mluis | 1:60184eda0066 | 76 | LMIC.frame[1] = LMIC.seqnoDn >> 8; |
mluis | 1:60184eda0066 | 77 | LMIC.frame[2] = LMIC.seqnoDn; |
mluis | 1:60184eda0066 | 78 | LMIC.frame[3] = LMIC.rssi >> 8; |
mluis | 1:60184eda0066 | 79 | LMIC.frame[4] = LMIC.rssi; |
mluis | 1:60184eda0066 | 80 | LMIC.frame[5] = LMIC.snr; |
mluis | 0:a2929fa6e4f0 | 81 | } |
mluis | 0:a2929fa6e4f0 | 82 | |
tamberg | 5:1f8829bd11ed | 83 | void processRxFrame (void) { |
tamberg | 4:f83ad3eee79d | 84 | debug_str("processRxFrame\r\n"); |
tamberg | 5:1f8829bd11ed | 85 | switch(LMIC.frame[LMIC.dataBeg - 1]) { // Check Rx port number |
mluis | 0:a2929fa6e4f0 | 86 | case 1: // The application LED can be controlled on port 1 or 2 |
mluis | 0:a2929fa6e4f0 | 87 | case 2: |
tamberg | 5:1f8829bd11ed | 88 | if(LMIC.dataLen == 1) { |
mluis | 0:a2929fa6e4f0 | 89 | AppLedStateOn = LMIC.frame[LMIC.dataBeg] & 0x01; |
tamberg | 5:1f8829bd11ed | 90 | debug_val("LED3 = ", AppLedStateOn); |
mluis | 0:a2929fa6e4f0 | 91 | } |
mluis | 0:a2929fa6e4f0 | 92 | break; |
mluis | 0:a2929fa6e4f0 | 93 | default: |
mluis | 0:a2929fa6e4f0 | 94 | break; |
mluis | 0:a2929fa6e4f0 | 95 | } |
mluis | 0:a2929fa6e4f0 | 96 | } |
mluis | 0:a2929fa6e4f0 | 97 | |
tamberg | 5:1f8829bd11ed | 98 | static void onSendFrame (osjob_t* j) { |
tamberg | 4:f83ad3eee79d | 99 | debug_str("onSendFrame\r\n"); |
tamberg | 5:1f8829bd11ed | 100 | prepareTxFrame(); |
tamberg | 5:1f8829bd11ed | 101 | LMIC_setTxData2( |
tamberg | 5:1f8829bd11ed | 102 | LORAWAN_APP_PORT, |
tamberg | 5:1f8829bd11ed | 103 | LMIC.frame, |
tamberg | 5:1f8829bd11ed | 104 | LORAWAN_APP_DATA_SIZE, |
tamberg | 5:1f8829bd11ed | 105 | LORAWAN_CONFIRMED_MSG_ON); |
mluis | 1:60184eda0066 | 106 | |
tamberg | 5:1f8829bd11ed | 107 | // debug_val("LED1 = ", 1); |
tamberg | 5:1f8829bd11ed | 108 | // os_setTimedCallback(&txLedJob, os_getTime() + ms2osticks(25), onTxLed); |
mluis | 0:a2929fa6e4f0 | 109 | } |
mluis | 0:a2929fa6e4f0 | 110 | |
tamberg | 5:1f8829bd11ed | 111 | static void onInit (osjob_t* j) { |
tamberg | 4:f83ad3eee79d | 112 | debug_str("onInit\r\n"); |
tamberg | 5:1f8829bd11ed | 113 | LMIC_reset(); // reset MAC state |
tamberg | 5:1f8829bd11ed | 114 | LMIC_setAdrMode(LORAWAN_ADR_ON); |
tamberg | 5:1f8829bd11ed | 115 | LMIC_setDrTxpow(DR_SF12, 14); |
tamberg | 5:1f8829bd11ed | 116 | LMIC_setSession( |
tamberg | 5:1f8829bd11ed | 117 | LORAWAN_NET_ID, |
tamberg | 5:1f8829bd11ed | 118 | LORAWAN_DEV_ADDR, |
tamberg | 5:1f8829bd11ed | 119 | NwkSKey, |
tamberg | 5:1f8829bd11ed | 120 | ArtSKey); |
tamberg | 5:1f8829bd11ed | 121 | onSendFrame(NULL); |
tamberg | 5:1f8829bd11ed | 122 | // onEvent() callback will be invoked... |
mluis | 1:60184eda0066 | 123 | } |
mluis | 1:60184eda0066 | 124 | |
tamberg | 5:1f8829bd11ed | 125 | int main(void) { |
tamberg | 4:f83ad3eee79d | 126 | debug_str("main\r\n"); |
mluis | 0:a2929fa6e4f0 | 127 | osjob_t initjob; |
tamberg | 5:1f8829bd11ed | 128 | os_init(); |
tamberg | 5:1f8829bd11ed | 129 | os_setCallback(&initjob, onInit); |
tamberg | 5:1f8829bd11ed | 130 | os_runloop(); // blocking |
mluis | 0:a2929fa6e4f0 | 131 | } |
mluis | 0:a2929fa6e4f0 | 132 | |
tamberg | 5:1f8829bd11ed | 133 | void onEvent (ev_t ev) { |
tamberg | 4:f83ad3eee79d | 134 | debug_str("onEvent\r\n"); |
mluis | 0:a2929fa6e4f0 | 135 | bool txOn = false; |
tamberg | 5:1f8829bd11ed | 136 | debug_event(ev); |
mluis | 0:a2929fa6e4f0 | 137 | |
tamberg | 5:1f8829bd11ed | 138 | switch(ev) { |
tamberg | 5:1f8829bd11ed | 139 | case EV_JOINED: // network joined, session established |
tamberg | 5:1f8829bd11ed | 140 | debug_val("Net ID = ", LMIC.netid); |
mluis | 0:a2929fa6e4f0 | 141 | txOn = true; |
mluis | 0:a2929fa6e4f0 | 142 | break; |
tamberg | 5:1f8829bd11ed | 143 | case EV_TXCOMPLETE: // scheduled data sent (optionally data received) |
tamberg | 5:1f8829bd11ed | 144 | debug_val("Datarate = ", LMIC.datarate); |
mluis | 0:a2929fa6e4f0 | 145 | // Check if we have a downlink on either Rx1 or Rx2 windows |
tamberg | 5:1f8829bd11ed | 146 | if ((LMIC.txrxFlags & (TXRX_DNW1 | TXRX_DNW2)) != 0) { |
tamberg | 5:1f8829bd11ed | 147 | debug_val("LED2 = ", 1); |
tamberg | 5:1f8829bd11ed | 148 | os_setTimedCallback(&rxLedJob, os_getTime() + ms2osticks(25), onRxLed); |
tamberg | 5:1f8829bd11ed | 149 | if (LMIC.dataLen != 0) { // data received in rx slot after tx |
tamberg | 5:1f8829bd11ed | 150 | debug_buf(LMIC.frame + LMIC.dataBeg, LMIC.dataLen); |
tamberg | 5:1f8829bd11ed | 151 | processRxFrame(); |
mluis | 0:a2929fa6e4f0 | 152 | } |
mluis | 0:a2929fa6e4f0 | 153 | } |
mluis | 0:a2929fa6e4f0 | 154 | txOn = true; |
mluis | 0:a2929fa6e4f0 | 155 | break; |
mluis | 0:a2929fa6e4f0 | 156 | default: |
mluis | 0:a2929fa6e4f0 | 157 | break; |
mluis | 0:a2929fa6e4f0 | 158 | } |
tamberg | 5:1f8829bd11ed | 159 | if (txOn == true) { |
tamberg | 5:1f8829bd11ed | 160 | os_setTimedCallback( |
tamberg | 5:1f8829bd11ed | 161 | &sendFrameJob, |
tamberg | 5:1f8829bd11ed | 162 | os_getTime() + ms2osticks(APP_TX_DUTYCYCLE + randr(-APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND)), |
tamberg | 5:1f8829bd11ed | 163 | onSendFrame); |
mluis | 0:a2929fa6e4f0 | 164 | } |
mluis | 0:a2929fa6e4f0 | 165 | } |