Temperature reading using NUCLEO-L152RE microcontroller and Grove – Temperature&Humidity Sensor Pro.

Dependencies:   DHT LMiC SX1276Lib mbed

Fork of LoRaWAN_send_text by Thomas Amberg

main.cpp

Committer:
tamberg
Date:
2015-09-14
Revision:
7:4adfa7248a0b
Parent:
6:fbfc95b5c979
Child:
8:747796516a2f

File content as of revision 7:4adfa7248a0b:

/* License: Revised BSD License, see LICENSE.TXT, (c)2015 tamberg.org, (c)2015 Semtech */

#include "mbed.h"
#include "lmic.h"
#include "debug.h"

#define LORAWAN_NET_ID (uint32_t) 0x00000000
#define LORAWAN_DEV_ADDR (uint32_t) 0x00001056
#define APP_TX_DUTYCYCLE 5000 // ms
#define APP_TX_DUTYCYCLE_RND 1000 // ms
#define LORAWAN_ADR_ON 1
#define LORAWAN_CONFIRMED_MSG_ON 1
#define LORAWAN_APP_PORT 15
#define LORAWAN_APP_DATA_SIZE 32 //6 // max 51

static uint8_t NwkSKey[] = {
    0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
    0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
};

static uint8_t ArtSKey[] = {
    0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
    0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
};

osjob_t sendFrameJob;

int32_t randr (int32_t min, int32_t max) {
    return (int32_t) rand() % (max - min + 1) + min;
}

void os_getArtEui (uint8_t *buf) {} // ignore
void os_getDevEui (uint8_t *buf) {} // ignore
void os_getDevKey (uint8_t *buf) {} // ignore

static void prepareTxFrame (void) {
    debug_str("prepareTxFrame\r\n");
    for (int i = 0; i < LORAWAN_APP_DATA_SIZE; i++) {
        LMIC.frame[i] = i;
    }
//    LMIC.frame[0] = AppLedStateOn;
//    LMIC.frame[1] = LMIC.seqnoDn >> 8;
//    LMIC.frame[2] = LMIC.seqnoDn;
//    LMIC.frame[3] = LMIC.rssi >> 8;
//    LMIC.frame[4] = LMIC.rssi;
//    LMIC.frame[5] = LMIC.snr;
}

void processRxFrame (void) {
    debug_str("processRxFrame\r\n");
    u1_t rxPort = LMIC.frame[LMIC.dataBeg - 1];
    debug_val("rxPort", rxPort);
    for (int i = 0; i < LMIC.dataLen; i++) {
        debug_hex(LMIC.frame[LMIC.dataBeg + i]);
        debug_char(' ');    
    }
}

static void onSendFrame (osjob_t* j) {
    debug_str("onSendFrame\r\n");
    prepareTxFrame();
    int result = LMIC_setTxData2(
        LORAWAN_APP_PORT, 
        LMIC.frame, 
        LORAWAN_APP_DATA_SIZE, 
        LORAWAN_CONFIRMED_MSG_ON); // calls onEvent()
    debug_val("LMIC_setTxData2, result = ", result);
}

static void onInit (osjob_t* j) {
    debug_str("onInit\r\n");
    LMIC_reset(); // reset MAC state
    LMIC_setAdrMode(LORAWAN_ADR_ON);
    LMIC_setDrTxpow(DR_SF12, 14);
    LMIC_setSession(
        LORAWAN_NET_ID, 
        LORAWAN_DEV_ADDR, 
        NwkSKey, 
        ArtSKey);
    onSendFrame(NULL);
}

int main (void) {
    debug_str("main\r\n");
    osjob_t initjob;
    os_init();
    os_setCallback(&initjob, onInit);
    os_runloop(); // blocking
}

void onEvent (ev_t ev) { // called by lmic.cpp, see also oslmic.h
    debug_str("onEvent\r\n");
    bool txOn = false;
    debug_event(ev);
    switch (ev) {
    case EV_JOINED: // network joined, session established
        debug_val("Net ID = ", LMIC.netid);
        txOn = true;
        break;
    case EV_TXCOMPLETE: // scheduled data sent (optionally data received)
        debug_val("Datarate = ", LMIC.datarate);
        // Check if we have a downlink on either Rx1 or Rx2 windows
        if ((LMIC.txrxFlags & (TXRX_DNW1 | TXRX_DNW2)) != 0) {
            //debug_val("LED2 = ", 1);
            //os_setTimedCallback(&rxLedJob, os_getTime() + ms2osticks(25), onRxLed);
            if (LMIC.dataLen != 0) { // data received in rx slot after tx
                debug_buf(LMIC.frame + LMIC.dataBeg, LMIC.dataLen);
                processRxFrame();
            }
        }
        txOn = true;
        break;
    default:
        break;
    }
    if (txOn == true) {
        os_setTimedCallback(
            &sendFrameJob,
            os_getTime() + ms2osticks(APP_TX_DUTYCYCLE + randr(-APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND)),
            onSendFrame);
    }
}