Hello, I am working on a project that uses BME280, a Micro SD card, Swan, and Notecarrier F. I am taking sensor readings every minute for a day and saving the data as a CSV file on the SD card(also including the date, time, latitude and longitude from the GPS). The idea is to sync the data to the Notehub every midnight daily. I want to keep track of the parameters (temp, pressure, humidity) over time. I can read the sensor data and save it to the sd card. Not sure how to go about uploading it to notehub. I have the code below but its not working as expected.
#include <Wire.h>
#include <Notecard.h>
#include <TimeLib.h>
#include <SPI.h>
#include <SD.h>
#define SD_CS 10
#define MEASUREMENTS_PER_NOTE 16
#define productUID "xxx"
Notecard notecard;
File myFile;
bool dataAvailable = true;
void setup() {
Serial.begin(9600);
Wire.begin();
notecard.setDebugOutputStream(Serial);
notecard.begin();
if (!SD.begin(SD_CS)) {
Serial.println(F("SD Card Error!"));
while(1); // Consider modifying this to avoid infinite loop
} else {
Serial.println(F("SD Card detected."));
}
J *req = notecard.newRequest("hub.set");
JAddStringToObject(req, "product", productUID);
JAddStringToObject(req, "mode", "periodic");
JAddNumberToObject(req, "outbound", 10);//every 30 mins
notecard.sendRequest(req);
myFile = SD.open("DATA/DATA000.csv");
if (!myFile) {
Serial.println("Error opening DATA/DATA000.csv");
return; // Exit if file cannot be opened
}
registerTemplate();
}
void loop() {
if (dataAvailable && myFile.available()) {
sendMeasurementsNote();
} else {
// No more data to read; stop further processing
dataAvailable = false;
Serial.println("All data has been sent.");
}
delay(2000);
}
void registerTemplate() {
J *req = notecard.newRequest("note.template");
JAddStringToObject(req, "file", "sensors.qo");
J *body = JCreateObject();
J *dataArray = JAddArrayToObject(body, "measurements");
JAddItemToArray(dataArray, JCreateString("string")); // Ensure correct type definitions are used
JAddItemToObject(req, "body", body);
if (!notecard.sendRequest(req)) {
Serial.println("Failed to send note.template request.");
}
}
void sendMeasurementsNote() {
J *req = notecard.newRequest("note.add");
JAddStringToObject(req, "file", "sensors.qo");
J *body = JCreateObject();
J *dataArray = JAddArrayToObject(body, "measurements");
String measurement = myFile.readStringUntil('\n');
JAddItemToArray(dataArray, JCreateString(measurement.c_str()));
JAddItemToObject(req, "body", body);
if (!notecard.sendRequest(req)) {
Serial.println("Failed to send note.add request.");
} else {
Serial.println("Measurements sent successfully.");
}
}