Hello, again.
So tried to convert a float value from the reading of the GPS shield to a char value, so that way I can store it in the MKR MEM shield's flash memory, but for some reason when I compile the code:
/* file.lseek(0, START); /* Rewind file pointer to the start */
// char buf[64] = {0};
// int const bytes_read = file.read(buf, sizeof(buf));
// buf[bytes_read] = '\0';
// Serial.println(buf);
#include <SPI.h>
#include <Arduino_MKRGPS.h>
#include <Arduino_MKRMEM.h>
static char const PANGRAM[] = "The quick brown fox jumps over the lazy dogukzewguikzweruiewgiruowerwegiurweuirwezui";
const char* fang = "ejewoidLIKZFUITRDTZ8DUTZRLIZG23986T9238TZUTFGTTTUZE677E66RI988UH7d.";
void setup() {
Serial.begin(9600);
while (!Serial) {
;
}
GPS.begin();
unsigned long const start = millis();
for(unsigned long now = millis(); !Serial && ((now - start) < 5000); now = millis()) { };
flash.begin();
/* ... */
int res = filesystem.mount();
if(res != SPIFFS_OK && res != SPIFFS_ERR_NOT_A_FS) {
Serial.println("mount() failed with error code "); Serial.println(res); return;
}
/* ... */
File file = filesystem.open("data.txt", CREATE | APPEND);
/* ... */
if (GPS.available()) {
// Read GPS data
float latitude = GPS.latitude();
float longitude = GPS.longitude();
float altitude = GPS.altitude();
float speed = GPS.speed();
int satellites = GPS.satellites();
char buffer[16];
dtostrf(latitude, 8, 7, buffer);
file.write((void *)buffer, strlen(PANGRAM));
file.write((void *)fang, strlen(PANGRAM));
/* ... */}
file.lseek(0, START); /* Rewind file pointer to the start */
char buf[2048] = {0};
int const bytes_read = file.read(buf, sizeof(buf));
buf[bytes_read] = '\0';
Serial.println(buf);
/* ... */
file.close();
unsigned int bytes_total = 0,
bytes_used = 0;
if(SPIFFS_OK != filesystem.info(bytes_total, bytes_used)) {
Serial.println("check() failed with error code "); Serial.println(filesystem.err()); return;
} else {
char msg[64] = {0};
snprintf(msg, sizeof(msg), "SPIFFS Info:\nBytes Total: %d\nBytes Used: %d", bytes_total, bytes_used);
Serial.println(msg);
}
filesystem.unmount();
}
void loop() {
}
returns with an error:
C:\Users\user\OneDrive\Documents\Arduino\sketch_sep14a\sketch_sep14a.ino: In function 'void setup()':
C:\Users\user\OneDrive\Documents\Arduino\sketch_sep14a\sketch_sep14a.ino:45:3: error: 'dtostrf' was not declared in this scope
dtostrf(latitude, 8, 7, buffer);
^~~~~~~
C:\Users\user\OneDrive\Documents\Arduino\sketch_sep14a\sketch_sep14a.ino:45:3: note: suggested alternative: 'strstr'
dtostrf(latitude, 8, 7, buffer);
^~~~~~~
strstr
exit status 1
Compilation error: 'dtostrf' was not declared in this scope
I've looked over the internet and websites say that the dtostrf function is supposed to be implemented in the arduino....
Can you send a link to where you got this code, I want to know what each command and variable does myself. In case this is your code I would be very grateful if you could explain the 9th line sprintf(buf,"x = %8.4f, cos(x) = %8.4f",x, cos(x));. Thanks
Sure, but the idea is to store it there temporarily and then send it to the cloud. I would like to use as little space as possible for the program itself. But if you think there is no other way I can also convert it to binary and then back...
That didn't work, unfortunately, but I got the dtostrf working, just one quick question if anyone knows the answer to, does file.read (this command is under MKRMEM library and has no connection to the main topic) only read one singular line of text or is there a way to read multi-line text
/**************************************************************************************
* PUBLIC MEMBER FUNCTIONS
**************************************************************************************/
int Arduino_SPIFFS_File::read(void * buf, int len)
{
return filesystem.read(_fh, buf, len);
}
int Arduino_SPIFFS_File::write(void * buf, int len)
{
return filesystem.write(_fh, buf, len);
}
read and write require a pointer to data and the number of bytes (to read or write) independent of the type of data. These functions do not take care of delimiters like '\0' as end of string.
You wrote that the suggested functions did not work. Would you mind to post how you used them and what the result was?
#include <SPI.h>
#include <Arduino_MKRGPS.h>
#include <Arduino_MKRMEM.h>
void setup() {
Serial.begin(9600);
while (!Serial) {
;
}
GPS.begin();
unsigned long const start = millis();
for(unsigned long now = millis(); !Serial && ((now - start) < 5000); now = millis()) { };
flash.begin();
/* ... */
int res = filesystem.mount();
if(res != SPIFFS_OK && res != SPIFFS_ERR_NOT_A_FS) {
Serial.println("mount() failed with error code "); Serial.println(res); return;
}
/* ... */
Serial.println("Staring GPS...");
/* ... */
}
void loop() {
if (GPS.available()) {
// Read GPS data
float latitude = GPS.latitude();
float longitude = GPS.longitude();
float altitude = GPS.altitude();
float speed = GPS.speed();
int satellites = GPS.satellites();
File file = filesystem.open("data.txt", CREATE | TRUNCATE | READ_WRITE | APPEND);
file.write((void *)longitude, 4);
}
}
I get this error:
C:\Users\user\AppData\Local\Temp\.arduinoIDE-unsaved2023816-9076-nwipjx.uja8g\sketch_sep16c\sketch_sep16c.ino:36:20: error: invalid cast from type 'float' to type 'void*'
file.write((void *)longitude, 4);
^~~~~~~~~
exit status 1
Compilation error: invalid cast from type 'float' to type 'void*'
I would assume this means that we must convert it with dtostrf.
But about the first part, where you mentioned the definitions, I would also assume that _fh means file handle?
I'm still quite new to this stuff so I may be saying some dumb things....