current working changes
This commit is contained in:
137
collar.cpp
137
collar.cpp
@@ -14,14 +14,79 @@ const long interval = 5000;
|
||||
float lat = 0;
|
||||
float lng = 0;
|
||||
|
||||
bool sending = false;
|
||||
|
||||
TinyGPSPlus gps;
|
||||
|
||||
SoftwareSerial ss(6, 7);
|
||||
|
||||
typedef struct lora_status_s {
|
||||
int sleep;
|
||||
} lora_status_t;
|
||||
|
||||
static lora_status_t lora_status;
|
||||
|
||||
void transmitLora() {
|
||||
// Encode as protobuf packet
|
||||
if (lat && lng) {
|
||||
uint8_t buffer[50] = {0};
|
||||
Fenceless_CollarResponse m = Fenceless_CollarResponse_init_zero;
|
||||
pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
|
||||
m.loc.x = lat;
|
||||
m.loc.y = lng;
|
||||
|
||||
int status = pb_encode(&stream, Fenceless_CollarResponse_fields, &m);
|
||||
|
||||
if (!status) {
|
||||
Serial.println("Failed to encode");
|
||||
}
|
||||
|
||||
Serial.print("Sending packet Lat / Long: ");
|
||||
Serial.println(lat);
|
||||
Serial.println(lng);
|
||||
|
||||
// send packet
|
||||
|
||||
LoRa.beginPacket();
|
||||
LoRa.print((char *) buffer);
|
||||
LoRa.endPacket();
|
||||
|
||||
}
|
||||
}
|
||||
void updateGPS() {
|
||||
// Gather GPS data
|
||||
if (gps.location.isValid()) {
|
||||
lat = gps.location.lat();
|
||||
lng = gps.location.lng();
|
||||
} else {
|
||||
lat = 0;
|
||||
lng = 0;
|
||||
}
|
||||
}
|
||||
ISR(TIMER1_OVF_vect) {
|
||||
// handle timer overflow interrupt at 1 per second
|
||||
updateGPS();
|
||||
if(!gps.location.isValid()) {
|
||||
Serial.println("Seeking GPS");
|
||||
}
|
||||
if(sending && gps.location.isValid()) {
|
||||
if(lora_status.sleep) {
|
||||
}
|
||||
transmitLora();
|
||||
}
|
||||
}
|
||||
void initialize_timer() {
|
||||
TCCR1A = 0;
|
||||
TCCR1B = 0;
|
||||
TCNT1 = 34286; // timer preload
|
||||
TCCR1B |= (1<<CS12); // prescale: 256 bits
|
||||
TIMSK1 |= (1<<TOIE1); // overflow interrupt
|
||||
interrupts();
|
||||
}
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
ss.begin(9600);
|
||||
|
||||
|
||||
while (!Serial);
|
||||
|
||||
Serial.println("LoRa Transceiver");
|
||||
@@ -30,11 +95,31 @@ void setup() {
|
||||
Serial.println("Starting LoRa failed!");
|
||||
while (1);
|
||||
}
|
||||
LoRa.setSpreadingFactor(7);
|
||||
LoRa.setSpreadingFactor(7);
|
||||
|
||||
lora_status.sleep = 1;
|
||||
//lora.sleep();
|
||||
|
||||
initialize_timer();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
/* Get user input
|
||||
*/
|
||||
if(Serial.available()) {
|
||||
char n = Serial.read();
|
||||
if(n=='p') {
|
||||
sending ? Serial.println("Sending is: disabled") : Serial.println("Sending is: enabled");
|
||||
sending = !sending;
|
||||
} else {
|
||||
Serial.println("Type the letter 'p' to toggle sending");
|
||||
}
|
||||
}
|
||||
/* GPS data recieved */
|
||||
while (ss.available() > 0) {
|
||||
gps.encode(ss.read());
|
||||
}
|
||||
/* Lora data recieved */
|
||||
int packetSize = LoRa.parsePacket();
|
||||
if (packetSize) {
|
||||
Serial.print("Received Packet: ");
|
||||
@@ -43,50 +128,4 @@ void loop() {
|
||||
}
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
if (ss.available() > 0)
|
||||
gps.encode(ss.read());
|
||||
|
||||
unsigned long currentMillis = millis();
|
||||
if (currentMillis - previousMillis >= interval) {
|
||||
previousMillis = currentMillis;
|
||||
|
||||
// Gather GPS data
|
||||
if (gps.location.isValid()) {
|
||||
Serial.println("Valid GPS");
|
||||
lat = gps.location.lat();
|
||||
lng = gps.location.lng();
|
||||
} else {
|
||||
lat = 0;
|
||||
lng = 0;
|
||||
}
|
||||
|
||||
// Encode as protobuf packet
|
||||
if (lat && lng) {
|
||||
uint8_t buffer[50] = {0};
|
||||
Fenceless_CollarResponse m = Fenceless_CollarResponse_init_zero;
|
||||
pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
|
||||
m.loc.x = lat;
|
||||
m.loc.y = lng;
|
||||
|
||||
int status = pb_encode(&stream, Fenceless_CollarResponse_fields, &m);
|
||||
|
||||
if (!status) {
|
||||
Serial.println("Failed to encode");
|
||||
}
|
||||
|
||||
Serial.print("Sending packet Lat / Long: ");
|
||||
Serial.println(lat);
|
||||
Serial.println(lng);
|
||||
|
||||
// send packet
|
||||
|
||||
LoRa.beginPacket();
|
||||
LoRa.print((char *) buffer);
|
||||
LoRa.endPacket();
|
||||
|
||||
} else {
|
||||
Serial.println("GPS has not yet initialized");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user