![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Final version of the exercise on the Things Network
Dependencies: X_NUCLEO_IKS01A2
Revision 51:9bb9a0374572, committed 2019-03-14
- Comitter:
- marcozecchini
- Date:
- Thu Mar 14 19:44:07 2019 +0000
- Parent:
- 50:5293a23c7ba9
- Commit message:
- Final version
Changed in this revision
--- a/DummySensor.h Tue Mar 12 13:02:18 2019 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,47 +0,0 @@ -/** - * Copyright (c) 2017, Arm Limited and affiliates. - * SPDX-License-Identifier: Apache-2.0 - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef MBED_LORAWAN_DUMMYSENSOR_H_ -#define MBED_LORAWAN_DUMMYSENSOR_H_ - -/* - * A dummy sensor for Mbed LoRa Test Application - */ -class DS1820 { -public: - DS1820(uint32_t) - { - value = 1.0f; - }; - bool begin() - { - return true; - }; - void startConversion() {}; - float read() - { - value += 1.1f; - return value; - } - -private: - float value; -}; - - - -#endif /* MBED_LORAWAN_DUMMYSENSOR_H_ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/X_NUCLEO_IKS01A2.lib Thu Mar 14 19:44:07 2019 +0000 @@ -0,0 +1,1 @@ +http://os.mbed.com/teams/ST/code/X_NUCLEO_IKS01A2/#138a7a28bd21
--- a/main.cpp Tue Mar 12 13:02:18 2019 +0000 +++ b/main.cpp Thu Mar 14 19:44:07 2019 +0000 @@ -1,270 +1,286 @@ -/** - * Copyright (c) 2017, Arm Limited and affiliates. - * SPDX-License-Identifier: Apache-2.0 - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#include <stdio.h> - -#include "lorawan/LoRaWANInterface.h" -#include "lorawan/system/lorawan_data_structures.h" -#include "events/EventQueue.h" - -// Application helpers -#include "DummySensor.h" -#include "trace_helper.h" -#include "lora_radio_helper.h" - -using namespace events; - -// Max payload size can be LORAMAC_PHY_MAXPAYLOAD. -// This example only communicates with much shorter messages (<30 bytes). -// If longer messages are used, these buffers must be changed accordingly. -uint8_t tx_buffer[30]; -uint8_t rx_buffer[30]; - -/* - * Sets up an application dependent transmission timer in ms. Used only when Duty Cycling is off for testing - */ -#define TX_TIMER 10000 - -/** - * Maximum number of events for the event queue. - * 10 is the safe number for the stack events, however, if application - * also uses the queue for whatever purposes, this number should be increased. - */ -#define MAX_NUMBER_OF_EVENTS 10 - -/** - * Maximum number of retries for CONFIRMED messages before giving up - */ -#define CONFIRMED_MSG_RETRY_COUNTER 3 - -/** - * Dummy pin for dummy sensor - */ -#define PC_9 0 - -/** - * Dummy sensor class object - */ -DS1820 ds1820(PC_9); - -/** -* This event queue is the global event queue for both the -* application and stack. To conserve memory, the stack is designed to run -* in the same thread as the application and the application is responsible for -* providing an event queue to the stack that will be used for ISR deferment as -* well as application information event queuing. -*/ -static EventQueue ev_queue(MAX_NUMBER_OF_EVENTS *EVENTS_EVENT_SIZE); - -/** - * Event handler. - * - * This will be passed to the LoRaWAN stack to queue events for the - * application which in turn drive the application. - */ -static void lora_event_handler(lorawan_event_t event); - -/** - * Constructing Mbed LoRaWANInterface and passing it the radio object from lora_radio_helper. - */ -static LoRaWANInterface lorawan(radio); - -/** - * Application specific callbacks - */ -static lorawan_app_callbacks_t callbacks; - -/** - * Entry point for application - */ -int main(void) -{ - // setup tracing - setup_trace(); - - // stores the status of a call to LoRaWAN protocol - lorawan_status_t retcode; - - // Initialize LoRaWAN stack - if (lorawan.initialize(&ev_queue) != LORAWAN_STATUS_OK) { - printf("\r\n LoRa initialization failed! \r\n"); - return -1; - } - - printf("\r\n Mbed LoRaWANStack initialized \r\n"); - - // prepare application callbacks - callbacks.events = mbed::callback(lora_event_handler); - lorawan.add_app_callbacks(&callbacks); - - // Set number of retries in case of CONFIRMED messages - if (lorawan.set_confirmed_msg_retries(CONFIRMED_MSG_RETRY_COUNTER) - != LORAWAN_STATUS_OK) { - printf("\r\n set_confirmed_msg_retries failed! \r\n\r\n"); - return -1; - } - - printf("\r\n CONFIRMED message retries : %d \r\n", - CONFIRMED_MSG_RETRY_COUNTER); - - // Enable adaptive data rate - if (lorawan.enable_adaptive_datarate() != LORAWAN_STATUS_OK) { - printf("\r\n enable_adaptive_datarate failed! \r\n"); - return -1; - } - - printf("\r\n Adaptive data rate (ADR) - Enabled \r\n"); - - retcode = lorawan.connect(); - - if (retcode == LORAWAN_STATUS_OK || - retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) { - } else { - printf("\r\n Connection error, code = %d \r\n", retcode); - return -1; - } - - printf("\r\n Connection - In Progress ...\r\n"); - - // make your event queue dispatching events forever - ev_queue.dispatch_forever(); - - return 0; -} - -/** - * Sends a message to the Network Server - */ -static void send_message() -{ - uint16_t packet_len; - int16_t retcode; - float sensor_value; - - if (ds1820.begin()) { - ds1820.startConversion(); - sensor_value = ds1820.read(); - printf("\r\n Dummy Sensor Value = %3.1f \r\n", sensor_value); - ds1820.startConversion(); - } else { - printf("\r\n No sensor found \r\n"); - return; - } - - packet_len = sprintf((char *) tx_buffer, "Dummy Sensor Value is %3.1f", - sensor_value); - - retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, tx_buffer, packet_len, - MSG_UNCONFIRMED_FLAG); - - if (retcode < 0) { - retcode == LORAWAN_STATUS_WOULD_BLOCK ? printf("send - WOULD BLOCK\r\n") - : printf("\r\n send() - Error code %d \r\n", retcode); - - if (retcode == LORAWAN_STATUS_WOULD_BLOCK) { - //retry in 3 seconds - if (MBED_CONF_LORA_DUTY_CYCLE_ON) { - ev_queue.call_in(3000, send_message); - } - } - return; - } - - printf("\r\n %d bytes scheduled for transmission \r\n", retcode); - memset(tx_buffer, 0, sizeof(tx_buffer)); -} - -/** - * Receive a message from the Network Server - */ -static void receive_message() -{ - uint8_t port; - int flags; - int16_t retcode = lorawan.receive(rx_buffer, sizeof(rx_buffer), port, flags); - - if (retcode < 0) { - printf("\r\n receive() - Error code %d \r\n", retcode); - return; - } - - printf(" RX Data on port %u (%d bytes): ", port, retcode); - for (uint8_t i = 0; i < retcode; i++) { - printf("%02x ", rx_buffer[i]); - } - printf("\r\n"); - - memset(rx_buffer, 0, sizeof(rx_buffer)); -} - -/** - * Event handler - */ -static void lora_event_handler(lorawan_event_t event) -{ - switch (event) { - case CONNECTED: - printf("\r\n Connection - Successful \r\n"); - if (MBED_CONF_LORA_DUTY_CYCLE_ON) { - send_message(); - } else { - ev_queue.call_every(TX_TIMER, send_message); - } - - break; - case DISCONNECTED: - ev_queue.break_dispatch(); - printf("\r\n Disconnected Successfully \r\n"); - break; - case TX_DONE: - printf("\r\n Message Sent to Network Server \r\n"); - if (MBED_CONF_LORA_DUTY_CYCLE_ON) { - send_message(); - } - break; - case TX_TIMEOUT: - case TX_ERROR: - case TX_CRYPTO_ERROR: - case TX_SCHEDULING_ERROR: - printf("\r\n Transmission Error - EventCode = %d \r\n", event); - // try again - if (MBED_CONF_LORA_DUTY_CYCLE_ON) { - send_message(); - } - break; - case RX_DONE: - printf("\r\n Received message from Network Server \r\n"); - receive_message(); - break; - case RX_TIMEOUT: - case RX_ERROR: - printf("\r\n Error in reception - Code = %d \r\n", event); - break; - case JOIN_FAILURE: - printf("\r\n OTAA Failed - Check Keys \r\n"); - break; - case UPLINK_REQUIRED: - printf("\r\n Uplink required by NS \r\n"); - if (MBED_CONF_LORA_DUTY_CYCLE_ON) { - send_message(); - } - break; - default: - MBED_ASSERT("Unknown Event"); - } -} - -// EOF +/** + * Copyright (c) 2017, Arm Limited and affiliates. + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include <stdio.h> + +#include "lorawan/LoRaWANInterface.h" +#include "lorawan/system/lorawan_data_structures.h" +#include "events/EventQueue.h" + +// Application helpers +#include "trace_helper.h" +#include "lora_radio_helper.h" + +// Extension Board library +#include "XNucleoIKS01A2.h" + + +using namespace events; + +// Max payload size can be LORAMAC_PHY_MAXPAYLOAD. +// This example only communicates with much shorter messages (<30 bytes). +// If longer messages are used, these buffers must be changed accordingly. +uint8_t tx_buffer[60]; +uint8_t rx_buffer[30]; + +/* + * Sets up an application dependent transmission timer in ms. Used only when Duty Cycling is off for testing + */ +#define TX_TIMER 10000 + +/** + * Maximum number of events for the event queue. + * 10 is the safe number for the stack events, however, if application + * also uses the queue for whatever purposes, this number should be increased. + */ +#define MAX_NUMBER_OF_EVENTS 10 + +/** + * Maximum number of retries for CONFIRMED messages before giving up + */ +#define CONFIRMED_MSG_RETRY_COUNTER 3 + + +/** +* This event queue is the global event queue for both the +* application and stack. To conserve memory, the stack is designed to run +* in the same thread as the application and the application is responsible for +* providing an event queue to the stack that will be used for ISR deferment as +* well as application information event queuing. +*/ +static EventQueue ev_queue(MAX_NUMBER_OF_EVENTS *EVENTS_EVENT_SIZE); + +/** + * Event handler. + * + * This will be passed to the LoRaWAN stack to queue events for the + * application which in turn drive the application. + */ +static void lora_event_handler(lorawan_event_t event); + +/** + * Constructing Mbed LoRaWANInterface and passing it the radio object from lora_radio_helper. + */ +static LoRaWANInterface lorawan(radio); + +/** + * Application specific callbacks + */ +static lorawan_app_callbacks_t callbacks; + +/* Instantiate the expansion board */ +XNucleoIKS01A2 *mems_expansion_board = XNucleoIKS01A2::instance(D14, D15, D4, D5); + +/* Sensor initialization */ +HTS221Sensor *hum_temp = mems_expansion_board->ht_sensor; +LSM6DSLSensor *acc_gyro = mems_expansion_board->acc_gyro; + +/** + * Auxiliary functions + */ +float get_temperature(){ + float value; + hum_temp->get_temperature(&value); + printf("\n\rTemperature %f\n\r", value); + + printf("%3.3f", value); + return value; +} + +/* Print the orientation. */ +bool send_orientation() { + uint8_t xl = 0; + uint8_t xh = 0; + uint8_t yl = 0; + uint8_t yh = 0; + uint8_t zl = 0; + uint8_t zh = 0; + + acc_gyro->get_6d_orientation_xl(&xl); + acc_gyro->get_6d_orientation_xh(&xh); + acc_gyro->get_6d_orientation_yl(&yl); + acc_gyro->get_6d_orientation_yh(&yh); + acc_gyro->get_6d_orientation_zl(&zl); + acc_gyro->get_6d_orientation_zh(&zh); + + if ( xl == 0 && yl == 0 && zl == 0 && xh == 0 && yh == 0 && zh == 1 ) { + return true; + } + + else { + return false; + } +} + +/** + * Entry point for application + */ +int main(void) +{ + + // SENSOR INIT + hum_temp->enable(); + acc_gyro->enable_x(); + /* Enable 6D Orientation. */ + acc_gyro->enable_6d_orientation(); + + // setup tracing + setup_trace(); + + // stores the status of a call to LoRaWAN protocol + lorawan_status_t retcode; + + // Initialize LoRaWAN stack + if (lorawan.initialize(&ev_queue) != LORAWAN_STATUS_OK) { + printf("\r\n LoRa initialization failed! \r\n"); + return -1; + } + + printf("\r\n Mbed LoRaWANStack initialized \r\n"); + + // prepare application callbacks + callbacks.events = mbed::callback(lora_event_handler); + lorawan.add_app_callbacks(&callbacks); + + // Set number of retries in case of CONFIRMED messages + if (lorawan.set_confirmed_msg_retries(CONFIRMED_MSG_RETRY_COUNTER) + != LORAWAN_STATUS_OK) { + printf("\r\n set_confirmed_msg_retries failed! \r\n\r\n"); + return -1; + } + + printf("\r\n CONFIRMED message retries : %d \r\n", + CONFIRMED_MSG_RETRY_COUNTER); + + // Enable adaptive data rate + if (lorawan.enable_adaptive_datarate() != LORAWAN_STATUS_OK) { + printf("\r\n enable_adaptive_datarate failed! \r\n"); + return -1; + } + + printf("\r\n Adaptive data rate (ADR) - Enabled \r\n"); + + retcode = lorawan.connect(); + + if (retcode == LORAWAN_STATUS_OK || + retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) { + } else { + printf("\r\n Connection error, code = %d \r\n", retcode); + return -1; + } + + printf("\r\n Connection - In Progress ...\r\n"); + + // make your event queue dispatching events forever + ev_queue.dispatch_forever(); + + return 0; +} + +/** + * Sends a message to the Network Server + */ +static void send_message() +{ + uint16_t packet_len; + int16_t retcode; + float temperature = 0.0; + int correct = 0; + + temperature = get_temperature(); + correct = send_orientation(); + + packet_len = sprintf((char *) tx_buffer, "%3.3f-%d", temperature, correct); + + + retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, tx_buffer, packet_len, + MSG_UNCONFIRMED_FLAG); + + if (retcode < 0) { + retcode == LORAWAN_STATUS_WOULD_BLOCK ? printf("send - WOULD BLOCK\r\n") + : printf("\r\n send() - Error code %d \r\n", retcode); + + if (retcode == LORAWAN_STATUS_WOULD_BLOCK) { + //retry in 3 seconds + if (MBED_CONF_LORA_DUTY_CYCLE_ON) { + ev_queue.call_in(3000, send_message); + } + } + return; + } + + printf("\r\n %d bytes scheduled for transmission \r\n", retcode); + memset(tx_buffer, 0, sizeof(tx_buffer)); +} + + +/** + * Event handler + */ +static void lora_event_handler(lorawan_event_t event) +{ + switch (event) { + case CONNECTED: + printf("\r\n Connection - Successful \r\n"); + if (MBED_CONF_LORA_DUTY_CYCLE_ON) { + send_message(); + } else { + ev_queue.call_every(TX_TIMER, send_message); + } + + break; + case DISCONNECTED: + ev_queue.break_dispatch(); + printf("\r\n Disconnected Successfully \r\n"); + break; + case TX_DONE: + printf("\r\n Message Sent to Network Server \r\n"); + if (MBED_CONF_LORA_DUTY_CYCLE_ON) { + send_message(); + } + break; + case TX_TIMEOUT: + case TX_ERROR: + case TX_CRYPTO_ERROR: + case TX_SCHEDULING_ERROR: + printf("\r\n Transmission Error - EventCode = %d \r\n", event); + // try again + if (MBED_CONF_LORA_DUTY_CYCLE_ON) { + send_message(); + } + break; + case RX_DONE: + printf("\r\n Received message from Network Server \r\n"); + break; + case RX_TIMEOUT: + case RX_ERROR: + printf("\r\n Error in reception - Code = %d \r\n", event); + break; + case JOIN_FAILURE: + printf("\r\n OTAA Failed - Check Keys \r\n"); + break; + case UPLINK_REQUIRED: + printf("\r\n Uplink required by NS \r\n"); + if (MBED_CONF_LORA_DUTY_CYCLE_ON) { + send_message(); + } + break; + default: + MBED_ASSERT("Unknown Event"); + } +} + +// EOF
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.h Thu Mar 14 19:44:07 2019 +0000 @@ -0,0 +1,1 @@ +#include "XNucleoIKS01A2.h"
--- a/mbed-lora-radio-drv.lib Tue Mar 12 13:02:18 2019 +0000 +++ b/mbed-lora-radio-drv.lib Thu Mar 14 19:44:07 2019 +0000 @@ -1,1 +1,1 @@ -https://github.com/ARMmbed/mbed-semtech-lora-rf-drivers#16958f814d505cfbbedfa16d9bf8b9dff0e0442b +https://github.com/ARMmbed/mbed-semtech-lora-rf-drivers/#16958f814d505cfbbedfa16d9bf8b9dff0e0442b
--- a/mbed_app.json Tue Mar 12 13:02:18 2019 +0000 +++ b/mbed_app.json Thu Mar 14 19:44:07 2019 +0000 @@ -30,12 +30,12 @@ "platform.stdio-convert-newlines": true, "platform.stdio-baud-rate": 115200, "platform.default-serial-baud-rate": 115200, - "lora.over-the-air-activation": true, - "lora.duty-cycle-on": true, + "lora.over-the-air-activation": false, + "lora.duty-cycle-on": false, "lora.phy": "EU868", - "lora.device-eui": "{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }", - "lora.application-eui": "{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }", - "lora.application-key": "{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }" + "lora.appskey": "{ 0xCD, 0xA9, 0x0D, 0x58, 0x4A, 0x45, 0x47, 0xE7, 0x77, 0x06, 0x63, 0x7E, 0x7B, 0x0D, 0x88, 0xC1 }", + "lora.nwkskey": "{ 0x30, 0xFD, 0x2C, 0xD2, 0x46, 0x06, 0xE5, 0xAE, 0x21, 0x8E, 0xCF, 0x29, 0xCE, 0xFB, 0xC1, 0xA1 }", + "lora.device-address": "0x26011BF1" }, "K64F": {