Converting from an Array of Characters to a String

Hello All,

I have a WiFi module that spits out data from the cloud to the serial monitor, in addition to a bunch of other stuff like http headers and what not. I want to ignore the other stuff and pick out just the data I'm retrieving from my website... and to do so (I'm not a programmer - so I'm just going by what I've been able to pick up by reading the forums)... I have to read each character and store it in an Array:

char inData[13]; // Allocate some space for the string
char inChar; // Where to store the character read
byte index = 0; // Index into array; where to store the character

      if(index < 12) // One less than the size of the array
      {
          inChar = Serial1.read(); // Read a character
          inData[index] = inChar; // Store it
          Serial.println(index);
          index++; // Increment where to write next
          inData[index] = '\0'; // Null terminate the string
      }

The data packet on the website is 13 characters long, hence an array of 13 values. I use "H" as the first character to denote the beginning of the data, followed by 12 values that can be either a 1 or 0 (eg H110010101100).

The question is this:

How do I look for, and pick out "H************" from inData[]?

I tried

String myString = myData.indexOf('H');

to no avail.

Any guidance is much appreciated.

/Fred

PS. I should mention that I can structure the data on my website in any format that makes sense. I simply want to turn on/off 12 LEDs attached to my arduino via a web interface. To do so, I've successfully attached an ESP8266 WiFi module to my Arduino Uno, and downloaded some sample code that allows me to read the data off my website (from a text file)... but given my limited programming skills, I'm unable to pick out part of output in the serial monitor that I need.

Here's the original thread I posted on this, if you want to see the entire code:

http://forum.arduino.cc/index.php?topic=353036.msg2433811

How do I look for, and pick out "H************" from inData[]?

Well, if you were smart, you would not begin storing data until the 'H' arrived. Then, it would be in the first element of the array (if you stored it at all; I wouldn't).

I tried
String myString = myData.indexOf('H');
to no avail.

Well, of course not, since inData is an array, not an instance of a class that has an indexOf() method.

PS. I should mention that I can structure the data on my website in any format that makes sense.

That's good, because 'H' is far too likely to appear in the headers, etc. Something like "<11011100110>" would be easy to recognize the start and end of data in, so that only the useful stuff would be stored.

if you want to turn only on/off a LED for example from D2 to D13 change code to:

char inData[12]; // Allocate some space for the string
char inChar; // Where to store the character read
byte index = 0; // Index into array; where to store the character

      if(index < 12 && Serial.available() ) // One less than the size of the array
      {
          inChar = Serial1.read(); // Read a character
          if (inChar == '0' || inChar == '1' )
          {
              inData[index] = inChar; // Store it
              Serial.println(index);
              index++; // Increment where to write next
           }
      }
      else if ( index >= 12 )
      {
           byte i;
           for(i = 0; i < 12; ++i)
               digitalWrite( i + 2, inData[i]);
           index = 0;
      }

I need more code for more precision example

In C, the convention is that a string is an array of characters terminated with a zero, an ASCII NUL, a '\0', however you want to say it.

So when I put "FOO!" in my code, the compiler creates a char array of length 5: ['F','O','O','!','\0'].

The arduino environment links in the libc library, which contains a number of standard functions that deal with these things (nul-terminated arrays of char)

http://www.nongnu.org/avr-libc/user-manual/group__avr__string.html

The C++ language has a 'String' class, but it's bloated and tends to hog memory, and is best avoided for arduino programming.

The data packet on the website is 13 characters long, hence an array of 13 values

If the data besides the headers is only 13 characters, then the below client test code can probably capture all that is returned into a String, then the String functions can be used to extract the data you desire. The below code is for use with an ethernet shield, but might have some useful info.

//zoomkat 11-04-13
//simple client test
//for use with IDE 1.0.1
//with DNS, DHCP, and Host
//open serial monitor and send an e to test client GET
//for use with W5100 based ethernet shields
//remove SD card if inserted
//data from weather server captured in readString 

#include <SPI.h>
#include <Ethernet.h>
String readString;

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address

char serverName[] = "api.openweathermap.org"; // myIP server test web page server
EthernetClient client;

//////////////////////

void setup(){

  Serial.begin(9600); 
  Serial.println("client readString test 11/04/13"); // so I can keep track of what is loaded
  Serial.println("Send an e in serial monitor to test"); // what to do to test
  Serial.println();
  
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    while(true);
  }
}

void loop(){
  // check for serial input
  if (Serial.available() > 0) //if something in serial buffer
  {
    byte inChar; // sets inChar as a byte
    inChar = Serial.read(); //gets byte from buffer
    if(inChar == 'e') // checks to see byte is an e
    {
      sendGET(); // call sendGET function below when byte is an e
    }
  }  
} 

//////////////////////////

void sendGET() //client function to send/receive GET request data.
{
  if (client.connect(serverName, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    client.println("GET /data/2.5/weather?zip=46526,us HTTP/1.1"); //download text
    client.println("Host: api.openweathermap.org");
    client.println("Connection: close");  //close 1.1 persistent connection  
    client.println(); //end of get request
  } 
  else {
    Serial.println("connection failed"); //error message if no client connect
    Serial.println();
  }

  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read(); //gets byte from ethernet buffer
    readString += c; //places captured byte in readString
  }

  //Serial.println();
  client.stop(); //stop client
  Serial.println("client disconnected.");
  Serial.println("Data from server captured in readString:");
  Serial.println();
  Serial.print(readString); //prints readString to serial monitor 
  Serial.println();  
  Serial.println();
  Serial.println("End of readString");
  Serial.println("=======e===========");
  Serial.println();
  readString=""; //clear readString variable

}