esp32cesp32serialcommtest02 — Esp32 Devkit C V4 simulation

An interactive Esp32 Devkit C V4 circuit simulation you can run free in your browser on Velxio, by bmw550i.

Sketch code

//Many thanks to Nick Gammon for the basis of this code
//http://www.gammon.com.au/serial

const unsigned int MAX_MESSAGE_LENGTH = 12;

void setup() {
 Serial.begin(9600);
 delay(500);
 Serial.println("Serial.begin gestartet...");

}


void loop() {

 //Check to see if anything is available in the serial receive buffer
 while (Serial.available() > 0)
 {
   Serial.println("while");
   //Create a place to hold the incoming message
   static char message[MAX_MESSAGE_LENGTH];
   static unsigned int message_pos = 0;

   //Read the next available byte in the serial receive buffer
   char inByte = Serial.read();

   //Message coming in (check not terminating character) and guard for over message size
   if ( inByte != '\n' && (message_pos < MAX_MESSAGE_LENGTH - 1) )
   {
     //Add the incoming byte to our message
     Serial.println("if ( inByte != '\n' && (message_pos < MAX_MESSAGE_LENGTH - 1) )");
     message[message_pos] = inByte;
     message_pos++;
     Serial.println("message: ");
     Serial.println(message);
   }
   //Full message received...
   else
   {
     Serial.println("---else---");
     //Add null character to string
     message[message_pos] = '\0';

     //Print the message (or do other things)
     Serial.println(message);

     //Reset for the next message
     message_pos = 0;
   }
   //Serial.Print(message);
 }

 //Serial.Print(message);
}

More projects by bmw550i