I am testing the GPS shield on a MKR1000. Apparently, Serial.write(reinterpret_cast<char*>(dataArray, 20)); is the problem for some reason. This is the code that doesn't run:
#include <Arduino_MKRGPS.h>
#include <string.h>
#include <"gps_101.h path here">
//#include <avr/dtostrf.h>
float latitude = GPS.latitude();
float longitude = GPS.longitude();
float altitude = GPS.altitude();
float speed = GPS.speed();
//int satellites = GPS.satellites();
char* dataArray[1024];
//byte fBuffer[4];
//char nBuffer[8] = "";
void setup() {
// initialize serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect.
}
if (!GPS.begin(GPS_MODE_SHIELD)) {
Serial.write("Failed to initialize GPS!");
while (1);
}
else {Serial.write("GPS initialized");
}
}
void loop() {
// delay(200);
// check if there is new GPS data available
if (GPS.available()) {
// read GPS values
// float
latitude = GPS.latitude();
// float
longitude = GPS.longitude();
// float
altitude = GPS.altitude();
// float
speed = GPS.speed();
// int
//satellites = GPS.satellites();
// print GPS values
to_Bytes(&latitude,dataArray,0);
to_Bytes(&longitude,dataArray,4);
to_Bytes(&altitude,dataArray,8);
to_Bytes(&speed,dataArray,12);
// byte fBuffer[32] = {
// ((uint8_t*)&latitude)[0], ((uint8_t*)&latitude)[1],
// ((uint8_t*)&latitude)[2], ((uint8_t*)&latitude)[3],
// ((uint8_t*)&longitude)[0], ((uint8_t*)&longitude)[1],
// ((uint8_t*)&longitude)[2], ((uint8_t*)&longitude)[3],
// ((uint8_t*)&altitude)[0], ((uint8_t*)&altitude)[1],
// ((uint8_t*)&altitude)[2], ((uint8_t*)&altitude)[3],
// ((uint8_t*)&speed)[0], ((uint8_t*)&speed)[1],
// ((uint8_t*)&speed)[2], ((uint8_t*)&speed)[3]
// };
// Serial.write(dtostrf(GPS.latitude(),3,3,nBuffer)); //Latitude
// Serial.write(dtostrf(GPS.longitude(),3,3,nBuffer)); //Longitude
// Serial.write(dtostrf(GPS.altitude(),5,3,nBuffer)); //Altitude
// Serial.write(dtostrf(GPS.speed(),3,3,nBuffer)); //Ground speed
Serial.write(reinterpret_cast<char*>(dataArray, 20));
}
//debug
// else {
// Serial.write("Pending data\n");
// }
}
If I un-comment this part and other parts needed to run this snippet:
// Serial.write(dtostrf(GPS.latitude(),3,3,nBuffer)); //Latitude
// Serial.write(dtostrf(GPS.longitude(),3,3,nBuffer)); //Longitude
// Serial.write(dtostrf(GPS.altitude(),5,3,nBuffer)); //Altitude
// Serial.write(dtostrf(GPS.speed(),3,3,nBuffer)); //Ground speed
and not using the to_Bytes function, then the code runs normally and GPS data is returned.
The gps_101.h file:
#ifndef gps_101_h
#define gps_101_h
template <typename T>
void to_Bytes(T* object, char* data, int startIdx);
#endif
The file with function:
#include <"gps_101.h path here">
template <typename T>
void to_Bytes(T* object, char* data[1024], int startIdx) {
memcpy(&data[startIdx], object, sizeof(T));
}
The purpose of this function is to replace a data of any type to an array of bytes to be written to serial.
Video for demonstration of the problem (it will expire eventually so please understand): https://files.catbox.moe/kqogyc.mp4
Any hint on why the top code doesn't return anything is appreciated.