I'm currently working on a project using a NodeMCU, and had a working library made by me to send commands to it using the serial port. This commands consist of a name and arguments, separated by commas.
This was working perfectly until a few days ago, when it started randomly loosing data. I haven't worked or touched the library in a long time.
This is the code:
SerialMenu.cpp :
void Menu::serialEvent() {
bool stringComplete = false;
String inputString = "";
inputString.reserve(50);
String args[MAX_ARG_NUMBER];
int argc = 0;
for(int i = 0; i < MAX_ARG_NUMBER; i++){
args[i] = "";
args[i].reserve(10);
}
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
if (inChar == '\n') {
stringComplete = true;
break;
} else if(inChar == ','){
argc++;
} else if(!argc){
inputString += inChar;
} else {
args[argc-1] += inChar;
}
}
if (stringComplete){
handleEvents(inputString, args, argc);
}
}
Main.ino :
void loop() {
int packetSize = udp.parsePacket();
if(packetSize) { //Espera un paquete UDP
handlePacket();
}
if(startPairingMode){
startPairing(pairingTime);
}
menu.serialEvent(); // This is the important part
Alarm.delay(0);
encoder.check();
checkPir();
panel.update();
}
The library was supossed to read an string from serial (sent from arduino ide), like this:
extwifi,ssid,password,0.0.0.0
And parse it so
inputString = "extwifi"
args[0] = "ssid"
etc.
It randomly seems to cut the message in parts. If I send the message above multiple times, it may respond:
message = "0.0.0"
or
message = "password,0.0.0.0"
or exceptionaly it reads correctly.
It is important to note that all the data is arriving correctly at the Serial.read(), you can see that if you print it immediately.
From what I can gather "argc" may be going back to the value 0 randomly, but I haven't been able to figure out why. Sorry for the long post, and thank you!