Extracting char string from TCP / UDP packet

I have an Arduino with a device server shield. The shield converts serial data from Arduino and puts out the sensor data as a char string in either TCP or UDP packets through WiFi.

I want to read the TCP/ UDP packets and strip out the data string. ( I am planning to use an Arduino with WiFi shield )

Any examples or method how to do this ??

The functions that you are interested in - string manipulation - are documented here. You won't get very many replies beyond that as your question really isn't specific enough to give a concrete answer to.

To answer this with some code, I'd need to know what these packets contain (as in, what characters/bytes) and what you want stripped out. There's also the question of if it's UDP and each packet should be a complete thing, or TCP and the data comes in as a stream.

With UDP, it is how you put it in the packet. With TCP, you must parse the header out.

Mogaraghu:
<…>
I want to read the TCP/ UDP packets and strip out the data string. ( I am planning to use an Arduino with WiFi shield )

Any examples or method how to do this ??

I have a project here that does a UDP of the GPS string $RMC and the receiver decode that puts that string into Time/Date.

Parsers are an art unto themselves and there are many ways to extract the info.

A fun test:

//   strtok test                                                               */
// Binary sketch size: 2,574 bytes (of a 30,720 byte maximum)

#include <string.h>

#define MAX_STRING_LEN  20

char *record1 = "one two three 123 456 789";
char *record2 = "Hello there friend";
char *p, *i;

void setup() {

   Serial.begin(9600);
   
   Serial.println("Split record1: ");
   Serial.println(subStr(record1, " ", 1));
   Serial.println(subStr(record1, " ", 2));
   Serial.println(subStr(record1, " ", 3));
   Serial.println(subStr(record1, " ", 4));
   Serial.println(subStr(record1, " ", 5));
   Serial.println(subStr(record1, " ", 6));
   Serial.println(subStr(record1, " ", 7));
   Serial.println(subStr(record1, " ", 8));
   Serial.println(subStr(record1, " ", 9));

   Serial.println("Split record2: ");
   Serial.println(subStr(record2, " ", 1));
   Serial.println(subStr(record2, " ", 2));
   Serial.println(subStr(record2, " ", 3));
}

void loop () {
}

// Function to return a substring defined by a delimiter at an index
char* subStr (char* str, char *delim, int index) {
   char *act, *sub, *ptr;
   static char copy[MAX_STRING_LEN];
   int i;

   // Since strtok consumes the first arg, make a copy
   strcpy(copy, str);

   for (i = 1, act = copy; i <= index; i++, act = NULL) {
      //Serial.print(".");
      sub = strtok_r(act, delim, &ptr);
      if (sub == NULL) break;
   }
   return sub;

}

and another:

/*
Example by mrburnette (ray) on nano 20140914
Binary sketch size: 5,844 bytes (of a 30,720 byte maximum)
"Public domain example"

http://forum.arduino.cc/index.php?topic=266669
"How do i get index 6+7 and 8+9 from a stream of 24 chars that come from the serial input into the Arduino? 
The connected serial device is in idle mode until i send a command to it. When i send “ZD”, 
it will send a timecode in the format ZD2b102c080c090e05ba0549, where ZD stands for the type of message it returns. 
2c represents the minutes and 08 the hours of the the current time (in HEX format). The last 4 chars are a checksum. 
There is no CR or NL at the end. I would like to get the minutes and hours as interger values."
*/

#include <Streaming.h>  // get used to this library - it makes serial output easy

char sInput[] = {"ZD2b102c080c090e05ba0549\0"} ;  // this data would come in over the serial connection
String sMM ;          // 0xHH\0 = 5 characters
String sHH ;          // Type String allows for concatenation and substrings
char Mbuf[5] ;        // 5 character user buffer for minutes, same type as serial input
char Hbuf[5] ;        // ditto - this one is for the 2 digit hours (HEX: "0x??\0")
int hours, minutes ;  // This is the final product that we are desiring

void setup(void)
{
  Serial.begin(9600);
  delay(1000);
  Serial << "Beginning Example Run...\r\n" ;
  Serial << "Original String: " << sInput << "\r\n\r\n" ;
  // alternate http://arduino.cc/en/Reference/StringSubstring
  // Ref:      http://arduino.cc/en/Reference/StringToCharArray
  // Ref:      http://stackoverflow.com/questions/23576827/arduino-convert-a-sting-hex-ffffff-into-3-int
  // 0123456789                  <----- Character count in input array (type char)
  //"ZD2b102c080c090e05ba0549\0" <----- Input stream representation stored in char array sInput[]
  //       mmhh                  <----- HEX values we desire within the stream
  sMM += "0x" ; sMM += sInput[6]; sMM += sInput[7]; sMM += '\0' ;  // concatenate individual string elements
  sHH += "0x" ; sHH += sInput[8]; sHH += sInput[9]; sHH += '\0' ;  // ditto
  Serial << "Substrings: " << sHH << ":" << sMM << "\r\n\r\n" ;    // show it on term
  sMM.toCharArray( Mbuf, 5 ) ;            // <----- convert String to Char Array
  sHH.toCharArray( Hbuf, 5 ) ;            //        ditto
  hours  = (int)strtol(Hbuf, NULL, 16);   // <----- cast to (int) because strtol returns long
  minutes= (int)strtol(Mbuf, NULL, 16);   //        ditto
  Serial << "Hours: " << hours << "  Minutes: " << minutes << "\r \n";  // show it on term
  
  Serial << "\r\n *****End of run*****\r\n" ;
  Serial.end();
}

void loop( void ) {}

/*  ============================== CONSOLE OUTPUT ==============================
Beginning Example Run...
Original String: ZD2b102c080c090e05ba0549

Substrings: 0x08:0x2c

Hours: 8  Minutes: 44
 
 *****End of run*****
*/

Ray

Parsers are an art unto themselves

Yep. Especially dealing with comma separated values where the value can be omitted as long as the comma is not, as in NMEA sentences sent by GPSs.

>> Parsers are an art unto themselves and there are many ways to extract the info.

Thanks - that was pretty educative and the Streaming library was a bonus to add on.

Also went through your project of getting GPS time without using Internet. Interesting.

( Though I could get the idea, your first sketch strtok test prints garbage - not sure where is the issue)

Mogaraghu:
<…>

( Though I could get the idea, your first sketch strtok test prints garbage - not sure where is the issue)

Example is from the old GUI 1.05 days… it needs refreshing, likely.

Ray