collar/collar.cpp

269 lines
6.8 KiB
C++
Raw Normal View History

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-15 23:12:03 -07:00
void os_getArtEui (u1_t* buf) { }
void os_getDevEui (u1_t* buf) { }
void os_getDevKey (u1_t* buf) { }
static osjob_t sendjob;
2020-05-15 18:27:51 -07:00
// 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;
2020-05-14 00:33:50 -07:00
static const u4_t DEVADDR = 0x260212B6;
2020-04-07 15:23:50 -07:00
2020-05-15 18:27:51 -07:00
void debug_function(char *str) {
2020-05-15 23:51:15 -07:00
// Serial.println(str);
2020-05-15 18:27:51 -07:00
}
2020-05-14 00:33:50 -07:00
// Schedule TX every this many seconds (might become longer due to duty
// cycle limitations).
2020-05-15 23:12:03 -07:00
const unsigned TX_INTERVAL = 5;
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-15 14:20:43 -07:00
uint8_t general_int;
2020-05-15 18:27:51 -07:00
volatile uint8_t n_poly;
2020-05-15 14:20:43 -07:00
#define isr general_int
#define timeout general_int
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-05-15 15:14:53 -07:00
float polyx[N_POLY_MAX*2+5];
float * const polyy = polyx + N_POLY_MAX;
2020-05-15 23:58:13 -07:00
void do_send(osjob_t* j);
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 23:51:15 -07:00
inline const int pnpoly
2020-05-15 15:14:53 -07:00
(const uint8_t nvert, const float *vertx, const float *verty, const float testx, const float testy)
2020-05-15 05:30:37 -07:00
{
2020-05-15 15:14:53 -07:00
uint8_t i, j, c = 0;
2020-04-18 20:07:23 -07:00
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 23:51:15 -07:00
inline 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 23:12:03 -07:00
inline void import_protobuf(const uint8_t *protobuffer, const uint8_t size) {
2020-05-14 23:19:37 -07:00
if(size != 122) {
2020-05-15 14:20:43 -07:00
Serial.println("nmd");
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
2020-05-15 01:49:46 -07:00
isr = protobuffer[1];
2020-05-15 14:20:43 -07:00
if(isr>N_POLY_MAX) isr = 0;
2020-05-15 05:30:37 -07:00
const uint8_t *ptr = protobuffer + 5;
2020-05-15 14:20:43 -07:00
for(uint8_t i=0;i<isr && i<N_POLY_MAX;i++) {
2020-05-15 23:51:15 -07:00
memcpy(&polyy[i], ptr + i*12, 4);
memcpy(&polyx[i], ptr + i*12+5, 4);
2020-05-15 01:49:46 -07:00
}
2020-05-15 18:27:51 -07:00
n_poly = isr;
2020-05-14 23:19:37 -07:00
}
2020-05-15 23:51:15 -07:00
2020-05-14 23:19:37 -07:00
void onEvent (ev_t ev) {
2020-05-15 05:30:37 -07:00
if(ev == EV_TXCOMPLETE) {
if (LMIC.dataLen) {
2020-05-15 23:51:15 -07:00
Serial.print(F(" -Received: "));
2020-05-15 23:12:03 -07:00
Serial.println(LMIC.dataLen);
2020-05-15 23:58:13 -07:00
import_protobuf(LMIC.frame + LMIC.dataBeg, LMIC.dataLen);
2020-05-15 23:12:03 -07:00
}
// Schedule next transmission
os_setTimedCallback(&sendjob, os_getTime()+sec2osticks(TX_INTERVAL), do_send);
}
}
enum STATE_ {
START,
START_GPS,
WAIT_GPS,
START_LORA,
WAIT_LORA,
END
};
volatile uint8_t state;
volatile uint32_t since;
void setup() {
Serial.begin(9600);
2020-05-15 23:51:15 -07:00
softserial_init();
2020-05-15 23:12:03 -07:00
os_init();
LMIC_reset();
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);
state = START_GPS;
do_send(&sendjob);
}
inline void read_gps() {
uint8_t available = softserial_available();
2020-05-15 23:51:15 -07:00
while(available) {
2020-05-15 23:12:03 -07:00
gps.encode(softserial_read());
available--;
}
}
inline void on_start_gps() {
state = WAIT_GPS;
}
inline void on_wait_gps() {
if(gps.location.isValid()) {
state = START_LORA;
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 23:51:15 -07:00
const char oob[] = " -OUT OF BOUNDS";
const char inb[] = " -IN BOUNDS";
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,
2020-05-15 14:20:43 -07:00
FIELD_TWO_VARIANT, 0, 0};
2020-05-14 23:19:37 -07:00
2020-05-15 23:12:03 -07:00
void on_start_lora() {
// Serial.println("Starting lora");
}
void send_lora() {
2020-05-14 00:33:50 -07:00
if (LMIC.opmode & OP_TXRXPEND) {
2020-05-15 23:12:03 -07:00
Serial.print(" -cns");
2020-05-14 00:33:50 -07:00
} else {
2020-05-15 14:20:43 -07:00
const float latitude = gps.location.lat();
2020-05-15 05:30:37 -07:00
const float longitude = gps.location.lng();
2020-05-15 14:20:43 -07:00
if(n_poly>0) {
2020-05-15 23:58:13 -07:00
const uint8_t out_of_bounds = !check_bounds(latitude, longitude);
2020-05-15 18:27:51 -07:00
if(out_of_bounds) {
2020-05-15 14:20:43 -07:00
Serial.println(oob);
} else {
Serial.println(inb);
}
2020-05-15 23:12:03 -07:00
// digitalWrite(LED_BUILTIN, out_of_bounds);
2020-05-15 18:27:51 -07:00
buffer[13] = out_of_bounds;
2020-05-15 14:20:43 -07:00
} else {
2020-05-15 23:12:03 -07:00
const uint8_t out_of_bounds = 0;
2020-05-15 18:27:51 -07:00
buffer[13] = out_of_bounds;
2020-05-15 14:20:43 -07:00
}
2020-05-14 00:33:50 -07:00
memcpy(buffer+3, (void*)&latitude, 4);
memcpy(buffer+8, (void*)&longitude, 4);
2020-05-15 15:14:53 -07:00
LMIC_setTxData2(1, buffer, sizeof(buffer)-1, 0);
2020-05-14 00:33:50 -07:00
}
2020-05-15 23:12:03 -07:00
since = millis();
state = WAIT_LORA;
}
void do_send(osjob_t* j){
send_lora();
2020-05-15 05:30:37 -07:00
}
2020-05-15 23:19:31 -07:00
#define TIMEOUT TX_INTERVAL * 2 * 1000UL
2020-05-15 23:12:03 -07:00
void on_wait_lora() {
2020-05-15 23:51:15 -07:00
if(millis()- since > TX_INTERVAL*1000UL) {
2020-05-15 23:12:03 -07:00
Serial.println(" -Lora Done Sending");
state = START_GPS;
2020-05-15 05:30:37 -07:00
}
2020-05-15 23:51:15 -07:00
if(millis() - since > TIMEOUT) {
Serial.println(" -Scheduling another job");
2020-05-15 23:19:31 -07:00
os_setTimedCallback(&sendjob, os_getTime()+sec2osticks(TX_INTERVAL), do_send);
}
2020-05-14 00:33:50 -07:00
}
2020-05-15 23:12:03 -07:00
#define LOOP_LATENCY_MS 200L
uint32_t time_last = 0;
2020-05-14 00:33:50 -07:00
void loop() {
2020-05-15 23:12:03 -07:00
uint32_t time = millis();
if((time - time_last) > LOOP_LATENCY_MS) {
time_last = time;
Serial.print(state);
Serial.print('.');
if(state == START_GPS) {
on_start_gps();
}
else if(state == WAIT_GPS) {
on_wait_gps();
}
else if(state == START_LORA) {
on_start_lora();
}
else if(state == WAIT_LORA) {
on_wait_lora();
}
2020-05-15 23:58:13 -07:00
}
2020-05-15 23:51:15 -07:00
read_gps();
2020-05-15 14:20:43 -07:00
os_runloop_once();
2020-02-17 19:32:58 -08:00
}