Send text with LoRaWAN.

Dependencies:   LMiC SX1276Lib mbed

Fork of LoRaWAN-lmic-app by Semtech

Committer:
tamberg
Date:
Mon Sep 14 14:26:55 2015 +0000
Revision:
7:4adfa7248a0b
Parent:
6:fbfc95b5c979
Child:
8:747796516a2f
Removed unused code.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tamberg 6:fbfc95b5c979 1 /* License: Revised BSD License, see LICENSE.TXT, (c)2015 tamberg.org, (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
tamberg 6:fbfc95b5c979 7 #define LORAWAN_NET_ID (uint32_t) 0x00000000
tamberg 6:fbfc95b5c979 8 #define LORAWAN_DEV_ADDR (uint32_t) 0x00001056
tamberg 6:fbfc95b5c979 9 #define APP_TX_DUTYCYCLE 5000 // ms
tamberg 6:fbfc95b5c979 10 #define APP_TX_DUTYCYCLE_RND 1000 // ms
tamberg 6:fbfc95b5c979 11 #define LORAWAN_ADR_ON 1
tamberg 6:fbfc95b5c979 12 #define LORAWAN_CONFIRMED_MSG_ON 1
tamberg 6:fbfc95b5c979 13 #define LORAWAN_APP_PORT 15
tamberg 7:4adfa7248a0b 14 #define LORAWAN_APP_DATA_SIZE 32 //6 // max 51
mluis 0:a2929fa6e4f0 15
tamberg 5:1f8829bd11ed 16 static uint8_t NwkSKey[] = {
tamberg 4:f83ad3eee79d 17 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
tamberg 4:f83ad3eee79d 18 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
mluis 0:a2929fa6e4f0 19 };
mluis 0:a2929fa6e4f0 20
tamberg 5:1f8829bd11ed 21 static uint8_t ArtSKey[] = {
tamberg 4:f83ad3eee79d 22 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
tamberg 4:f83ad3eee79d 23 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
mluis 0:a2929fa6e4f0 24 };
mluis 0:a2929fa6e4f0 25
mluis 0:a2929fa6e4f0 26 osjob_t sendFrameJob;
mluis 0:a2929fa6e4f0 27
tamberg 5:1f8829bd11ed 28 int32_t randr (int32_t min, int32_t max) {
tamberg 5:1f8829bd11ed 29 return (int32_t) rand() % (max - min + 1) + min;
tamberg 5:1f8829bd11ed 30 }
tamberg 5:1f8829bd11ed 31
tamberg 7:4adfa7248a0b 32 void os_getArtEui (uint8_t *buf) {} // ignore
tamberg 7:4adfa7248a0b 33 void os_getDevEui (uint8_t *buf) {} // ignore
tamberg 7:4adfa7248a0b 34 void os_getDevKey (uint8_t *buf) {} // ignore
mluis 0:a2929fa6e4f0 35
tamberg 5:1f8829bd11ed 36 static void prepareTxFrame (void) {
tamberg 4:f83ad3eee79d 37 debug_str("prepareTxFrame\r\n");
tamberg 6:fbfc95b5c979 38 for (int i = 0; i < LORAWAN_APP_DATA_SIZE; i++) {
tamberg 6:fbfc95b5c979 39 LMIC.frame[i] = i;
tamberg 6:fbfc95b5c979 40 }
tamberg 7:4adfa7248a0b 41 // LMIC.frame[0] = AppLedStateOn;
tamberg 7:4adfa7248a0b 42 // LMIC.frame[1] = LMIC.seqnoDn >> 8;
tamberg 7:4adfa7248a0b 43 // LMIC.frame[2] = LMIC.seqnoDn;
tamberg 7:4adfa7248a0b 44 // LMIC.frame[3] = LMIC.rssi >> 8;
tamberg 7:4adfa7248a0b 45 // LMIC.frame[4] = LMIC.rssi;
tamberg 7:4adfa7248a0b 46 // LMIC.frame[5] = LMIC.snr;
mluis 0:a2929fa6e4f0 47 }
mluis 0:a2929fa6e4f0 48
tamberg 5:1f8829bd11ed 49 void processRxFrame (void) {
tamberg 4:f83ad3eee79d 50 debug_str("processRxFrame\r\n");
tamberg 6:fbfc95b5c979 51 u1_t rxPort = LMIC.frame[LMIC.dataBeg - 1];
tamberg 7:4adfa7248a0b 52 debug_val("rxPort", rxPort);
tamberg 7:4adfa7248a0b 53 for (int i = 0; i < LMIC.dataLen; i++) {
tamberg 7:4adfa7248a0b 54 debug_hex(LMIC.frame[LMIC.dataBeg + i]);
tamberg 7:4adfa7248a0b 55 debug_char(' ');
mluis 0:a2929fa6e4f0 56 }
mluis 0:a2929fa6e4f0 57 }
mluis 0:a2929fa6e4f0 58
tamberg 5:1f8829bd11ed 59 static void onSendFrame (osjob_t* j) {
tamberg 4:f83ad3eee79d 60 debug_str("onSendFrame\r\n");
tamberg 5:1f8829bd11ed 61 prepareTxFrame();
tamberg 6:fbfc95b5c979 62 int result = LMIC_setTxData2(
tamberg 5:1f8829bd11ed 63 LORAWAN_APP_PORT,
tamberg 5:1f8829bd11ed 64 LMIC.frame,
tamberg 5:1f8829bd11ed 65 LORAWAN_APP_DATA_SIZE,
tamberg 7:4adfa7248a0b 66 LORAWAN_CONFIRMED_MSG_ON); // calls onEvent()
tamberg 7:4adfa7248a0b 67 debug_val("LMIC_setTxData2, result = ", result);
mluis 0:a2929fa6e4f0 68 }
mluis 0:a2929fa6e4f0 69
tamberg 5:1f8829bd11ed 70 static void onInit (osjob_t* j) {
tamberg 4:f83ad3eee79d 71 debug_str("onInit\r\n");
tamberg 5:1f8829bd11ed 72 LMIC_reset(); // reset MAC state
tamberg 5:1f8829bd11ed 73 LMIC_setAdrMode(LORAWAN_ADR_ON);
tamberg 5:1f8829bd11ed 74 LMIC_setDrTxpow(DR_SF12, 14);
tamberg 5:1f8829bd11ed 75 LMIC_setSession(
tamberg 5:1f8829bd11ed 76 LORAWAN_NET_ID,
tamberg 5:1f8829bd11ed 77 LORAWAN_DEV_ADDR,
tamberg 5:1f8829bd11ed 78 NwkSKey,
tamberg 5:1f8829bd11ed 79 ArtSKey);
tamberg 5:1f8829bd11ed 80 onSendFrame(NULL);
mluis 1:60184eda0066 81 }
mluis 1:60184eda0066 82
tamberg 6:fbfc95b5c979 83 int main (void) {
tamberg 4:f83ad3eee79d 84 debug_str("main\r\n");
mluis 0:a2929fa6e4f0 85 osjob_t initjob;
tamberg 5:1f8829bd11ed 86 os_init();
tamberg 5:1f8829bd11ed 87 os_setCallback(&initjob, onInit);
tamberg 5:1f8829bd11ed 88 os_runloop(); // blocking
mluis 0:a2929fa6e4f0 89 }
mluis 0:a2929fa6e4f0 90
tamberg 6:fbfc95b5c979 91 void onEvent (ev_t ev) { // called by lmic.cpp, see also oslmic.h
tamberg 4:f83ad3eee79d 92 debug_str("onEvent\r\n");
mluis 0:a2929fa6e4f0 93 bool txOn = false;
tamberg 5:1f8829bd11ed 94 debug_event(ev);
tamberg 6:fbfc95b5c979 95 switch (ev) {
tamberg 5:1f8829bd11ed 96 case EV_JOINED: // network joined, session established
tamberg 5:1f8829bd11ed 97 debug_val("Net ID = ", LMIC.netid);
mluis 0:a2929fa6e4f0 98 txOn = true;
mluis 0:a2929fa6e4f0 99 break;
tamberg 5:1f8829bd11ed 100 case EV_TXCOMPLETE: // scheduled data sent (optionally data received)
tamberg 5:1f8829bd11ed 101 debug_val("Datarate = ", LMIC.datarate);
mluis 0:a2929fa6e4f0 102 // Check if we have a downlink on either Rx1 or Rx2 windows
tamberg 5:1f8829bd11ed 103 if ((LMIC.txrxFlags & (TXRX_DNW1 | TXRX_DNW2)) != 0) {
tamberg 6:fbfc95b5c979 104 //debug_val("LED2 = ", 1);
tamberg 6:fbfc95b5c979 105 //os_setTimedCallback(&rxLedJob, os_getTime() + ms2osticks(25), onRxLed);
tamberg 5:1f8829bd11ed 106 if (LMIC.dataLen != 0) { // data received in rx slot after tx
tamberg 5:1f8829bd11ed 107 debug_buf(LMIC.frame + LMIC.dataBeg, LMIC.dataLen);
tamberg 5:1f8829bd11ed 108 processRxFrame();
mluis 0:a2929fa6e4f0 109 }
mluis 0:a2929fa6e4f0 110 }
mluis 0:a2929fa6e4f0 111 txOn = true;
mluis 0:a2929fa6e4f0 112 break;
mluis 0:a2929fa6e4f0 113 default:
mluis 0:a2929fa6e4f0 114 break;
mluis 0:a2929fa6e4f0 115 }
tamberg 5:1f8829bd11ed 116 if (txOn == true) {
tamberg 5:1f8829bd11ed 117 os_setTimedCallback(
tamberg 5:1f8829bd11ed 118 &sendFrameJob,
tamberg 5:1f8829bd11ed 119 os_getTime() + ms2osticks(APP_TX_DUTYCYCLE + randr(-APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND)),
tamberg 5:1f8829bd11ed 120 onSendFrame);
mluis 0:a2929fa6e4f0 121 }
mluis 0:a2929fa6e4f0 122 }