// Example 2 - Receive with an end-marker
boolean getConfig = true;
char SSID[30] = "xxx";
char pass[30] = "unset";
void setup() {
Serial.begin(19200);
Serial.println("<Arduino are ready>");
}
void loop() {
//recvWithEndMarker();
// showNewData();
if (getConfig) {
Serial.println("Staring...");
displayConfigMenu();
getConfigData();
}
}
void displayConfigMenu() {
Serial.println("");
Serial.println("==========Config Menu=========");
Serial.print("a) SSID:");
Serial.println(SSID);
Serial.print("b) Password:"); Serial.println(pass);
Serial.println("w) Write config");
Serial.println("x) quit Config Menu");
Serial.println("y) redisplay menu");
Serial.println("z) Reboot");
}
void getConfigData() {
const byte numChars = 32;
char receivedChars[numChars]; // an array to store the received data
byte receivedBytes[32];
boolean newData = false;
uint16_t menuTimeout = millis() + 120000;
char menuChar ;
static byte ndx = 0;
// char endMarker = '\n';
//char endMarker = "\n";
char endMarker = 0x03;
char rc;
while ( getConfig ) {
// call 1 second script
if (millis() > menuTimeout) {
getConfig = false;
Serial.println();
Serial.println(". Config menu timeout");
Serial.println(". Standard operations resuming ...");
return;
}
strcpy(menuChar, "");
strcpy(receivedChars, "");
newData = false;
Serial.setTimeout(10000);
while (Serial.available() == 0 && millis() < menuTimeout ) {
;
}Serial.println("something in serial buffer");
menuChar = Serial.read();
Serial.print("Enter data");
while ( newData == false && millis() < menuTimeout) {
while (Serial.available() == 0 && millis() < menuTimeout ) {
;
}
Serial.readBytesUntil(endMarker,receivedBytes,numChars);
newData = true;
}
//receivedChars= receivedBytes;
sprintf(receivedChars,"%s",receivedBytes);
Serial.println("");
Serial.print("Menu:--");
Serial.println(menuChar);
Serial.print("Text:--");
Serial.println(receivedChars);
}
}
/* was from inside the getConfigData
while (Serial.available() == 0 && millis() < menuTimeout){
;
}
if (Serial.available() > 0){
rc = Serial.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}*/
/*
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = '\n';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
void showNewData() {
if (newData == true) {
Serial.print("This just in ... ");
Serial.println(receivedChars);
newData = false;
}
}
*/