end node on synchronous star LoRa network.

Dependencies:   SX127x sx12xx_hal TSL2561

radio chip selection

Radio chip driver is not included, allowing choice of radio device.
If you're using SX1272 or SX1276, then import sx127x driver into your program.
if you're using SX1261 or SX1262, then import sx126x driver into your program.
if you're using SX1280, then import sx1280 driver into your program.
If you're using NAmote72 or Murata discovery, then you must import only sx127x driver.

This project for use with LoRaWAN_singlechannel_gateway project.

Alternately gateway running on raspberry pi can be used as gateway.

LoRaWAN on single radio channel

Network description is at gateway project page. Synchronous star network.

Hardware Support

This project supports SX1276 and SX1272, sx126x kit, sx126x shield, and sx128x 2.4GHz. The ST board B-L072Z-LRWAN1 is also supported (TypeABZ module). When B-L072Z-LRWAN1 target is selected, TARGET_DISCO_L072CZ_LRWAN1 is defined by tools, allowing correct radio driver configuration for this platform. Alternately, any mbed board that can use LoRa radio shield board should work, but NUCLEO boards are tested.

End-node Unique ID

DevEUI is created from CPU serial number. AppEUI and AppKey are declared as software constants.

End-node Configuration

Data rate definition LORAMAC_DEFAULT_DATARATE configured in LoRaMac-definitions.h. See gateway project page for configuration of gateway.
LoRaWAN addressing is configured in Comissioning.h; only OTA mode is functional.
Header file board/lora_config.h, selects application layer options (i.e. sensors) to be compiled in.

Serial Interface

Serial port operates at 115200bps.
Application layer single_us915_main.cpp User button triggers uplink (i.e. blue button on nucleo board), or jumper enables continuously sends repeated uplink packets. The MAC layer holds each uplink request until the allocated timeslot.

commandargumentsdescription
?-print available commands
. (period)-print status (DevEUI, DevAddr, etc)
ullength integerset payload length of test uplink packets

sensor demo

Selected grove sensors may be plugged into SX1272 shield.
To enable, edit lora_config.h to define SENSORS.

Sensor connections on SX1272MB2xAS:

D8 D9: buttonRX TX: (unused)A3 A4: Rotary Angle Sensor
D6 D7: RGB LEDSCL SDA: digital light sensorA1 A2: Rotary Angle Sensor

Digital input pin, state reported via uplink: PC8
Digital output pin, controlled via downlink: PC6
PWM out: PB_10

Jumper enables auto-repeated transmit: PC10 and PC12 on NUCLEO board, located on end of morpho headers nearby JP4.

