Embedded System Design 2 - Project
lora_wrappers.c File Reference

LoRa wrapper methods. More...

#include <stdlib.h>
#include <stdbool.h>
#include "em_gpio.h"
#include "em_leuart.h"
#include "lora.h"
#include "lpp.h"
#include "pm.h"
#include "lora_settings.h"
#include "lora_wrappers.h"
#include "pin_mapping.h"
#include "debug_dbprint.h"
#include "datatypes.h"
#include "util.h"

Go to the source code of this file.

Functions

void initLoRaWAN (void)
 Initialize LoRaWAN functionality. More...
 
void disableLoRaWAN (void)
 Disable LoRaWAN functionality. More...
 
void sleepLoRaWAN (uint32_t sSleep)
 Let the LoRaWAN module sleep for a specified amount of time. More...
 
void wakeLoRaWAN (void)
 Wake up the LoRaWAN module early after putting it to sleep using sleepLoRaWAN. More...
 
void sendMeasurements (MeasurementData_t data)
 Send measured battery voltages and internal and external temperatures to the cloud using LoRaWAN. More...
 
void sendStormDetected (bool stormDetected)
 Send a packet to the cloud using LoRaWAN to indicate that a storm has been detected. More...
 
void sendCableBroken (bool cableBroken)
 Send a packet to the cloud using LoRaWAN to indicate that the cable is broken. More...
 
void sendStatus (uint8_t status)
 Send a packet to indicate a status. More...
 
void sendTest (MeasurementData_t data)
 Send ONE measured battery voltage, internal and external temperature, stormDetected, cableBroken and status value to the cloud using LoRaWAN. This method uses the deprecated methods to test if the data gets send correctly. More...
 

Variables

LoRaSettings_t loraSettings = LORA_INIT_MY_DEVICE
 
LoRaStatus_t loraStatus
 
LPP_Buffer_t appData
 

Detailed Description

LoRa wrapper methods.

Version
2.4
Author
Benjamin Van der Smissen
Heavily modified by Brecht Van Eeckhoudt

Versions

  • v1.0: Started with original code from Benjamin.
  • v1.1: Modified a lot of code, implemented custom LPP data type methods.
  • v1.2: Updated code to use new functionality to add data to the LPP packet.
  • v1.3: Added method to use deprecated methods to test if data gets send correctly.
  • v1.4: Changed error numbering and removed unnecessary variables and definitions.
  • v1.5: Moved data.index reset to LoRaWAN sending functionality.
  • v1.6: Moved data.index reset back to main.c because it doesn't affect the correct variable here.
  • v2.0: Added functionality to exit methods after error call and updated version number.
  • v2.1: Added extra dbprint debugging statements.
  • v2.2: Fixed suboptimal buffer logic causing lockups after some runtime.
  • v2.3: Chanced logic to clear the buffer before going to sleep.
  • v2.4: Removed static before the local variables (not necessary).

Todo:
Future improvements:
  • Save LoRaWAN settings during INIT before calling disableLoRaWAN?
    • Should be possible in ABP (saving to EEPROM? saveMAC?) See reference manual!
    • Update code where initLoRaWAN and other methods are called.
  • Fix sleepLoRaWAN and wakeLoRaWAN methods.
    • First separate ULFRCO definition in delay.c

License

Copyright (C) 2019 - Brecht Van Eeckhoudt

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

A copy of the GNU General Public License can be found in the LICENSE file along with this source code.


Some methods use code obtained from examples from Silicon Labs' GitHub. These sections are licensed under the Silabs License Agreement. See the file "Silabs_License_Agreement.txt" for details. Before using this software for any purpose, you must agree to the terms of that agreement.

Definition in file lora_wrappers.c.

Function Documentation

◆ disableLoRaWAN()

void disableLoRaWAN ( void  )

Disable LoRaWAN functionality.

Definition at line 115 of file lora_wrappers.c.

116 {
117  LEUART_Reset(RN2483_UART);
119  GPIO_PinOutClear(RN2483_RESET_PORT, RN2483_RESET_PIN);
120  GPIO_PinOutClear(RN2483_RX_PORT, RN2483_RX_PIN);
121  GPIO_PinOutClear(RN2483_TX_PORT, RN2483_TX_PIN);
122 
123 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
124  dbinfo("LoRaWAN disabled.");
125 #endif /* DEBUG_DBPRINT */
126 
127 }
#define RN2483_RESET_PORT
Definition: pin_mapping.h:121
#define RN2483_RESET_PIN
Definition: pin_mapping.h:122
#define RN2483_UART
Definition: pin_mapping.h:115
#define RN2483_TX_PIN
Definition: pin_mapping.h:118
#define RN2483_RX_PORT
Definition: pin_mapping.h:119
void dbinfo(char *message)
Print an info string (char array) to USARTx and go to the next line.
Definition: dbprint.c:503
#define RN2483_TX_PORT
Definition: pin_mapping.h:117
#define RN2483_RX_PIN
Definition: pin_mapping.h:120
void PM_Disable(PM_SubSystem_t pmss)
Definition: pm.c:58
Definition: pm.h:26

