collar/collar.cpp
2020-05-16 02:27:51 +01:00

248 lines
7.1 KiB
C++

#include <lmic.h>
#include <hal/hal.h>
#include <TinyGPS++.h>
#include <AltSoftSerial.h>
// 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 };
// Chirpstack keys
// static const u1_t PROGMEM NWKSKEY[16] = {0x5b,0xe6,0x8b,0xb7,0xaa,0x4f,0x01,0x85,0x54,0x72,0xd9,0x6f,0xd8,0xba,0xbc,0x99};
// static const u1_t PROGMEM APPSKEY[16] = {0xee,0x9a,0x94,0x96,0x9d,0x59,0xfb,0xc2,0x7a,0xe6,0x07,0xe1,0x6e,0x04,0x37,0x5b};
// static const u4_t DEVADDR = 0x005d96f5;
// 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
// void printf(char *str) {
// Serial.println(str);
// }
static osjob_t sendjob;
void debug_function(char *str) {
Serial.println(str);
}
// Schedule TX every this many seconds (might become longer due to duty
// cycle limitations).
const unsigned TX_INTERVAL = 10;
// Pin mapping for Adafruit Feather M0 LoRa
const lmic_pinmap lmic_pins = {
.nss = 10,
.rxtx = LMIC_UNUSED_PIN,
.rst = 9,
.dio = {2, 3, LMIC_UNUSED_PIN},
};
/****************************************************
* Arduino drivers
* - LoRaWAN
* - GPS
***************************************************/
TinyGPSPlus gps;
uint8_t general_int;
volatile uint8_t n_poly;
#define isr general_int
#define timeout general_int
/****************************************************
* Track each pair of X and Y coordinates
* - arrays are used by the pnpoly function
***************************************************/
const uint8_t N_POLY_MAX=10;
float polyx[N_POLY_MAX*2+5];
float * const polyy = polyx + N_POLY_MAX;
/****************************************************
* Check a pair of coordinates against two lists
* of vertices
* - https://wrf.ecse.rpi.edu//Research/Short_Notes/pnpoly.html
***************************************************/
const int pnpoly
(const uint8_t nvert, const float *vertx, const float *verty, const float testx, const float testy)
{
uint8_t i, j, c = 0;
for (i = 0, j = nvert-1; i < nvert; j = i++) {
if ( ((verty[i]>testy) != (verty[j]>testy)) &&
(testx < (vertx[j]-vertx[i]) * (testy-verty[i]) / (verty[j]-verty[i]) + vertx[i]) )
c = !c;
}
return c;
}
/****************************************************
* Test a coordinate against all vertices
* - takes current GPS coordinates
* - return 1 if in bounds
***************************************************/
const int check_bounds(const float x, const float y) {
return pnpoly(n_poly, polyx, polyy, x, y);
}
/****************************************************
* Load coordinates from protobuff stream
* - currently a maximum of 10 coordinates
* - loading arrays in nanopb does not appear
* to work.
***************************************************/
void import_protobuf(const uint8_t *protobuffer, const uint32_t size) {
if(size != 122) {
Serial.println("nmd");
return;
}
Serial.println("Recieved valid protobuf data?");
isr = protobuffer[1];
if(isr>N_POLY_MAX) isr = 0;
const uint8_t *ptr = protobuffer + 5;
for(uint8_t i=0;i<isr && i<N_POLY_MAX;i++) {
memcpy(&polyx[i], ptr + i*12, 4);
memcpy(&polyy[i], ptr + i*12+5, 4);
Serial.print((int)(polyx[i]*100));
Serial.print(' ');
Serial.print((int)(polyy[i]*100));
Serial.print('\n');
}
n_poly = isr;
}
static volatile uint8_t is_sending = 0;
void onEvent (ev_t ev) {
// 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: "));
}
}
#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
const char oob[] = "OUT OF BOUNDS";
const char inb[] = "IN BOUNDS";
uint8_t buffer[] = {
TYPE_STRING,
PROTO_LEN,
FIELD_ONE_FLOAT, 0x00, 0x00, 0x48, 0x43,
FIELD_TWO_FLOAT, 0x00, 0x00, 0xc8, 0x42,
FIELD_TWO_VARIANT, 0, 0};
void do_send(osjob_t* j){
// 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.
const float latitude = gps.location.lat();
const float longitude = gps.location.lng();
if(n_poly>0) {
uint8_t out_of_bounds = !check_bounds(latitude, longitude);
if(out_of_bounds) {
Serial.println(oob);
} else {
Serial.println(inb);
}
digitalWrite(LED_BUILTIN, out_of_bounds);
buffer[13] = out_of_bounds;
} else {
uint8_t out_of_bounds = 0;
buffer[13] = out_of_bounds;
}
memcpy(buffer+3, (void*)&latitude, 4);
memcpy(buffer+8, (void*)&longitude, 4);
LMIC_setTxData2(1, buffer, sizeof(buffer)-1, 0);
}
}
#define GPS_MAX_ENCODES 60
void read_gps(){
general_int = softserial_available();
while(--general_int > 0) {
gps.encode(softserial_read());
}
if(!is_sending && gps.location.isValid()) {
Serial.println("gps");
is_sending = 1;
os_setTimedCallback(&sendjob, os_getTime()+sec2osticks(TX_INTERVAL), do_send);
}
}
void setup() {
Serial.begin(9600);
softserial_init();
delay(100);
Serial.println(F("Starting"));
pinMode(LED_BUILTIN, OUTPUT);
// 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));
LMIC_setSession (0x13, DEVADDR, nwkskey, appskey);
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);
delay(1000);
}
void loop() {
os_runloop_once();
read_gps();
}