top of page

Char Array Processing with Arduino (Finding IP Address)

Below is the code used in our tutorial :

const unsigned int MAX_MESSAGE_LENGTH = 16;  //192.168.255.255 + "\0"
bool stringMatch = false;

void setup() {
  Serial.begin(9600);
  while (stringMatch == false) {
    stringReturn();
  }
  //do something else
  Serial.println("Yaaay... Ip Address is obtained");
}

void loop() {

}

void stringReturn() {
  while (Serial.available() > 0) {
    static char message[MAX_MESSAGE_LENGTH];
    static unsigned int message_pos = 0;
    char inByte = Serial.read(); //buffer

    if (inByte != '\n' && (message_pos < MAX_MESSAGE_LENGTH - 1)) {
      message[message_pos] = inByte;
      message_pos++;
    } else {
      message[message_pos] = '\0';
      Serial.print("The message keyed in is : ");
      Serial.println(message);

      static bool itIsNumber = false;
      if (isDigit(*message)) {  // tests if myChar is a digit
        Serial.println("The input is a number");
        itIsNumber = true;
      } else {
        Serial.println("The input is not a number");
        itIsNumber = false;
      }

      static int count = 0;  //IP Address would have 3 dots      
      for (int i = 0; i < strlen(message); i++) {
        if (message[i] == '.') {
          count++;
        }
      }

      static unsigned int minIpLength=11; //192.168.X.X
      if (count == 3 && strlen(message) >= minIpLength && itIsNumber == true) {
        Serial.println("IP Address obtained");
        stringMatch = true;
      } else {
        Serial.println("IP Addresss Wrong!! Try Again...");
        Serial.print("No of dots in the input is : ");
        Serial.println(count);
      }

      message_pos = 0;
      count = 0;
    }
  }
}

Let's discuss this code in more detail.


  1. MAX_MESSAGE_LENGTH: This constant defines the maximum length of the message that can be entered, including the null terminator.

  2. stringMatch: A boolean variable indicating whether a valid IP address has been obtained.

  3. Obtaining IP Address:

  • while (stringMatch == false) { stringReturn(); }: This loop continuously calls the stringReturn() function until stringMatch becomes true. The purpose is to wait until a valid IP address is obtained before proceeding with the rest of the setup.


The main part of the code lies within stringReturn() function :


  1. Reading Characters:

  • The function continuously checks for available characters in the serial buffer using Serial.available().

  • If characters are available, it reads one character at a time using Serial.read().

  1. Building the Message:

    static char message[MAX_MESSAGE_LENGTH];
    static unsigned int message_pos = 0;
    char inByte = Serial.read(); //buffer

if (inByte != '\n' && (message_pos < MAX_MESSAGE_LENGTH - 1)) {
      message[message_pos] = inByte;
      message_pos++;
    } else {
      message[message_pos] = '\0';
      Serial.print("The message keyed in is : ");
      Serial.println(message);
  • The function uses a static character array message to store the entered characters.

  • It keeps track of the current position in the message using message_pos.

  • Characters are added to the message until a newline character is encountered or the maximum length is reached.

Processing the Message:

When a newline or the maximum length is reached, the message is null-terminated.

The function prints the entered message using Serial.println().


Checking if it's a Number:

It checks if the first character of the message is a digit using isDigit().

Prints whether the input is a number or not.


Counting Dots in the Message:

It counts the number of dots in the entered message to determine if it resembles an IP address.


Validating the IP Address:

if (count == 3 && strlen(message) >= minIpLength && itIsNumber == true) {

Checks if the count of dots is 3, the length of the message is greater than or equal to the minimum required, and the input is a number.

If the conditions are met, it indicates that a valid IP address has been obtained, and it sets stringMatch to true.


Otherwise, it prompts the user to try again and provides information about the number of dots in the input.


Resetting Variables:

message_pos = 0;
count = 0;

Resets the message position (message_pos) and dot count (count) for the next input.


This function essentially handles the input of characters from the serial buffer, validates the entered message as a potential IP address, and sets a flag (stringMatch) when a valid IP address is obtained. It also provides feedback to the user about the entered data. Adjustments can be made to the validation criteria based on specific requirements.


For more detailed instruction of the video, feel free to check out my YouTube video :




bottom of page