mbed_app.json configured to operate on senet
To enable cayenne, edit mbed_app.json
, assign pins for desired function
Example:
"cayenne": { "help": "compile main_cayenne.cpp instead of main.cpp", "value": 1 }, "analog_in_pin": { "help": "PA_0, PA_4, PA_5", "value": "PA_0" }, "analog_out_pin": { "help": "PA_0, PB_5, PB_10, PB_11", "value": "PB_11" }, "_digital_out_pin": { "help": "", "value": "" }
main_cayenne.cpp@61:9a84b5964019, 2020-05-04 (annotated)
- Committer:
- dudmuck
- Date:
- Mon May 04 03:05:28 2020 +0000
- Revision:
- 61:9a84b5964019
add cayenne option
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dudmuck | 61:9a84b5964019 | 1 | /** |
dudmuck | 61:9a84b5964019 | 2 | * Copyright (c) 2017, Arm Limited and affiliates. |
dudmuck | 61:9a84b5964019 | 3 | * SPDX-License-Identifier: Apache-2.0 |
dudmuck | 61:9a84b5964019 | 4 | * |
dudmuck | 61:9a84b5964019 | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
dudmuck | 61:9a84b5964019 | 6 | * you may not use this file except in compliance with the License. |
dudmuck | 61:9a84b5964019 | 7 | * You may obtain a copy of the License at |
dudmuck | 61:9a84b5964019 | 8 | * |
dudmuck | 61:9a84b5964019 | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
dudmuck | 61:9a84b5964019 | 10 | * |
dudmuck | 61:9a84b5964019 | 11 | * Unless required by applicable law or agreed to in writing, software |
dudmuck | 61:9a84b5964019 | 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
dudmuck | 61:9a84b5964019 | 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
dudmuck | 61:9a84b5964019 | 14 | * See the License for the specific language governing permissions and |
dudmuck | 61:9a84b5964019 | 15 | * limitations under the License. |
dudmuck | 61:9a84b5964019 | 16 | */ |
dudmuck | 61:9a84b5964019 | 17 | #ifdef MBED_CONF_APP_CAYENNE |
dudmuck | 61:9a84b5964019 | 18 | |
dudmuck | 61:9a84b5964019 | 19 | #include <stdio.h> |
dudmuck | 61:9a84b5964019 | 20 | |
dudmuck | 61:9a84b5964019 | 21 | #include "lorawan/LoRaWANInterface.h" |
dudmuck | 61:9a84b5964019 | 22 | #include "lorawan/system/lorawan_data_structures.h" |
dudmuck | 61:9a84b5964019 | 23 | #include "events/EventQueue.h" |
dudmuck | 61:9a84b5964019 | 24 | |
dudmuck | 61:9a84b5964019 | 25 | // Application helpers |
dudmuck | 61:9a84b5964019 | 26 | #include "trace_helper.h" |
dudmuck | 61:9a84b5964019 | 27 | #include "lora_radio_helper.h" |
dudmuck | 61:9a84b5964019 | 28 | #include "mbed.h" |
dudmuck | 61:9a84b5964019 | 29 | |
dudmuck | 61:9a84b5964019 | 30 | using namespace events; |
dudmuck | 61:9a84b5964019 | 31 | |
dudmuck | 61:9a84b5964019 | 32 | // Max payload size can be LORAMAC_PHY_MAXPAYLOAD. |
dudmuck | 61:9a84b5964019 | 33 | // This example only communicates with much shorter messages (<30 bytes). |
dudmuck | 61:9a84b5964019 | 34 | // If longer messages are used, these buffers must be changed accordingly. |
dudmuck | 61:9a84b5964019 | 35 | uint8_t tx_buffer[30]; |
dudmuck | 61:9a84b5964019 | 36 | uint8_t rx_buffer[30]; |
dudmuck | 61:9a84b5964019 | 37 | |
dudmuck | 61:9a84b5964019 | 38 | /* |
dudmuck | 61:9a84b5964019 | 39 | * Sets up an application dependent transmission timer in ms. Used only when Duty Cycling is off for testing |
dudmuck | 61:9a84b5964019 | 40 | */ |
dudmuck | 61:9a84b5964019 | 41 | #define TX_TIMER 10000 |
dudmuck | 61:9a84b5964019 | 42 | |
dudmuck | 61:9a84b5964019 | 43 | /** |
dudmuck | 61:9a84b5964019 | 44 | * Maximum number of events for the event queue. |
dudmuck | 61:9a84b5964019 | 45 | * 10 is the safe number for the stack events, however, if application |
dudmuck | 61:9a84b5964019 | 46 | * also uses the queue for whatever purposes, this number should be increased. |
dudmuck | 61:9a84b5964019 | 47 | */ |
dudmuck | 61:9a84b5964019 | 48 | #define MAX_NUMBER_OF_EVENTS 10 |
dudmuck | 61:9a84b5964019 | 49 | |
dudmuck | 61:9a84b5964019 | 50 | /** |
dudmuck | 61:9a84b5964019 | 51 | * Maximum number of retries for CONFIRMED messages before giving up |
dudmuck | 61:9a84b5964019 | 52 | */ |
dudmuck | 61:9a84b5964019 | 53 | #define CONFIRMED_MSG_RETRY_COUNTER 3 |
dudmuck | 61:9a84b5964019 | 54 | |
dudmuck | 61:9a84b5964019 | 55 | /* https://developers.mydevices.com/cayenne/docs/lora/#lora-cayenne-low-power-payload-reference-implementation-cayenne-lpp-cc-constants-definitions |
dudmuck | 61:9a84b5964019 | 56 | */ |
dudmuck | 61:9a84b5964019 | 57 | |
dudmuck | 61:9a84b5964019 | 58 | #define LPP_DIGITAL_INPUT 0 // 1 byte |
dudmuck | 61:9a84b5964019 | 59 | #define LPP_DIGITAL_OUTPUT 1 // 1 byte |
dudmuck | 61:9a84b5964019 | 60 | #define LPP_ANALOG_INPUT 2 // 2 bytes, 0.01 signed |
dudmuck | 61:9a84b5964019 | 61 | #define LPP_ANALOG_OUTPUT 3 // 2 bytes, 0.01 signed |
dudmuck | 61:9a84b5964019 | 62 | #define LPP_LUMINOSITY 101 // 2 bytes, 1 lux unsigned |
dudmuck | 61:9a84b5964019 | 63 | #define LPP_PRESENCE 102 // 1 byte, 1 |
dudmuck | 61:9a84b5964019 | 64 | #define LPP_TEMPERATURE 103 // 2 bytes, 0.1°C signed |
dudmuck | 61:9a84b5964019 | 65 | #define LPP_RELATIVE_HUMIDITY 104 // 1 byte, 0.5% unsigned |
dudmuck | 61:9a84b5964019 | 66 | #define LPP_ACCELEROMETER 113 // 2 bytes per axis, 0.001G |
dudmuck | 61:9a84b5964019 | 67 | #define LPP_BAROMETRIC_PRESSURE 115 // 2 bytes 0.1 hPa Unsigned |
dudmuck | 61:9a84b5964019 | 68 | #define LPP_GYROMETER 134 // 2 bytes per axis, 0.01 °/s |
dudmuck | 61:9a84b5964019 | 69 | #define LPP_GPS 136 // 3 byte lon/lat 0.0001 °, 3 bytes alt 0.01m |
dudmuck | 61:9a84b5964019 | 70 | |
dudmuck | 61:9a84b5964019 | 71 | |
dudmuck | 61:9a84b5964019 | 72 | // Data ID + Data Type + Data Size |
dudmuck | 61:9a84b5964019 | 73 | #define LPP_DIGITAL_INPUT_SIZE 3 |
dudmuck | 61:9a84b5964019 | 74 | #define LPP_DIGITAL_OUTPUT_SIZE 3 |
dudmuck | 61:9a84b5964019 | 75 | #define LPP_ANALOG_INPUT_SIZE 4 |
dudmuck | 61:9a84b5964019 | 76 | #define LPP_ANALOG_OUTPUT_SIZE 4 |
dudmuck | 61:9a84b5964019 | 77 | #define LPP_LUMINOSITY_SIZE 4 |
dudmuck | 61:9a84b5964019 | 78 | #define LPP_PRESENCE_SIZE 3 |
dudmuck | 61:9a84b5964019 | 79 | #define LPP_TEMPERATURE_SIZE 4 |
dudmuck | 61:9a84b5964019 | 80 | #define LPP_RELATIVE_HUMIDITY_SIZE 3 |
dudmuck | 61:9a84b5964019 | 81 | #define LPP_ACCELEROMETER_SIZE 8 |
dudmuck | 61:9a84b5964019 | 82 | #define LPP_BAROMETRIC_PRESSURE_SIZE 4 |
dudmuck | 61:9a84b5964019 | 83 | #define LPP_GYROMETER_SIZE 8 |
dudmuck | 61:9a84b5964019 | 84 | |
dudmuck | 61:9a84b5964019 | 85 | #define CAYENNE_CH_DOUT 2 |
dudmuck | 61:9a84b5964019 | 86 | #define CAYENNE_CH_AOUT 3 |
dudmuck | 61:9a84b5964019 | 87 | #define CAYENNE_CH_TEMP 0 |
dudmuck | 61:9a84b5964019 | 88 | #ifdef MBED_CONF_APP_ANALOG_IN_PIN |
dudmuck | 61:9a84b5964019 | 89 | AnalogIn a_in(MBED_CONF_APP_ANALOG_IN_PIN); |
dudmuck | 61:9a84b5964019 | 90 | #define CAYENNE_CH_ANALOG_IN 1 |
dudmuck | 61:9a84b5964019 | 91 | #endif |
dudmuck | 61:9a84b5964019 | 92 | |
dudmuck | 61:9a84b5964019 | 93 | #ifdef MBED_CONF_APP_DIGITAL_OUT_PIN |
dudmuck | 61:9a84b5964019 | 94 | DigitalOut dig_out(MBED_CONF_APP_DIGITAL_OUT_PIN); |
dudmuck | 61:9a84b5964019 | 95 | #endif /* MBED_CONF_APP_DIGITAL_OUT_PIN */ |
dudmuck | 61:9a84b5964019 | 96 | #ifdef MBED_CONF_APP_ANALOG_OUT_PIN |
dudmuck | 61:9a84b5964019 | 97 | PwmOut pwm(MBED_CONF_APP_ANALOG_OUT_PIN); |
dudmuck | 61:9a84b5964019 | 98 | #endif /* MBED_CONF_APP_ANALOG_OUT_PIN */ |
dudmuck | 61:9a84b5964019 | 99 | |
dudmuck | 61:9a84b5964019 | 100 | volatile int cayenne_ack_ch; |
dudmuck | 61:9a84b5964019 | 101 | |
dudmuck | 61:9a84b5964019 | 102 | |
dudmuck | 61:9a84b5964019 | 103 | /** |
dudmuck | 61:9a84b5964019 | 104 | * This event queue is the global event queue for both the |
dudmuck | 61:9a84b5964019 | 105 | * application and stack. To conserve memory, the stack is designed to run |
dudmuck | 61:9a84b5964019 | 106 | * in the same thread as the application and the application is responsible for |
dudmuck | 61:9a84b5964019 | 107 | * providing an event queue to the stack that will be used for ISR deferment as |
dudmuck | 61:9a84b5964019 | 108 | * well as application information event queuing. |
dudmuck | 61:9a84b5964019 | 109 | */ |
dudmuck | 61:9a84b5964019 | 110 | static EventQueue ev_queue(MAX_NUMBER_OF_EVENTS *EVENTS_EVENT_SIZE); |
dudmuck | 61:9a84b5964019 | 111 | |
dudmuck | 61:9a84b5964019 | 112 | /** |
dudmuck | 61:9a84b5964019 | 113 | * Event handler. |
dudmuck | 61:9a84b5964019 | 114 | * |
dudmuck | 61:9a84b5964019 | 115 | * This will be passed to the LoRaWAN stack to queue events for the |
dudmuck | 61:9a84b5964019 | 116 | * application which in turn drive the application. |
dudmuck | 61:9a84b5964019 | 117 | */ |
dudmuck | 61:9a84b5964019 | 118 | static void lora_event_handler(lorawan_event_t event); |
dudmuck | 61:9a84b5964019 | 119 | |
dudmuck | 61:9a84b5964019 | 120 | /** |
dudmuck | 61:9a84b5964019 | 121 | * Constructing Mbed LoRaWANInterface and passing it the radio object from lora_radio_helper. |
dudmuck | 61:9a84b5964019 | 122 | */ |
dudmuck | 61:9a84b5964019 | 123 | static LoRaWANInterface lorawan(radio); |
dudmuck | 61:9a84b5964019 | 124 | |
dudmuck | 61:9a84b5964019 | 125 | /** |
dudmuck | 61:9a84b5964019 | 126 | * Application specific callbacks |
dudmuck | 61:9a84b5964019 | 127 | */ |
dudmuck | 61:9a84b5964019 | 128 | static lorawan_app_callbacks_t callbacks; |
dudmuck | 61:9a84b5964019 | 129 | |
dudmuck | 61:9a84b5964019 | 130 | /** |
dudmuck | 61:9a84b5964019 | 131 | * Entry point for application |
dudmuck | 61:9a84b5964019 | 132 | */ |
dudmuck | 61:9a84b5964019 | 133 | int main(void) |
dudmuck | 61:9a84b5964019 | 134 | { |
dudmuck | 61:9a84b5964019 | 135 | // setup tracing |
dudmuck | 61:9a84b5964019 | 136 | setup_trace(); |
dudmuck | 61:9a84b5964019 | 137 | |
dudmuck | 61:9a84b5964019 | 138 | // stores the status of a call to LoRaWAN protocol |
dudmuck | 61:9a84b5964019 | 139 | lorawan_status_t retcode; |
dudmuck | 61:9a84b5964019 | 140 | |
dudmuck | 61:9a84b5964019 | 141 | // Initialize LoRaWAN stack |
dudmuck | 61:9a84b5964019 | 142 | if (lorawan.initialize(&ev_queue) != LORAWAN_STATUS_OK) { |
dudmuck | 61:9a84b5964019 | 143 | printf("\r\n LoRa initialization failed! \r\n"); |
dudmuck | 61:9a84b5964019 | 144 | return -1; |
dudmuck | 61:9a84b5964019 | 145 | } |
dudmuck | 61:9a84b5964019 | 146 | |
dudmuck | 61:9a84b5964019 | 147 | printf("\r\n Mbed LoRaWANStack initialized \r\n"); |
dudmuck | 61:9a84b5964019 | 148 | |
dudmuck | 61:9a84b5964019 | 149 | // prepare application callbacks |
dudmuck | 61:9a84b5964019 | 150 | callbacks.events = mbed::callback(lora_event_handler); |
dudmuck | 61:9a84b5964019 | 151 | lorawan.add_app_callbacks(&callbacks); |
dudmuck | 61:9a84b5964019 | 152 | |
dudmuck | 61:9a84b5964019 | 153 | // Set number of retries in case of CONFIRMED messages |
dudmuck | 61:9a84b5964019 | 154 | if (lorawan.set_confirmed_msg_retries(CONFIRMED_MSG_RETRY_COUNTER) |
dudmuck | 61:9a84b5964019 | 155 | != LORAWAN_STATUS_OK) { |
dudmuck | 61:9a84b5964019 | 156 | printf("\r\n set_confirmed_msg_retries failed! \r\n\r\n"); |
dudmuck | 61:9a84b5964019 | 157 | return -1; |
dudmuck | 61:9a84b5964019 | 158 | } |
dudmuck | 61:9a84b5964019 | 159 | |
dudmuck | 61:9a84b5964019 | 160 | printf("\r\n CONFIRMED message retries : %d \r\n", |
dudmuck | 61:9a84b5964019 | 161 | CONFIRMED_MSG_RETRY_COUNTER); |
dudmuck | 61:9a84b5964019 | 162 | |
dudmuck | 61:9a84b5964019 | 163 | // Enable adaptive data rate |
dudmuck | 61:9a84b5964019 | 164 | if (lorawan.enable_adaptive_datarate() != LORAWAN_STATUS_OK) { |
dudmuck | 61:9a84b5964019 | 165 | printf("\r\n enable_adaptive_datarate failed! \r\n"); |
dudmuck | 61:9a84b5964019 | 166 | return -1; |
dudmuck | 61:9a84b5964019 | 167 | } |
dudmuck | 61:9a84b5964019 | 168 | |
dudmuck | 61:9a84b5964019 | 169 | printf("\r\n Adaptive data rate (ADR) - Enabled \r\n"); |
dudmuck | 61:9a84b5964019 | 170 | |
dudmuck | 61:9a84b5964019 | 171 | retcode = lorawan.connect(); |
dudmuck | 61:9a84b5964019 | 172 | |
dudmuck | 61:9a84b5964019 | 173 | if (retcode == LORAWAN_STATUS_OK || |
dudmuck | 61:9a84b5964019 | 174 | retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) { |
dudmuck | 61:9a84b5964019 | 175 | } else { |
dudmuck | 61:9a84b5964019 | 176 | printf("\r\n Connection error, code = %d \r\n", retcode); |
dudmuck | 61:9a84b5964019 | 177 | return -1; |
dudmuck | 61:9a84b5964019 | 178 | } |
dudmuck | 61:9a84b5964019 | 179 | |
dudmuck | 61:9a84b5964019 | 180 | printf("\r\n Connection - In Progress ...\r\n"); |
dudmuck | 61:9a84b5964019 | 181 | |
dudmuck | 61:9a84b5964019 | 182 | // make your event queue dispatching events forever |
dudmuck | 61:9a84b5964019 | 183 | ev_queue.dispatch_forever(); |
dudmuck | 61:9a84b5964019 | 184 | |
dudmuck | 61:9a84b5964019 | 185 | return 0; |
dudmuck | 61:9a84b5964019 | 186 | } |
dudmuck | 61:9a84b5964019 | 187 | |
dudmuck | 61:9a84b5964019 | 188 | /** |
dudmuck | 61:9a84b5964019 | 189 | * Sends a message to the Network Server |
dudmuck | 61:9a84b5964019 | 190 | */ |
dudmuck | 61:9a84b5964019 | 191 | static void send_message() |
dudmuck | 61:9a84b5964019 | 192 | { |
dudmuck | 61:9a84b5964019 | 193 | uint16_t packet_len = 0; |
dudmuck | 61:9a84b5964019 | 194 | int16_t retcode; |
dudmuck | 61:9a84b5964019 | 195 | int32_t sensor_value; |
dudmuck | 61:9a84b5964019 | 196 | |
dudmuck | 61:9a84b5964019 | 197 | if (cayenne_ack_ch != -1) { |
dudmuck | 61:9a84b5964019 | 198 | switch (cayenne_ack_ch) { |
dudmuck | 61:9a84b5964019 | 199 | tx_buffer[packet_len++] = cayenne_ack_ch; |
dudmuck | 61:9a84b5964019 | 200 | #ifdef MBED_CONF_APP_DIGITAL_OUT_PIN |
dudmuck | 61:9a84b5964019 | 201 | case CAYENNE_CH_DOUT: |
dudmuck | 61:9a84b5964019 | 202 | tx_buffer[packet_len++] = LPP_DIGITAL_OUTPUT; |
dudmuck | 61:9a84b5964019 | 203 | tx_buffer[packet_len++] = dig_out.read(); |
dudmuck | 61:9a84b5964019 | 204 | printf("digitalOut-ack%u\r\n", dig_out.read()); |
dudmuck | 61:9a84b5964019 | 205 | break; |
dudmuck | 61:9a84b5964019 | 206 | #endif /* MBED_CONF_APP_DIGITAL_OUT_PIN */ |
dudmuck | 61:9a84b5964019 | 207 | #ifdef MBED_CONF_APP_ANALOG_OUT_PIN |
dudmuck | 61:9a84b5964019 | 208 | case CAYENNE_CH_AOUT: |
dudmuck | 61:9a84b5964019 | 209 | tx_buffer[packet_len++] = LPP_ANALOG_OUTPUT; |
dudmuck | 61:9a84b5964019 | 210 | { |
dudmuck | 61:9a84b5964019 | 211 | uint16_t u16 = pwm.read() * 100; |
dudmuck | 61:9a84b5964019 | 212 | tx_buffer[packet_len++] = u16 >> 8; |
dudmuck | 61:9a84b5964019 | 213 | tx_buffer[packet_len++] = u16; |
dudmuck | 61:9a84b5964019 | 214 | printf("pwmAck:%x\r\n", u16); |
dudmuck | 61:9a84b5964019 | 215 | } |
dudmuck | 61:9a84b5964019 | 216 | break; |
dudmuck | 61:9a84b5964019 | 217 | #endif /* MBED_CONF_APP_ANALOG_OUT_PIN */ |
dudmuck | 61:9a84b5964019 | 218 | } |
dudmuck | 61:9a84b5964019 | 219 | cayenne_ack_ch = -1; |
dudmuck | 61:9a84b5964019 | 220 | } |
dudmuck | 61:9a84b5964019 | 221 | |
dudmuck | 61:9a84b5964019 | 222 | #ifdef MBED_CONF_APP_ANALOG_IN_PIN |
dudmuck | 61:9a84b5964019 | 223 | { |
dudmuck | 61:9a84b5964019 | 224 | uint16_t rot, u16 = a_in.read_u16(); |
dudmuck | 61:9a84b5964019 | 225 | float f = u16 / 198.6; // scale 65535/3.3 to 0.01v per bit |
dudmuck | 61:9a84b5964019 | 226 | tx_buffer[packet_len++] = CAYENNE_CH_ANALOG_IN; |
dudmuck | 61:9a84b5964019 | 227 | tx_buffer[packet_len++] = LPP_ANALOG_INPUT; |
dudmuck | 61:9a84b5964019 | 228 | rot = (uint16_t) f; |
dudmuck | 61:9a84b5964019 | 229 | tx_buffer[packet_len++] = rot >> 8; |
dudmuck | 61:9a84b5964019 | 230 | tx_buffer[packet_len++] = rot; |
dudmuck | 61:9a84b5964019 | 231 | printf("analog_in:%u -> %04x\r\n", u16, rot); |
dudmuck | 61:9a84b5964019 | 232 | } |
dudmuck | 61:9a84b5964019 | 233 | #endif /* MBED_CONF_APP_ANALOG_IN_PIN */ |
dudmuck | 61:9a84b5964019 | 234 | |
dudmuck | 61:9a84b5964019 | 235 | retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, tx_buffer, packet_len, |
dudmuck | 61:9a84b5964019 | 236 | MSG_UNCONFIRMED_FLAG); |
dudmuck | 61:9a84b5964019 | 237 | |
dudmuck | 61:9a84b5964019 | 238 | if (retcode < 0) { |
dudmuck | 61:9a84b5964019 | 239 | retcode == LORAWAN_STATUS_WOULD_BLOCK ? printf("send - WOULD BLOCK\r\n") |
dudmuck | 61:9a84b5964019 | 240 | : printf("\r\n send() - Error code %d \r\n", retcode); |
dudmuck | 61:9a84b5964019 | 241 | |
dudmuck | 61:9a84b5964019 | 242 | if (retcode == LORAWAN_STATUS_WOULD_BLOCK) { |
dudmuck | 61:9a84b5964019 | 243 | //retry in 3 seconds |
dudmuck | 61:9a84b5964019 | 244 | if (MBED_CONF_LORA_DUTY_CYCLE_ON) { |
dudmuck | 61:9a84b5964019 | 245 | ev_queue.call_in(3000, send_message); |
dudmuck | 61:9a84b5964019 | 246 | } |
dudmuck | 61:9a84b5964019 | 247 | } |
dudmuck | 61:9a84b5964019 | 248 | return; |
dudmuck | 61:9a84b5964019 | 249 | } |
dudmuck | 61:9a84b5964019 | 250 | |
dudmuck | 61:9a84b5964019 | 251 | printf("\r\n %d bytes scheduled for transmission \r\n", retcode); |
dudmuck | 61:9a84b5964019 | 252 | memset(tx_buffer, 0, sizeof(tx_buffer)); |
dudmuck | 61:9a84b5964019 | 253 | } |
dudmuck | 61:9a84b5964019 | 254 | |
dudmuck | 61:9a84b5964019 | 255 | /** |
dudmuck | 61:9a84b5964019 | 256 | * Receive a message from the Network Server |
dudmuck | 61:9a84b5964019 | 257 | */ |
dudmuck | 61:9a84b5964019 | 258 | static void receive_message() |
dudmuck | 61:9a84b5964019 | 259 | { |
dudmuck | 61:9a84b5964019 | 260 | uint8_t port; |
dudmuck | 61:9a84b5964019 | 261 | int flags; |
dudmuck | 61:9a84b5964019 | 262 | int16_t retcode = lorawan.receive(rx_buffer, sizeof(rx_buffer), port, flags); |
dudmuck | 61:9a84b5964019 | 263 | |
dudmuck | 61:9a84b5964019 | 264 | if (retcode < 0) { |
dudmuck | 61:9a84b5964019 | 265 | printf("\r\n receive() - Error code %d \r\n", retcode); |
dudmuck | 61:9a84b5964019 | 266 | return; |
dudmuck | 61:9a84b5964019 | 267 | } |
dudmuck | 61:9a84b5964019 | 268 | |
dudmuck | 61:9a84b5964019 | 269 | printf(" RX Data on port %u (%d bytes): ", port, retcode); |
dudmuck | 61:9a84b5964019 | 270 | for (uint8_t i = 0; i < retcode; i++) { |
dudmuck | 61:9a84b5964019 | 271 | printf("%02x ", rx_buffer[i]); |
dudmuck | 61:9a84b5964019 | 272 | } |
dudmuck | 61:9a84b5964019 | 273 | printf("\r\n"); |
dudmuck | 61:9a84b5964019 | 274 | |
dudmuck | 61:9a84b5964019 | 275 | { |
dudmuck | 61:9a84b5964019 | 276 | unsigned n; |
dudmuck | 61:9a84b5964019 | 277 | for (n = 0; n < retcode; n += 4) { |
dudmuck | 61:9a84b5964019 | 278 | uint16_t val = rx_buffer[n+1] << 8; |
dudmuck | 61:9a84b5964019 | 279 | val += rx_buffer[n+2]; |
dudmuck | 61:9a84b5964019 | 280 | cayenne_ack_ch = rx_buffer[n]; |
dudmuck | 61:9a84b5964019 | 281 | switch (rx_buffer[n]) { |
dudmuck | 61:9a84b5964019 | 282 | case CAYENNE_CH_DOUT: |
dudmuck | 61:9a84b5964019 | 283 | #ifdef MBED_CONF_APP_DIGITAL_OUT_PIN |
dudmuck | 61:9a84b5964019 | 284 | dig_out.write(val); |
dudmuck | 61:9a84b5964019 | 285 | printf("digitalOut write%u\r\n", val); |
dudmuck | 61:9a84b5964019 | 286 | #endif /* MBED_CONF_APP_DIGITAL_OUT_PIN */ |
dudmuck | 61:9a84b5964019 | 287 | break; |
dudmuck | 61:9a84b5964019 | 288 | case CAYENNE_CH_AOUT: |
dudmuck | 61:9a84b5964019 | 289 | #ifdef MBED_CONF_APP_ANALOG_OUT_PIN |
dudmuck | 61:9a84b5964019 | 290 | pwm.write(val / 100.0); |
dudmuck | 61:9a84b5964019 | 291 | printf("PWM-write_%x\r\n", val); |
dudmuck | 61:9a84b5964019 | 292 | #endif /* MBED_CONF_APP_ANALOG_OUT_PIN */ |
dudmuck | 61:9a84b5964019 | 293 | break; |
dudmuck | 61:9a84b5964019 | 294 | default: |
dudmuck | 61:9a84b5964019 | 295 | break; |
dudmuck | 61:9a84b5964019 | 296 | } |
dudmuck | 61:9a84b5964019 | 297 | } |
dudmuck | 61:9a84b5964019 | 298 | } |
dudmuck | 61:9a84b5964019 | 299 | |
dudmuck | 61:9a84b5964019 | 300 | memset(rx_buffer, 0, sizeof(rx_buffer)); |
dudmuck | 61:9a84b5964019 | 301 | } |
dudmuck | 61:9a84b5964019 | 302 | |
dudmuck | 61:9a84b5964019 | 303 | /** |
dudmuck | 61:9a84b5964019 | 304 | * Event handler |
dudmuck | 61:9a84b5964019 | 305 | */ |
dudmuck | 61:9a84b5964019 | 306 | static void lora_event_handler(lorawan_event_t event) |
dudmuck | 61:9a84b5964019 | 307 | { |
dudmuck | 61:9a84b5964019 | 308 | switch (event) { |
dudmuck | 61:9a84b5964019 | 309 | case CONNECTED: |
dudmuck | 61:9a84b5964019 | 310 | cayenne_ack_ch = -1; |
dudmuck | 61:9a84b5964019 | 311 | printf("\r\n Connection - Successful \r\n"); |
dudmuck | 61:9a84b5964019 | 312 | if (MBED_CONF_LORA_DUTY_CYCLE_ON) { |
dudmuck | 61:9a84b5964019 | 313 | send_message(); |
dudmuck | 61:9a84b5964019 | 314 | } else { |
dudmuck | 61:9a84b5964019 | 315 | ev_queue.call_every(TX_TIMER, send_message); |
dudmuck | 61:9a84b5964019 | 316 | } |
dudmuck | 61:9a84b5964019 | 317 | |
dudmuck | 61:9a84b5964019 | 318 | break; |
dudmuck | 61:9a84b5964019 | 319 | case DISCONNECTED: |
dudmuck | 61:9a84b5964019 | 320 | ev_queue.break_dispatch(); |
dudmuck | 61:9a84b5964019 | 321 | printf("\r\n Disconnected Successfully \r\n"); |
dudmuck | 61:9a84b5964019 | 322 | break; |
dudmuck | 61:9a84b5964019 | 323 | case TX_DONE: |
dudmuck | 61:9a84b5964019 | 324 | printf("\r\n Message Sent to Network Server \r\n"); |
dudmuck | 61:9a84b5964019 | 325 | if (MBED_CONF_LORA_DUTY_CYCLE_ON) { |
dudmuck | 61:9a84b5964019 | 326 | send_message(); |
dudmuck | 61:9a84b5964019 | 327 | } |
dudmuck | 61:9a84b5964019 | 328 | break; |
dudmuck | 61:9a84b5964019 | 329 | case TX_TIMEOUT: |
dudmuck | 61:9a84b5964019 | 330 | case TX_ERROR: |
dudmuck | 61:9a84b5964019 | 331 | case TX_CRYPTO_ERROR: |
dudmuck | 61:9a84b5964019 | 332 | case TX_SCHEDULING_ERROR: |
dudmuck | 61:9a84b5964019 | 333 | printf("\r\n Transmission Error - EventCode = %d \r\n", event); |
dudmuck | 61:9a84b5964019 | 334 | // try again |
dudmuck | 61:9a84b5964019 | 335 | if (MBED_CONF_LORA_DUTY_CYCLE_ON) { |
dudmuck | 61:9a84b5964019 | 336 | send_message(); |
dudmuck | 61:9a84b5964019 | 337 | } |
dudmuck | 61:9a84b5964019 | 338 | break; |
dudmuck | 61:9a84b5964019 | 339 | case RX_DONE: |
dudmuck | 61:9a84b5964019 | 340 | printf("\r\n Received message from Network Server \r\n"); |
dudmuck | 61:9a84b5964019 | 341 | receive_message(); |
dudmuck | 61:9a84b5964019 | 342 | break; |
dudmuck | 61:9a84b5964019 | 343 | case RX_TIMEOUT: |
dudmuck | 61:9a84b5964019 | 344 | case RX_ERROR: |
dudmuck | 61:9a84b5964019 | 345 | printf("\r\n Error in reception - Code = %d \r\n", event); |
dudmuck | 61:9a84b5964019 | 346 | break; |
dudmuck | 61:9a84b5964019 | 347 | case JOIN_FAILURE: |
dudmuck | 61:9a84b5964019 | 348 | printf("\r\n OTAA Failed - Check Keys \r\n"); |
dudmuck | 61:9a84b5964019 | 349 | break; |
dudmuck | 61:9a84b5964019 | 350 | case UPLINK_REQUIRED: |
dudmuck | 61:9a84b5964019 | 351 | printf("\r\n Uplink required by NS \r\n"); |
dudmuck | 61:9a84b5964019 | 352 | if (MBED_CONF_LORA_DUTY_CYCLE_ON) { |
dudmuck | 61:9a84b5964019 | 353 | send_message(); |
dudmuck | 61:9a84b5964019 | 354 | } |
dudmuck | 61:9a84b5964019 | 355 | break; |
dudmuck | 61:9a84b5964019 | 356 | default: |
dudmuck | 61:9a84b5964019 | 357 | MBED_ASSERT("Unknown Event"); |
dudmuck | 61:9a84b5964019 | 358 | } |
dudmuck | 61:9a84b5964019 | 359 | } |
dudmuck | 61:9a84b5964019 | 360 | |
dudmuck | 61:9a84b5964019 | 361 | // EOF |
dudmuck | 61:9a84b5964019 | 362 | #endif /* MBED_CONF_APP_CAYENNE */ |