2020-05-14 00:33:50 -07:00
|
|
|
#include <lmic.h>
|
|
|
|
#include <hal/hal.h>
|
2020-04-18 23:27:19 -07:00
|
|
|
#include <TinyGPS++.h>
|
2020-05-14 00:33:50 -07:00
|
|
|
|
2020-05-15 05:30:37 -07:00
|
|
|
#include <AltSoftSerial.h>
|
2020-02-17 19:32:58 -08:00
|
|
|
|
2020-05-14 00:33:50 -07:00
|
|
|
// LoRaWAN NwkSKey, network session key
|
|
|
|
static const PROGMEM u1_t NWKSKEY[16] = { 0x52, 0x92, 0xC0, 0x72, 0x2D, 0x3C, 0x55, 0x5E, 0xE4, 0xB9, 0x9E, 0x9B, 0x88, 0x66, 0x47, 0xF1 };
|
|
|
|
|
|
|
|
// LoRaWAN AppSKey, application session key
|
|
|
|
static const u1_t PROGMEM APPSKEY[16] = { 0xC4, 0x30, 0xEF, 0x56, 0x4F, 0x6D, 0xA2, 0x56, 0x1F, 0x15, 0x2F, 0xB8, 0x62, 0xC7, 0xCA, 0xC2 };
|
2020-02-17 19:32:58 -08:00
|
|
|
|
2020-05-14 00:33:50 -07:00
|
|
|
// LoRaWAN end-device address (DevAddr)
|
|
|
|
// See http://thethingsnetwork.org/wiki/AddressSpace
|
|
|
|
// The library converts the address to network byte order as needed.
|
|
|
|
#ifndef COMPILE_REGRESSION_TEST
|
|
|
|
static const u4_t DEVADDR = 0x260212B6;
|
|
|
|
#else
|
|
|
|
static const u4_t DEVADDR = 0;
|
|
|
|
#endif
|
2020-04-07 15:23:50 -07:00
|
|
|
|
2020-05-14 00:33:50 -07:00
|
|
|
// These callbacks are only used in over-the-air activation, so they are
|
|
|
|
// left empty here (we cannot leave them out completely unless
|
|
|
|
// DISABLE_JOIN is set in arduino-lmic/project_config/lmic_project_config.h,
|
|
|
|
// otherwise the linker will complain).
|
|
|
|
void os_getArtEui (u1_t* buf) { }
|
|
|
|
void os_getDevEui (u1_t* buf) { }
|
|
|
|
void os_getDevKey (u1_t* buf) { }
|
2020-02-17 19:32:58 -08:00
|
|
|
|
2020-05-14 00:33:50 -07:00
|
|
|
static osjob_t sendjob;
|
2020-05-15 05:43:49 -07:00
|
|
|
static osjob_t gpsjob;
|
2020-05-14 00:33:50 -07:00
|
|
|
|
|
|
|
// Schedule TX every this many seconds (might become longer due to duty
|
|
|
|
// cycle limitations).
|
2020-05-14 23:19:37 -07:00
|
|
|
const unsigned TX_INTERVAL = 10;
|
2020-05-15 05:30:37 -07:00
|
|
|
const unsigned GPS_INTERVAL = 1;
|
2020-05-14 00:33:50 -07:00
|
|
|
|
|
|
|
// Pin mapping for Adafruit Feather M0 LoRa
|
|
|
|
const lmic_pinmap lmic_pins = {
|
2020-05-14 23:19:37 -07:00
|
|
|
.nss = 10,
|
|
|
|
.rxtx = LMIC_UNUSED_PIN,
|
|
|
|
.rst = 9,
|
|
|
|
.dio = {2, 3, LMIC_UNUSED_PIN},
|
2020-05-14 00:33:50 -07:00
|
|
|
};
|
2020-04-07 15:23:50 -07:00
|
|
|
|
2020-05-12 22:37:12 -07:00
|
|
|
/****************************************************
|
|
|
|
* Arduino drivers
|
|
|
|
* - LoRaWAN
|
|
|
|
* - GPS
|
|
|
|
***************************************************/
|
2020-04-18 23:27:19 -07:00
|
|
|
TinyGPSPlus gps;
|
2020-04-07 15:23:50 -07:00
|
|
|
|
2020-05-12 19:23:32 -07:00
|
|
|
/****************************************************
|
|
|
|
* Track each pair of X and Y coordinates
|
|
|
|
* - arrays are used by the pnpoly function
|
|
|
|
***************************************************/
|
2020-05-12 21:49:37 -07:00
|
|
|
const uint8_t N_POLY_MAX=10;
|
2020-04-18 20:07:23 -07:00
|
|
|
float polyx[N_POLY_MAX];
|
|
|
|
float polyy[N_POLY_MAX];
|
|
|
|
int n_poly=0;
|
2020-05-12 19:23:32 -07:00
|
|
|
|
|
|
|
/****************************************************
|
|
|
|
* Add a coordinate to the arrays
|
|
|
|
* - stores a total of N_POLY_MAX pairs
|
|
|
|
***************************************************/
|
2020-05-15 05:30:37 -07:00
|
|
|
const int push_vert(const float x, const float y) {
|
2020-04-18 20:07:23 -07:00
|
|
|
if(n_poly>N_POLY_MAX)
|
|
|
|
return 0;
|
|
|
|
polyx[n_poly]=x;
|
|
|
|
polyy[n_poly]=y;
|
|
|
|
n_poly++;
|
|
|
|
return 1;
|
|
|
|
}
|
2020-05-15 05:30:37 -07:00
|
|
|
|
2020-05-12 19:23:32 -07:00
|
|
|
/****************************************************
|
|
|
|
* 'Clear' pairs of coordinates
|
|
|
|
***************************************************/
|
2020-04-18 20:07:23 -07:00
|
|
|
void clear_verts() {
|
|
|
|
n_poly=0;
|
|
|
|
}
|
2020-05-14 00:33:50 -07:00
|
|
|
|
2020-05-12 19:23:32 -07:00
|
|
|
/****************************************************
|
|
|
|
* Check a pair of coordinates against two lists
|
|
|
|
* of vertices
|
|
|
|
* - https://wrf.ecse.rpi.edu//Research/Short_Notes/pnpoly.html
|
|
|
|
***************************************************/
|
2020-05-15 05:30:37 -07:00
|
|
|
const int pnpoly(int nvert, const float *vertx, const float *verty, const float testx, const float testy)
|
|
|
|
{
|
2020-04-18 20:07:23 -07:00
|
|
|
int i, j, c = 0;
|
|
|
|
for (i = 0, j = nvert-1; i < nvert; j = i++) {
|
|
|
|
if ( ((verty[i]>testy) != (verty[j]>testy)) &&
|
2020-05-14 23:19:37 -07:00
|
|
|
(testx < (vertx[j]-vertx[i]) * (testy-verty[i]) / (verty[j]-verty[i]) + vertx[i]) )
|
|
|
|
c = !c;
|
2020-04-18 20:07:23 -07:00
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
2020-05-15 05:30:37 -07:00
|
|
|
|
2020-05-12 19:23:32 -07:00
|
|
|
/****************************************************
|
|
|
|
* Test a coordinate against all vertices
|
2020-05-15 01:49:46 -07:00
|
|
|
* - takes current GPS coordinates
|
|
|
|
* - return 1 if in bounds
|
2020-05-12 19:23:32 -07:00
|
|
|
***************************************************/
|
2020-05-15 05:30:37 -07:00
|
|
|
const int check_bounds(const float x, const float y) {
|
2020-04-18 20:07:23 -07:00
|
|
|
return pnpoly(n_poly, polyx, polyy, x, y);
|
|
|
|
}
|
2020-05-15 05:30:37 -07:00
|
|
|
|
2020-05-12 19:23:32 -07:00
|
|
|
/****************************************************
|
|
|
|
* Load coordinates from protobuff stream
|
|
|
|
* - currently a maximum of 10 coordinates
|
|
|
|
* - loading arrays in nanopb does not appear
|
|
|
|
* to work.
|
|
|
|
***************************************************/
|
2020-05-15 05:30:37 -07:00
|
|
|
void import_protobuf(const uint8_t *protobuffer, const uint32_t size) {
|
2020-05-14 23:19:37 -07:00
|
|
|
if(size != 122) {
|
2020-04-18 20:07:23 -07:00
|
|
|
Serial.println("Failed to decode");
|
2020-05-14 23:19:37 -07:00
|
|
|
return;
|
2020-04-18 20:07:23 -07:00
|
|
|
}
|
2020-05-15 05:30:37 -07:00
|
|
|
|
|
|
|
Serial.println("Recieved valid protobuf data?");
|
2020-05-14 00:33:50 -07:00
|
|
|
|
2020-05-15 01:49:46 -07:00
|
|
|
uint32_t isr;
|
|
|
|
isr = 0;
|
|
|
|
isr = protobuffer[1];
|
|
|
|
|
|
|
|
if(isr>N_POLY_MAX) isr = N_POLY_MAX;
|
|
|
|
|
|
|
|
clear_verts();
|
2020-05-15 05:30:37 -07:00
|
|
|
|
|
|
|
const uint8_t *ptr = protobuffer + 5;
|
2020-05-15 01:49:46 -07:00
|
|
|
for(uint32_t i=0;i<isr;i++) {
|
|
|
|
float x,y;
|
|
|
|
memcpy(&x, ptr + i*12, 4);
|
|
|
|
memcpy(&y, ptr + i*12+5, 4);
|
2020-05-15 05:30:37 -07:00
|
|
|
|
2020-05-15 01:49:46 -07:00
|
|
|
push_vert(x, y);
|
|
|
|
}
|
2020-05-14 23:19:37 -07:00
|
|
|
}
|
|
|
|
|
2020-05-15 05:30:37 -07:00
|
|
|
uint8_t is_sending = 0;
|
2020-05-14 23:19:37 -07:00
|
|
|
void onEvent (ev_t ev) {
|
2020-05-15 05:30:37 -07:00
|
|
|
// Serial.print(os_getTime());
|
|
|
|
// Serial.print(": ");
|
|
|
|
|
|
|
|
if(ev == EV_TXCOMPLETE) {
|
|
|
|
Serial.println("EV_TXCOMPLETE (includes waiting for RX windows)");
|
|
|
|
|
|
|
|
if (LMIC.dataLen) {
|
|
|
|
Serial.println(F("Received "));
|
|
|
|
import_protobuf(LMIC.frame + LMIC.dataBeg, LMIC.dataLen);
|
|
|
|
}
|
|
|
|
is_sending = 0;
|
|
|
|
}
|
|
|
|
else if(ev == EV_TXSTART) {
|
|
|
|
Serial.println(F("EV_TXSTART"));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Serial.print(F("Unknown event: "));
|
2020-05-14 23:19:37 -07:00
|
|
|
}
|
2020-05-14 00:33:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#define TYPE_STRING 0x0A
|
|
|
|
#define TYPE_VARIANT 0x10
|
|
|
|
#define PROTO_LEN 0x0A
|
|
|
|
#define FIELD_ONE_FLOAT 0x0D
|
|
|
|
#define FIELD_TWO_FLOAT 0x15
|
|
|
|
#define FIELD_TWO_VARIANT 0x10
|
|
|
|
|
2020-05-15 01:49:46 -07:00
|
|
|
uint8_t buffer[] = {
|
2020-05-15 05:30:37 -07:00
|
|
|
TYPE_STRING,
|
|
|
|
PROTO_LEN,
|
2020-05-15 01:49:46 -07:00
|
|
|
FIELD_ONE_FLOAT, 0x00, 0x00, 0x48, 0x43,
|
|
|
|
FIELD_TWO_FLOAT, 0x00, 0x00, 0xc8, 0x42,
|
|
|
|
FIELD_TWO_VARIANT, 0};
|
2020-05-14 23:19:37 -07:00
|
|
|
|
2020-05-15 01:49:46 -07:00
|
|
|
void do_send(osjob_t* j){
|
2020-05-14 00:33:50 -07:00
|
|
|
// Check if there is not a current TX/RX job running
|
|
|
|
if (LMIC.opmode & OP_TXRXPEND) {
|
|
|
|
Serial.println(F("OP_TXRXPEND, not sending"));
|
|
|
|
} else {
|
|
|
|
// prepare upstream data transmission at the next possible time.
|
|
|
|
// transmit on port 1 (the first parameter); you can use any value from 1 to 223 (others are reserved).
|
|
|
|
// don't request an ack (the last parameter, if not zero, requests an ack from the network).
|
|
|
|
// Remember, acks consume a lot of network resources; don't ask for an ack unless you really need it.
|
2020-05-15 05:30:37 -07:00
|
|
|
const float latitude = gps.location.lat();
|
|
|
|
const float longitude = gps.location.lng();
|
|
|
|
const int oob = check_bounds(latitude, longitude);
|
2020-05-14 00:33:50 -07:00
|
|
|
|
|
|
|
memcpy(buffer+3, (void*)&latitude, 4);
|
|
|
|
memcpy(buffer+8, (void*)&longitude, 4);
|
2020-05-15 01:49:46 -07:00
|
|
|
memcpy(buffer+13, (void*)&oob, 1);
|
2020-05-14 00:33:50 -07:00
|
|
|
|
2020-05-15 01:49:46 -07:00
|
|
|
LMIC_setTxData2(1, buffer, sizeof(buffer), 0);
|
2020-05-14 00:33:50 -07:00
|
|
|
}
|
2020-05-15 05:30:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void do_gps(osjob_t* j){
|
|
|
|
while(softserial_available()>0) {
|
|
|
|
gps.encode(softserial_read());
|
|
|
|
}
|
|
|
|
if(!is_sending && gps.location.isValid()) {
|
2020-05-15 05:55:33 -07:00
|
|
|
is_sending = 1;
|
2020-05-15 05:30:37 -07:00
|
|
|
do_send(&sendjob);
|
|
|
|
}
|
2020-05-15 05:43:49 -07:00
|
|
|
os_setTimedCallback(&gpsjob, os_getTime()+sec2osticks(GPS_INTERVAL), do_gps);
|
2020-05-14 00:33:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void setup() {
|
2020-05-15 01:49:46 -07:00
|
|
|
Serial.begin(4800);
|
2020-05-15 05:30:37 -07:00
|
|
|
softserial_init();
|
2020-05-14 23:19:37 -07:00
|
|
|
|
2020-05-14 00:33:50 -07:00
|
|
|
delay(100);
|
|
|
|
Serial.println(F("Starting"));
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
// On AVR, these values are stored in flash and only copied to RAM
|
|
|
|
// once. Copy them to a temporary buffer here, LMIC_setSession will
|
|
|
|
// copy them into a buffer of its own again.
|
|
|
|
uint8_t appskey[sizeof(APPSKEY)];
|
|
|
|
uint8_t nwkskey[sizeof(NWKSKEY)];
|
|
|
|
memcpy_P(appskey, APPSKEY, sizeof(APPSKEY));
|
|
|
|
memcpy_P(nwkskey, NWKSKEY, sizeof(NWKSKEY));
|
|
|
|
|
2020-05-15 05:30:37 -07:00
|
|
|
LMIC_setSession (0x13, DEVADDR, nwkskey, appskey);
|
2020-05-14 00:33:50 -07:00
|
|
|
LMIC_selectSubBand(1);
|
|
|
|
|
|
|
|
// Disable link check validation
|
|
|
|
LMIC_setLinkCheckMode(0);
|
|
|
|
|
|
|
|
// TTN uses SF9 for its RX2 window.
|
|
|
|
LMIC.dn2Dr = DR_SF9;
|
|
|
|
|
|
|
|
// Set data rate and transmit power for uplink
|
|
|
|
LMIC_setDrTxpow(DR_SF7,14);
|
|
|
|
|
2020-05-15 05:43:49 -07:00
|
|
|
os_setTimedCallback(&gpsjob, os_getTime()+sec2osticks(GPS_INTERVAL), do_gps);
|
2020-05-12 21:49:37 -07:00
|
|
|
}
|
2020-05-15 01:49:46 -07:00
|
|
|
|
2020-05-14 00:33:50 -07:00
|
|
|
void loop() {
|
2020-05-15 05:30:37 -07:00
|
|
|
os_runloop_once();
|
2020-02-17 19:32:58 -08:00
|
|
|
}
|