Thanks for your feedback Nick. I'm making some good progress with the code and I've included it below in order to give a better idea of what I am doing. There will be 15 sensors rigged up to the Arduino, I'm using Micro's attached to a bespoke PCB. I think that logging the data to an SD card on a separate Arduino is a good approach to manage with a power or internet outage? I fear that if the internet connection drops for a day the Arduino would be well and truly clogged up, however an SD card wouldn't be.
All I need to achieve now is getting the data from that CSV up to the internet, any thoughts on this? Perhaps I could upload the file to an FTP server for cron processing and then delete the file from the Arduino after?
Arduino 1:
#include <Wire.h>
// Setup S02
int S02pin = 19;
int S02index = 0;
int S02reading[10];
int S02currentreading = 0;
unsigned long S02currenttimestamp = 0;
unsigned long S02timestamp[10];
boolean S02laststate = 0;
// Setup Delay Between Readings
unsigned long readingdelay = 1000;
unsigned long looptime = 0;
unsigned long nextsend = 0;
boolean backlog = 0;
int SendData(byte sensor, unsigned long timestamp, int reading)
{
Wire.beginTransmission(4);
Wire.write(sensor);
Wire.write(timestamp >> 24);
Wire.write(timestamp >> 16);
Wire.write(timestamp >> 8);
Wire.write(timestamp);
Wire.write(reading >> 8);
Wire.write(reading);
return Wire.endTransmission();
}
// START Bootup Execution
void setup()
{
Serial.begin(9600);
Wire.begin();
}
// START Main Execution Loop
void loop()
{
looptime = micros();
// Check if S02 is ready to save
if(S02currenttimestamp != 0 && S02currenttimestamp < (millis() - readingdelay))
{
if(SendData(2, S02currenttimestamp, S02currentreading) != 0)
{
S02reading[S02index] = S02currentreading;
S02timestamp[S02index] = S02currenttimestamp;
S02index++;
backlog = 1;
}
S02currentreading = 0;
S02currenttimestamp = 0;
}
// START Read Data S02
if(digitalRead(S02pin) != S02laststate)
{
// Prep to pickup change next time
S02laststate = digitalRead(S02pin);
S02currentreading++;
S02currenttimestamp = millis();
}
// Send Data
if( S02index > 0 && millis() > nextsend )
{
// START Wire Transmission
while(S02index != 0)
{
S02index--;
Wire.beginTransmission(4);
byte sensor = 2;
Wire.write(sensor);
Wire.write(S02timestamp[S02index] >> 24);
Wire.write(S02timestamp[S02index] >> 16);
Wire.write(S02timestamp[S02index] >> 8);
Wire.write(S02timestamp[S02index]);
Wire.write(S02reading[S02index] >> 8);
Wire.write(S02reading[S02index]);
if(Wire.endTransmission() != 0)
{
S02index++;
break;
}
else
{
S02timestamp[S02index] = 0;
S02reading[S02index] = 0;
}
}
//backlog = 0;
nextsend = millis() + 5000;
Serial.print(micros() - looptime);
Serial.println("us Loop Execution Time");
}
}
// END Loop
#include <Wire.h>
#include <SD.h>
int i = 0;
unsigned long ul = 0;
const int chipSelect = 4;
void setup()
{
Wire.begin(4);
Wire.onReceive(receiveEvent);
Serial.begin(9600);
Serial.print("Initializing SD card...");
pinMode(10, OUTPUT); // make sure that the default chip select pin is set to output, even if you don't use it:
SD.begin(chipSelect);
}
void loop()
{
delay(100);
}
void receiveEvent(int howMany)
{
while(0 < Wire.available())
{
// Read Sensor
String dataString = "";
byte sensor = Wire.read();
dataString += String(sensor);
Serial.print("S: ");
Serial.print(sensor);
// Read Timestamp
dataString += ",";
Serial.print(", T: ");
ul = 0;
ul = Wire.read() << 24;
ul |= Wire.read() << 16;
ul |= Wire.read() << 8;
ul |= Wire.read();
dataString += String(ul);
Serial.print(ul);
// Read Value
dataString += ",";
Serial.print(", R: ");
i = 0;
i = Wire.read() << 8;
i |= Wire.read();
dataString += String(i);
Serial.println(i);
File dataFile = SD.open("datalog.csv", FILE_WRITE);
if (dataFile) { // if the file is available, write to it:
dataFile.println(dataString);
dataFile.close();
Serial.print("Wrote: ");
Serial.println(dataString);
}
}
}