collar/collar.cpp

562 lines
16 KiB
C++
Raw Normal View History

2020-05-14 00:33:50 -07:00
/*******************************************************************************
* The Things Network - ABP Feather
*
* Example of using an Adafruit Feather M0 and DHT22 with a
* single-channel TheThingsNetwork gateway.
*
* This uses ABP (Activation by Personalization), where session keys for
* communication would be assigned/generated by TTN and hard-coded on the device.
*
* Learn Guide: https://learn.adafruit.com/lora-pi
*
* Copyright (c) 2015 Thomas Telkamp and Matthijs Kooijman
* Copyright (c) 2018 Terry Moore, MCCI
* Copyright (c) 2018 Brent Rubell, Adafruit Industries
*
* 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.
*******************************************************************************/
#include <lmic.h>
#include <hal/hal.h>
#include <SPI.h>
2020-04-18 23:27:19 -07:00
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
2020-05-14 00:33:50 -07:00
2020-04-18 23:27:19 -07:00
#include "Base64.h"
2020-04-18 20:07:23 -07:00
#include "gateway/message.pb.h"
2020-02-17 19:32:58 -08:00
#include "pb_common.h"
#include "pb.h"
#include "pb_encode.h"
2020-04-18 20:07:23 -07:00
#include "pb_decode.h"
2020-02-17 19:32:58 -08:00
#include "gateway/message.pb.h"
2020-02-17 19:32:58 -08:00
2020-05-14 00:33:50 -07:00
// DHT digital pin and sensor type
2020-05-14 23:19:37 -07:00
#define DHTPIN 60
2020-05-14 00:33:50 -07:00
#define DHTTYPE DHT22
//
// For normal use, we require that you edit the sketch to replace FILLMEIN
// with values assigned by the TTN console. However, for regression tests,
// we want to be able to compile these scripts. The regression tests define
// COMPILE_REGRESSION_TEST, and in that case we define FILLMEIN to a non-
// working but innocuous value.
//
/*
#ifdef COMPILE_REGRESSION_TEST
# define FILLMEIN 0
#else
# warning "You must replace the values marked FILLMEIN with real values from the TTN control panel!"
# define FILLMEIN (#dont edit this, edit the lines that use FILLMEIN)
#endif
*/
// 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
// payload to send to TTN gateway
//static uint8_t payload[] = "Hello, world!";
2020-02-17 19:32:58 -08:00
2020-04-18 20:07:23 -07:00
// Data Packet to Send to TTN
2020-05-14 00:33:50 -07:00
u1_t loraData[Fenceless_CollarResponse_size+1] = {0};
static osjob_t sendjob;
// 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-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-14 00:33:50 -07:00
// init. DHT
//DHT dht(DHTPIN, DHTTYPE);
2020-04-07 15:23:50 -07:00
2020-05-12 22:37:12 -07:00
/****************************************************
* Arduino drivers
* - LoRaWAN
* - GPS
* - Software Serial
***************************************************/
2020-04-18 23:27:19 -07:00
TinyGPSPlus gps;
2020-05-15 01:49:46 -07:00
// SoftwareSerial ss(6, 7);
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-04-18 20:07:23 -07:00
int push_vert(float x, float y) {
if(n_poly>N_POLY_MAX)
return 0;
polyx[n_poly]=x;
polyy[n_poly]=y;
n_poly++;
return 1;
}
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-04-18 20:07:23 -07:00
int pnpoly(int nvert, float *vertx, float *verty, float testx, float testy)
2020-05-12 22:37:12 -07:00
{
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-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-04-18 20:07:23 -07:00
int check_bounds(float x, float y) {
return pnpoly(n_poly, polyx, polyy, x, y);
}
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-04-18 20:07:23 -07:00
void import_protobuf(uint8_t *protobuffer, uint32_t size) {
2020-05-14 23:19:37 -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
#define FIELD_ONE_VARIANT 0x08
#define FIELD_TWO_STRING 0x12
#define FIELD_THREE_STRING 0x1A
#define FIELD_FOUR_STRING 0x22
#define FIELD_FIVE_STRING 0x2A
#define FIELD_SIX_STRING 0x32
#define FIELD_SEVEN_STRING 0x3A
#define FIELD_EIGHT_STRING 0x42
#define FIELD_NINE_STRING 0x4A
#define FIELD_TEN_STRING 0x52
#define FIELD_ELEVEN_STRING 0x5A
/*uint8_t buffer0[] {
FIELD_ONE_VARIANT, 0x01,
FIELD_TWO_STRING, 0x0A,
FIELD_ONE_FLOAT, 0x1B, 0x91, 0xF6, 0xC2,
FIELD_TWO_FLOAT, 0x00, 0x00, 0x00, 0x40,
FIELD_THREE_STRING, 0x0A,
FIELD_ONE_FLOAT, 0xB5, 0x3B, 0x32, 0x42,
FIELD_TWO_FLOAT, 0x00, 0x00, 0x00, 0x40,
FIELD_FOUR_STRING, 0x0A,
FIELD_ONE_FLOAT, 0x00, 0x00, 0x80, 0x3F,
FIELD_TWO_FLOAT, 0x00, 0x00, 0x00, 0x40,
FIELD_FIVE_STRING, 0x0A,
FIELD_ONE_FLOAT, 0x00, 0x00, 0x80, 0x3F,
FIELD_TWO_FLOAT, 0x00, 0x00, 0x00, 0x40,
FIELD_SIX_STRING, 0x0A,
FIELD_ONE_FLOAT, 0x00, 0x00, 0x80, 0x3F,
FIELD_TWO_FLOAT, 0x00, 0x00, 0x00, 0x40,
FIELD_SEVEN_STRING, 0x0A,
FIELD_ONE_FLOAT, 0x00, 0x00, 0x80, 0x3F,
FIELD_TWO_FLOAT, 0x00, 0x00, 0x00, 0x40,
FIELD_EIGHT_STRING, 0x0A,
FIELD_ONE_FLOAT, 0x00, 0x00, 0x80, 0x3F,
FIELD_TWO_FLOAT, 0x00, 0x00, 0x00, 0x40,
FIELD_NINE_STRING, 0x0A,
FIELD_ONE_FLOAT, 0x00, 0x00, 0x80, 0x3F,
FIELD_TWO_FLOAT, 0x00, 0x00, 0x00, 0x40,
FIELD_TEN_STRING, 0x0A,
FIELD_ONE_FLOAT, 0x00, 0x00, 0x80, 0x3F,
FIELD_TWO_FLOAT, 0x00, 0x00, 0x00, 0x40,
FIELD_ELEVEN_STRING, 0x0A,
FIELD_ONE_FLOAT, 0x00, 0x00, 0x80, 0x3F,
FIELD_TWO_FLOAT, 0x00, 0x00, 0x00, 0x40
};*/
Serial.println("DECODE FUNCTION");
if(size != 122) {
2020-04-18 20:07:23 -07:00
Serial.println("Failed to decode");
2020-05-14 23:19:37 -07:00
Serial.print("Size:");
Serial.println(size);
return;
2020-04-18 20:07:23 -07:00
}
2020-05-14 23:19:37 -07:00
/*
* this stuff does not work yet
*/
2020-05-14 00:33:50 -07:00
2020-05-15 01:49:46 -07:00
uint32_t isr;
isr = 0;
isr = protobuffer[1];
Serial.print("Isr: ");
Serial.println(isr);
if(isr>N_POLY_MAX) isr = N_POLY_MAX;
Serial.println("Recieved valid protobuf data?");
clear_verts();
uint8_t *ptr = protobuffer+5;
for(uint32_t i=0;i<isr;i++) {
if(i%5==0)
Serial.println();
float x,y;
memcpy(&x, ptr + i*12, 4);
memcpy(&y, ptr + i*12+5, 4);
Serial.print('(');
Serial.print(x);
Serial.print(',');
Serial.print(y);
Serial.print(')');
Serial.print(' ');
push_vert(x, y);
}
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 23:19:37 -07:00
void onEvent (ev_t ev) {
Serial.print(os_getTime());
Serial.print(": ");
switch(ev) {
case EV_SCAN_TIMEOUT:
Serial.println(F("EV_SCAN_TIMEOUT"));
break;
case EV_BEACON_FOUND:
Serial.println(F("EV_BEACON_FOUND"));
break;
case EV_BEACON_MISSED:
Serial.println(F("EV_BEACON_MISSED"));
break;
case EV_BEACON_TRACKED:
Serial.println(F("EV_BEACON_TRACKED"));
break;
case EV_JOINING:
Serial.println(F("EV_JOINING"));
break;
case EV_JOINED:
Serial.println(F("EV_JOINED"));
break;
/*
|| This event is defined but not used in the code. No
|| point in wasting codespace on it.
||
|| case EV_RFU1:
|| Serial.println(F("EV_RFU1"));
|| break;
*/
case EV_JOIN_FAILED:
Serial.println(F("EV_JOIN_FAILED"));
break;
case EV_REJOIN_FAILED:
Serial.println(F("EV_REJOIN_FAILED"));
break;
case EV_TXCOMPLETE:
Serial.println(F("EV_TXCOMPLETE (includes waiting for RX windows)"));
2020-05-15 01:49:46 -07:00
if (LMIC.txrxFlags & TXRX_ACK)
2020-05-14 23:19:37 -07:00
Serial.println(F("Received ack"));
2020-05-15 01:49:46 -07:00
if (LMIC.dataLen) {
Serial.println(F("Received "));
Serial.println(LMIC.dataLen);
// Serial.println(F(" bytes of payload"));
// for(int i=0;i<LMIC.dataLen;i++) {
// Serial.print(LMIC.frame[LMIC.dataBeg + i], HEX);
// Serial.print(' ');
// Serial.print('-');
// Serial.print(' ');
// if(i%10==0)
// Serial.println();
// }
// Serial.println();
import_protobuf(LMIC.frame + LMIC.dataBeg, LMIC.dataLen);
}
2020-05-14 23:19:37 -07:00
2020-05-15 01:49:46 -07:00
os_setTimedCallback(&sendjob, os_getTime()+sec2osticks(TX_INTERVAL), do_send);
2020-05-14 23:19:37 -07:00
break;
case EV_LOST_TSYNC:
Serial.println(F("EV_LOST_TSYNC"));
break;
case EV_RESET:
Serial.println(F("EV_RESET"));
break;
case EV_RXCOMPLETE:
// data received in ping slot
Serial.println(F("EV_RXCOMPLETE"));
break;
case EV_LINK_DEAD:
Serial.println(F("EV_LINK_DEAD"));
break;
case EV_LINK_ALIVE:
Serial.println(F("EV_LINK_ALIVE"));
break;
/*
|| This event is defined but not used in the code. No
|| point in wasting codespace on it.
||
|| case EV_SCAN_FOUND:
|| Serial.println(F("EV_SCAN_FOUND"));
|| break;
*/
case EV_TXSTART:
Serial.println(F("EV_TXSTART"));
break;
case EV_TXCANCELED:
Serial.println(F("EV_TXCANCELED"));
break;
case EV_RXSTART:
/* do not print anything -- it wrecks timing */
break;
case EV_JOIN_TXCOMPLETE:
Serial.println(F("EV_JOIN_TXCOMPLETE: no JoinAccept"));
break;
default:
Serial.print(F("Unknown event: "));
Serial.println((unsigned) ev);
break;
}
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-14 23:19:37 -07:00
2020-05-15 01:49:46 -07:00
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};
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 01:49:46 -07:00
float latitude = gps.location.lat();
float longitude = gps.location.lng();
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
}
// Next TX is scheduled after TX_COMPLETE event.
}
void setup() {
2020-05-14 23:19:37 -07:00
delay(1000);
2020-05-15 01:49:46 -07:00
Serial.begin(4800);
// ss.begin(4800);
2020-05-14 23:19:37 -07:00
2020-05-14 00:33:50 -07:00
delay(100);
Serial.println(F("Starting"));
2020-04-18 20:07:23 -07:00
pinMode(LED_BUILTIN, OUTPUT);
2020-05-14 00:33:50 -07:00
// 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);
/*
// We'll disable all 72 channels used by TTN
for (int c = 0; c < 72; c++){
LMIC_disableChannel(c);
2020-02-17 19:32:58 -08:00
}
2020-04-18 20:22:39 -07:00
2020-05-14 00:33:50 -07:00
// We'll only enable Channel 16 (905.5Mhz) since we're transmitting on a single-channel
LMIC_enableChannel(16);
*/
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 01:49:46 -07:00
delay(2000);
2020-05-14 00:33:50 -07:00
// Start job
2020-05-14 23:19:37 -07:00
// do_send(&sendjob);
2020-02-17 19:32:58 -08:00
}
2020-05-12 19:23:32 -07:00
/****************************************************
2020-05-12 21:49:37 -07:00
* Read a byte from GPS over software serial
2020-05-12 19:23:32 -07:00
***************************************************/
2020-05-12 21:49:37 -07:00
int read_gps() {
int ret = 0;
2020-05-15 01:49:46 -07:00
// int timeout = 0;
while(Serial.available()>0) {// || timeout < 20) {
gps.encode(Serial.read());
ret = 1;
//timeout++;
}
2020-05-12 21:49:37 -07:00
return ret;
}
/****************************************************
* Set cursor to beginning of line and clear it
***************************************************/
2020-05-12 22:37:12 -07:00
const int16_t PROGRESS_BAR_COUNT = 50;
const int16_t START_OF_LINE = 13;
2020-05-15 01:49:46 -07:00
int led_on = 0;
2020-05-12 21:49:37 -07:00
void clear_line() {
Serial.write(START_OF_LINE);
for(int i=0;i<PROGRESS_BAR_COUNT;i++)
Serial.write(' ');
Serial.write(START_OF_LINE);
2020-05-15 01:49:46 -07:00
digitalWrite(LED_BUILTIN, led_on);
led_on = !led_on;
2020-05-12 21:49:37 -07:00
}
2020-05-15 01:49:46 -07:00
2020-05-12 21:49:37 -07:00
/****************************************************
* State variables
* - track events of main loop
***************************************************/
enum STATE_ {
START_GPS,
WAITING_GPS,
VERIFYING_GPS,
SENDING_LORA,
WAITING_LORA,
LORA_DONE
};
2020-05-15 01:49:46 -07:00
int state = START_GPS;
2020-05-12 21:49:37 -07:00
int loopCounter = 0;
int startTime = 0;
2020-05-15 01:49:46 -07:00
uint32_t got_data = 0;
2020-05-14 00:33:50 -07:00
void loop() {
2020-05-15 01:49:46 -07:00
switch(state) {
case START_GPS:
Serial.println("Waiting for GPS");
2020-05-12 21:49:37 -07:00
state = WAITING_GPS;
2020-05-15 01:49:46 -07:00
break;
case WAITING_GPS:
got_data =
read_gps();
/****************************************************
* loading bar animation
***************************************************/
if(got_data) {
if(loopCounter%100==0)
Serial.write('.');
if(loopCounter>PROGRESS_BAR_COUNT*100) {
clear_line();
loopCounter=0;
state = VERIFYING_GPS;
}
loopCounter++;
}
break;
case VERIFYING_GPS:
/****************************************************
* if no data has been received from the gps in 5 seconds
* then the GPS is probably not connected properly
***************************************************/
if (millis() > 5000 && gps.charsProcessed() < 10)
{
Serial.println(F("No GPS detected: check wiring."));
while(true);
}
/****************************************************
* only send to LoRaWAN if valid GPS coordinates are
* available
***************************************************/
if(gps.location.isValid())
state = SENDING_LORA;
else
state = WAITING_GPS;
break;
case SENDING_LORA:
do_send(&sendjob);
digitalWrite(LED_BUILTIN, 0);
2020-05-12 21:49:37 -07:00
state = LORA_DONE;
2020-05-15 01:49:46 -07:00
break;
default:
break;
2020-05-12 21:49:37 -07:00
}
2020-05-14 23:19:37 -07:00
os_runloop_once();
2020-02-17 19:32:58 -08:00
}