Implementing GPS and PB
This commit is contained in:
parent
cd39a64bed
commit
3aa6c89e84
89
sender.ino
Normal file
89
sender.ino
Normal file
|
@ -0,0 +1,89 @@
|
|||
#include <SPI.h>
|
||||
#include <LoRa.h>
|
||||
#include <TinyGPS++.h>
|
||||
#include <SoftwareSerial.h>
|
||||
|
||||
#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");
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user