◆ initLoRaWAN()

void initLoRaWAN ( void  )

Initialize LoRaWAN functionality.

Definition at line 92 of file lora_wrappers.c.

93 {
94  // Before: memset(&appData, 0, sizeof(appData));
95  appData.length = 0;
96  appData.fill = 0;
97  appData.buffer = NULL;
98 
99  /* Initialize LoRaWAN communication */
101 
102  if (loraStatus != JOINED) error(30);
103 
104 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
105  else dbinfo("LoRaWAN initialized.");
106 #endif /* DEBUG_DBPRINT */
107 
108 }
uint8_t fill
Definition: lpp.h:41
LoRaSettings_t loraSettings
Definition: lora_wrappers.c:83
void dbinfo(char *message)
Print an info string (char array) to USARTx and go to the next line.
Definition: dbprint.c:503
Definition: lora.h:34
void error(uint8_t number)
Error method.
Definition: util.c:131
uint8_t * buffer
Definition: lpp.h:40
LoRaStatus_t LoRa_Init(LoRaSettings_t init)
Definition: lora.c:37
LoRaStatus_t loraStatus
Definition: lora_wrappers.c:84
LPP_Buffer_t appData
Definition: lora_wrappers.c:85
uint8_t length
Definition: lpp.h:42

◆ sendCableBroken()

void sendCableBroken ( bool  cableBroken)

Send a packet to the cloud using LoRaWAN to indicate that the cable is broken.

The value gets added to the LPP packet following the custom message convention.

Parameters
[in]cableBroken
  • true - The cable is intact.
  • false - The cable is broken!

Definition at line 251 of file lora_wrappers.c.

252 {
253  /* Initialize LPP-formatted payload - We need 4 bytes */
254  if (!LPP_InitBuffer(&appData, 4))
255  {
256  error(37);
257  return; /* Exit function */
258  }
259 
260  /* Add value to the LPP packet using the custom convention */
261  if (!LPP_AddCableBroken(&appData, cableBroken))
262  {
263  error(38);
264  return; /* Exit function */
265  }
266 
267 
268  /* Send custom LPP-like-formatted payload */
270  {
271  error(39);
272  return; /* Exit function */
273  }
274 
275  LPP_FreeBuffer(&appData); // Clear buffer before going to sleep
276 }
bool LPP_InitBuffer(LPP_Buffer_t *b, uint8_t size)
Definition: lpp.c:84
#define LORA_UNCONFIMED
Definition: lora.h:23
void error(uint8_t number)
Error method.
Definition: util.c:131
Definition: lora.h:35
bool LPP_AddCableBroken(LPP_Buffer_t *b, uint8_t cableBroken)
Add a value to indicate that the cable has been broken to the LPP packet following the custom message...
Definition: lpp.c:317
void LPP_FreeBuffer(LPP_Buffer_t *b)
Definition: lpp.c:129
LoRaStatus_t LoRa_SendLppBuffer(LPP_Buffer_t b, bool ackNoAck)
Definition: lora.c:61
LPP_Buffer_t appData
Definition: lora_wrappers.c:85

◆ sendMeasurements()

void sendMeasurements ( MeasurementData_t  data)

Send measured battery voltages and internal and external temperatures to the cloud using LoRaWAN.

The measurements get added to the LPP packet following the custom message convention to save bytes to send.

Parameters
[in]dataThe struct which contains the measurements to send using LoRaWAN.

Definition at line 166 of file lora_wrappers.c.

167 {
168  /* Initialize LPP-formatted payload
169  * For 6 measurements we need a max amount of 43 bytes (see `LPP_AddMeasurements` method documentation for the calculation) */
170  if (!LPP_InitBuffer(&appData, 43))
171  {
172  error(31);
173  return; /* Exit function */
174  }
175 
176  /* Add measurements to the LPP packet using the custom convention to save bytes send */
177  if (!LPP_AddMeasurements(&appData, data))
178  {
179  error(32);
180  return; /* Exit function */
181  }
182 
183 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
184  dbinfo("Started sending LPP buffer...");
185 #endif /* DEBUG_DBPRINT */
186 
187  /* Send custom LPP-like-formatted payload */
189  {
190  error(33);
191  return; /* Exit function */
192  }
193 
194 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
195  dbinfo("LPP buffer send.");
196 #endif /* DEBUG_DBPRINT */
197 
198  LPP_FreeBuffer(&appData); // Clear buffer before going to sleep
199 }
bool LPP_InitBuffer(LPP_Buffer_t *b, uint8_t size)
Definition: lpp.c:84
#define LORA_UNCONFIMED
Definition: lora.h:23
void dbinfo(char *message)
Print an info string (char array) to USARTx and go to the next line.
Definition: dbprint.c:503
void error(uint8_t number)
Error method.
Definition: util.c:131
Definition: lora.h:35
void LPP_FreeBuffer(LPP_Buffer_t *b)
Definition: lpp.c:129
bool LPP_AddMeasurements(LPP_Buffer_t *b, MeasurementData_t data)
Add measurement data to the LPP packet following the custom message convention to save bytes to send...
Definition: lpp.c:182
LoRaStatus_t LoRa_SendLppBuffer(LPP_Buffer_t b, bool ackNoAck)
Definition: lora.c:61
LPP_Buffer_t appData
Definition: lora_wrappers.c:85

