This commit is contained in:
sessionm21
2020-04-19 07:27:19 +01:00
parent f51f10574c
commit f6156743cf
4 changed files with 81 additions and 18 deletions

View File

@@ -9,8 +9,11 @@
//
// Modified by Brent Rubell for Adafruit Industries, 2018
/************************** Configuration ***********************************/
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include <TinyLoRa.h>
#include <SPI.h>
#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);
}