diff --git a/Makefile b/Makefile index f0bca4b..e6fa039 100644 --- a/Makefile +++ b/Makefile @@ -17,6 +17,7 @@ CFLAGS?=-Datmega328p\ -DARDUINO=200\ -DARDUINO_AVR_NANO\ -DARDUINO_ARCH_AVR\ + -DARDUINO_LMIC_PROJECT_CONFIG_H=fenceless_lmic_config.h\ -Os\ -ffunction-sections\ -fdata-sections\ @@ -35,6 +36,7 @@ CXXFLAGS?=-Datmega328p\ -DARDUINO=200\ -DARDUINO_AVR_NANO\ -DARDUINO_ARCH_AVR\ + -DARDUINO_LMIC_PROJECT_CONFIG_H=fenceless_lmic_config.h\ -Os\ -ffunction-sections\ -fdata-sections\ @@ -42,7 +44,7 @@ CXXFLAGS?=-Datmega328p\ -flto\ -fno-fat-lto-objects\ -fuse-linker-plugin\ - -Wall + -Wall\ INC_DIRS?=-I./\ -I./$(ARDUINO_DIR)/libraries/SPI/src\ @@ -57,7 +59,8 @@ INC_DIRS?=-I./\ -I./libraries/arduino-lmic/src/hal/\ -I./libraries/arduino-lmic/src/lmic/\ -I./libraries/arduino-lmic/src/aes/\ - -I./libraries/AltSoftSerial/ + -I./libraries/AltSoftSerial/\ + -I./ SRC_FILES?=./$(ARDUINO_DIR)/cores/arduino/main.cpp\ ./$(ARDUINO_DIR)/cores/arduino/wiring_digital.c\ @@ -144,14 +147,13 @@ flash: $(OUT)$(NAME) avrdude -v -patmega328p -carduino -P/dev/ttyUSB0 -b115200 -D -Uflash:w:$(OUT)$(NAME).hex:i run: flash - screen /dev/ttyUSB0 4800 + screen /dev/ttyUSB0 start: flash systemctl start lora-gateway-bridge loraserver stop: avrdude -v -patmega328p -carduino -P/dev/ttyUSB0 -b115200 -D -Uflash:w:nothing.hex:i - systemctl stop lora-gateway-bridge loraserver clean: rm -r protobuf diff --git a/collar.cpp b/collar.cpp index f4ee3ab..8310729 100644 --- a/collar.cpp +++ b/collar.cpp @@ -28,12 +28,10 @@ void os_getDevEui (u1_t* buf) { } void os_getDevKey (u1_t* buf) { } static osjob_t sendjob; -static osjob_t gpsjob; // Schedule TX every this many seconds (might become longer due to duty // cycle limitations). const unsigned TX_INTERVAL = 10; -const unsigned GPS_INTERVAL = 1; // Pin mapping for Adafruit Feather M0 LoRa const lmic_pinmap lmic_pins = { @@ -50,6 +48,11 @@ const lmic_pinmap lmic_pins = { ***************************************************/ TinyGPSPlus gps; + +uint8_t general_int; +#define isr general_int +#define timeout general_int +#define n_poly general_int /**************************************************** * Track each pair of X and Y coordinates * - arrays are used by the pnpoly function @@ -57,28 +60,7 @@ TinyGPSPlus gps; const uint8_t N_POLY_MAX=10; float polyx[N_POLY_MAX]; float polyy[N_POLY_MAX]; -int n_poly=0; - -/**************************************************** - * Add a coordinate to the arrays - * - stores a total of N_POLY_MAX pairs - ***************************************************/ -const int push_vert(const float x, const float y) { - if(n_poly>N_POLY_MAX) - return 0; - polyx[n_poly]=x; - polyy[n_poly]=y; - n_poly++; - return 1; -} - -/**************************************************** - * 'Clear' pairs of coordinates - ***************************************************/ -void clear_verts() { - n_poly=0; -} - +//int n_poly=0; /**************************************************** * Check a pair of coordinates against two lists * of vertices @@ -112,31 +94,28 @@ const int check_bounds(const float x, const float y) { ***************************************************/ void import_protobuf(const uint8_t *protobuffer, const uint32_t size) { if(size != 122) { - Serial.println("Failed to decode"); + Serial.println("nmd"); return; } Serial.println("Recieved valid protobuf data?"); - uint32_t isr; - isr = 0; isr = protobuffer[1]; - if(isr>N_POLY_MAX) isr = N_POLY_MAX; - - clear_verts(); - + if(isr>N_POLY_MAX) isr = 0; const uint8_t *ptr = protobuffer + 5; - for(uint32_t i=0;i0) { + const uint8_t oob = !check_bounds(latitude, longitude); + if(oob) { + Serial.println(oob); + } else { + Serial.println(inb); + } + memcpy(buffer+13, (void*)&oob, 1); + } else { + const uint8_t oob = 0; + memcpy(buffer+13, (void*)&oob, 1); + } memcpy(buffer+3, (void*)&latitude, 4); memcpy(buffer+8, (void*)&longitude, 4); - memcpy(buffer+13, (void*)&oob, 1); - - LMIC_setTxData2(1, buffer, sizeof(buffer), 0); + LMIC_setTxData2(1, buffer, sizeof(buffer)-2, 0); } } -void do_gps(osjob_t* j){ - while(softserial_available()>0) { +#define GPS_MAX_ENCODES 60 +void read_gps(){ + timeout = 0; + while(softserial_available()>0 && timeout < GPS_MAX_ENCODES) { gps.encode(softserial_read()); + timeout ++; } if(!is_sending && gps.location.isValid()) { + Serial.println("gps"); is_sending = 1; - do_send(&sendjob); + os_setTimedCallback(&sendjob, os_getTime()+sec2osticks(TX_INTERVAL), do_send); } - os_setTimedCallback(&gpsjob, os_getTime()+sec2osticks(GPS_INTERVAL), do_gps); } void setup() { - Serial.begin(4800); + Serial.begin(9600); softserial_init(); delay(100); @@ -237,10 +230,9 @@ void setup() { // Set data rate and transmit power for uplink LMIC_setDrTxpow(DR_SF7,14); - - os_setTimedCallback(&gpsjob, os_getTime()+sec2osticks(GPS_INTERVAL), do_gps); } void loop() { - os_runloop_once(); + os_runloop_once(); + read_gps(); } diff --git a/fenceless_lmic_config.h b/fenceless_lmic_config.h new file mode 100644 index 0000000..587899e --- /dev/null +++ b/fenceless_lmic_config.h @@ -0,0 +1,15 @@ +// project-specific definitions +//#define CFG_eu868 1 +#define CFG_us915 1 +//#define CFG_au915 1 +//#define CFG_as923 1 +// #define LMIC_COUNTRY_CODE LMIC_COUNTRY_CODE_JP /* for as923-JP */ +//#define CFG_kr920 1 +//#define CFG_in866 1 +#define CFG_sx1276_radio 1 +//#define LMIC_USE_INTERRUPTS +#define DISABLE_JOIN +#define DISABLE_PING +#define DISABLE_BEACONS +#define DISABLE_LMIC_FAILURE_TO +#define USE_IDEETRON_AES diff --git a/libraries/AltSoftSerial/.AltSoftSerial.cpp.swp b/libraries/AltSoftSerial/.AltSoftSerial.cpp.swp new file mode 100644 index 0000000..7fb15e1 Binary files /dev/null and b/libraries/AltSoftSerial/.AltSoftSerial.cpp.swp differ diff --git a/libraries/AltSoftSerial/AltSoftSerial.cpp b/libraries/AltSoftSerial/AltSoftSerial.cpp index 833f0af..8883e49 100644 --- a/libraries/AltSoftSerial/AltSoftSerial.cpp +++ b/libraries/AltSoftSerial/AltSoftSerial.cpp @@ -47,7 +47,7 @@ static uint8_t rx_bit = 0; static uint16_t rx_target; static volatile uint8_t rx_buffer_head; static volatile uint8_t rx_buffer_tail; -#define RX_BUFFER_SIZE 80 +#define RX_BUFFER_SIZE 30 static volatile uint8_t rx_buffer[RX_BUFFER_SIZE]; const static uint32_t cycles_per_bit = 1667UL;