ESP32 program videos.
Jul 29, 2020 8:14:49 GMT
Post by c3po on Jul 29, 2020 8:14:49 GMT
I think you want to be using JSON & deserialise as well as the message handling code below
If you want to send different data fields in the same message then use JSON & Deserialise...
But to do the serial bit this works a treat....
Use the format Serial.print("<abcdef123456hello>"); for the transmit message
So the < & > are the start and stop header/footer
// Example 3 - Receive with start- and end-markers
const byte numChars = 64;
char receivedChars[numChars];
boolean newData = false;
void setup() {
Serial.begin(9600);
Serial.println("<Arduino is ready>");
}
void loop() {
recvWithStartEndMarkers();
showNewData();
}
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '';
char endMarker = '';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
void showNewData() {
if (newData == true) {
Serial.print("This just in ... ");
Serial.println(receivedChars);
newData = false;
}
}