Skip to content

Commit

Permalink
GPS coord's are now properly sent and received. Added payload functions.
Browse files Browse the repository at this point in the history
Signed-off-by: Brady <brady.aiello@gmail.com>
  • Loading branch information
brady-aiello committed Jan 2, 2018
1 parent 24ad218 commit f178aaa
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 21 deletions.
File renamed without changes.
5 changes: 5 additions & 0 deletions Seeeduino-LoRaWAN-ABP/payload_function.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function Decoder(bytes, port) {
return {
frame_counter: bytes[0]
}
}
50 changes: 29 additions & 21 deletions Seeeduino-LoRaWAN-GPS-app/Seeeduino-LoRaWAN-GPS-app.ino
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@ int sec = 0;

// The TinyGPS++ object
TinyGPSPlus gps;
float *latlong;
unsigned char *latlongbytes;
typedef union {
float f[2]; // Assigning fVal.f will also populate fVal.bytes;
unsigned char bytes[8]; // Both fVal.f and fVal.bytes share the same 4 bytes of memory.
} floatArr2Val;

floatArr2Val latlong;

float latitude;
float longitude;

Expand All @@ -44,9 +49,11 @@ void displayInfo()
SerialUSB.println("Got real lon");
}
SerialUSB.print(F("Location: "));
SerialUSB.print(latitude, 6);
SerialUSB.print(latlong.f[0], 6);
//SerialUSB.print(latitude, 6);
SerialUSB.print(F(","));
SerialUSB.print(longitude, 6);
SerialUSB.print(latlong.f[1], 6);
//SerialUSB.print(longitude, 6);
SerialUSB.println();
}

Expand Down Expand Up @@ -110,42 +117,43 @@ void setup()
{
Serial.begin(9600);
SerialUSB.begin(115200);
latlong = (float *)calloc(8,1);
latlongbytes = (unsigned char*)latlong;

memset(buffer, 0, 256);
setupLoRaABP();
//setupLoRaOTAA();
//TimerTcc0.initialize(60000000); 1 Minute
//TimerTcc0.initialize(60000000); 1 Minute
TimerTcc0.initialize(10000000); //10 seconds
TimerTcc0.attachInterrupt(timerIsr);
}

void loop()
{
if (sec == 3){
//Serial.write("h");
}
if (sec >= 4 ) {

// if (sec == 3){
// //Serial.write("h"); //Turn on GPS
// }
if (sec <= 2 ) {
while (Serial.available() > 0){
char currChar = Serial.read();
SerialUSB.print(currChar);
gps.encode(currChar);
}
latitude = gps.location.lat();
longitude = gps.location.lng();
if((latitude && longitude) && latitude != latlong[0]
&& longitude != latlong[1]){
latlong[0] = latitude;
latlong[1] = longitude;
if((latitude && longitude) && latitude != latlong.f[0]
&& longitude != latlong.f[1]){
latlong.f[0] = latitude;
latlong.f[1] = longitude;

SerialUSB.print("LatLong: ");
SerialUSB.println("LatLong: ");
for(int i = 0; i < 8; i++){
SerialUSB.write(latlongbytes + i, 1);
SerialUSB.print(latlong.bytes[i], HEX);
}
bool result = lora.transferPacket((unsigned char*)latlong, (char)8, (char)DEFAULT_RESPONSE_TIMEOUT);
SerialUSB.println("");
bool result = lora.transferPacket(latlong.bytes, 8, DEFAULT_RESPONSE_TIMEOUT);
}
}
else if (sec == 1 ){
//Serial.write("$PMTK161,0*28\r\n");
}
// else if (sec == 5 ){
// //Serial.write("$PMTK161,0*28\r\n"); //Put GPS to sleep
// }
}
25 changes: 25 additions & 0 deletions Seeeduino-LoRaWAN-GPS-app/payload_function.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
function Bytes2Float32(bytes) {
var sign = (bytes & 0x80000000) ? -1 : 1;
var exponent = ((bytes >> 23) & 0xFF) - 127;
var significand = (bytes & ~(-1 << 23));

if (exponent == 128)
return sign * ((significand) ? Number.NaN : Number.POSITIVE_INFINITY);

if (exponent == -127) {
if (significand == 0) return sign * 0.0;
exponent = -126;
significand /= (1 << 22);
} else significand = (significand | (1 << 23)) / (1 << 23);

return sign * significand * Math.pow(2, exponent);
}

function Decoder(bytes, port) {
var lat = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
var lon = bytes[7] << 24 | bytes[6] << 16 | bytes[5] << 8 | bytes[4];
return{
latitude: Bytes2Float32(lat),
longitude: Bytes2Float32(lon)
};
}

0 comments on commit f178aaa

Please sign in to comment.