This is an example application based on Mbed-OS LoRaWAN protocol APIs. The Mbed-OS LoRaWAN stack implementation is compliant with LoRaWAN v1.0.2 specification.

Dependencies:   Lorawan_Version_0_1

Dependents:   Lorawan_Version_0_1

Committer:
jacktractive
Date:
Tue Feb 04 14:38:51 2020 +0000
Revision:
74:b05ae4efbd12
Parent:
73:974c1df98553
final 1_0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jacktractive 69:316fee01f5d9 1 #include <stdio.h>
jacktractive 69:316fee01f5d9 2 #include "lorawan/LoRaWANInterface.h"
jacktractive 69:316fee01f5d9 3 #include "lorawan/system/lorawan_data_structures.h"
jacktractive 69:316fee01f5d9 4 #include "Event.h"
jacktractive 69:316fee01f5d9 5 #include "lora_radio_helper.h"
jacktractive 69:316fee01f5d9 6 #include <mbed.h>
jacktractive 69:316fee01f5d9 7 #include "Light.h"
jacktractive 69:316fee01f5d9 8 #include "GPS.h"
jacktractive 69:316fee01f5d9 9 #include "Lora.h"
jacktractive 69:316fee01f5d9 10
jacktractive 69:316fee01f5d9 11 DigitalOut StatusLED(PB_8);
jacktractive 69:316fee01f5d9 12
jacktractive 73:974c1df98553 13
jacktractive 69:316fee01f5d9 14 static EventQueue *Ref_Events;
jacktractive 69:316fee01f5d9 15 static GPS *Ref_GPS;
jacktractive 69:316fee01f5d9 16 static Light *Ref_Light;
jacktractive 69:316fee01f5d9 17
jacktractive 71:ca2425c0a864 18 bool still_sending;
jacktractive 71:ca2425c0a864 19
jacktractive 69:316fee01f5d9 20 bool connected;
jacktractive 69:316fee01f5d9 21
jacktractive 71:ca2425c0a864 22
jacktractive 69:316fee01f5d9 23 Lora::Lora(EventQueue *q, Light *l, GPS *gps)
jacktractive 69:316fee01f5d9 24 {
jacktractive 71:ca2425c0a864 25 //wir speichern uns Pointer auf die Objekte die wir in der Klasse verwenden möchten
jacktractive 69:316fee01f5d9 26 Ref_Events=q;
jacktractive 69:316fee01f5d9 27 Ref_GPS= gps;
jacktractive 69:316fee01f5d9 28 Ref_Light = l;
jacktractive 69:316fee01f5d9 29 }
jacktractive 69:316fee01f5d9 30 using namespace events;
jacktractive 69:316fee01f5d9 31
jacktractive 69:316fee01f5d9 32 // Max payload size can be LORAMAC_PHY_MAXPAYLOAD.
jacktractive 69:316fee01f5d9 33 uint8_t tx_buffer[13];
jacktractive 69:316fee01f5d9 34 uint8_t rx_buffer[13];
jacktractive 69:316fee01f5d9 35
jacktractive 69:316fee01f5d9 36 /**
jacktractive 69:316fee01f5d9 37 * Maximum number of retries for CONFIRMED messages before giving up
jacktractive 69:316fee01f5d9 38 */
jacktractive 70:65b2f1cc2859 39 #define CONFIRMED_MSG_RETRY_COUNTER 2
jacktractive 69:316fee01f5d9 40
jacktractive 69:316fee01f5d9 41 /**
jacktractive 69:316fee01f5d9 42 * Event handler.
jacktractive 69:316fee01f5d9 43 *
jacktractive 69:316fee01f5d9 44 * This will be passed to the LoRaWAN stack to queue events for the
jacktractive 69:316fee01f5d9 45 * application which in turn drive the application.
jacktractive 69:316fee01f5d9 46 */
jacktractive 69:316fee01f5d9 47 static void lora_event_handler(lorawan_event_t event);
jacktractive 69:316fee01f5d9 48
jacktractive 69:316fee01f5d9 49 /**
jacktractive 69:316fee01f5d9 50 * Constructing Mbed LoRaWANInterface and passing it the radio object from lora_radio_helper.
jacktractive 69:316fee01f5d9 51 */
jacktractive 69:316fee01f5d9 52 static LoRaWANInterface lorawan(radio);
jacktractive 69:316fee01f5d9 53
jacktractive 69:316fee01f5d9 54 /**
jacktractive 69:316fee01f5d9 55 * Application specific callbacks
jacktractive 69:316fee01f5d9 56 */
jacktractive 69:316fee01f5d9 57 static lorawan_app_callbacks_t callbacks;
jacktractive 69:316fee01f5d9 58
jacktractive 69:316fee01f5d9 59 /**
jacktractive 69:316fee01f5d9 60 * Sends a message to the Network Server
jacktractive 69:316fee01f5d9 61 */
jacktractive 69:316fee01f5d9 62 static void send_message()
jacktractive 69:316fee01f5d9 63 {
jacktractive 69:316fee01f5d9 64 uint16_t packet_len;
jacktractive 69:316fee01f5d9 65 int16_t retcode;
jacktractive 71:ca2425c0a864 66
jacktractive 71:ca2425c0a864 67 still_sending=true; // wir setzten das Bit zurück wenn Nachricht erfolgreich rausgegangen ist
jacktractive 71:ca2425c0a864 68
jacktractive 71:ca2425c0a864 69 if (!Ref_GPS->GPS_signal_okay){
jacktractive 71:ca2425c0a864 70 packet_len = sizeof(tx_buffer[0]);
jacktractive 71:ca2425c0a864 71 retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, tx_buffer, 1,
jacktractive 73:974c1df98553 72 MSG_UNCONFIRMED_FLAG);
jacktractive 73:974c1df98553 73 printf("\n[LORA] Send: #00 lifetick");
jacktractive 73:974c1df98553 74
jacktractive 71:ca2425c0a864 75 }
jacktractive 71:ca2425c0a864 76 else{
jacktractive 71:ca2425c0a864 77 packet_len = sizeof(tx_buffer);
jacktractive 71:ca2425c0a864 78 retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, tx_buffer, packet_len,
jacktractive 71:ca2425c0a864 79 MSG_UNCONFIRMED_FLAG);
jacktractive 73:974c1df98553 80
jacktractive 73:974c1df98553 81 printf("\n[LORA] Send: %02X:%02X%02X%02X%02X:%02X%02X%02X%02X:%02X%02X%02X%02X \n",
jacktractive 73:974c1df98553 82 tx_buffer[0],
jacktractive 73:974c1df98553 83 tx_buffer[1],tx_buffer[2],tx_buffer[3],tx_buffer[4],
jacktractive 73:974c1df98553 84 tx_buffer[5],tx_buffer[6],tx_buffer[7],tx_buffer[8],
jacktractive 73:974c1df98553 85 tx_buffer[9],tx_buffer[10],tx_buffer[11],tx_buffer[12]);
jacktractive 73:974c1df98553 86
jacktractive 71:ca2425c0a864 87 }
jacktractive 71:ca2425c0a864 88
jacktractive 69:316fee01f5d9 89 if (retcode < 0) {
jacktractive 69:316fee01f5d9 90 retcode == LORAWAN_STATUS_WOULD_BLOCK ? printf("send - WOULD BLOCK\r\n")
jacktractive 69:316fee01f5d9 91 : printf("\r\n send() - Error code %d \r\n", retcode);
jacktractive 69:316fee01f5d9 92 return;
jacktractive 69:316fee01f5d9 93 }
jacktractive 69:316fee01f5d9 94
jacktractive 69:316fee01f5d9 95 printf("\r\n %d bytes scheduled for transmission \r\n", retcode);
jacktractive 69:316fee01f5d9 96 memset(tx_buffer, 0, sizeof(tx_buffer));
jacktractive 69:316fee01f5d9 97 }
jacktractive 69:316fee01f5d9 98
jacktractive 69:316fee01f5d9 99
jacktractive 69:316fee01f5d9 100 void Lora::send_Position_to_Lora(uint8_t OptCode,float time,float longitude,float latitude)
jacktractive 69:316fee01f5d9 101 {
jacktractive 73:974c1df98553 102
jacktractive 73:974c1df98553 103 if (latitude == 0){
jacktractive 73:974c1df98553 104 OptCode = 0x00 ; // only send x00 as Livebyte when no gps available
jacktractive 71:ca2425c0a864 105 tx_buffer[0] = OptCode;
jacktractive 71:ca2425c0a864 106 }
jacktractive 73:974c1df98553 107 else{
jacktractive 73:974c1df98553 108 //1Byte Opcode 0x01 4Byte Timestemp 4Byte Longitude 4Byte Latitude
jacktractive 73:974c1df98553 109 uint8_t tmpbytes[sizeof(float)];
jacktractive 73:974c1df98553 110
jacktractive 73:974c1df98553 111 if (Ref_GPS->mode == 1) OptCode = 0x01 ;
jacktractive 73:974c1df98553 112 if (Ref_GPS->mode == 2) OptCode = 0x02 ;
jacktractive 73:974c1df98553 113 if (Ref_GPS->mode == 3) OptCode = 0x03 ;
jacktractive 73:974c1df98553 114
jacktractive 73:974c1df98553 115 tx_buffer[0] = OptCode;
jacktractive 70:65b2f1cc2859 116
jacktractive 73:974c1df98553 117 *((float *)tmpbytes) = time;
jacktractive 73:974c1df98553 118 tx_buffer[1] = tmpbytes[0];
jacktractive 73:974c1df98553 119 tx_buffer[2] = tmpbytes[1];
jacktractive 73:974c1df98553 120 tx_buffer[3] = tmpbytes[2];
jacktractive 73:974c1df98553 121 tx_buffer[4] = tmpbytes[3];
jacktractive 69:316fee01f5d9 122
jacktractive 73:974c1df98553 123 *((float *)tmpbytes) = longitude;
jacktractive 73:974c1df98553 124 tx_buffer[5] = tmpbytes[0];
jacktractive 73:974c1df98553 125 tx_buffer[6] = tmpbytes[1];
jacktractive 73:974c1df98553 126 tx_buffer[7] = tmpbytes[2];
jacktractive 73:974c1df98553 127 tx_buffer[8] = tmpbytes[3];
jacktractive 73:974c1df98553 128
jacktractive 73:974c1df98553 129 *((float *)tmpbytes) = latitude;
jacktractive 73:974c1df98553 130 tx_buffer[9] = tmpbytes[0];
jacktractive 73:974c1df98553 131 tx_buffer[10] = tmpbytes[1];
jacktractive 73:974c1df98553 132 tx_buffer[11] = tmpbytes[2];
jacktractive 73:974c1df98553 133 tx_buffer[12] = tmpbytes[3];
jacktractive 73:974c1df98553 134 }
jacktractive 73:974c1df98553 135
jacktractive 73:974c1df98553 136 if (connected && !still_sending)
jacktractive 73:974c1df98553 137 {
jacktractive 69:316fee01f5d9 138 send_message();
jacktractive 70:65b2f1cc2859 139 }
jacktractive 71:ca2425c0a864 140 else{
jacktractive 71:ca2425c0a864 141 if (!connected) printf("\n[LORA] not yet connected\n"); // sonst gibt es natürlich SendeErrors
jacktractive 71:ca2425c0a864 142 if (still_sending) printf("\n[LORA] Still Sending - waiting for reply\n"); // sonst machen wir unseren Eventhandler voll
jacktractive 71:ca2425c0a864 143 }
jacktractive 73:974c1df98553 144
jacktractive 73:974c1df98553 145
jacktractive 69:316fee01f5d9 146 }
jacktractive 69:316fee01f5d9 147
jacktractive 69:316fee01f5d9 148
jacktractive 69:316fee01f5d9 149 /**
jacktractive 69:316fee01f5d9 150 * Sends a message to the Network Server
jacktractive 69:316fee01f5d9 151 */
jacktractive 69:316fee01f5d9 152 int Lora::Lora_init()
jacktractive 69:316fee01f5d9 153 {
jacktractive 71:ca2425c0a864 154 StatusLED.write(0);
jacktractive 71:ca2425c0a864 155
jacktractive 69:316fee01f5d9 156 // Initialize LoRaWAN stack
jacktractive 69:316fee01f5d9 157 if (lorawan.initialize(Ref_Events) != LORAWAN_STATUS_OK) {
jacktractive 69:316fee01f5d9 158 printf("\r\n LoRa initialization failed! \r\n");
jacktractive 69:316fee01f5d9 159 return -1;
jacktractive 69:316fee01f5d9 160 }
jacktractive 69:316fee01f5d9 161
jacktractive 69:316fee01f5d9 162 printf("\r\n Mbed LoRaWANStack initialized \r\n");
jacktractive 69:316fee01f5d9 163
jacktractive 69:316fee01f5d9 164 // prepare application callbacks
jacktractive 69:316fee01f5d9 165 callbacks.events = mbed::callback(lora_event_handler);
jacktractive 69:316fee01f5d9 166 lorawan.add_app_callbacks(&callbacks);
jacktractive 69:316fee01f5d9 167
jacktractive 69:316fee01f5d9 168 // Set number of retries in case of CONFIRMED messages
jacktractive 70:65b2f1cc2859 169 if (lorawan.set_confirmed_msg_retries(CONFIRMED_MSG_RETRY_COUNTER)!= LORAWAN_STATUS_OK) {
jacktractive 69:316fee01f5d9 170 printf("\r\n set_confirmed_msg_retries failed! \r\n\r\n");
jacktractive 69:316fee01f5d9 171 return -1;
jacktractive 69:316fee01f5d9 172 }
jacktractive 69:316fee01f5d9 173
jacktractive 70:65b2f1cc2859 174 printf("\r\n CONFIRMED message retries : %d \r\n", CONFIRMED_MSG_RETRY_COUNTER);
jacktractive 69:316fee01f5d9 175
jacktractive 69:316fee01f5d9 176 // Enable adaptive data rate
jacktractive 69:316fee01f5d9 177 if (lorawan.enable_adaptive_datarate() != LORAWAN_STATUS_OK) {
jacktractive 69:316fee01f5d9 178 printf("\r\n enable_adaptive_datarate failed! \r\n");
jacktractive 69:316fee01f5d9 179 return -1;
jacktractive 69:316fee01f5d9 180 }
jacktractive 69:316fee01f5d9 181
jacktractive 69:316fee01f5d9 182 printf("\r\n Adaptive data rate (ADR) - Enabled \r\n");
jacktractive 69:316fee01f5d9 183
jacktractive 69:316fee01f5d9 184 // stores the status of a call to LoRaWAN protocol
jacktractive 69:316fee01f5d9 185 lorawan_status_t retcode;
jacktractive 69:316fee01f5d9 186 retcode = lorawan.connect();
jacktractive 69:316fee01f5d9 187 if (retcode == LORAWAN_STATUS_OK ||
jacktractive 69:316fee01f5d9 188 retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) {
jacktractive 69:316fee01f5d9 189 } else {
jacktractive 69:316fee01f5d9 190 printf("\r\n Connection error, code = %d \r\n", retcode);
jacktractive 69:316fee01f5d9 191 return -1;
jacktractive 69:316fee01f5d9 192 }
jacktractive 69:316fee01f5d9 193
jacktractive 69:316fee01f5d9 194 printf("\r\n Connection - In Progress ...\r\n");
jacktractive 69:316fee01f5d9 195 return 0;
jacktractive 69:316fee01f5d9 196 }
jacktractive 69:316fee01f5d9 197
jacktractive 69:316fee01f5d9 198
jacktractive 69:316fee01f5d9 199 /**
jacktractive 69:316fee01f5d9 200 * Receive a message from the Network Server
jacktractive 69:316fee01f5d9 201 */
jacktractive 69:316fee01f5d9 202 static void receive_message()
jacktractive 69:316fee01f5d9 203 {
jacktractive 69:316fee01f5d9 204 uint8_t port;
jacktractive 69:316fee01f5d9 205 int flags;
jacktractive 69:316fee01f5d9 206 int16_t retcode = lorawan.receive(rx_buffer, sizeof(rx_buffer), port, flags);
jacktractive 69:316fee01f5d9 207
jacktractive 69:316fee01f5d9 208 if (retcode < 0) {
jacktractive 69:316fee01f5d9 209 printf("\r\n receive() - Error code %d \r\n", retcode);
jacktractive 69:316fee01f5d9 210 return;
jacktractive 69:316fee01f5d9 211 }
jacktractive 69:316fee01f5d9 212
jacktractive 69:316fee01f5d9 213 printf(" RX Data on port %u (%d bytes): ", port, retcode);
jacktractive 69:316fee01f5d9 214 for (uint8_t i = 0; i < retcode; i++) {
jacktractive 69:316fee01f5d9 215 printf("%02x ", rx_buffer[i]);
jacktractive 69:316fee01f5d9 216 }
jacktractive 71:ca2425c0a864 217
jacktractive 71:ca2425c0a864 218 //Fernsteuerung Blinken
jacktractive 71:ca2425c0a864 219 if (rx_buffer[0] == 0xb1){Ref_Light->Blinken_ein(15000);}
jacktractive 71:ca2425c0a864 220
jacktractive 71:ca2425c0a864 221 //Fernsteuerung GPS manuel an/aus
jacktractive 73:974c1df98553 222 if (rx_buffer[0] == 0xc1) Ref_GPS->mode=1;
jacktractive 73:974c1df98553 223 if (rx_buffer[0] == 0xc2) Ref_GPS->mode=2;
jacktractive 73:974c1df98553 224 if (rx_buffer[0] == 0xc3) Ref_GPS->mode=3;
jacktractive 69:316fee01f5d9 225
jacktractive 71:ca2425c0a864 226 //Eingangsfach wieder leer machen
jacktractive 69:316fee01f5d9 227 memset(rx_buffer, 0, sizeof(rx_buffer));
jacktractive 69:316fee01f5d9 228 }
jacktractive 69:316fee01f5d9 229
jacktractive 69:316fee01f5d9 230 /**
jacktractive 69:316fee01f5d9 231 * Event handler
jacktractive 69:316fee01f5d9 232 */
jacktractive 69:316fee01f5d9 233 static void lora_event_handler(lorawan_event_t event)
jacktractive 69:316fee01f5d9 234 {
jacktractive 69:316fee01f5d9 235 switch (event) {
jacktractive 69:316fee01f5d9 236 case CONNECTED:
jacktractive 69:316fee01f5d9 237 connected=1;
jacktractive 71:ca2425c0a864 238 StatusLED.write(1);
jacktractive 69:316fee01f5d9 239 printf("\r\n Connection - Successful \r\n");
jacktractive 69:316fee01f5d9 240 break;
jacktractive 69:316fee01f5d9 241 case DISCONNECTED:
jacktractive 69:316fee01f5d9 242 connected=0;
jacktractive 69:316fee01f5d9 243 printf("\r\n Disconnected Successfully \r\n");
jacktractive 69:316fee01f5d9 244 break;
jacktractive 69:316fee01f5d9 245 case TX_DONE:
jacktractive 69:316fee01f5d9 246 printf("\r\n Message Sent to Network Server \r\n");
jacktractive 69:316fee01f5d9 247 if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
jacktractive 71:ca2425c0a864 248 still_sending=false;
jacktractive 69:316fee01f5d9 249 }
jacktractive 69:316fee01f5d9 250 break;
jacktractive 69:316fee01f5d9 251 case TX_TIMEOUT:
jacktractive 69:316fee01f5d9 252
jacktractive 69:316fee01f5d9 253 connected=0;
jacktractive 69:316fee01f5d9 254 case TX_ERROR:
jacktractive 69:316fee01f5d9 255 case TX_CRYPTO_ERROR:
jacktractive 69:316fee01f5d9 256 case TX_SCHEDULING_ERROR:
jacktractive 69:316fee01f5d9 257 printf("\r\n Transmission Error - EventCode = %d \r\n", event);
jacktractive 69:316fee01f5d9 258 // try again
jacktractive 69:316fee01f5d9 259 if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
jacktractive 69:316fee01f5d9 260 send_message();
jacktractive 69:316fee01f5d9 261 }
jacktractive 69:316fee01f5d9 262 break;
jacktractive 69:316fee01f5d9 263 case RX_DONE:
jacktractive 69:316fee01f5d9 264 printf("\r\n Received message from Network Server \r\n");
jacktractive 69:316fee01f5d9 265 receive_message();
jacktractive 69:316fee01f5d9 266 break;
jacktractive 69:316fee01f5d9 267 case RX_TIMEOUT:
jacktractive 69:316fee01f5d9 268 case RX_ERROR:
jacktractive 69:316fee01f5d9 269 printf("\r\n Error in reception - Code = %d \r\n", event);
jacktractive 69:316fee01f5d9 270 break;
jacktractive 69:316fee01f5d9 271 case JOIN_FAILURE:
jacktractive 69:316fee01f5d9 272 printf("\r\n OTAA Failed - Check Keys \r\n");
jacktractive 69:316fee01f5d9 273 break;
jacktractive 69:316fee01f5d9 274 case UPLINK_REQUIRED:
jacktractive 69:316fee01f5d9 275 printf("\r\n Uplink required by NS \r\n");
jacktractive 69:316fee01f5d9 276 if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
jacktractive 69:316fee01f5d9 277 send_message();
jacktractive 69:316fee01f5d9 278 }
jacktractive 69:316fee01f5d9 279 break;
jacktractive 69:316fee01f5d9 280 default:
jacktractive 69:316fee01f5d9 281 MBED_ASSERT("Unknown Event");
jacktractive 69:316fee01f5d9 282 }
jacktractive 69:316fee01f5d9 283 }
jacktractive 69:316fee01f5d9 284
jacktractive 69:316fee01f5d9 285
jacktractive 69:316fee01f5d9 286
jacktractive 69:316fee01f5d9 287
jacktractive 69:316fee01f5d9 288
jacktractive 69:316fee01f5d9 289 // EOF