Send text with LoRaWAN.

Dependencies:   LMiC SX1276Lib mbed

Fork of LoRaWAN-lmic-app by Semtech

Committer:
tamberg
Date:
Mon Sep 14 09:26:25 2015 +0000
Revision:
6:fbfc95b5c979
Parent:
5:1f8829bd11ed
Child:
7:4adfa7248a0b
Increased payload length.

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 6:fbfc95b5c979 14 #define LORAWAN_APP_DATA_SIZE 51 // 6 // max 51
mluis 1:60184eda0066 15
tamberg 5:1f8829bd11ed 16 static const uint8_t AppEui[8] = {
mluis 1:60184eda0066 17 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
mluis 0:a2929fa6e4f0 18 };
mluis 0:a2929fa6e4f0 19
tamberg 5:1f8829bd11ed 20 static const u1_t DevEui[8] = {
tamberg 4:f83ad3eee79d 21 0xF0, 0x3D, 0x29, 0x10, 0x00, 0x00, 0x10, 0x56
mluis 0:a2929fa6e4f0 22 };
mluis 0:a2929fa6e4f0 23
tamberg 5:1f8829bd11ed 24 static const uint8_t DevKey[16] = {
mluis 1:60184eda0066 25 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
mluis 1:60184eda0066 26 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
mluis 0:a2929fa6e4f0 27 };
mluis 0:a2929fa6e4f0 28
tamberg 5:1f8829bd11ed 29 static uint8_t NwkSKey[] = {
tamberg 4:f83ad3eee79d 30 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
tamberg 4:f83ad3eee79d 31 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
mluis 0:a2929fa6e4f0 32 };
mluis 0:a2929fa6e4f0 33
tamberg 5:1f8829bd11ed 34 static uint8_t ArtSKey[] = {
tamberg 4:f83ad3eee79d 35 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
tamberg 4:f83ad3eee79d 36 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
mluis 0:a2929fa6e4f0 37 };
mluis 0:a2929fa6e4f0 38
mluis 0:a2929fa6e4f0 39 osjob_t sendFrameJob;
mluis 0:a2929fa6e4f0 40
mluis 0:a2929fa6e4f0 41 static bool AppLedStateOn = false;
mluis 0:a2929fa6e4f0 42
tamberg 5:1f8829bd11ed 43 int32_t randr (int32_t min, int32_t max) {
tamberg 5:1f8829bd11ed 44 return (int32_t) rand() % (max - min + 1) + min;
tamberg 5:1f8829bd11ed 45 }
tamberg 5:1f8829bd11ed 46
tamberg 5:1f8829bd11ed 47 void os_getArtEui (uint8_t *buf) {
tamberg 5:1f8829bd11ed 48 debug_str("os_getArtEui\r\n");
tamberg 5:1f8829bd11ed 49 memcpy(buf, AppEui, 8);
mluis 1:60184eda0066 50 }
mluis 1:60184eda0066 51
tamberg 5:1f8829bd11ed 52 void os_getDevEui (uint8_t *buf) {
tamberg 5:1f8829bd11ed 53 debug_str("os_getDevEui\r\n");
tamberg 5:1f8829bd11ed 54 memcpy(buf, DevEui, 8);
mluis 0:a2929fa6e4f0 55 }
mluis 0:a2929fa6e4f0 56
tamberg 5:1f8829bd11ed 57 void os_getDevKey (uint8_t *buf) {
tamberg 5:1f8829bd11ed 58 debug_str("os_getDevKey\r\n");
tamberg 5:1f8829bd11ed 59 memcpy(buf, DevKey, 16);
mluis 0:a2929fa6e4f0 60 }
mluis 0:a2929fa6e4f0 61
tamberg 5:1f8829bd11ed 62 static void prepareTxFrame (void) {
tamberg 4:f83ad3eee79d 63 debug_str("prepareTxFrame\r\n");
tamberg 6:fbfc95b5c979 64 for (int i = 0; i < LORAWAN_APP_DATA_SIZE; i++) {
tamberg 6:fbfc95b5c979 65 LMIC.frame[i] = i;
tamberg 6:fbfc95b5c979 66 }
tamberg 6:fbfc95b5c979 67 // LMIC.frame[0] = 0;//AppLedStateOn;
tamberg 6:fbfc95b5c979 68 // LMIC.frame[1] = 1;//LMIC.seqnoDn >> 8;
tamberg 6:fbfc95b5c979 69 // LMIC.frame[2] = 2;//LMIC.seqnoDn;
tamberg 6:fbfc95b5c979 70 // LMIC.frame[3] = 3;//LMIC.rssi >> 8;
tamberg 6:fbfc95b5c979 71 // LMIC.frame[4] = 4;//LMIC.rssi;
tamberg 6:fbfc95b5c979 72 // LMIC.frame[5] = 5;//LMIC.snr;
mluis 0:a2929fa6e4f0 73 }
mluis 0:a2929fa6e4f0 74
tamberg 5:1f8829bd11ed 75 void processRxFrame (void) {
tamberg 4:f83ad3eee79d 76 debug_str("processRxFrame\r\n");
tamberg 6:fbfc95b5c979 77 u1_t rxPort = LMIC.frame[LMIC.dataBeg - 1];
tamberg 6:fbfc95b5c979 78 switch(rxPort) {
mluis 0:a2929fa6e4f0 79 case 1: // The application LED can be controlled on port 1 or 2
mluis 0:a2929fa6e4f0 80 case 2:
tamberg 5:1f8829bd11ed 81 if(LMIC.dataLen == 1) {
mluis 0:a2929fa6e4f0 82 AppLedStateOn = LMIC.frame[LMIC.dataBeg] & 0x01;
tamberg 5:1f8829bd11ed 83 debug_val("LED3 = ", AppLedStateOn);
mluis 0:a2929fa6e4f0 84 }
mluis 0:a2929fa6e4f0 85 break;
mluis 0:a2929fa6e4f0 86 default:
mluis 0:a2929fa6e4f0 87 break;
mluis 0:a2929fa6e4f0 88 }
mluis 0:a2929fa6e4f0 89 }
mluis 0:a2929fa6e4f0 90
tamberg 5:1f8829bd11ed 91 static void onSendFrame (osjob_t* j) {
tamberg 4:f83ad3eee79d 92 debug_str("onSendFrame\r\n");
tamberg 5:1f8829bd11ed 93 prepareTxFrame();
tamberg 6:fbfc95b5c979 94 // int LMIC_setTxData2 (u1_t port, xref2u1_t data, u1_t dlen, u1_t confirmed);
tamberg 6:fbfc95b5c979 95 debug_str("LMIC_setTxData2\r\n");
tamberg 6:fbfc95b5c979 96 int result = LMIC_setTxData2(
tamberg 5:1f8829bd11ed 97 LORAWAN_APP_PORT,
tamberg 5:1f8829bd11ed 98 LMIC.frame,
tamberg 5:1f8829bd11ed 99 LORAWAN_APP_DATA_SIZE,
tamberg 5:1f8829bd11ed 100 LORAWAN_CONFIRMED_MSG_ON);
tamberg 6:fbfc95b5c979 101 debug_val("result = ", result);
mluis 0:a2929fa6e4f0 102 }
mluis 0:a2929fa6e4f0 103
tamberg 5:1f8829bd11ed 104 static void onInit (osjob_t* j) {
tamberg 4:f83ad3eee79d 105 debug_str("onInit\r\n");
tamberg 5:1f8829bd11ed 106 LMIC_reset(); // reset MAC state
tamberg 5:1f8829bd11ed 107 LMIC_setAdrMode(LORAWAN_ADR_ON);
tamberg 5:1f8829bd11ed 108 LMIC_setDrTxpow(DR_SF12, 14);
tamberg 5:1f8829bd11ed 109 LMIC_setSession(
tamberg 5:1f8829bd11ed 110 LORAWAN_NET_ID,
tamberg 5:1f8829bd11ed 111 LORAWAN_DEV_ADDR,
tamberg 5:1f8829bd11ed 112 NwkSKey,
tamberg 5:1f8829bd11ed 113 ArtSKey);
tamberg 5:1f8829bd11ed 114 onSendFrame(NULL);
tamberg 5:1f8829bd11ed 115 // onEvent() callback will be invoked...
mluis 1:60184eda0066 116 }
mluis 1:60184eda0066 117
tamberg 6:fbfc95b5c979 118 int main (void) {
tamberg 4:f83ad3eee79d 119 debug_str("main\r\n");
mluis 0:a2929fa6e4f0 120 osjob_t initjob;
tamberg 5:1f8829bd11ed 121 os_init();
tamberg 5:1f8829bd11ed 122 os_setCallback(&initjob, onInit);
tamberg 5:1f8829bd11ed 123 os_runloop(); // blocking
mluis 0:a2929fa6e4f0 124 }
mluis 0:a2929fa6e4f0 125
tamberg 6:fbfc95b5c979 126 void onEvent (ev_t ev) { // called by lmic.cpp, see also oslmic.h
tamberg 4:f83ad3eee79d 127 debug_str("onEvent\r\n");
mluis 0:a2929fa6e4f0 128 bool txOn = false;
tamberg 5:1f8829bd11ed 129 debug_event(ev);
tamberg 6:fbfc95b5c979 130 switch (ev) {
tamberg 5:1f8829bd11ed 131 case EV_JOINED: // network joined, session established
tamberg 5:1f8829bd11ed 132 debug_val("Net ID = ", LMIC.netid);
mluis 0:a2929fa6e4f0 133 txOn = true;
mluis 0:a2929fa6e4f0 134 break;
tamberg 5:1f8829bd11ed 135 case EV_TXCOMPLETE: // scheduled data sent (optionally data received)
tamberg 5:1f8829bd11ed 136 debug_val("Datarate = ", LMIC.datarate);
mluis 0:a2929fa6e4f0 137 // Check if we have a downlink on either Rx1 or Rx2 windows
tamberg 5:1f8829bd11ed 138 if ((LMIC.txrxFlags & (TXRX_DNW1 | TXRX_DNW2)) != 0) {
tamberg 6:fbfc95b5c979 139 //debug_val("LED2 = ", 1);
tamberg 6:fbfc95b5c979 140 //os_setTimedCallback(&rxLedJob, os_getTime() + ms2osticks(25), onRxLed);
tamberg 5:1f8829bd11ed 141 if (LMIC.dataLen != 0) { // data received in rx slot after tx
tamberg 5:1f8829bd11ed 142 debug_buf(LMIC.frame + LMIC.dataBeg, LMIC.dataLen);
tamberg 5:1f8829bd11ed 143 processRxFrame();
mluis 0:a2929fa6e4f0 144 }
mluis 0:a2929fa6e4f0 145 }
mluis 0:a2929fa6e4f0 146 txOn = true;
mluis 0:a2929fa6e4f0 147 break;
mluis 0:a2929fa6e4f0 148 default:
mluis 0:a2929fa6e4f0 149 break;
mluis 0:a2929fa6e4f0 150 }
tamberg 5:1f8829bd11ed 151 if (txOn == true) {
tamberg 5:1f8829bd11ed 152 os_setTimedCallback(
tamberg 5:1f8829bd11ed 153 &sendFrameJob,
tamberg 5:1f8829bd11ed 154 os_getTime() + ms2osticks(APP_TX_DUTYCYCLE + randr(-APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND)),
tamberg 5:1f8829bd11ed 155 onSendFrame);
mluis 0:a2929fa6e4f0 156 }
mluis 0:a2929fa6e4f0 157 }