Updated: I was able to make the SDfat sample library work now. What should I do for the next step?
#include <HttpClient.h>
#include <SdFat.h>
#include "application.h"
// 5 X 4 array
#define ROW_DIM 5
#define COL_DIM 4
int SD_INITIALISED = 0;
int counter1 = 0;
byte cnt = 0;
SdFat sd;
SdFile file;
File myFile;
int U = 91720;
int S = 0;
int F = 75;
int V = 0;
HttpClient http;
http_header_t headers[] = {
// { "Content-Type", "application/json" },
// { "Accept" , "application/json" },
{ "Accept" , "*/*"},
{ "User-agent", "Particle HttpClient"},
{ NULL, NULL } // NOTE: Always terminate headers will NULL
};
http_request_t request;
http_response_t response;
#define SPI_CONFIGURATION 0
#define SPI_SPEED SD_SCK_MHZ(4)
// Setup SPI configuration.
#if SPI_CONFIGURATION == 0
// Primary SPI with DMA
// SCK => A3, MISO => A4, MOSI => A5, SS => A2 (default)
const uint8_t chipSelect = SS;
#elif SPI_CONFIGURATION == 1
// Secondary SPI with DMA
// SCK => D4, MISO => D3, MOSI => D2, SS => D1
SdFat sd(1);
const uint8_t chipSelect = D1;
#elif SPI_CONFIGURATION == 2
// Primary SPI with Arduino SPI library style byte I/O.
// SCK => A3, MISO => A4, MOSI => A5, SS => A2 (default)
SdFatLibSpi sd;
const uint8_t chipSelect = SS;
#elif SPI_CONFIGURATION == 3
// Software SPI. Use any digital pins.
// MISO => D5, MOSI => D6, SCK => D7, SS => D0
SdFatSoftSpi<D5, D6, D7> sd;
const uint8_t chipSelect = D0;
#endif // SPI_CONFIGURATION
size_t readField(File* file, char* str, size_t size, const char* delim) {
char ch;
size_t n = 0;
while ((n + 1) < size && file->read(&ch, 1) == 1) {
// Delete CR.
if (ch == '\r') {
continue;
}
str[n++] = ch;
if (strchr(delim, ch)) {
break;
}
}
str[n] = '\0';
return n;
}
// //------------------------------------------------------------------------------
// #define errorHalt(msg) {Serial.println(F(msg)); SysCall::halt();}
// //------------------------------------------------------------------------------
void setup() {
Serial.begin(9600);
// Time.zone(-4);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
attachInterrupt(echoPin, echo_interrupt, CHANGE);
while (!Serial) {
SysCall::yield();
}
if (!sd.begin(chipSelect, SD_SCK_MHZ(4))) {
sd.initErrorHalt();
}
if (!myFile.open("test.txt", O_RDWR | O_CREAT | O_AT_END)) {
sd.errorHalt("opening test.txt for write failed");
}
myFile.close();
if (!myFile.open("test.txt", O_READ)) {
sd.errorHalt("opening test.txt for read failed");
}
Serial.println("test.txt content:");
// Rewind file so test data is not appended.
myFile.rewind();
// Write test data.
myFile.print(F(
"11,12,13,14\r\n"
"21,22,23,24\r\n"
"31,32,33,34\r\n"
"41,42,43,44\r\n"
"51,52,53,54" // no delimiter
));
// Rewind the file for read.
myFile.rewind();
// Array for data.
int array[ROW_DIM][COL_DIM];
int i = 0; // First array index.
int j = 0; // Second array index
size_t n; // Length of returned field with delimiter.
char str[20]; // Must hold longest field with delimiter and zero byte.
char *ptr; // Test for valid field.
// Read the file and print fields.
for (i = 0; i < ROW_DIM; i++) {
for (j = 0; j < COL_DIM; j++) {
n = readField(&myFile, str, sizeof(str), ",\n");
if (n == 0) {
// errorHalt("Too few lines");
}
array[i][j] = strtol(str, &ptr, 10);
if (ptr == str) {
//errorHalt("bad number");
}
while (*ptr == ' ') {
ptr++;
}
if (*ptr != ',' && *ptr != '\n' && *ptr != '\0') {
// errorHalt("extra characters in field");
}
if (j < (COL_DIM-1) && str[n-1] != ',') {
// errorHalt("line with too few fields");
}
}
// Allow missing endl at eof.
if (str[n-1] != '\n' && myFile.available()) {
// errorHalt("missing endl");
}
}
// Print the array.
for (i = 0; i < ROW_DIM; i++) {
for (j = 0; j < COL_DIM; j++) {
if (j) {
Serial.print(' ');
}
Serial.print(array[i][j]);
}
Serial.println();
}
Serial.println("Done");
myFile.close();
Serial.println(myFile);
}
int dlist[5] = {0,0,0,0,0};
void loop() {
myFile.open("test.txt", O_RDWR | O_CREAT | O_AT_END);
myFile.print(U);
myFile.print(",");
myFile.print(S);
myFile.print(",");
myFile.print(F);
myFile.print(",");
myFile.print(V);
myFile.println("");
myFile.close();
if (cnt <= 3){
dlist[cnt]=S;
}
else{
Serial.println("sending a data");
SendData();
cnt = 0;
}
cnt = cnt +1;
delay(10000);
}
//Send data
void SendData(){
Serial.println();
Serial.println("Application>\tStart of Loop.");
request.hostname = "abc.def.com"; //the address is confidential
request.port = 80;
char data[256];
snprintf(data, sizeof(data), "/Cabs/asdf?U=%d&S=%d&F=%d&V=%d", U, S, F, V); //the address is confidential
request.path = data;
http.get(request, response, headers);
Serial.print("Application>\tResponse status: ");
Serial.println(response.status);
Serial.print("Application>\tHTTP Response Body: ");
Serial.println(response.body);
delay(1000);
Serial.print("sent all data, all done!");
}
}