Hello there, first post so hopefully I don't bungle any rules here.
I'm working on a telemetry logging project and the specific problem I'm running into is a confusing interaction seemingly between the serialTransfer library and the combination of the SPI and SD libraries.
The problem is that gps data that I have gathered on one Arduino Uno, and then transferred to the second Arduino Uno, doesn't get written to an SD card attached to the second Arduino.
I've put together as minimally reproducible of an example as I could make and attached the code in 3 scripts.
1 would be running on the first Arduino, while 2 or 3 would be running on the second Arduino.
The wiring setup for all 3 scripts is commented in the 3rd script
- GPSsend.ino collects the gps data and sends the data off utilizing the serialTransfer library.
#include <TinyGPSPlus.h>
#include <SoftwareSerial.h>
#include <SerialTransfer.h>
static const int RXPin = 4, TXPin = 3;
static const uint32_t GPSBaud = 9600;
float lon=0;
int h = 0,m=0,cs=0;
// The TinyGPSPlus object
TinyGPSPlus gps;
// The serial connection to the GPS device
SoftwareSerial ss(RXPin, TXPin);
SerialTransfer myTransfer;
struct STRUCT {
int s;
float lat;
} testStruct;
void setup()
{
Serial.begin(9600);
ss.begin(GPSBaud);
myTransfer.begin(Serial);
testStruct.s = 6;
testStruct.lat = 4.5;
Serial.println("Starting GPS test");
delay(1000);
}
void loop()
{
// This sketch displays information every time a new sentence is correctly encoded.
while(ss.available() > 0){
if (gps.encode(ss.read())){
if (gps.location.isValid()){
testStruct.lat = gps.location.lat();
}
if (gps.time.isValid()){
testStruct.s = gps.time.second();
}
}
}
myTransfer.sendDatum(testStruct);
delay(10);
}
- GPSrecieve.ino accepts the data from the arduino running GPSsend.ino and then displays the results.
#include "SerialTransfer.h"
SerialTransfer myTransfer;
struct STRUCT {
int s;
float lat;
} testStruct;
void setup()
{
Serial.begin(9600);
myTransfer.begin(Serial);
}
void loop()
{
if(myTransfer.available())
{
myTransfer.rxObj(testStruct);
Serial.print(testStruct.s);
Serial.print(" | ");
Serial.println(testStruct.lat);
}
}
Code 1 and 2 work together as intended, after a brief gps warmup the serial monitor displays the lat and s values.
- GPSrecieveAndLog.ino is the code that is acting up. It is the same as code 2 but has the added SD logging functionality. I have tested this functionality and it logs data that is generated on a single arduino just fine, however now that it has data coming in through serialTransfer, it does not ever seem to make it past the void setup chunk as I never see anything written to the serial monitor, and no Telem.txt file is ever created on the sd card.
#include <SPI.h>
#include <SD.h>
#include "SerialTransfer.h"
// Wiring Setup-----------------
// 5v everything running through dom arduino 5v
// grounds Everything back to dom arduino grounds
//0(RX)dom to 1(tx)sub
//1(tx)dom to 0(rx)sub
//sub 4 to tx gps
//sub 3 to rx gps
// dom 6 to cs sd
// dom 11 to mosi sd
// dom 12 to miso sd
// dom 13 to sck sd
SerialTransfer myTransfer;
struct STRUCT {
int s;
float lat;
} testStruct;
File myFile;
void setup()
{
Serial.begin(9600);
//sd setup
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
if (!SD.begin(6)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
myFile = SD.open("TELEM.txt", FILE_WRITE);
myFile.println("Lat, Sec");
//close the file:
myFile.close();
myTransfer.begin(Serial);
}
void loop()
{
myFile = SD.open("TELEM.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to TELEM.txt...");
if(myTransfer.available()){
myTransfer.rxObj(testStruct);
myFile.print(testStruct.s);
myFile.print(", ");
myFile.println(testStruct.lat);
} else {
myFile.print("NA");
myFile.print(", ");
myFile.println("NA");
}
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
delay(10); // delay in between reads for stability
}
If anyone has any ideas of how to fix this and use the existing methodology, great! Otherwise, I'm sure that there's a better method of doing this, but as it's my first attempt at sending info from one Arduino to another I'm rather overwhelmed with options that I don't fully understand. I've looked at the serialTransfer library's spi_tx_datum/spi_rx_datum examples but I'm unclear on how I would wire the connection between Arduino 1 and Arduino 2 and don't see documentation on how to do so.
I know I haven't given specific info on the gps or sd module but I don't believe the issue is dependent on those specs. I'm happy to provide them if someone believes otherwise though.
Thank you for any help!