-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
193 lines (144 loc) · 4.57 KB
/
main.c
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "board.h"
#include "thread.h"
#include "msg.h"
#include "periph/uart.h"
#include "minmea.h"
#include "xtimer.h"
#include "fmt.h"
#include "net/loramac.h"
#include "semtech_loramac.h"
/* we will use Cayenne LPP for displaying our data */
#include "cayenne_lpp.h"
#define BUF_SIZE 64
#define LORA_PORT 1
/* Use a unique value for DEV_EUI in TTN */
#define DEV_EUI "000D526824C2E27A"
/* APP_EUI and AP_KEY are provided by the network server (TTN)*/
#define APP_EUI "70B3D57ED000912E"
#define APP_KEY "446E62ABCC4A77B865253A3C4B30AA1D"
#define LORAWAN_DATARATE 5
/* we use "Dynamic Sensor Payload for our data */
#define CAYENNE_LPP_CHANNEL 1
/* we must respect the duty cycle limitations */
#define SLEEP_TIME 10
#define UART_BUFSIZE (128U)
#define PRINTER_PRIO (THREAD_PRIORITY_MAIN - 1)
#define TX_PERIOD (10000000U)
#define BAUDRATE (9600)
static cayenne_lpp_t lpp;
semtech_loramac_t g_loramac;
static char rx_mem[UART_BUFSIZE];
static kernel_pid_t printer_pid;
static char printer_stack[THREAD_STACKSIZE_MAIN];
static int counter=0;
void setup_lora(semtech_loramac_t *loramac) {
uint8_t buf[BUF_SIZE];
/* init LoRaMAC */
semtech_loramac_init(loramac);
/* load required keys into LoRaMAC */
fmt_hex_bytes(buf, DEV_EUI);
semtech_loramac_set_deveui(loramac, buf);
fmt_hex_bytes(buf, APP_EUI);
semtech_loramac_set_appeui(loramac, buf);
fmt_hex_bytes(buf, APP_KEY);
semtech_loramac_set_appkey(loramac, buf);
/* Try to join by Over The Air Activation */
if (semtech_loramac_join(loramac, LORAMAC_JOIN_OTAA) != SEMTECH_LORAMAC_JOIN_SUCCEEDED) {
puts("Join failed");
}
else {
puts("Join Success");
}
}
void send_lora_data(semtech_loramac_t *loramac, float lat, float lon) {
printf("Sending:");
/* reset our cayenne buffer */
cayenne_lpp_reset(&lpp);
cayenne_lpp_add_gps(&lpp, CAYENNE_LPP_CHANNEL, lat, lon ,0);
semtech_loramac_set_tx_mode(loramac, LORAMAC_TX_UNCNF);
semtech_loramac_set_tx_port(loramac, LORA_PORT);
/* set datarate */
semtech_loramac_set_dr(loramac, LORAWAN_DATARATE);
/* try to send data */
switch (semtech_loramac_send(loramac, lpp.buffer, lpp.cursor)) {
case SEMTECH_LORAMAC_NOT_JOINED:
puts("Failed: not joined");
return;
case SEMTECH_LORAMAC_BUSY:
puts("Failed: mac is busy");
return;
}
/* check if something was received */
switch (semtech_loramac_recv(loramac)) {
case SEMTECH_LORAMAC_DATA_RECEIVED:
loramac->rx_data.payload[loramac->rx_data.payload_len] = 0;
printf("Data received: %s, port: %d\n",
(char *)loramac->rx_data.payload, loramac->rx_data.port);
break;
case SEMTECH_LORAMAC_TX_CNF_FAILED:
puts("Confirmable TX failed");
break;
case SEMTECH_LORAMAC_TX_DONE:
puts("TX complete, no data received");
break;
}
}
static void rx_cb(void *arg, uint8_t data)
{
(void) arg;
rx_mem[counter++] = data;
if (data == '\n') {
rx_mem[counter++] = 0;
counter = 0;
msg_t msg;
msg_send(&msg, printer_pid);
}
}
void cb(void *arg)
{
(void) arg;
uart_poweron(UART_DEV(1));
}
static void *printer(void *arg)
{
(void)arg;
msg_t msg;
msg_t msg_queue[8];
msg_init_queue(msg_queue, 8);
struct minmea_sentence_rmc frame;
xtimer_t uart_timer;
uart_timer.callback = cb;
while (1) {
msg_receive(&msg);
if(minmea_sentence_id(rx_mem, false) == MINMEA_SENTENCE_RMC) {
minmea_parse_rmc(&frame, rx_mem);
printf("TX: %d, %d\n", (int) frame.latitude.value, (int) frame.longitude.value);
//Send value
send_lora_data(&g_loramac, minmea_tocoord(&frame.latitude), minmea_tocoord(&frame.longitude));
uart_poweroff(UART_DEV(1));
xtimer_set(&uart_timer, TX_PERIOD);
}
}
/* this should never be reached */
return NULL;
}
int main(void)
{
#if 0
gpio_init(GPIO_PIN(0, 12), GPIO_OUT);
gpio_init(GPIO_PIN(1, 3), GPIO_OUT);
gpio_set(GPIO_PIN(0, 12));
gpio_clear(GPIO_PIN(1, 3));
EN3V3_ON;
#endif
/* initialize UART */
uart_init(UART_DEV(1), BAUDRATE, rx_cb, NULL);
setup_lora(&g_loramac);
/* start the printer thread */
printer_pid = thread_create(printer_stack, sizeof(printer_stack),
PRINTER_PRIO, 0, printer, NULL, "printer");
return 0;
}