From f6156743cf8aacf8e1e451c527cda37ce09b4d23 Mon Sep 17 00:00:00 2001 From: sessionm21 Date: Sun, 19 Apr 2020 07:27:19 +0100 Subject: [PATCH] cleanup --- .gitmodules | 3 +++ Makefile | 3 +++ README.md | 18 +++++++++++++ collar.cpp | 75 ++++++++++++++++++++++++++++++++++++++++------------- 4 files changed, 81 insertions(+), 18 deletions(-) diff --git a/.gitmodules b/.gitmodules index feca297..1d21aad 100644 --- a/.gitmodules +++ b/.gitmodules @@ -16,3 +16,6 @@ [submodule "libraries/arduino-lmic"] path = libraries/arduino-lmic url = https://github.com/mcci-catena/arduino-lmic.git +[submodule "libraries/arduino-base64"] + path = libraries/arduino-base64 + url = https://github.com/adamvr/arduino-base64.git diff --git a/Makefile b/Makefile index 4aa8562..e9239c0 100644 --- a/Makefile +++ b/Makefile @@ -32,6 +32,7 @@ INC_DIRS?=-I./$(ARDUINO_DIR)/libraries/SPI/src\ -I./libraries/arduino-LoRa/src\ -I./libraries/TinyGPSPlus/src\ -I./libraries/TinyLoRa\ + -I./libraries/arduino-base64\ -I./$(NANOPB_DIR)\ -I./protobuf\ -I./$(ARDUINO_DIR)/libraries/SoftwareSerial/src/ @@ -57,6 +58,7 @@ SRC_FILES?=./$(ARDUINO_DIR)/cores/arduino/main.cpp\ ./libraries/arduino-LoRa/src/LoRa.cpp\ ./libraries/TinyGPSPlus/src/TinyGPS++.cpp\ ./libraries/TinyLoRa/TinyLoRa.cpp\ + ./libraries/arduino-base64/Base64.cpp\ ./$(NANOPB_DIR)/pb_encode.c\ ./$(NANOPB_DIR)/pb_decode.c\ ./$(NANOPB_DIR)/pb_common.c\ @@ -96,6 +98,7 @@ run: flash 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 diff --git a/README.md b/README.md index 55fbef7..40831e7 100644 --- a/README.md +++ b/README.md @@ -7,3 +7,21 @@ # Python3 dependencies - protobuf - grpcio-tools + +# Setup + +On a clean clone one would most likely have to run +``` +git submodule update --recursive +``` + +# Makefile Stuff + +The most helpful makefile recipies are start, stop, and run + +- start flashes the arduino and starts system services for lorawan + +- stop flashes a dummy program to the arduino and stops services for lorawan + +- run flashes the arduino and connects to uart using screen + diff --git a/collar.cpp b/collar.cpp index 5eed5f4..a7a3511 100644 --- a/collar.cpp +++ b/collar.cpp @@ -9,8 +9,11 @@ // // Modified by Brent Rubell for Adafruit Industries, 2018 /************************** Configuration ***********************************/ +#include +#include #include #include +#include "Base64.h" #include "gateway/message.pb.h" #include "pb_common.h" @@ -42,6 +45,8 @@ const unsigned int sendInterval = 60; // Pinout for Adafruit Feather 32u4 LoRa TinyLoRa lora = TinyLoRa(2, 10, 9); +TinyGPSPlus gps; +SoftwareSerial ss(6, 7); // Pinout for Adafruit Feather M0 LoRa //TinyLoRa lora = TinyLoRa(3, 8, 4); @@ -140,14 +145,30 @@ void on_recieve(int n) { } void setup() { - delay(2000); + delay(1000); + // start uart Serial.begin(9600); + // start software uart for GPS + ss.begin(4800); while (! Serial); - delay(4000); + delay(1000); // Initialize pin LED_BUILTIN as an output pinMode(LED_BUILTIN, OUTPUT); + // Initialize GPS - GPS does not work indoors - This will block until GPS available + // Serial.println("Starting GPS"); + // while(!gps.location.isValid()) { + // while(ss.available()>0) { + // gps.encode(ss.read()); + // } + // if (millis() > 5000 && gps.charsProcessed() < 10) + // { + // Serial.println(F("No GPS detected: check wiring.")); + // while(true); + // } + // } + // Initialize LoRa Serial.print("Starting LoRa..."); // define multi-channel sending @@ -161,13 +182,6 @@ void setup() while(true); } - Fenceless_CollarResponse coord; - coord.loc.x = 0; - coord.loc.y = 0; - - pb_ostream_t stream; - stream = pb_ostream_from_buffer(loraData, sizeof(loraData)); - int err = pb_encode(&stream, Fenceless_CollarResponse_fields, &coord); // Optional set transmit power. If not set default is +17 dBm. // Valid options are: -80, 1 to 17, 20 (dBm). // For safe operation in 20dBm: your antenna must be 3:1 VWSR or better @@ -186,30 +200,55 @@ void setup() Serial.println("OK"); } +// TIMER1_OVF_vect is called once per second int sendCounter = 0; ISR(TIMER1_OVF_vect) { digitalWrite(LED_BUILTIN, sendCounter%2); - if(sendCounter==0) { + if(sendCounter==0 && gps.location.isValid()) { + Serial.println("Valid gps"); + + // reset send counter sendCounter = sendInterval; + Fenceless_CollarResponse coord; + coord.loc.x = gps.location.lat(); + coord.loc.y = gps.location.lng(); + + pb_ostream_t stream; + stream = pb_ostream_from_buffer(loraData, sizeof(loraData)); + int err = pb_encode(&stream, Fenceless_CollarResponse_fields, &coord); + + // Generate copy pasteable base64 + // char base64[50]; + // base64_encode(base64, (char*)loraData, stream.bytes_written); + // Serial.println(stream.bytes_written); + // Serial.println(base64); + Serial.println("Sending LoRa Data..."); - lora.sendData(loraData, sizeof(loraData), lora.frameCounter); + lora.sendData(loraData, stream.bytes_written, lora.frameCounter); // Optionally set the Frame Port (1 to 255) // uint8_t framePort = 1; // lora.sendData(loraData, sizeof(loraData), lora.frameCounter, framePort); Serial.print("Frame Counter: ");Serial.println(lora.frameCounter); lora.frameCounter++; - - Serial.println("delaying..."); + } + else if (!gps.location.isValid()) { + Serial.println("waiting for gps"); + } + else { + Serial.println("delaying til next frame counter"); } sendCounter--; } void loop() { - if(recieved) { - Serial.println("Recieved something"); - } else { - Serial.println("Nothing yet"); - } + // For later when recieving from gateway is implemented + // if(recieved) { + // Serial.println("Recieved something"); + // } else { + // Serial.println("Nothing yet"); + // } + if(ss.available()>0) + gps.encode(ss.read()); delay(1000); }