◆ sendStatus()

void sendStatus ( uint8_t  status)

Send a packet to indicate a status.

Parameters
[in]statusThe status value to send.

Definition at line 286 of file lora_wrappers.c.

287 {
288  /* Initialize LPP-formatted payload - We need 4 bytes */
289  if (!LPP_InitBuffer(&appData, 4))
290  {
291  error(40);
292  return; /* Exit function */
293  }
294 
295  /* Add value to the LPP packet using the custom convention */
296  if (!LPP_AddStatus(&appData, status))
297  {
298  error(41);
299  return; /* Exit function */
300  }
301 
302  /* Send custom LPP-like-formatted payload */
304  {
305  error(42);
306  return; /* Exit function */
307  }
308 
309  LPP_FreeBuffer(&appData); // Clear buffer before going to sleep
310 }
bool LPP_InitBuffer(LPP_Buffer_t *b, uint8_t size)
Definition: lpp.c:84
#define LORA_UNCONFIMED
Definition: lora.h:23
void error(uint8_t number)
Error method.
Definition: util.c:131
Definition: lora.h:35
void LPP_FreeBuffer(LPP_Buffer_t *b)
Definition: lpp.c:129
LoRaStatus_t LoRa_SendLppBuffer(LPP_Buffer_t b, bool ackNoAck)
Definition: lora.c:61
bool LPP_AddStatus(LPP_Buffer_t *b, uint8_t status)
Add a value to indicate a program status to the LPP packet following the custom message convention...
Definition: lpp.c:360
LPP_Buffer_t appData
Definition: lora_wrappers.c:85

◆ sendStormDetected()

void sendStormDetected ( bool  stormDetected)

Send a packet to the cloud using LoRaWAN to indicate that a storm has been detected.

The value gets added to the LPP packet following the custom message convention.

Parameters
[in]stormDetected
  • true - A storm has been detected!
  • false - No storm is detected.

Definition at line 213 of file lora_wrappers.c.

214 {
215  /* Initialize LPP-formatted payload - We need 4 bytes */
216  if (!LPP_InitBuffer(&appData, 4))
217  {
218  error(34);
219  return; /* Exit function */
220  }
221 
222  /* Add value to the LPP packet using the custom convention */
223  if (!LPP_AddStormDetected(&appData, stormDetected))
224  {
225  error(35);
226  return; /* Exit function */
227  }
228 
229  /* Send custom LPP-like-formatted payload */
231  {
232  error(36);
233  return; /* Exit function */
234  }
235 
236  LPP_FreeBuffer(&appData); // Clear buffer before going to sleep
237 }
bool LPP_InitBuffer(LPP_Buffer_t *b, uint8_t size)
Definition: lpp.c:84
bool LPP_AddStormDetected(LPP_Buffer_t *b, uint8_t stormDetected)
Add a value to indicate that a storm has been detected to the LPP packet following the custom message...
Definition: lpp.c:274
#define LORA_UNCONFIMED
Definition: lora.h:23
void error(uint8_t number)
Error method.
Definition: util.c:131
Definition: lora.h:35
void LPP_FreeBuffer(LPP_Buffer_t *b)
Definition: lpp.c:129
LoRaStatus_t LoRa_SendLppBuffer(LPP_Buffer_t b, bool ackNoAck)
Definition: lora.c:61
LPP_Buffer_t appData
Definition: lora_wrappers.c:85

◆ sendTest()

void sendTest ( MeasurementData_t  data)

Send ONE measured battery voltage, internal and external temperature, stormDetected, cableBroken and status value to the cloud using LoRaWAN. This method uses the deprecated methods to test if the data gets send correctly.

Parameters
[in]dataThe struct which contains the measurements to send using LoRaWAN.

Definition at line 323 of file lora_wrappers.c.

