![](/media/cache/profiles/5a245e9a52805642a6a095aeb71ee307.jpg.50x50_q85.jpg)
Demo program for LoRaWan with data formated for cayenne interface on mydevices.com Check on https://goo.gl/fTUDNc
Demonstration d'un node LoRaWan sur carte : Discovery IOT STmicro : B-L072Z-LRWAN1 https://www.st.com/en/evaluation-tools/b-l072z-lrwan1.html
L e code original MBED-ARM : https://os.mbed.com/teams/mbed-os-examples/code/mbed-os-example-lorawan/ est une application de l'API LoRAWan https://os.mbed.com/docs/v5.9/reference/lorawan.html
Le code original a été adapté pour une carte B-L072Z-LRWAN1 équipée d'un capteur de température LM35 connecté en 3.3v sur le port PA_0 (port analogique AN0) Les données sont formatées "cayenne" et visualisables sur mydevices.com ( https://goo.gl/fTUDNc ) Documentation cayenne : https://mydevices.com/cayenne/docs/lora/#lora-cayenne-low-power-payload
Les essais ont été réalisés avec une passerelle TTN https://www.thethingsnetwork.org/ le "Payload Format" ayant été configuré pour "Cayenne LPP"
Des capteurs virtuels on été également ajoutés (humidité, température, lumière, etc...) pour les essais au format cayenne.
Données physiques transmises (downlink)
- Température sur capteur LM35 - Tension sur PA_1 (AN1) est transmise entre 0% et 100% - Etat du bouton bleu
Données physiques reçues (uplink) Un actionneur permet d'allumer/eteindre à distance la led verte de la carte B-L072Z-LRWAN1
L'interface mydevice.com proposé permet de visualiser :
- Les capteurs virtuels - La température réelle sur LM35 - L'état du bouton bleu
main.cpp@43:74226f6ca42c, 2018-11-29 (annotated)
- Committer:
- cdupaty
- Date:
- Thu Nov 29 07:48:18 2018 +0000
- Revision:
- 43:74226f6ca42c
- Parent:
- 26:f07f5febf97f
Demo with LM52 captor on analog A0.; Data formated for cayenne (mydevices.com); Led green (LED1) on B-L072Z-LRWAN1 flash when data reception
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:7037ed05f54f | 1 | /** |
mbed_official | 0:7037ed05f54f | 2 | * Copyright (c) 2017, Arm Limited and affiliates. |
mbed_official | 0:7037ed05f54f | 3 | * SPDX-License-Identifier: Apache-2.0 |
mbed_official | 0:7037ed05f54f | 4 | * |
mbed_official | 0:7037ed05f54f | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mbed_official | 0:7037ed05f54f | 6 | * you may not use this file except in compliance with the License. |
mbed_official | 0:7037ed05f54f | 7 | * You may obtain a copy of the License at |
mbed_official | 0:7037ed05f54f | 8 | * |
mbed_official | 0:7037ed05f54f | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbed_official | 0:7037ed05f54f | 10 | * |
mbed_official | 0:7037ed05f54f | 11 | * Unless required by applicable law or agreed to in writing, software |
mbed_official | 0:7037ed05f54f | 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
mbed_official | 0:7037ed05f54f | 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mbed_official | 0:7037ed05f54f | 14 | * See the License for the specific language governing permissions and |
mbed_official | 0:7037ed05f54f | 15 | * limitations under the License. |
cdupaty | 43:74226f6ca42c | 16 | * |
cdupaty | 43:74226f6ca42c | 17 | * Adaptation for Discovery IOT STmicro B-L072Z-LRWAN1, TTN and CAYENNE (mydevices.com) |
cdupaty | 43:74226f6ca42c | 18 | * Christian Dupaty Lycee Fourcade 13120 Gardanne France |
cdupaty | 43:74226f6ca42c | 19 | * 11-2018 |
cdupaty | 43:74226f6ca42c | 20 | * Peripherals on B-L072Z-LRWAN1 |
cdupaty | 43:74226f6ca42c | 21 | LED1 = PB_5, // Green |
cdupaty | 43:74226f6ca42c | 22 | LED2 = PA_5, // Red |
cdupaty | 43:74226f6ca42c | 23 | LED3 = PB_6, // Blue |
cdupaty | 43:74226f6ca42c | 24 | LED4 = PB_7, // Red |
cdupaty | 43:74226f6ca42c | 25 | USER_BUTTON = PB_2, |
cdupaty | 43:74226f6ca42c | 26 | */ |
mbed_official | 0:7037ed05f54f | 27 | #include <stdio.h> |
mbed_official | 3:8c7198d1a2a1 | 28 | |
mbed_official | 0:7037ed05f54f | 29 | #include "lorawan/LoRaWANInterface.h" |
mbed_official | 0:7037ed05f54f | 30 | #include "lorawan/system/lorawan_data_structures.h" |
mbed_official | 0:7037ed05f54f | 31 | #include "events/EventQueue.h" |
cdupaty | 43:74226f6ca42c | 32 | #include "CayenneLPP.h" |
mbed_official | 0:7037ed05f54f | 33 | |
mbed_official | 0:7037ed05f54f | 34 | // Application helpers |
mbed_official | 0:7037ed05f54f | 35 | #include "DummySensor.h" |
mbed_official | 0:7037ed05f54f | 36 | #include "trace_helper.h" |
mbed_official | 0:7037ed05f54f | 37 | #include "lora_radio_helper.h" |
mbed_official | 0:7037ed05f54f | 38 | |
mbed_official | 0:7037ed05f54f | 39 | using namespace events; |
mbed_official | 0:7037ed05f54f | 40 | |
mbed_official | 12:5015dfead3f2 | 41 | // Max payload size can be LORAMAC_PHY_MAXPAYLOAD. |
mbed_official | 12:5015dfead3f2 | 42 | // This example only communicates with much shorter messages (<30 bytes). |
mbed_official | 12:5015dfead3f2 | 43 | // If longer messages are used, these buffers must be changed accordingly. |
mbed_official | 12:5015dfead3f2 | 44 | uint8_t tx_buffer[30]; |
mbed_official | 12:5015dfead3f2 | 45 | uint8_t rx_buffer[30]; |
mbed_official | 0:7037ed05f54f | 46 | |
mbed_official | 0:7037ed05f54f | 47 | /* |
mbed_official | 0:7037ed05f54f | 48 | * Sets up an application dependent transmission timer in ms. Used only when Duty Cycling is off for testing |
mbed_official | 0:7037ed05f54f | 49 | */ |
mbed_official | 0:7037ed05f54f | 50 | #define TX_TIMER 10000 |
mbed_official | 0:7037ed05f54f | 51 | |
mbed_official | 0:7037ed05f54f | 52 | /** |
mbed_official | 0:7037ed05f54f | 53 | * Maximum number of events for the event queue. |
mbed_official | 12:5015dfead3f2 | 54 | * 10 is the safe number for the stack events, however, if application |
mbed_official | 0:7037ed05f54f | 55 | * also uses the queue for whatever purposes, this number should be increased. |
mbed_official | 0:7037ed05f54f | 56 | */ |
mbed_official | 12:5015dfead3f2 | 57 | #define MAX_NUMBER_OF_EVENTS 10 |
mbed_official | 0:7037ed05f54f | 58 | |
mbed_official | 0:7037ed05f54f | 59 | /** |
mbed_official | 0:7037ed05f54f | 60 | * Maximum number of retries for CONFIRMED messages before giving up |
mbed_official | 0:7037ed05f54f | 61 | */ |
mbed_official | 0:7037ed05f54f | 62 | #define CONFIRMED_MSG_RETRY_COUNTER 3 |
mbed_official | 0:7037ed05f54f | 63 | |
mbed_official | 0:7037ed05f54f | 64 | /** |
mbed_official | 0:7037ed05f54f | 65 | * Dummy pin for dummy sensor |
mbed_official | 0:7037ed05f54f | 66 | */ |
mbed_official | 0:7037ed05f54f | 67 | #define PC_9 0 |
mbed_official | 0:7037ed05f54f | 68 | |
mbed_official | 0:7037ed05f54f | 69 | /** |
mbed_official | 0:7037ed05f54f | 70 | * Dummy sensor class object |
mbed_official | 0:7037ed05f54f | 71 | */ |
mbed_official | 0:7037ed05f54f | 72 | DS1820 ds1820(PC_9); |
mbed_official | 0:7037ed05f54f | 73 | |
cdupaty | 43:74226f6ca42c | 74 | // added by C.Dupaty |
cdupaty | 43:74226f6ca42c | 75 | |
cdupaty | 43:74226f6ca42c | 76 | CayenneLPP cayenne(100); // data to cayenne format |
cdupaty | 43:74226f6ca42c | 77 | AnalogIn lm35(A0); // for test we have connect a |
cdupaty | 43:74226f6ca42c | 78 | //LM35 temperature captor on A0 Arduino analog port |
cdupaty | 43:74226f6ca42c | 79 | AnalogIn ana(A1); |
cdupaty | 43:74226f6ca42c | 80 | DigitalIn bp(USER_BUTTON); |
cdupaty | 43:74226f6ca42c | 81 | DigitalOut ledTest(LED1); |
cdupaty | 43:74226f6ca42c | 82 | |
mbed_official | 0:7037ed05f54f | 83 | /** |
mbed_official | 0:7037ed05f54f | 84 | * This event queue is the global event queue for both the |
mbed_official | 0:7037ed05f54f | 85 | * application and stack. To conserve memory, the stack is designed to run |
mbed_official | 0:7037ed05f54f | 86 | * in the same thread as the application and the application is responsible for |
mbed_official | 0:7037ed05f54f | 87 | * providing an event queue to the stack that will be used for ISR deferment as |
mbed_official | 0:7037ed05f54f | 88 | * well as application information event queuing. |
mbed_official | 0:7037ed05f54f | 89 | */ |
mbed_official | 0:7037ed05f54f | 90 | static EventQueue ev_queue(MAX_NUMBER_OF_EVENTS * EVENTS_EVENT_SIZE); |
mbed_official | 0:7037ed05f54f | 91 | |
mbed_official | 0:7037ed05f54f | 92 | /** |
mbed_official | 0:7037ed05f54f | 93 | * Event handler. |
mbed_official | 0:7037ed05f54f | 94 | * |
mbed_official | 0:7037ed05f54f | 95 | * This will be passed to the LoRaWAN stack to queue events for the |
mbed_official | 0:7037ed05f54f | 96 | * application which in turn drive the application. |
mbed_official | 0:7037ed05f54f | 97 | */ |
mbed_official | 0:7037ed05f54f | 98 | static void lora_event_handler(lorawan_event_t event); |
mbed_official | 0:7037ed05f54f | 99 | |
mbed_official | 0:7037ed05f54f | 100 | /** |
mbed_official | 2:dc95ac6d6d4e | 101 | * Constructing Mbed LoRaWANInterface and passing it down the radio object. |
mbed_official | 0:7037ed05f54f | 102 | */ |
mbed_official | 2:dc95ac6d6d4e | 103 | static LoRaWANInterface lorawan(radio); |
mbed_official | 0:7037ed05f54f | 104 | |
mbed_official | 0:7037ed05f54f | 105 | /** |
mbed_official | 0:7037ed05f54f | 106 | * Application specific callbacks |
mbed_official | 0:7037ed05f54f | 107 | */ |
mbed_official | 0:7037ed05f54f | 108 | static lorawan_app_callbacks_t callbacks; |
mbed_official | 0:7037ed05f54f | 109 | |
mbed_official | 0:7037ed05f54f | 110 | /** |
mbed_official | 0:7037ed05f54f | 111 | * Entry point for application |
mbed_official | 0:7037ed05f54f | 112 | */ |
mbed_official | 0:7037ed05f54f | 113 | int main (void) |
mbed_official | 0:7037ed05f54f | 114 | { |
mbed_official | 0:7037ed05f54f | 115 | // setup tracing |
mbed_official | 0:7037ed05f54f | 116 | setup_trace(); |
mbed_official | 0:7037ed05f54f | 117 | |
mbed_official | 0:7037ed05f54f | 118 | // stores the status of a call to LoRaWAN protocol |
mbed_official | 0:7037ed05f54f | 119 | lorawan_status_t retcode; |
mbed_official | 0:7037ed05f54f | 120 | |
mbed_official | 0:7037ed05f54f | 121 | // Initialize LoRaWAN stack |
mbed_official | 2:dc95ac6d6d4e | 122 | if (lorawan.initialize(&ev_queue) != LORAWAN_STATUS_OK) { |
cdupaty | 43:74226f6ca42c | 123 | printf("LoRa initialization failed! \r\n"); |
mbed_official | 0:7037ed05f54f | 124 | return -1; |
mbed_official | 0:7037ed05f54f | 125 | } |
cdupaty | 43:74226f6ca42c | 126 | printf("\r\nTEST LORAWAN Lycee Fourcade \r\n"); |
cdupaty | 43:74226f6ca42c | 127 | printf("--------------------------- \r\n"); |
cdupaty | 43:74226f6ca42c | 128 | printf("\r\nMbed LoRaWANStack initialized \r\n"); |
mbed_official | 0:7037ed05f54f | 129 | |
mbed_official | 0:7037ed05f54f | 130 | // prepare application callbacks |
mbed_official | 0:7037ed05f54f | 131 | callbacks.events = mbed::callback(lora_event_handler); |
mbed_official | 2:dc95ac6d6d4e | 132 | lorawan.add_app_callbacks(&callbacks); |
mbed_official | 0:7037ed05f54f | 133 | |
mbed_official | 0:7037ed05f54f | 134 | // Set number of retries in case of CONFIRMED messages |
mbed_official | 2:dc95ac6d6d4e | 135 | if (lorawan.set_confirmed_msg_retries(CONFIRMED_MSG_RETRY_COUNTER) |
mbed_official | 0:7037ed05f54f | 136 | != LORAWAN_STATUS_OK) { |
cdupaty | 43:74226f6ca42c | 137 | printf("set_confirmed_msg_retries failed! \r\n\r\n"); |
mbed_official | 0:7037ed05f54f | 138 | return -1; |
mbed_official | 0:7037ed05f54f | 139 | } |
mbed_official | 0:7037ed05f54f | 140 | |
cdupaty | 43:74226f6ca42c | 141 | printf("CONFIRMED message retries : %d \r\n", |
mbed_official | 0:7037ed05f54f | 142 | CONFIRMED_MSG_RETRY_COUNTER); |
mbed_official | 0:7037ed05f54f | 143 | |
mbed_official | 0:7037ed05f54f | 144 | // Enable adaptive data rate |
mbed_official | 2:dc95ac6d6d4e | 145 | if (lorawan.enable_adaptive_datarate() != LORAWAN_STATUS_OK) { |
cdupaty | 43:74226f6ca42c | 146 | printf("enable_adaptive_datarate failed! \r\n"); |
mbed_official | 0:7037ed05f54f | 147 | return -1; |
mbed_official | 0:7037ed05f54f | 148 | } |
mbed_official | 0:7037ed05f54f | 149 | |
cdupaty | 43:74226f6ca42c | 150 | printf("Adaptive data rate (ADR) - Enabled \r\n"); |
mbed_official | 0:7037ed05f54f | 151 | |
mbed_official | 2:dc95ac6d6d4e | 152 | retcode = lorawan.connect(); |
mbed_official | 0:7037ed05f54f | 153 | |
mbed_official | 0:7037ed05f54f | 154 | if (retcode == LORAWAN_STATUS_OK || |
mbed_official | 0:7037ed05f54f | 155 | retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) { |
mbed_official | 0:7037ed05f54f | 156 | } else { |
cdupaty | 43:74226f6ca42c | 157 | printf("Connection error, code = %d \r\n", retcode); |
mbed_official | 0:7037ed05f54f | 158 | return -1; |
mbed_official | 0:7037ed05f54f | 159 | } |
mbed_official | 0:7037ed05f54f | 160 | |
cdupaty | 43:74226f6ca42c | 161 | printf("Connection - In Progress ...\r\n"); |
cdupaty | 43:74226f6ca42c | 162 | printf("it may take a while\r\n"); |
mbed_official | 0:7037ed05f54f | 163 | |
mbed_official | 0:7037ed05f54f | 164 | // make your event queue dispatching events forever |
mbed_official | 0:7037ed05f54f | 165 | ev_queue.dispatch_forever(); |
mbed_official | 3:8c7198d1a2a1 | 166 | |
mbed_official | 3:8c7198d1a2a1 | 167 | return 0; |
mbed_official | 0:7037ed05f54f | 168 | } |
mbed_official | 0:7037ed05f54f | 169 | |
mbed_official | 0:7037ed05f54f | 170 | /** |
mbed_official | 0:7037ed05f54f | 171 | * Sends a message to the Network Server |
mbed_official | 0:7037ed05f54f | 172 | */ |
mbed_official | 0:7037ed05f54f | 173 | static void send_message() |
mbed_official | 0:7037ed05f54f | 174 | { |
mbed_official | 0:7037ed05f54f | 175 | uint16_t packet_len; |
mbed_official | 0:7037ed05f54f | 176 | int16_t retcode; |
cdupaty | 43:74226f6ca42c | 177 | float sensor_value; // dummy |
cdupaty | 43:74226f6ca42c | 178 | float lum=0.0; // dummy |
cdupaty | 43:74226f6ca42c | 179 | float hum; // dummy |
cdupaty | 43:74226f6ca42c | 180 | float temp; // dummy |
cdupaty | 43:74226f6ca42c | 181 | float an; // AN1 on Arduino connectors |
cdupaty | 43:74226f6ca42c | 182 | int btBleu; // The bleu button on Nucleo board |
cdupaty | 43:74226f6ca42c | 183 | |
cdupaty | 43:74226f6ca42c | 184 | float tempLM35; |
cdupaty | 43:74226f6ca42c | 185 | static int cpt=0; |
mbed_official | 0:7037ed05f54f | 186 | |
cdupaty | 43:74226f6ca42c | 187 | printf("\n\rEmission : %d\r\n",++cpt); |
cdupaty | 43:74226f6ca42c | 188 | printf("-------------\r\n"); |
cdupaty | 43:74226f6ca42c | 189 | // Dummy sensors are used for test only |
mbed_official | 0:7037ed05f54f | 190 | if (ds1820.begin()) { |
mbed_official | 0:7037ed05f54f | 191 | ds1820.startConversion(); |
mbed_official | 0:7037ed05f54f | 192 | sensor_value = ds1820.read(); |
cdupaty | 43:74226f6ca42c | 193 | printf("Dummy Sensor Value = %3.1f \r\n", sensor_value); |
mbed_official | 0:7037ed05f54f | 194 | ds1820.startConversion(); |
mbed_official | 0:7037ed05f54f | 195 | } else { |
cdupaty | 43:74226f6ca42c | 196 | printf("No sensor found \r\n"); |
mbed_official | 0:7037ed05f54f | 197 | return; |
mbed_official | 0:7037ed05f54f | 198 | } |
cdupaty | 43:74226f6ca42c | 199 | lum+=111.0; |
cdupaty | 43:74226f6ca42c | 200 | if (lum>9999.0) lum=0.0; |
cdupaty | 43:74226f6ca42c | 201 | hum+=2.5; |
cdupaty | 43:74226f6ca42c | 202 | if (temp>100.0) temp=1.1; |
cdupaty | 43:74226f6ca42c | 203 | an=ana.read()*100.0; |
cdupaty | 43:74226f6ca42c | 204 | btBleu=bp.read(); |
cdupaty | 43:74226f6ca42c | 205 | |
cdupaty | 43:74226f6ca42c | 206 | // LM35 must be connected on A0 analog port |
cdupaty | 43:74226f6ca42c | 207 | tempLM35=lm35.read()*330.0; |
cdupaty | 43:74226f6ca42c | 208 | printf("Capteur LM35 : %3.2f C\r\n", tempLM35); |
cdupaty | 43:74226f6ca42c | 209 | |
mbed_official | 0:7037ed05f54f | 210 | |
cdupaty | 43:74226f6ca42c | 211 | // mise en forme format CAYENNE LPP |
cdupaty | 43:74226f6ca42c | 212 | // doc ici https://mydevices.com/cayenne/docs/lora/#lora-cayenne-low-power-payload |
cdupaty | 43:74226f6ca42c | 213 | // doc logiciel : CayenneLPP.h |
cdupaty | 43:74226f6ca42c | 214 | cayenne.reset(); |
cdupaty | 43:74226f6ca42c | 215 | cayenne.addLuminosity(1, lum); |
cdupaty | 43:74226f6ca42c | 216 | cayenne.addPresence(2,1); |
cdupaty | 43:74226f6ca42c | 217 | cayenne.addTemperature(3, temp); |
cdupaty | 43:74226f6ca42c | 218 | cayenne.addRelativeHumidity(4, hum); |
cdupaty | 43:74226f6ca42c | 219 | cayenne.addDigitalInput(5, btBleu); |
cdupaty | 43:74226f6ca42c | 220 | cayenne.addAnalogInput(6, an); |
cdupaty | 43:74226f6ca42c | 221 | cayenne.addDigitalOutput(7,0); |
cdupaty | 43:74226f6ca42c | 222 | cayenne.addTemperature(8, tempLM35); |
cdupaty | 43:74226f6ca42c | 223 | cayenne.addTemperature(9, sensor_value); |
cdupaty | 43:74226f6ca42c | 224 | |
cdupaty | 43:74226f6ca42c | 225 | cayenne.copy(tx_buffer); |
cdupaty | 43:74226f6ca42c | 226 | |
cdupaty | 43:74226f6ca42c | 227 | packet_len=cayenne.getSize(); |
cdupaty | 43:74226f6ca42c | 228 | printf("Emission du packet : \n\r"); |
cdupaty | 43:74226f6ca42c | 229 | for (int i=0;i<packet_len;i++) printf("%02X ",tx_buffer[i]); |
cdupaty | 43:74226f6ca42c | 230 | printf("\n\r"); |
cdupaty | 43:74226f6ca42c | 231 | // packet_len = sprintf((char*) tx_buffer, "Dummy Sensor Value is %3.1f", sensor_value); |
mbed_official | 0:7037ed05f54f | 232 | |
mbed_official | 2:dc95ac6d6d4e | 233 | retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, tx_buffer, packet_len, |
mbed_official | 0:7037ed05f54f | 234 | MSG_CONFIRMED_FLAG); |
mbed_official | 0:7037ed05f54f | 235 | |
mbed_official | 0:7037ed05f54f | 236 | if (retcode < 0) { |
mbed_official | 0:7037ed05f54f | 237 | retcode == LORAWAN_STATUS_WOULD_BLOCK ? printf("send - WOULD BLOCK\r\n") |
cdupaty | 43:74226f6ca42c | 238 | : printf("send() - Error code %d \r\n", retcode); |
mbed_official | 26:f07f5febf97f | 239 | |
mbed_official | 26:f07f5febf97f | 240 | if (retcode == LORAWAN_STATUS_WOULD_BLOCK) { |
mbed_official | 26:f07f5febf97f | 241 | //retry in 3 seconds |
mbed_official | 26:f07f5febf97f | 242 | if (MBED_CONF_LORA_DUTY_CYCLE_ON) { |
mbed_official | 26:f07f5febf97f | 243 | ev_queue.call_in(3000, send_message); |
mbed_official | 26:f07f5febf97f | 244 | } |
mbed_official | 26:f07f5febf97f | 245 | } |
mbed_official | 0:7037ed05f54f | 246 | return; |
mbed_official | 0:7037ed05f54f | 247 | } |
mbed_official | 0:7037ed05f54f | 248 | |
cdupaty | 43:74226f6ca42c | 249 | printf("%d bytes scheduled for transmission \r\n", retcode); |
mbed_official | 12:5015dfead3f2 | 250 | memset(tx_buffer, 0, sizeof(tx_buffer)); |
mbed_official | 0:7037ed05f54f | 251 | } |
mbed_official | 0:7037ed05f54f | 252 | |
mbed_official | 0:7037ed05f54f | 253 | /** |
mbed_official | 0:7037ed05f54f | 254 | * Receive a message from the Network Server |
mbed_official | 0:7037ed05f54f | 255 | */ |
mbed_official | 0:7037ed05f54f | 256 | static void receive_message() |
mbed_official | 0:7037ed05f54f | 257 | { |
mbed_official | 0:7037ed05f54f | 258 | int16_t retcode; |
cdupaty | 43:74226f6ca42c | 259 | uint8_t port; // var to store port number provided by the stack |
cdupaty | 43:74226f6ca42c | 260 | int flags; // var to store flags provided by the stack |
cdupaty | 43:74226f6ca42c | 261 | retcode = lorawan.receive( rx_buffer,sizeof(rx_buffer), port, flags); |
cdupaty | 43:74226f6ca42c | 262 | printf("\x1B[1m"); // yellow text |
mbed_official | 0:7037ed05f54f | 263 | if (retcode < 0) { |
cdupaty | 43:74226f6ca42c | 264 | printf("receive() - Error code %d \r\n", retcode); |
mbed_official | 0:7037ed05f54f | 265 | return; |
mbed_official | 0:7037ed05f54f | 266 | } |
cdupaty | 43:74226f6ca42c | 267 | printf(" Reception on port : %d \n",port); |
cdupaty | 43:74226f6ca42c | 268 | printf(" Flags are : %d \n",flags); |
cdupaty | 43:74226f6ca42c | 269 | printf(" Data: "); |
mbed_official | 0:7037ed05f54f | 270 | |
mbed_official | 0:7037ed05f54f | 271 | for (uint8_t i = 0; i < retcode; i++) { |
cdupaty | 43:74226f6ca42c | 272 | printf("%02X ", rx_buffer[i]); |
mbed_official | 0:7037ed05f54f | 273 | } |
mbed_official | 0:7037ed05f54f | 274 | |
cdupaty | 43:74226f6ca42c | 275 | printf("\n\r Data Length: %d\r\n", retcode); |
cdupaty | 43:74226f6ca42c | 276 | printf("\x1B[0m"); // white text |
cdupaty | 43:74226f6ca42c | 277 | printf("End reception\n\r"); |
cdupaty | 43:74226f6ca42c | 278 | |
cdupaty | 43:74226f6ca42c | 279 | if (rx_buffer[2]==0x64) ledTest=1; |
cdupaty | 43:74226f6ca42c | 280 | if (rx_buffer[2]==0x00) ledTest=0; |
mbed_official | 0:7037ed05f54f | 281 | |
mbed_official | 12:5015dfead3f2 | 282 | memset(rx_buffer, 0, sizeof(rx_buffer)); |
mbed_official | 0:7037ed05f54f | 283 | } |
mbed_official | 0:7037ed05f54f | 284 | |
mbed_official | 0:7037ed05f54f | 285 | /** |
mbed_official | 0:7037ed05f54f | 286 | * Event handler |
mbed_official | 0:7037ed05f54f | 287 | */ |
mbed_official | 0:7037ed05f54f | 288 | static void lora_event_handler(lorawan_event_t event) |
mbed_official | 0:7037ed05f54f | 289 | { |
mbed_official | 0:7037ed05f54f | 290 | switch (event) { |
mbed_official | 0:7037ed05f54f | 291 | case CONNECTED: |
cdupaty | 43:74226f6ca42c | 292 | printf("Connection - Successful \r\n"); |
mbed_official | 0:7037ed05f54f | 293 | if (MBED_CONF_LORA_DUTY_CYCLE_ON) { |
mbed_official | 0:7037ed05f54f | 294 | send_message(); |
mbed_official | 0:7037ed05f54f | 295 | } else { |
mbed_official | 0:7037ed05f54f | 296 | ev_queue.call_every(TX_TIMER, send_message); |
mbed_official | 0:7037ed05f54f | 297 | } |
mbed_official | 0:7037ed05f54f | 298 | |
mbed_official | 0:7037ed05f54f | 299 | break; |
mbed_official | 0:7037ed05f54f | 300 | case DISCONNECTED: |
mbed_official | 0:7037ed05f54f | 301 | ev_queue.break_dispatch(); |
cdupaty | 43:74226f6ca42c | 302 | printf("Disconnected Successfully \r\n"); |
mbed_official | 0:7037ed05f54f | 303 | break; |
mbed_official | 0:7037ed05f54f | 304 | case TX_DONE: |
cdupaty | 43:74226f6ca42c | 305 | printf("Message Sent to Network Server \r\n"); |
mbed_official | 0:7037ed05f54f | 306 | if (MBED_CONF_LORA_DUTY_CYCLE_ON) { |
mbed_official | 0:7037ed05f54f | 307 | send_message(); |
mbed_official | 0:7037ed05f54f | 308 | } |
mbed_official | 0:7037ed05f54f | 309 | break; |
mbed_official | 0:7037ed05f54f | 310 | case TX_TIMEOUT: |
mbed_official | 0:7037ed05f54f | 311 | case TX_ERROR: |
mbed_official | 0:7037ed05f54f | 312 | case TX_CRYPTO_ERROR: |
mbed_official | 0:7037ed05f54f | 313 | case TX_SCHEDULING_ERROR: |
cdupaty | 43:74226f6ca42c | 314 | printf("Transmission Error - EventCode = %d \r\n", event); |
mbed_official | 0:7037ed05f54f | 315 | // try again |
mbed_official | 0:7037ed05f54f | 316 | if (MBED_CONF_LORA_DUTY_CYCLE_ON) { |
mbed_official | 0:7037ed05f54f | 317 | send_message(); |
mbed_official | 0:7037ed05f54f | 318 | } |
mbed_official | 0:7037ed05f54f | 319 | break; |
mbed_official | 0:7037ed05f54f | 320 | case RX_DONE: |
cdupaty | 43:74226f6ca42c | 321 | printf("Received message from Network Server \r\n"); |
mbed_official | 0:7037ed05f54f | 322 | receive_message(); |
mbed_official | 0:7037ed05f54f | 323 | break; |
mbed_official | 0:7037ed05f54f | 324 | case RX_TIMEOUT: |
mbed_official | 0:7037ed05f54f | 325 | case RX_ERROR: |
cdupaty | 43:74226f6ca42c | 326 | printf("Error in reception - Code = %d \r\n", event); |
mbed_official | 0:7037ed05f54f | 327 | break; |
mbed_official | 0:7037ed05f54f | 328 | case JOIN_FAILURE: |
cdupaty | 43:74226f6ca42c | 329 | printf("OTAA Failed - Check Keys \r\n"); |
mbed_official | 0:7037ed05f54f | 330 | break; |
mbed_official | 26:f07f5febf97f | 331 | case UPLINK_REQUIRED: |
cdupaty | 43:74226f6ca42c | 332 | printf("Uplink required by NS \r\n"); |
mbed_official | 26:f07f5febf97f | 333 | if (MBED_CONF_LORA_DUTY_CYCLE_ON) { |
mbed_official | 26:f07f5febf97f | 334 | send_message(); |
mbed_official | 26:f07f5febf97f | 335 | } |
mbed_official | 26:f07f5febf97f | 336 | break; |
mbed_official | 0:7037ed05f54f | 337 | default: |
mbed_official | 0:7037ed05f54f | 338 | MBED_ASSERT("Unknown Event"); |
mbed_official | 0:7037ed05f54f | 339 | } |
mbed_official | 0:7037ed05f54f | 340 | } |
mbed_official | 0:7037ed05f54f | 341 | |
mbed_official | 0:7037ed05f54f | 342 | // EOF |