-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLoRa-P2P.ino
196 lines (168 loc) · 4.3 KB
/
LoRa-P2P.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/**
@file app.cpp
@author Bernd Giesecke ([email protected])
@brief Application specific functions. Mandatory to have init_app(),
app_event_handler(), ble_data_handler(), lora_data_handler()
and lora_tx_finished()
@version 0.1
@date 2021-04-23
@copyright Copyright (c) 2021
*/
#include "app.h"
/** Set the device name, max length is 10 characters */
char g_ble_dev_name[10] = "RAK-TEST";
/** Send Fail counter **/
uint8_t send_fail = 0;
/** Flag for low battery protection */
bool low_batt_protection = false;
/** Battery level uinion */
batt_s batt_level;
/** LPWAN packet */
lpwan_data_s g_lpwan_data;
/**
@brief Application specific setup functions
*/
void setup_app(void)
{
pinMode(LED_BLUE, OUTPUT);
#if API_DEBUG == 0
// Initialize Serial for debug output
Serial.begin(115200);
time_t serial_timeout = millis();
// On nRF52840 the USB serial is not available immediately
while (!Serial)
{
if ((millis() - serial_timeout) < 1000)
{
delay(100);
digitalWrite(LED_GREEN, !digitalRead(LED_GREEN));
}
else
{
break;
}
}
#endif
#ifdef NRF52_SERIES
// Enable BLE
g_enable_ble = true;
#endif
}
/**
@brief Application specific initializations
@return true Initialization success
@return false Initialization failure
*/
bool init_app(void)
{
MYLOG("APP", "init_app");
return true;
}
/**
@brief Application specific event handler
Requires as minimum the handling of STATUS event
Here you handle as well your application specific events
*/
void app_event_handler(void)
{
// Timer triggered event
if ((g_task_event_type & STATUS) == STATUS)
{
g_task_event_type &= N_STATUS;
MYLOG("APP", "Timer wakeup");
#ifdef NRF52_SERIES
// If BLE is enabled, restart Advertising
if (g_enable_ble)
{
if (g_lorawan_settings.auto_join)
{
restart_advertising(60);
}
}
#endif
if (!low_batt_protection)
{
// Sensor readings here
}
// Get battery level
batt_level.batt16 = read_batt() / 10;
g_lpwan_data.batt_1 = batt_level.batt8[1];
g_lpwan_data.batt_2 = batt_level.batt8[0];
// Protection against battery drain
if (batt_level.batt16 < 290)
{
// Battery is very low, change send time to 1 hour to protect battery
low_batt_protection = true; // Set low_batt_protection active
api_timer_restart(1 * 60 * 60 * 1000); // Set send time to one hour
MYLOG("APP", "Battery protection activated");
}
else if ((batt_level.batt16 > 380) && low_batt_protection)
{
// Battery is higher than 4V, change send time back to original setting
low_batt_protection = false;
api_timer_restart(g_lorawan_settings.send_repeat_time);
MYLOG("APP", "Battery protection deactivated");
}
if (!send_p2p_packet((uint8_t *)&g_lpwan_data, LPWAN_DATA_LEN))
{
MYLOG("APP", "Failed to send P2P packet");
}
}
}
#ifdef NRF52_SERIES
/**
@brief Handle BLE UART data
*/
void ble_data_handler(void)
{
if (g_enable_ble)
{
// BLE UART data handling
if ((g_task_event_type & BLE_DATA) == BLE_DATA)
{
MYLOG("AT", "RECEIVED BLE");
/** BLE UART data arrived */
g_task_event_type &= N_BLE_DATA;
while (g_ble_uart.available() > 0)
{
at_serial_input(uint8_t(g_ble_uart.read()));
delay(5);
}
at_serial_input(uint8_t('\n'));
}
}
}
#endif
/**
@brief Handle received LoRa Data
*/
void lora_data_handler(void)
{
// LoRa TX finished handling
if ((g_task_event_type & LORA_TX_FIN) == LORA_TX_FIN)
{
g_task_event_type &= N_LORA_TX_FIN;
MYLOG("APP", "LPWAN TX cycle %s", g_rx_fin_result ? "finished ACK" : "failed NAK");
}
// LoRa data handling
if ((g_task_event_type & LORA_DATA) == LORA_DATA)
{
digitalWrite(LED_BLUE, !digitalRead(LED_BLUE));
/**************************************************************/
/**************************************************************/
/// \todo LoRa data arrived
/// \todo parse them here
/**************************************************************/
/**************************************************************/
g_task_event_type &= N_LORA_DATA;
MYLOG("APP", "Received package over LoRa");
char log_buff[g_rx_data_len * 3] = {0};
uint8_t log_idx = 0;
for (int idx = 0; idx < g_rx_data_len; idx++)
{
sprintf(&log_buff[log_idx], "%02X ", g_rx_lora_data[idx]);
log_idx += 3;
}
MYLOG("APP", "%s", log_buff);
}
}