324 {
325  /* Initialize LPP-formatted payload - We need 21 bytes */
326  if (!LPP_InitBuffer(&appData, 21))
327  {
328  error(43);
329  return; /* Exit function */
330  }
331 
332  /* Add measurements to the LPP packet */
333  int16_t batteryLPP = (int16_t)(round((float)data.voltage[0]/10));
334  if (!LPP_deprecated_AddVBAT(&appData, batteryLPP))
335  {
336  error(44);
337  return; /* Exit function */
338  }
339 
340  int16_t intTempLPP = (int16_t)(round((float)data.intTemp[0]/100));
341  if (!LPP_deprecated_AddIntTemp(&appData, intTempLPP))
342  {
343  error(45);
344  return; /* Exit function */
345  }
346 
347  int16_t extTempLPP = (int16_t)(round((float)data.extTemp[0]/100));
348  if (!LPP_deprecated_AddExtTemp(&appData, extTempLPP))
349  {
350  error(46);
351  return; /* Exit function */
352  }
353 
355  {
356  error(47);
357  return; /* Exit function */
358  }
359 
361  {
362  error(48);
363  return; /* Exit function */
364  }
365 
367  {
368  error(49);
369  return; /* Exit function */
370  }
371 
372  /* Send LPP-formatted payload */
374  {
375  error(50);
376  return; /* Exit function */
377  }
378 
379  LPP_FreeBuffer(&appData); // Clear buffer before going to sleep
380 }
bool LPP_InitBuffer(LPP_Buffer_t *b, uint8_t size)
Definition: lpp.c:84
#define LORA_UNCONFIMED
Definition: lora.h:23
bool LPP_deprecated_AddCableBroken(LPP_Buffer_t *b, uint8_t cableBroken)
Add a cable break value to the LPP packet, disguised as a Digital Input packet (1 byte)...
Definition: lpp.c:529
bool LPP_deprecated_AddStatus(LPP_Buffer_t *b, uint8_t status)
Add a status value to the LPP packet, disguised as a Digital Input packet (1 byte). The channel is defined by LPP_STATUS_CHANNEL and is 0x15.
Definition: lpp.c:560
int32_t intTemp[6]
Definition: datatypes.h:73
void error(uint8_t number)
Error method.
Definition: util.c:131
Definition: lora.h:35
void LPP_FreeBuffer(LPP_Buffer_t *b)
Definition: lpp.c:129
int32_t extTemp[6]
Definition: datatypes.h:74
bool LPP_deprecated_AddVBAT(LPP_Buffer_t *b, int16_t vbat)
Add a battery voltage measurement to the LPP packet, disguised as an Analog Input packet (2 bytes)...
Definition: lpp.c:400
bool LPP_deprecated_AddIntTemp(LPP_Buffer_t *b, int16_t intTemp)
Add an internal temperature measurement (2 bytes) to the LPP packet. The channel is defined by LPP_TE...
Definition: lpp.c:433
LoRaStatus_t LoRa_SendLppBuffer(LPP_Buffer_t b, bool ackNoAck)
Definition: lora.c:61
LPP_Buffer_t appData
Definition: lora_wrappers.c:85
int32_t voltage[6]
Definition: datatypes.h:72
bool LPP_deprecated_AddStormDetected(LPP_Buffer_t *b, uint8_t stormDetected)
Add a storm value to the LPP packet, disguised as a Digital Input packet (1 byte). The channel is defined by LPP_STORM_CHANNEL and is 0x13.
Definition: lpp.c:498
bool LPP_deprecated_AddExtTemp(LPP_Buffer_t *b, int16_t extTemp)
Add an external temperature measurement (2 bytes) to the LPP packet. The channel is defined by LPP_TE...
Definition: lpp.c:466

◆ sleepLoRaWAN()

void sleepLoRaWAN ( uint32_t  sSleep)

Let the LoRaWAN module sleep for a specified amount of time.

Parameters
[in]sSleepThe amount of seconds for the module to go to sleep.

Definition at line 137 of file lora_wrappers.c.

138 {
139  bool wakeUp = false;
140  LoRa_Sleep((1000*sSleep), &wakeUp); /* "wakeUp" is not used in underlying method */
141 }
void LoRa_Sleep(uint32_t durationMs, volatile bool *wakeUp)
Definition: lora.c:76

◆ wakeLoRaWAN()

void wakeLoRaWAN ( void  )

Wake up the LoRaWAN module early after putting it to sleep using sleepLoRaWAN.

Definition at line 148 of file lora_wrappers.c.

149 {
150  LoRa_WakeUp();
151 }
LoRaStatus_t LoRa_WakeUp(void)
Definition: lora.c:80

Variable Documentation

◆ appData

LPP_Buffer_t appData

Definition at line 85 of file lora_wrappers.c.

◆ loraSettings

Definition at line 83 of file lora_wrappers.c.

◆ loraStatus

LoRaStatus_t loraStatus

Definition at line 84 of file lora_wrappers.c.