Hi All,
I am using snprintf in an attempt to move away from using Strings as much as possible. The trouble is I am still using a string for an rfid variable. Here is my snprintf:
const char* timeStamp = Time.format(Time.now(), "%Y-%m-%d %H:%M:%S");
const char* visitStartTime = visitStartTimeString.c_str();
String rfid = data.substring(0,16); // taking substring as data has a CR at position 17
if(tareCheck) {
rfid = "idle";
}
char s[300];
snprintf(s, sizeof(s)
, "{\"Em\": \"%s\", \"L1\": \"%.2f\", \"L2\": \"%.2f\", \"L3\": \"%.2f\", \"L4\": \"%.2f\", \"L5\": \"%.2f\", \"rf\": \"%s\", \"t\": \"%s\", \"std\": \"%.2f\", \"visit\": \"%d\", \"visitStartTime\": \"%s\", \"arrayFillTime\": \"%d\" }"
,Em
, LC1
, LC2
, LC3
, LC4
, LC5
, rfid.c_str()
, timeStamp
, stdDev
, visit
, visitStartTime
, arrayFillTime);
The issue I am seeing is the rfid.c_str() is also populating the timestamp variable string. Does anyone have any pointers of where my issue lies?
I am pretty sure the rfid variable is the one that I am having issues with. My code is long and ugly but here are some snippets on how the rfid variable should be assigned a value.
char rfidData[40];
String data = "";
int Read_RFID_Tag(int scanTime){
digitalWrite(RFIDON, HIGH);
long setime = millis();
char offset = 0;
byte C = 0;
Serial1.println(F("SRA"));
while(millis() - setime < scanTime){ // for the first two seconds
if (C == 13 && offset == 17){ // if the rfid is a certain length and ends in a CR (C==13) then assume data is good.
data = rfidData;
Serial1.println(F("SRD"));
Serial.print(" ");
digitalWrite(RFIDON, LOW);
return 1;
}
rfidData[0] = 0; // Clear the buffer
offset = 0; // Offset into buffer
while (Serial1.available()) {
C = Serial1.read();
rfidData[offset] = C;
Serial.print(C);
delay(1);
offset++;
}
}
Serial1.println(F("SRD"));
digitalWrite(RFIDON, LOW);
data = "unknown";
return 0;
The rfid includes a carriage return which is why I use substring prior to my snprintf.