diff --git a/sender.ino b/sender.ino new file mode 100644 index 0000000..b5bcfc3 --- /dev/null +++ b/sender.ino @@ -0,0 +1,89 @@ +#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.location.isValid()) { + Serial.println("Valid GPS"); + lat = gps.location.lat(); + lng = gps.location.lng(); + } else { + lat = 0; + lng = 0; + } + + // Encode as protobuf packet + if (1) { + uint8_t buffer[10]; + Fenceless_Coordinate m = Fenceless_Coordinate_init_zero; + pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer)); + //m.x = lat; + //m.y = lng; + m.x = 44.558750; + m.y = -123.283744; + + if (!pb_encode(&stream, Fenceless_Coordinate_fields, &m)) { + Serial.println("Failed to encode"); + } + + 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"); + } + } +}