title: Extract numbers from AT command in Arduino
body: <p>So I'm doing an AT command and the reply is</p>
  <pre><code>AT+CGDCONT?
  +CGDCONT: 1,&quot;IP&quot;,&quot;hologram&quot;,&quot;0.0.0.0&quot;,0,0,0,0
  +CGDCONT: 13,&quot;IP&quot;,&quot;hologram&quot;,&quot;0.0.0.0&quot;,0,0,0,0
  +CGDCONT: 14,&quot;IP&quot;,&quot;hologram&quot;,&quot;0.0.0.0&quot;,0,0,0,0
  OK
  </code></pre>
  <p>I need to get the numbers out of this that are written before the &quot;IP&quot; (for this example they are 1,13,14). The numbers range from 1-24 as do the number of lines.</p>
  <p>I was reading the reply from the command and saving it as a String with</p>
  <pre class="lang-cpp prettyprint-override"><code>  String input;
    SerialAT.println(&quot;AT+CGDCONT?&quot;);
    delay(500);
    if (SerialAT.available()) {
      input = SerialAT.readString();
      Serial.println(input);
    } 
  </code></pre>
  <p>Right now I am reading the stream reply and saving it as a String. The String is then cut at the newline (\n) and stored pieces in an array. After that, I have it read the array and stop once it hits a &quot;,&quot; which is right after the number. Then it identifies the section that contains &quot;: &quot; which is right before the number and gets the index position of the &quot;: &quot; and uses that to take a substring of what is left which is just the number.
  So for the example that I have, it would put the first row into the array looking like this</p>
  <pre><code>+CGDCONT: 1,&quot;IP&quot;,&quot;hologram&quot;,&quot;0.0.0.0&quot;,0,0,0,0
  </code></pre>
  <p>It would be read one char at a time until the first &quot;,&quot; is identified like this</p>
  <pre><code>+
  +C
  +CG
  +CGD
  +CGDC
  +CGDCO
  +CGDCON
  +CGDCONT
  +CGDCONT:
  +CGDCONT: 
  +CGDCONT: 1
  </code></pre>
  <p>then the position of &quot;: &quot; would be identified and for this example, it would be 9. So everything after the 9th position inside of the array would be the output which is 1.</p>
  <p>This is working flawlessly and I have even sent in garbled text before the +CGDCONT: because sometimes that's what the modem replies if it didn't have time to fully process the command and it has always fed me the numbers out correctly.</p>
  <p>My code for this is</p>
  <pre class="lang-cpp prettyprint-override"><code>
    String input;
    SerialAT.println(&quot;AT+CGDCONT?&quot;);
    delay(500);
    if (SerialAT.available()) {
      input = SerialAT.readString();
      Serial.println(input);
    } else {
      Serial.println(&quot;Failed to get PDP!&quot;);
    }
  
  
    const int numberOfPieces = 10;
    String pieces[numberOfPieces];
    int counter = 0;
    int lastIndex = 0;
  
    for (int i = 0; i &lt; input.length(); i++) {
      if (input.substring(i, i + 1) == &quot;\n&quot;) {
        pieces[counter] = input.substring(lastIndex, i);
        lastIndex = i + 1;
        counter++;
      }
      if (i == input.length() - 1) {
        pieces[counter] = input.substring(lastIndex, i);
      }
    }
  
    input = &quot;&quot;;
    counter = 0;
    lastIndex = 0;
  
    String readString, data;
    int D;
    int PDPcounter = 0;
  
    for ( int i = 0; i &lt; numberOfPieces; i++) {
      for ( int x = 0; x &lt; pieces[i].length(); x++) {
        char c = pieces[i][x];  //gets one byte from buffer
        Serial.println(readString);
        if (c == ',') {
          if (readString.indexOf(&quot;: &quot;) &gt;= 0) {
            data = readString.substring((readString.indexOf(&quot;: &quot;) + 1));
            D = data.toInt();
            Serial.println(D);
            PDP[PDPcounter] = D;
            Serial.print(&quot;PDPcounter = &quot;); Serial.println(PDPcounter);
            PDPcounter++;
            readString = &quot;&quot;;
            data = &quot;&quot;;
            break;
          }
          readString = &quot;&quot;;
          data = &quot;&quot;;
        }
        else {
          readString += c;
        }
      }
    }
  </code></pre>
  <p>At the very end, I try and read the contents of the PDP array with</p>
  <pre class="lang-cpp prettyprint-override"><code>    for ( int i = 0; i &lt; 10; i++) {
        Serial.print(i);
        Serial.print(&quot; - &quot;);
        Serial.println(PDP[i]);
      }
  </code></pre>
  <p>and I am getting the following</p>
  <pre><code>0 - 1
  1 - 13
  2 - 14
  3 - 0
  4 - 0
  5 - 0
  6 - 0
  7 - 0
  8 - 0
  9 - 0
  </code></pre>
  <p>which is exactly what I need.</p>
  <hr />
  <p>I guess the only help that I need now is code cleanup recommendations.</p>