forked from tftelkamp/arduino-lmic-v1.5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
200 lines (178 loc) · 6.8 KB
/
main.cpp
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
197
198
199
200
/*******************************************************************************
* Copyright (c) 2015 Thomas Telkamp and Matthijs Kooijman
*
* Permission is hereby granted, free of charge, to anyone
* obtaining a copy of this document and accompanying files,
* to do whatever they want with them without any restriction,
* including, but not limited to, copying, modification and redistribution.
* NO WARRANTY OF ANY KIND IS PROVIDED.
*
* This example sends a valid LoRaWAN packet with payload "Hello, world!", that
* will be processed by The Things Network server.
*
* Note: LoRaWAN per sub-band duty-cycle limitation is enforced (1% in g1,
* 0.1% in g2).
*
* Change DEVADDR to a unique address!
* See http://thethingsnetwork.org/wiki/AddressSpace
*
* Do not forget to define the radio type correctly in config.h, default is:
* #define CFG_sx1272_radio 1
* for SX1272 and RFM92, but change to:
* #define CFG_sx1276_radio 1
* for SX1276 and RFM95.
*
*******************************************************************************/
#include <mbed.h>
#include <lmic.h>
#include <hal/hal.h>
#include <SPI.h>
#if 1
#define SINGLE_CHANNEL_GATEWAY //force it to use 902.3 MHz only
#define TRANSMIT_INTERVAL 10 //in seconds, TOO OFTEN MAY GET YOUR TRAFFIC IGNORED
#include "test_node_secrets.h"
static const u1_t APPEUI[8] = MY_APPEUI;
static const u1_t DEVEUI[8] = MY_DEVEUI;
static const u1_t DEVKEY[16] = MY_NWKSKEY;
static const u1_t ARTKEY[16] = MY_APPSKEY;
static const u4_t DEVADDR = MY_DEVADDR;
#else
#define TRANSMIT_INTERVAL 120
#warning YOU SHOULD SET CREATE AN APP AND A NODE IDENTITY (use a personal node)
#warning SEE http://staging.thethingsnetwork.org/wiki/Backend/ttnctl/QuickStart
// LoRaWAN Application identifier (AppEUI)
// Not used in this example
static const u1_t APPEUI[8] = { 0x02, 0x00, 0x00, 0x00, 0x00, 0xEE, 0xFF, 0xC0 };
// LoRaWAN DevEUI, unique device ID (LSBF)
// Not used in this example
static const u1_t DEVEUI[8] = { 0x42, 0x42, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF };
// LoRaWAN NwkSKey, network session key
// Use this key for The Things Network
static const u1_t DEVKEY[16] = { 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C };
// LoRaWAN AppSKey, application session key
// Use this key to get your data decrypted by The Things Network
static const u1_t ARTKEY[16] = { 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C };
// LoRaWAN end-device address (DevAddr)
// See http://thethingsnetwork.org/wiki/AddressSpace
#warning FIXME You MUST SET A CUSTOM ADDRESS
static const u4_t DEVADDR = 0x03FF0001 ; // <-- Change this address for every node!
#endif
//////////////////////////////////////////////////
// APPLICATION CALLBACKS
//////////////////////////////////////////////////
// provide application router ID (8 bytes, LSBF)
void os_getArtEui (u1_t* buf) {
memcpy(buf, APPEUI, 8);
}
// provide device ID (8 bytes, LSBF)
void os_getDevEui (u1_t* buf) {
memcpy(buf, DEVEUI, 8);
}
// provide device key (16 bytes)
void os_getDevKey (u1_t* buf) {
memcpy(buf, DEVKEY, 16);
}
uint8_t mydata[] = "Hello, from NYC!";
static osjob_t sendjob;
// Pin mapping
//lmic_pinmap pins;
/*= {
.nss = 10,
.rxtx = 7, // Not connected on RFM92/RFM95
.rst = 9, // Needed on RFM92/RFM95
.dio = {2, 5, 6},
};
*/
void onEvent (ev_t ev) {
//debug_event(ev);
switch(ev) {
// scheduled data sent (optionally data received)
// note: this includes the receive window!
case EV_TXCOMPLETE:
// use this event to keep track of actual transmissions
//Serial.print("Event EV_TXCOMPLETE, time: ");
//Serial.println(millis() / 1000);
if(LMIC.dataLen) { // data received in rx slot after tx
//debug_buf(LMIC.frame+LMIC.dataBeg, LMIC.dataLen);
//Serial.println("Data Received!");
printf("Data Received!\n");
}
break;
default:
break;
}
}
unsigned int xmit_count = 0;
void do_send(osjob_t* j){
// Serial.print("Time: ");
// Serial.println(millis() / 1000);
printf("Time: %lu\n", (unsigned int) (millis() / 1000));
// Show TX channel (channel numbers are local to LMIC)
// Serial.print("Send, txCnhl: ");
// Serial.println(LMIC.txChnl);
// Serial.print("Opmode check: ");
printf("Send, txCnhl: %u Opmode check: ", LMIC.txChnl);
// Check if there is not a current TX/RX job running
if (LMIC.opmode & (1 << 7)) {
//Serial.println("OP_TXRXPEND, not sending");
printf("OP_TXRXPEND, not sending\n");
} else {
//Serial.println("ok");
printf("ok\n");
// Prepare upstream data transmission at the next possible time.
// LMIC_setTxData2(1, mydata, sizeof(mydata)-1, 0);
char buf[128];
sprintf(buf, "NYC TTN test packet %u\n", xmit_count++);
LMIC_setTxData2(1, (unsigned char *) buf, strlen(buf)-1, 0);
printf("sending %s\n", buf);
}
// Schedule a timed job to run at the given timestamp (absolute system time)
os_setTimedCallback(j, os_getTime()+sec2osticks(TRANSMIT_INTERVAL), do_send);
}
void setup() {
// Serial.begin(9600);
//Serial.println("Starting");
printf("Starting, will wait 1 second\n");
//SPI.begin();
//delay(3000); //Give teensy USB some time
while (millis() < 1000);
//Serial.println("setting up");
printf("setting up\n");
fprintf(stderr, "stderr test\n");
// LMIC init
os_init();
// Reset the MAC state. Session and pending data transfers will be discarded.
LMIC_reset();
// Set static session parameters. Instead of dynamically establishing a session
// by joining the network, precomputed session parameters are be provided.
LMIC_setSession (0x1, DEVADDR, (uint8_t*)DEVKEY, (uint8_t*)ARTKEY);
// Disable data rate adaptation
LMIC_setAdrMode(0);
// Disable link check validation
LMIC_setLinkCheckMode(0);
// Disable beacon tracking
LMIC_disableTracking ();
// Stop listening for downstream data (periodical reception)
LMIC_stopPingable();
// Set data rate and transmit power (note: txpow seems to be ignored by the library)
LMIC_setDrTxpow(DR_SF7,14);
//
// Serial.flush();
fflush(stdout);
#ifdef SINGLE_CHANNEL_GATEWAY
//Serial.println("Disabling all channels but 0 (902.3 MHz) for single-channel gateway compatibility");
printf("Disabling all channels but 0 (902.3 MHz) for single-channel gateway compatibility\n");
for (int i=1; i<75; i++)
LMIC_disableChannel(i);
#endif
}
void loop() {
do_send(&sendjob);
while(1) {
os_runloop_once();
}
}
int main(int argc, char **argv) {
setup();
while(1) loop();
}