Revision:
0:8f0d0ae0a077
Child:
1:53c30224eda8
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/single_us915_main.cpp	Thu May 18 15:11:53 2017 -0700
@@ -0,0 +1,832 @@
+/*
+ / _____)             _              | |
+( (____  _____ ____ _| |_ _____  ____| |__
+ \____ \| ___ |    (_   _) ___ |/ ___)  _ \
+ _____) ) ____| | | || |_| ____( (___| | | |
+(______/|_____)_|_|_| \__)_____)\____)_| |_|
+    (C)2015 Semtech
+
+Description: LoRaMac classA device implementation
+
+License: Revised BSD License, see LICENSE.TXT file include in the project
+
+Maintainer: Miguel Luis and Gregory Cristian
+*/
+#include "mbed.h"
+#include "board.h"
+#include "radio.h"
+
+#include "LoRaMac.h"
+#include "Commissioning.h"
+
+Serial pc( USBTX, USBRX );
+char pcbuf[128];
+int pcbuf_len;
+
+/*!
+ * Defines the application data transmission duty cycle. 5s, value in [ms].
+ */
+#define APP_TX_DUTYCYCLE                            5000
+
+/*!
+ * Defines a random delay for application data transmission duty cycle. 1s,
+ * value in [ms].
+ */
+#define APP_TX_DUTYCYCLE_RND                        1000
+
+/*!
+ * Default datarate
+ */
+#define LORAWAN_DEFAULT_DATARATE                    DR_10
+
+/*!
+ * LoRaWAN confirmed messages
+ */
+#define LORAWAN_CONFIRMED_MSG_ON                    true
+
+/*!
+ * LoRaWAN application port
+ */
+#define LORAWAN_APP_PORT                            15
+
+/*!
+ * User application data buffer size
+ */
+#if ( LORAWAN_CONFIRMED_MSG_ON == 1 )
+#define LORAWAN_APP_DATA_SIZE                       6
+
+#else
+#define LORAWAN_APP_DATA_SIZE                       1
+
+#endif
+
+static uint8_t DevEui[] = LORAWAN_DEVICE_EUI;
+static uint8_t AppEui[] = LORAWAN_APPLICATION_EUI;
+static uint8_t AppKey[] = LORAWAN_APPLICATION_KEY;
+
+#if( OVER_THE_AIR_ACTIVATION == 0 )
+
+static uint8_t NwkSKey[] = LORAWAN_NWKSKEY;
+static uint8_t AppSKey[] = LORAWAN_APPSKEY;
+
+/*!
+ * Device address
+ */
+static uint32_t DevAddr = LORAWAN_DEVICE_ADDRESS;
+
+#endif
+
+/*!
+ * Application port
+ */
+static uint8_t AppPort = LORAWAN_APP_PORT;
+
+/*!
+ * User application data size
+ */
+static uint8_t AppDataSize = LORAWAN_APP_DATA_SIZE;
+static unsigned int uplink_length;   // user assigned
+
+/*!
+ * User application data buffer size
+ */
+#define LORAWAN_APP_DATA_MAX_SIZE                           128
+
+/*!
+ * User application data
+ */
+static uint8_t AppData[LORAWAN_APP_DATA_MAX_SIZE];
+
+/*!
+ * Indicates if the node is sending confirmed or unconfirmed messages
+ */
+static uint8_t IsTxConfirmed = LORAWAN_CONFIRMED_MSG_ON;
+
+/*!
+ * Defines the application data transmission duty cycle
+ */
+static uint32_t TxDutyCycleTime;
+
+/*!
+ * Timer to handle the application data transmission duty cycle
+ */
+LowPowerTimeout TxNextPacketTimer;
+
+volatile bool send_at_beacon;
+volatile bool awaiting_mcps_indic;
+/*!
+ * Specifies the state of the application LED
+ */
+static bool AppLedStateOn = false;
+
+/*!
+ * Indicates if a new packet can be sent
+ */
+static bool NextTx = true;
+
+/*!
+ * Device states
+ */
+static enum eDeviceState
+{
+    DEVICE_STATE_INIT,
+    DEVICE_STATE_JOIN,
+    DEVICE_STATE_SEND,
+    DEVICE_STATE_CYCLE,
+    DEVICE_STATE_SLEEP
+}DeviceState;
+
+/*!
+ * Strucure containing the Uplink status
+ */
+struct sLoRaMacUplinkStatus
+{
+    uint8_t Acked;
+    //int8_t Datarate;
+    uint16_t UplinkCounter;
+    uint8_t Port;
+    uint8_t *Buffer;
+    uint8_t BufferSize;
+}LoRaMacUplinkStatus;
+//volatile bool UplinkStatusUpdated = false;
+
+/*!
+ * Strucure containing the Downlink status
+ */
+struct sLoRaMacDownlinkStatus
+{
+    int16_t Rssi;
+    int8_t Snr;
+    uint16_t DownlinkCounter;
+    bool RxData;
+    uint8_t Port;
+    uint8_t *Buffer;
+    uint8_t BufferSize;
+}LoRaMacDownlinkStatus;
+
+
+/*!
+ * \brief   Prepares the payload of the frame
+ */
+static void PrepareTxFrame( uint8_t port )
+{
+    switch( port )
+    {
+    case 15:
+        {
+            AppData[0] = AppLedStateOn;
+            if( IsTxConfirmed == true )
+            {
+                AppData[1] = LoRaMacDownlinkStatus.DownlinkCounter >> 8;
+                AppData[2] = LoRaMacDownlinkStatus.DownlinkCounter;
+                AppData[3] = LoRaMacDownlinkStatus.Rssi >> 8;
+                AppData[4] = LoRaMacDownlinkStatus.Rssi;
+                AppData[5] = LoRaMacDownlinkStatus.Snr;
+            }
+        }
+        break;
+    default:
+        break;
+    }
+}
+
+void
+LoRaMacEventInfoStatus_to_string(LoRaMacEventInfoStatus_t status, char* dst)
+{
+    const char* ptr = NULL;
+
+    switch (status) {
+        case LORAMAC_EVENT_INFO_STATUS_RX_ERROR: ptr = "RX_ERROR"; break;
+        case LORAMAC_EVENT_INFO_STATUS_OK: ptr = "OK"; break;
+        case LORAMAC_EVENT_INFO_STATUS_ERROR: ptr = "ERROR"; break;
+        case LORAMAC_EVENT_INFO_STATUS_TX_TIMEOUT: ptr = "TX_TIMEOUT"; break;
+        case LORAMAC_EVENT_INFO_STATUS_RX_TIMEOUT: ptr = "RX_TIMEOUT"; break;
+        case LORAMAC_EVENT_INFO_STATUS_JOIN_FAIL: ptr = "JOIN_FAIL"; break;
+        case LORAMAC_EVENT_INFO_STATUS_DOWNLINK_REPEATED: ptr = "DOWNLINK_REPEATED"; break;
+        case LORAMAC_EVENT_INFO_STATUS_TX_DR_PAYLOAD_SIZE_ERROR: ptr = "TX_DR_PAYLOAD_SIZE_ERROR"; break;
+        case LORAMAC_EVENT_INFO_STATUS_DOWNLINK_TOO_MANY_FRAMES_LOSS: ptr = "DOWNLINK_TOO_MANY_FRAMES_LOSS"; break;
+        case LORAMAC_EVENT_INFO_STATUS_ADDRESS_FAIL: ptr = "ADDRESS_FAIL"; break;
+        case LORAMAC_EVENT_INFO_STATUS_MIC_FAIL: ptr = "MIC_FAIL"; break;
+        case LORAMAC_EVENT_INFO_STATUS_BEACON_LOCKED: ptr = "BEACON_LOCKED"; break;
+        case LORAMAC_EVENT_INFO_STATUS_BEACON_LOST: ptr = "BEACON_LOST"; break;
+        case LORAMAC_EVENT_INFO_STATUS_BEACON_NOT_FOUND: ptr = "BEACON_NOT_FOUND"; break;
+    }
+
+    if (ptr != NULL)
+        strcpy(dst, ptr);
+}
+
+void
+LoRaMacStatus_to_string(LoRaMacStatus_t status, char* dst)
+{
+    const char* ptr = NULL;
+
+    switch (status) {
+        case LORAMAC_STATUS_OK: ptr = "OK"; break;
+        case LORAMAC_STATUS_BUSY: ptr = "BUSY"; break;
+        case LORAMAC_STATUS_SERVICE_UNKNOWN: ptr = "SERVICE_UNKNOWN"; break;
+        case LORAMAC_STATUS_PARAMETER_INVALID: ptr = "PARAMETER_INVALID"; break;
+        case LORAMAC_STATUS_FREQUENCY_INVALID: ptr = "FREQUENCY_INVALID"; break;
+        case LORAMAC_STATUS_DATARATE_INVALID: ptr = "DATARATE_INVALID"; break;
+        case LORAMAC_STATUS_FREQ_AND_DR_INVALID: ptr = "FREQ_AND_DR_INVALID"; break;
+        case LORAMAC_STATUS_NO_NETWORK_JOINED: ptr = "NO_NETWORK_JOINED"; break;
+        case LORAMAC_STATUS_LENGTH_ERROR: ptr = "LENGTH_ERROR"; break;
+        case LORAMAC_STATUS_MAC_CMD_LENGTH_ERROR: ptr = "MAC_CMD_LENGTH_ERROR"; break;
+        case LORAMAC_STATUS_DEVICE_OFF: ptr = "DEVICE_OFF"; break;
+    }
+    if (ptr != NULL)
+        strcpy(dst, ptr);
+}
+
+/*!
+ * \brief   Prepares the payload of the frame
+ *
+ * \retval  [0: frame could be send, 1: error]
+ */
+static bool SendFrame( void )
+{
+    McpsReq_t mcpsReq;
+    LoRaMacTxInfo_t txInfo;
+    LoRaMacStatus_t status;
+    char str[64];
+
+    if( LoRaMacQueryTxPossible( AppDataSize, &txInfo ) != LORAMAC_STATUS_OK )
+    {
+        // Send empty frame in order to flush MAC commands
+        mcpsReq.Type = MCPS_UNCONFIRMED;
+        mcpsReq.Req.Unconfirmed.fBuffer = NULL;
+        mcpsReq.Req.Unconfirmed.fBufferSize = 0;
+        //mcpsReq.Req.Unconfirmed.Datarate = LORAWAN_DEFAULT_DATARATE;
+
+        LoRaMacUplinkStatus.Acked = false;
+        LoRaMacUplinkStatus.Port = 0;
+        LoRaMacUplinkStatus.Buffer = NULL;
+        LoRaMacUplinkStatus.BufferSize = 0;
+    }
+    else
+    {
+        LoRaMacUplinkStatus.Acked = false;
+        LoRaMacUplinkStatus.Port = AppPort;
+        LoRaMacUplinkStatus.Buffer = AppData;
+        LoRaMacUplinkStatus.BufferSize = AppDataSize;
+
+        if( IsTxConfirmed == false )
+        {
+            mcpsReq.Type = MCPS_UNCONFIRMED;
+            mcpsReq.Req.Unconfirmed.fPort = AppPort;
+            mcpsReq.Req.Unconfirmed.fBuffer = AppData;
+            mcpsReq.Req.Unconfirmed.fBufferSize = AppDataSize;
+            //mcpsReq.Req.Unconfirmed.Datarate = LORAWAN_DEFAULT_DATARATE;
+        }
+        else
+        {
+            mcpsReq.Type = MCPS_CONFIRMED;
+            mcpsReq.Req.Confirmed.fPort = AppPort;
+            mcpsReq.Req.Confirmed.fBuffer = AppData;
+            mcpsReq.Req.Confirmed.fBufferSize = AppDataSize;
+            mcpsReq.Req.Confirmed.NbTrials = 8;
+            //mcpsReq.Req.Confirmed.Datarate = LORAWAN_DEFAULT_DATARATE;
+        }
+    }
+
+    status = LoRaMacMcpsRequest( &mcpsReq );
+    if( status == LORAMAC_STATUS_OK )
+    {
+        awaiting_mcps_indic = true;
+        return false;
+    }
+    LoRaMacStatus_to_string(status, str);
+    printf("send failed:%s\r\n", str);
+    send_at_beacon = true;
+    return true;
+}
+
+/*!
+ * \brief Function executed on TxNextPacket Timeout event
+ */
+static void OnTxNextPacketTimerEvent( void )
+{
+    MibRequestConfirm_t mibReq;
+    LoRaMacStatus_t status;
+
+    mibReq.Type = MIB_NETWORK_JOINED;
+    status = LoRaMacMibGetRequestConfirm( &mibReq );
+
+    if( status == LORAMAC_STATUS_OK )
+    {
+        if( mibReq.Param.IsNetworkJoined == true )
+        {
+            DeviceState = DEVICE_STATE_SEND;
+            NextTx = true;
+        }
+        else
+        {
+            DeviceState = DEVICE_STATE_JOIN;
+        }
+    }
+}
+
+void
+send_uplink()
+{
+    AppDataSize = uplink_length;
+    SendFrame( );
+}
+
+
+/*!
+ * \brief   MCPS-Confirm event function
+ *
+ * \param   [IN] mcpsConfirm - Pointer to the confirm structure,
+ *               containing confirm attributes.
+ */
+static void McpsConfirm( McpsConfirm_t *mcpsConfirm )
+{
+    char str[64];
+    printf("McpsConfirm ");
+    if( mcpsConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK )
+    {
+        printf("OK ");
+        switch( mcpsConfirm->McpsRequest )
+        {
+            case MCPS_UNCONFIRMED:
+            {
+                printf("UNCONFIRMED");
+                // Check Datarate
+                // Check TxPower
+                break;
+            }
+            case MCPS_CONFIRMED:
+            {
+                printf("CONFIRMED");
+                // Check Datarate
+                // Check TxPower
+                // Check AckReceived
+                // Check NbTrials
+                LoRaMacUplinkStatus.Acked = mcpsConfirm->AckReceived;
+                break;
+            }
+            case MCPS_PROPRIETARY:
+            {
+                break;
+            }
+            default:
+                break;
+        }
+        //LoRaMacUplinkStatus.Datarate = mcpsConfirm->Datarate;
+        LoRaMacUplinkStatus.UplinkCounter = mcpsConfirm->UpLinkCounter;
+
+    } else {
+        LoRaMacEventInfoStatus_to_string(mcpsConfirm->Status, str);
+        printf("%s ", str);
+
+#ifndef MANUAL_UPLINK
+        /* mcpsIndication may not come. last uplink done, send another uplink */
+        TxNextPacketTimer.attach_us(&send_uplink, 100000);
+#endif /* !MANUAL_UPLINK */
+    }
+
+    NextTx = true;
+    printf("\r\n");
+}
+
+/*!
+ * \brief   MCPS-Indication event function
+ *
+ * \param   [IN] mcpsIndication - Pointer to the indication structure,
+ *               containing indication attributes.
+ */
+static void McpsIndication( McpsIndication_t *mcpsIndication )
+{
+    printf("McpsIndication ");
+
+#ifndef MANUAL_UPLINK
+    /* last uplink done, send another uplink */
+    TxNextPacketTimer.attach_us(&send_uplink, 100000);
+#endif /* !MANUAL_UPLINK */
+
+    if( mcpsIndication->Status != LORAMAC_EVENT_INFO_STATUS_OK )
+    {
+        printf("\r\n");
+        return;
+    }
+
+    awaiting_mcps_indic = false;
+
+    switch( mcpsIndication->McpsIndication )
+    {
+        case MCPS_UNCONFIRMED:
+        {
+            printf("UNCONFIRMED ");
+            break;
+        }
+        case MCPS_CONFIRMED:
+        {
+            printf("CONFIRMED ");
+            break;
+        }
+        case MCPS_PROPRIETARY:
+        {
+            break;
+        }
+        case MCPS_MULTICAST:
+        {
+            break;
+        }
+        default:
+            break;
+    }
+
+    // Check Multicast
+    // Check Port
+    // Check Datarate
+    // Check FramePending
+    // Check Buffer
+    // Check BufferSize
+    // Check Rssi
+    // Check Snr
+    // Check RxSlot
+    LoRaMacDownlinkStatus.Rssi = mcpsIndication->Rssi;
+    if( mcpsIndication->Snr & 0x80 ) // The SNR sign bit is 1
+    {
+        // Invert and divide by 4
+        LoRaMacDownlinkStatus.Snr = ( ( ~mcpsIndication->Snr + 1 ) & 0xFF ) >> 2;
+        LoRaMacDownlinkStatus.Snr = -LoRaMacDownlinkStatus.Snr;
+    }
+    else
+    {
+        // Divide by 4
+        LoRaMacDownlinkStatus.Snr = ( mcpsIndication->Snr & 0xFF ) >> 2;
+    }
+    LoRaMacDownlinkStatus.DownlinkCounter++;
+    LoRaMacDownlinkStatus.RxData = mcpsIndication->RxData;
+    LoRaMacDownlinkStatus.Port = mcpsIndication->Port;
+    LoRaMacDownlinkStatus.Buffer = mcpsIndication->Buffer;
+    LoRaMacDownlinkStatus.BufferSize = mcpsIndication->BufferSize;
+
+    if( mcpsIndication->RxData == true )
+    {
+        int i;
+        printf("RxData %u ", mcpsIndication->BufferSize);
+        for (i = 0; i < mcpsIndication->BufferSize; i++) {
+            printf("%02x ", mcpsIndication->Buffer[i]);
+        }
+        printf("\r\n");
+
+        switch( mcpsIndication->Port )
+        {
+        case 1: // The application LED can be controlled on port 1 or 2
+        case 2:
+            if( mcpsIndication->BufferSize == 1 )
+            {
+                AppLedStateOn = mcpsIndication->Buffer[0] & 0x01;
+                //Led3StateChanged = true;
+            }
+            break;
+        default:
+            break;
+        }
+    }
+
+    printf("\r\n");
+}
+
+/*!
+ * \brief   MLME-Confirm event function
+ *
+ * \param   [IN] mlmeConfirm - Pointer to the confirm structure,
+ *               containing confirm attributes.
+ */
+static void MlmeConfirm( MlmeConfirm_t *mlmeConfirm )
+{
+    printf("MlmeConfirm ");
+    switch( mlmeConfirm->MlmeRequest )
+    {
+        case MLME_JOIN:
+        {
+            printf("MLME_JOIN ");
+            if( mlmeConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK )
+            {
+                // Status is OK, node has joined the network
+                //DeviceState = DEVICE_STATE_SEND;
+                DeviceState = DEVICE_STATE_SLEEP;
+                printf("ok");
+            }
+            else
+            {
+                // Join was not successful. Try to join again
+                DeviceState = DEVICE_STATE_JOIN;
+                printf("fail");
+            }
+            break;
+        }
+        case MLME_LINK_CHECK:
+        {
+            printf("LINK_CHECK");
+            if( mlmeConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK )
+            {
+                // Check DemodMargin
+                //mlmeConfirm->DemodMargin;
+                // Check NbGateways
+                //mlmeConfirm->NbGateways;
+            }
+            break;
+        }
+        default:
+            break;
+    }
+    NextTx = true;
+    printf("\r\n");
+}
+
+
+static void MlmeIndication( MlmeIndication_t *MlmeIndication )
+{
+    char str[64];
+    //MibRequestConfirm_t mibReq;
+
+    printf("MlmeIndication ");
+    switch( MlmeIndication->MlmeIndication )
+    {
+        case MLME_BEACON:
+        {
+            printf("send_at_beacon%u BEACON ", send_at_beacon);
+            LoRaMacEventInfoStatus_to_string(MlmeIndication->Status, str);
+            printf("%s ", str);
+#ifndef MANUAL_UPLINK
+            if (send_at_beacon) {
+                TxNextPacketTimer.attach_us(&send_uplink, 100000);
+                send_at_beacon = false;
+            }
+
+#endif /* !MANUAL_UPLINK */
+            break;
+
+        }
+        default:
+            printf("<%d> ", MlmeIndication->MlmeIndication);
+            break;
+    }
+
+    printf("\r\n");
+}
+
+void
+send_pcbuf_uplink()
+{
+    bool ret;
+    memcpy(AppData, pcbuf, pcbuf_len);
+    AppDataSize = pcbuf_len;
+
+    ret = SendFrame( );
+    printf("%d = SendFrame()\r\n", ret);
+}
+
+void cmd_status(uint8_t idx)
+{
+    printf("DeviceState:%d\r\n", DeviceState);
+    printf("send_at_beacon:%d\r\n", send_at_beacon);
+    printf("awaiting_mcps_indic:%d\r\n", awaiting_mcps_indic);
+    loramac_print_status();
+}
+
+void cmd_uplink_length(uint8_t idx)
+{
+    if (pcbuf[idx] >= '0' && pcbuf[idx] <= '9') {
+        sscanf(pcbuf+idx, "%u", &uplink_length);
+    }
+    printf("uplink_length:%u\r\n", uplink_length);
+}
+
+typedef struct {
+    const char* const cmd;
+    void (*handler)(uint8_t args_at);
+    const char* const arg_descr;
+    const char* const description;
+} menu_item_t;
+
+void cmd_help(uint8_t args_at);
+
+const menu_item_t menu_items[] = 
+{   /* after first character, command names must be [A-Za-z] */
+    { "?", cmd_help, "","show available commands"},
+    { ".", cmd_status, "","print status"}, 
+    { "ul", cmd_uplink_length, "","set uplink payload length"}, 
+    { NULL, NULL, NULL, NULL }
+};
+
+void cmd_help(uint8_t args_at)
+{
+    int i;
+    
+    for (i = 0; menu_items[i].cmd != NULL ; i++) {
+        printf("%s%s\t%s\r\n", menu_items[i].cmd, menu_items[i].arg_descr, menu_items[i].description);
+    }
+    
+}
+
+void
+console()
+{
+    bool parsed;
+    int i;
+    uint8_t user_cmd_len;
+    
+    if (pcbuf_len < 0) {    // ctrl-C
+        //printf("abort\r\n");
+        return;
+    }
+    if (pcbuf_len == 0)
+        return;
+        
+    printf("\r\n");
+        
+    /* get end of user-entered command */
+    user_cmd_len = 1;   // first character can be any character
+    for (i = 1; i <= pcbuf_len; i++) {
+        if (pcbuf[i] < 'A' || (pcbuf[i] > 'Z' && pcbuf[i] < 'a') || pcbuf[i] > 'z') {
+            user_cmd_len = i;
+            break;
+        }
+    }
+
+    parsed = false;
+    for (i = 0; menu_items[i].cmd != NULL ; i++) {
+        int mi_len = strlen(menu_items[i].cmd);
+
+        if (menu_items[i].handler && user_cmd_len == mi_len && (strncmp(pcbuf, menu_items[i].cmd, mi_len) == 0)) {
+            while (pcbuf[mi_len] == ' ')   // skip past spaces
+                mi_len++;
+            menu_items[i].handler(mi_len);
+            parsed = true;
+            break;
+        }
+    }
+
+    if (!parsed)
+        send_pcbuf_uplink();
+   
+    pcbuf_len = 0;
+    printf("> ");
+    fflush(stdout); 
+}
+
+void rx_callback()
+{
+    static uint8_t pcbuf_idx = 0;
+    static uint8_t prev_len = 0;;
+    char c = pc.getc();
+
+    if (c == 8) {
+        if (pcbuf_idx > 0) {
+            pc.putc(8);
+            pc.putc(' ');
+            pc.putc(8);
+            pcbuf_idx--;
+        }
+    } else if (c == 3) {    // ctrl-C
+        pcbuf_len = -1;
+    } else if (c == '\r') {
+        if (pcbuf_idx == 0) {
+            pcbuf_len = prev_len;
+        } else {
+            pcbuf[pcbuf_idx] = 0;   // null terminate
+            prev_len = pcbuf_idx;
+            pcbuf_idx = 0;
+            pcbuf_len = prev_len;
+        }
+    } else if (pcbuf_idx < sizeof(pcbuf)) {
+        pcbuf[pcbuf_idx++] = c;
+        pc.putc(c);
+    }
+}
+
+/**
+ * Main application entry point.
+ */
+int main( void )
+{
+    LoRaMacPrimitives_t LoRaMacPrimitives;
+    LoRaMacCallback_t LoRaMacCallbacks;
+    MibRequestConfirm_t mibReq;
+
+    BoardInit( );
+    pc.baud(115200);
+    printf("\r\nreset\r\n");
+    pc.attach(rx_callback);
+    DeviceState = DEVICE_STATE_INIT;
+
+    while( 1 )
+    {
+        console();
+
+        switch( DeviceState )
+        {
+            case DEVICE_STATE_INIT:
+            {
+                printf("DEVICE_STATE_INIT\r\n");
+                uplink_length = 2;
+                LoRaMacPrimitives.MacMcpsConfirm = McpsConfirm;
+                LoRaMacPrimitives.MacMcpsIndication = McpsIndication;
+                LoRaMacPrimitives.MacMlmeConfirm = MlmeConfirm;
+                LoRaMacPrimitives.MacMlmeIndication = MlmeIndication;
+                LoRaMacCallbacks.GetBatteryLevel = BoardGetBatteryLevel;
+                if (LORAMAC_STATUS_OK != LoRaMacInitialization( &LoRaMacPrimitives, &LoRaMacCallbacks )) {
+                    printf("LoRaMacInitialization() failed\r\n");
+                    for (;;) ;
+                }
+                printf("INIT-ok\r\n");
+
+                mibReq.Type = MIB_PUBLIC_NETWORK;
+                mibReq.Param.EnablePublicNetwork = LORAWAN_PUBLIC_NETWORK;
+                LoRaMacMibSetRequestConfirm( &mibReq );
+
+                LoRaMacDownlinkStatus.DownlinkCounter = 0;
+
+                DeviceState = DEVICE_STATE_JOIN;
+                break;
+            }
+            case DEVICE_STATE_JOIN:
+            {
+                send_at_beacon = true;
+#if( OVER_THE_AIR_ACTIVATION != 0 )
+                MlmeReq_t mlmeReq;
+                // override software definition with hardware value
+                BoardGetUniqueId(DevEui);
+                printf("DevEUI ");
+                for (int i = 0; i < 8; i++)
+                    printf("%02x ", DevEui[i]);
+                printf("\r\n");
+
+                mlmeReq.Type = MLME_JOIN;
+
+                mlmeReq.Req.Join.DevEui = DevEui;
+                mlmeReq.Req.Join.AppEui = AppEui;
+                mlmeReq.Req.Join.AppKey = AppKey;
+                mlmeReq.Req.Join.NbTrials = 255;
+
+                if( NextTx == true )
+                {
+                    LoRaMacMlmeRequest( &mlmeReq );
+                }
+                DeviceState = DEVICE_STATE_SLEEP;
+#else
+                mibReq.Type = MIB_NET_ID;
+                mibReq.Param.NetID = LORAWAN_NETWORK_ID;
+                LoRaMacMibSetRequestConfirm( &mibReq );
+
+                mibReq.Type = MIB_DEV_ADDR;
+                mibReq.Param.DevAddr = DevAddr;
+                LoRaMacMibSetRequestConfirm( &mibReq );
+
+                mibReq.Type = MIB_NWK_SKEY;
+                mibReq.Param.NwkSKey = NwkSKey;
+                LoRaMacMibSetRequestConfirm( &mibReq );
+
+                mibReq.Type = MIB_APP_SKEY;
+                mibReq.Param.AppSKey = AppSKey;
+                LoRaMacMibSetRequestConfirm( &mibReq );
+
+                mibReq.Type = MIB_NETWORK_JOINED;
+                mibReq.Param.IsNetworkJoined = true;
+                LoRaMacMibSetRequestConfirm( &mibReq );
+
+                DeviceState = DEVICE_STATE_SEND;
+#endif
+                break;
+            }
+            case DEVICE_STATE_SEND:
+            {
+                if( NextTx == true )
+                {
+                    PrepareTxFrame( AppPort );
+
+                    NextTx = SendFrame( );
+                }
+                // Schedule next packet transmission
+                TxDutyCycleTime = APP_TX_DUTYCYCLE + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND );
+                DeviceState = DEVICE_STATE_CYCLE;
+                break;
+            }
+            case DEVICE_STATE_CYCLE:
+            {
+                DeviceState = DEVICE_STATE_SLEEP;
+
+                // Schedule next packet transmission
+                TxNextPacketTimer.attach_us(&OnTxNextPacketTimerEvent, TxDutyCycleTime * 1000);
+                break;
+            }
+            case DEVICE_STATE_SLEEP:
+            {
+                //printf("sleep %u\r\n", queue.tick());
+                // Wake up through events
+                break;
+            }
+            default:
+            {
+                DeviceState = DEVICE_STATE_INIT;
+                break;
+            }
+        }
+    }
+}