I don't know where "0;1;0;0;" will show. Sometimes it's in the beginning and sometimes at the middle or end. I can't use string.indexOf(), string.substring() and string.startsWith()
In "3;0;1;0;0;28.8" I need to get the "3" which is the id of the sensor and "28.8" which is the temperature in Celsius.
What is the expected output (like what is the format), are you looking for the first and last numbers or are you specifically looking for something containing "0;1;0;0;"?
You don't know what strtok() does, do you? It looks for ANY delimiter in the string supplied, NOT the whole string as a delimiter.
Once you've fucked around wrapping the string in a String, it makes no sense switching back and forth, trying to use string and String methods to parse the same data. Use ONE mechanism.
You might be able to use the below simple code to detect if there is some type of end of data marker included in the byte String captured. Received data that is on more than one line indicates that a carriage return and line feed is being sent in the byte stream.
//zoomkat 6-29-14 Simple serial echo test
//type or paste text in serial monitor and send
String readString;
void setup() {
Serial.begin(9600);
Serial.println("Simple serial echo test"); // so I can keep track of what is loaded
}
void loop() {
while (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the String readString
delay(2); //slow looping to allow buffer to fill with next character
}
if (readString.length() >0) {
Serial.println(readString); //so you can see the captured String
readString="";
}
}
arduinoTime:
because I know 0;1;0;0; will appear in the middle of my desired values, I search for 0 and 1--not zero only because it often appears.
0 -> a-2
; -> a-1
1 -> a
I expect it to search for "0;1;0;0;"
That is not the issue. I dont know what happens after, however, when that function is called the first time, a is 0 and "a - 2" is -2, you are trying to access nrfData[-2], which is an error...
I just want to remind you that although some of us may have memorized most of the C standard library, the Arduino is aimed at beginners, and it would be courteous to just post a link or hint about the appropriate way of achieving the end result, without resorting to name-calling or expletives.
Ps991:
That is not the issue. I dont know what happens after, however, when that function is called the first time, a is 0 and "a - 2" is -2, you are trying to access nrfData[-2], which is an error...
You also have an "a - 4" in there
Oh, ok. I should do it like this?
char nrfData[200];
int a = 0;
char pureData[200];
int e = 0;
void setup() {
Serial.begin(9600);
Serial2.begin(115200);
Serial.println("NRF24L01 RX/TX");
}
void loop() {
receive_nrf();
}
void receive_nrf() {
while (Serial2.available()) {
delay(3); //delay to allow buffer to fill
if (Serial2.available() > 0) {
char inChar = Serial2.read();
//Serial.print(inChar);
nrfData[a] = inChar;
if (a > 12) {
if (nrfData[a-11] == '0' && nrfData[a-9] == '1' && nrfData[a-7] == '0' && nrfData[a-5] == '0') {
for(int i = 13; i >= 0; i--) {
pureData[e] = nrfData[a-i];
e++;
}
}
}
a++;
}
}
String incomingString = String(nrfData);
if (incomingString.length() > 0) {
Serial.println("-----");
Serial.print("nrfData: ");
Serial.println(incomingString);
Serial.print("pureData: ");
Serial.println(pureData);
char* id = strtok(pureData, ";");
char* temp = strtok(NULL, "0;1;0;0;");
int sensor_id = atoi(id);
float sensor_temp = atof(temp);
if (id > 0 && temp > 0) {
Serial.print("ID: ");
Serial.println(sensor_id);
Serial.print("Temp: ");
Serial.println(sensor_temp);
}
clearSerial(nrfData);
a = 0;
clearSerial(pureData);
e = 0;
}
}
void clearSerial(char *data) {
for( int i = 0; i < sizeof(data); ++i )
data[i] = (char)0;
}
If ever the "3;0;1;0;0;33.0" is in the beginning, I will not get any error or input in pureData, yes?
If you are looking for an exact string, then you should compare for an exact string. For example, "0a1b0c0defgh" also satisfies that expression but it obviously is not what you are looking for. It will probably work most of the time, I just think you can improve your code to prevent all possible errors.
If you are looking for an exact string, then you should compare for an exact string. For example, "0a1b0c0defgh" also satisfies that expression but it obviously is not what you are looking for. It will probably work most of the time, I just think you can improve your code to prevent all possible errors.