Hello folks,
I’ve been working on an Arduino Mega project with Ethernet Shield, an RTC, a little buzzer, a graphic LCD and a configurable number of sensors that measures temperature, pressure, humidity etc.
I publish the data on Cayenne’s Dashboard and also store the measurements on SD as backup.
During the refinement of the code, I started having errors on writing the data to SD. This was driving me nuts as I could not figure out where the problem was.
SD writing worked fine on setup{} but on loop{} it didn’t.
Making the story short, I finally found that if I comment //Cayenne.run() within loop{} the logging on SD works fine. If I enable the line, SD writing gives me writing error then stops or sometimes during debugging, it doesn’t stops or give errors, but logging does not happen. Again, when disabling Cayenne.run line, logging works.
The code is getting big but at compile, I still have 3879 bytes for local storage.
I thought that I might not have enough memory but using freemem I had at least 2K on critical points of the code so I believe I have ram.
My loop looks like this:
void loop() {
tm = now();
if ((tm - tmPreviousTimeText) > 1) {
updateTimeText(); // gets current time and stores it in decreasing CSV text from year to second like “2017/04/10,22:14:46”
tmPreviousTimeText = tm;
}
if ((tm - tmPreviousLCD) > 1 ) {
prindSnsrsLCD(); // prints sensors result to LCD.
tmPreviousLCD = tm;
}
if ((tm - tmPreviousTakeReading) > smplFreq) {
takeReading(); // gets data from sensors
writeResultsToSD(); // writes data do SD usinf sdFAT.h library
tmPreviousTakeReading = tm;
}
if ((tm - tmPreviousOscilateLED) > 1) {
iAmAlive = !iAmAlive;
digitalWrite(ledPin, iAmAlive) ; just a led blink to indicate the system is working.
tmPreviousOscilateLED = tm;
}
Cayenne.run();
}
I disabled each of the possible routines above simplifying and reducing the code except of course writeResultsToSD() and Cayenne.run() to see if they work together but they don’t like each other
with Cayenne enabled, data is properly sent to Dashboard but SD doesn’t work
with Cayenne disabled, SD writing works.
All measurements are sent to Cayenne using Virtual ports.
A sample code for a Virtual port is:
CAYENNE_OUT(V1) {
byte i = 0;
optional call method
if (sensorUnit[i] == F(" C “)) {
Cayenne.virtualWrite(V1, sensorMeasureVal[i], TEMPERATURE, CELSIUS);
} else if (sensorUnit[i] == F(” % “)) {
Cayenne.virtualWrite(V1, sensorMeasureVal[i], HUMIDITY, PERCENT);
} else if (sensorUnit[i] == F(” hPa")) {
Cayenne.hectoPascalWrite(V1, sensorMeasureVal[i]);
}
}
I’d love to hear suggestions, hints, tips of where a bug could be?
If I get this solved, I will certainly share.
Thanks
Carlos.