I'm trying to parse the last character from the string of the reply from server and print them to serial monitor.

My arduino code:

// This will send the request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
           "Host: " + host + "\r\n" + 
           "Connection: close\r\n\r\n");

  // Read all the lines of the reply from server and print them to Serial
    String line = client.readStringUntil('\r');

My PHP page only shows the following though:

  Output: 1
  Watering: 1

I get the following output from my Serial Monitor:

  HTTP/1.1 200 OK
  Date: Fri, 28 Apr 2017 19:27:40 GMT
  Server: Apache/2.4.25 (Win32) OpenSSL/1.0.2j PHP/7.1.1
  X-Powered-By: PHP/7.1.1
  Content-Length: 9
  Connection: close
  Content-Type: text/html; charset=UTF-8

  Output: 1
  Watering: 1

I want to get the value of Output(In this case it's a 1), and Watering(Also a 1), etc...

Preferably store them in an int.

I tried using startsWith,endsWith,etc. but to no avail :(

  • Welcome to Arduino SE. Be sure to take the tour to see how this works and to earn a badge: arduino.stackexchange.com/Tour – SDsolar Apr 29 '17 at 8:14
  • 1
    I'm voting to close this question as off-topic because it's an elemental programming question, not related to Arduino in any way. – user31481 Nov 14 '17 at 14:43

If you have a String s, then s.charAt(s.length() - 1) give you that last char.

The function length gives you the number of chars in the string (not counting the '0' marker). The function charAt give you a single char at the position. The first char is at position 0.

For converting a single char digit D (0..9), you can use (D - '0').


void setup() {
  String s = "Watering: 1";
  char last = s.charAt(s.length() - 1);
  int value = (int) (last - '0');

void loop() { 
  • As I've said previously the char position changes on an hourly basis and won't be in the same place so I need to string search for that integer. Also this was posted back in April.. – William Nov 14 '17 at 16:36
  • @William. Beg your pardon? Your question starts with *I'm trying to parse the last character from the string of the reply from server and print them to serial monitor." – user31481 Nov 14 '17 at 16:55

I've converted the string to a char array and just checked if there is a 1 or a 0 at that region.

// Read all the lines of the reply from server and print them to Serial
String line;
int value=0;
  line += client.readStringUntil('\r');
char charBuffer[line.length()+1];
line.toCharArray(charBuffer, line.length()+1);
for(int i=207;i<line.length()+1;i++)
  if(charBuffer[i] == '1')
    value = charBuffer[i];
  if(charBuffer[i] == '0')
    value = charBuffer[i];
  • You don't need to copy the entire string to a buffer; charAt give you access to the internal String buffer. Your method doesn't work for string shorter than 207 chars. Your method return any 1 or 0 in the string, not the last char in the string. – user31481 Nov 14 '17 at 14:30
  • I know it doesn't work for anything shorter than 207 hence why I started to search for info at char 207 and above. charAt won't work properly because the server sends a different responce in the morning opposed to an evening. You didn't even understand this question in the first place and I've already posted a solution that works for me back in April. – William Nov 14 '17 at 16:35
  • Your code is also inefficient, both in time and space. You can read from client char by char and detect/print the last digit at the same time. No need to waste space holding the whole string. No need to make a second pass looking for the last digit. – user31481 Nov 14 '17 at 17:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.