#include #include #include #include #include "pb_common.h" #include "pb.h" #include "pb_encode.h" #include "message.pb.h" unsigned long previousMillis = 0; const long interval = 5000; float lat = 0; float lng = 0; TinyGPSPlus gps; SoftwareSerial ss(6, 7); void setup() { Serial.begin(9600); ss.begin(9600); while (!Serial); Serial.println("LoRa Transceiver"); if (!LoRa.begin(915E6)) { Serial.println("Starting LoRa failed!"); while (1); } LoRa.setSpreadingFactor(7); } void loop() { int packetSize = LoRa.parsePacket(); if (packetSize) { Serial.print("Received Packet: "); while (LoRa.available()) { Serial.print((char) LoRa.read()); } Serial.println(); } unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval) { previousMillis = currentMillis; // Gather GPS data if (gps.encode(ss.read()) && 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"); } else { Serial.print("Status: "); Serial.println(status); } Serial.print("Sending packet Lat / Long: "); Serial.print(lat); Serial.println(lng); // send packet LoRa.beginPacket(); LoRa.print((char *) buffer); LoRa.endPacket(); } else { Serial.println("GPS has not yet initialized"); } } }