Finding a integer at the end of a string and saving it

Hello everybody,

First of all I am using ESP8266 Board ESP-12E which I am programming with the arduino IDE

I am trying to code WS2812B leds controllable with your phone. The phone will input a web address and then run the code. To start with, I am ignoring the LEDS so the code below doesn't contain anything to do with that. After inputting the web address the serial monitor shows that is indeed working, however I would like to send analog data over the wifi, I can't find a command to get only numbers in the end so I can convert to an Integer so is there a command inbuilt in the ESP8266WIFI Library to find this or any other solution any one can think

#include <ESP8266WiFi.h>

const char* ssid = "BTHub6-CWHT";
const char* password = "UQUedkhwayt9";
const char* host = "***********"; //it will tell you the IP once it starts up
                                        //just write it here afterwards and upload

bool off = false;
bool fade = false;
bool analog = false;

WiFiServer server(301); //just pick any port number you like

void setup() {
  Serial.begin(115200);
  delay(10);
  Serial.println(WiFi.localIP());
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  server.begin();
  Serial.println("Server started");
  Serial.println(WiFi.localIP());
  
}

void loop() {
  // Check if a client has connected
  
  WiFiClient client = server.available();
  if (!client) {
    return;
  }

  // Wait until the client sends some data
  while (!client.available()) {
    delay(1);
  }

  // Read the first line of the request
  String req = client.readStringUntil('\r');
  client.flush();

  // Match the request
  if (req.indexOf("") != -10) {  //checks if you're on the main page

    if (req.indexOf("/OFF") != -1) { //checks if you clicked OFF or inputted the address
      Serial.println("You clicked OFF");
      off = true;
      fade = false;
    }
    if (req.indexOf("/FADE") != -1) { //checks if you clicked fade or inputted the address
      Serial.println("You clicked FADE");
      fade = true;
      off = false;
    }
  }

  else {
    Serial.println("invalid request");
   client.stop();
    return;
  }

  // Prepare the response
  String s = "HTTP/1.1 200 OK\r\n";
  s += "Content-Type: text/html\r\n\r\n";
  s += "<!DOCTYPE HTML>\r\n<html>\r\n";
  s += "
<input type=\"button\" name=\"bl\" value=\"Turn LED FADE \" onclick=\"location.href='/FADE'\">";
  s += "


";
  s += "
<input type=\"button\" name=\"bl\" value=\"Turn LED OFF\" onclick=\"location.href='/OFF'\">";
  s += "</html>\n";

  client.flush();

  // Send the response to the client
  client.print(s);
  delay(1);


  
}

Send the number(s) surrounded by unique delimiters, and use the handy routines in <string.h> to find and convert.

Hi
I completely understand the process of what to do but when compiling the program it says that the
intializer failed to determine the size of str[]. Which is understandable as arrays usually have predetermined lengths for the index. Any ideas of how to help

Current non working code:

#include <ESP8266WiFi.h>

const char* ssid = "BTHub6-CWHT";
const char* password = "UQUedkhwayt9";
const char* host = "***********"; //it will tell you the IP once it starts up
                                        //just write it here afterwards and upload

bool off = false;
bool fade = false;
bool analog = false;

WiFiServer server(301); //just pick any port number you like

void setup() {
  Serial.begin(115200);
  delay(10);
  Serial.println(WiFi.localIP());
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  server.begin();
  Serial.println("Server started");
  Serial.println(WiFi.localIP());
  
}

void loop() {
  // Check if a client has connected
  
  WiFiClient client = server.available();
  if (!client) {
    return;
  }

  // Wait until the client sends some data
  while (!client.available()) {
    delay(1);
  }

  // Read the first line of the request
  String req = client.readStringUntil('\r');
  String analogp = client.readStringUntil('.');
  client.flush();

  // Match the request
  if (req.indexOf("") != -10) {  //checks if you're on the main page

    if (req.indexOf("/OFF") != -1) { //checks if you clicked OFF or inputted the address
      Serial.println("You clicked OFF");
      off = true;
      fade = false;
    }
    if (req.indexOf("/FADE") != -1) { //checks if you clicked fade or inputted the address
      Serial.println("You clicked FADE");
      fade = true;
      off = false;
    }
    if(req.indexOf("/RED") != -1)    //checks for what value to output 
    {
      char str[] = client.readStringUntil('.');
      char * pch;
      pch = strtok (str," ,.-");
      while (pch != NULL)
    {
     Value = pch + pch+ pch + pch  //puts them together and since it is a string it doesn't mathematically add
     pch = strtok (NULL, " ,.-");
    }
    }
  }

  else {
    Serial.println("invalid request");
   client.stop();
    return;
  }

  // Prepare the response
  String s = "HTTP/1.1 200 OK\r\n";
  s += "Content-Type: text/html\r\n\r\n";
  s += "<!DOCTYPE HTML>\r\n<html>\r\n";
  s += "
<input type=\"button\" name=\"bl\" value=\"Turn LED FADE \" onclick=\"location.href='/FADE'\">";
  s += "


";
  s += "
<input type=\"button\" name=\"bl\" value=\"Turn LED OFF\" onclick=\"location.href='/OFF'\">";
  s += "</html>\n";

  client.flush();

  // Send the response to the client
  client.print(s);
  delay(1);


  
}

Value does not seem to be declared, and the following will not work to concatenate C-strings (zero terminated character arrays). Use strcat() instead.

     Value = pch + pch+ pch + pch  //puts them together and since it is a string it doesn't mathematically add

Declare the array big enough to hold the biggest possible input, plus the strimg terminal character.

When putting stuff in the string, always check first if you have run off the end of the defined size.

Yep, best to use the "n" variants of the C-string functions (strncat(), strncpy() etc.), because if you use them properly, they won't write outside of the array bounds .

Hello

Thanks for the ideas but to be honest I am really not to sure how to setup a char array to the biggest possible value

Current Code:

#include <ESP8266WiFi.h>

const char* ssid = "BTHub6-CWHT";             //both are fake (to simulate real)
const char* password = "UQUedkhwayt9";
const char* host = "***********"; //it will tell you the IP once it starts up
                                        //just write it here afterwards and upload
int Value;
bool off = false;
bool fade = false;
bool analog = false;

WiFiServer server(301); //just pick any port number you like

void setup() {
  Serial.begin(115200);
  delay(10);
  Serial.println(WiFi.localIP());
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  server.begin();
  Serial.println("Server started");
  Serial.println(WiFi.localIP());
  
}

void loop() {
  // Check if a client has connected
  
  WiFiClient client = server.available();
  if (!client) {
    return;
  }

  // Wait until the client sends some data
  while (!client.available()) {
    delay(1);
  }

  // Read the first line of the request
  String req = client.readStringUntil('\r');
  String analogp = client.readStringUntil('.');
  client.flush();

  // Match the request
  if (req.indexOf("") != -10) {  //checks if you're on the main page

    if (req.indexOf("/OFF") != -1) { //checks if you clicked OFF or inputted the address
      Serial.println("You clicked OFF");
      off = true;
      fade = false;
    }
    if (req.indexOf("/FADE") != -1) { //checks if you clicked fade or inputted the address
      Serial.println("You clicked FADE");
      fade = true;
      off = false;
    }
    if(req.indexOf("/RED") != -1)
    {
      char str[] = client.readStringUntil('.');
      char * pch;
      pch = strtok (str," ,.-");
      while (pch != NULL)
    {
     Value = strcat(pch,pch,pch,pch)
     pch = strtok (NULL, " ,.-");
    }
    }
  }

  else {
    Serial.println("invalid request");
   client.stop();
    return;
  }
  // Prepare the response
  String s = "HTTP/1.1 200 OK\r\n";
  s += "Content-Type: text/html\r\n\r\n";
  s += "<!DOCTYPE HTML>\r\n<html>\r\n";
  s += "
<input type=\"button\" name=\"bl\" value=\"Turn LED FADE \" onclick=\"location.href='/FADE'\">";
  s += "


";
  s += "
<input type=\"button\" name=\"bl\" value=\"Turn LED OFF\" onclick=\"location.href='/OFF'\">";
  s += "</html>\n";

  client.flush();

  // Send the response to the client
  client.print(s);
  delay(1);


  
}

Techinc1510:
Thanks for the ideas but to be honest I am really not to sure how to setup a char array to the biggest possible value

Same way as any other array.

const byte bufferSize=25;
char rcvData[bufferSize];

Sorry everyone for all this but it still doesn't work following even dougp's idea. With his idea I created 3 different codes which all dont seem to work

Code - All I did was declare the variable bufferSize and then when declaring the char in the brackets I I I wrote bufferSize Error - array must be intialized with a brace enclosed intializer

#include <ESP8266WiFi.h>

const char* ssid = "BTHub6-CWHT";             //both are fake (to simulate real)
const char* password = "UQUedkhwayt9";
const char* host = "***********"; //it will tell you the IP once it starts up

const byte bufferSize=25;           //just write it here afterwards and upload
String Value;
bool off = false;
bool fade = false;
bool analog = false;

WiFiServer server(301); //just pick any port number you like

void setup() {
  Serial.begin(115200);
  delay(10);
  Serial.println(WiFi.localIP());
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  server.begin();
  Serial.println("Server started");
  Serial.println(WiFi.localIP());
  
}

void loop() {
  // Check if a client has connected
  
  WiFiClient client = server.available();
  if (!client) {
    return;
  }

  // Wait until the client sends some data
  while (!client.available()) {
    delay(1);
  }

  // Read the first line of the request
  String req = client.readStringUntil('\r');
  String analogp = client.readStringUntil('.');
  client.flush();

  // Match the request
  if (req.indexOf("") != -10) {  //checks if you're on the main page

    if (req.indexOf("/OFF") != -1) { //checks if you clicked OFF or inputted the address
      Serial.println("You clicked OFF");
      off = true;
      fade = false;
    }
    if (req.indexOf("/FADE") != -1) { //checks if you clicked fade or inputted the address
      Serial.println("You clicked FADE");
      fade = true;
      off = false;
    }
    if(req.indexOf("/RED") != -1)
    {
      char str[bufferSize] = client.readString();   
      char * pch;
      pch = strtok (str," -");
      while (pch != NULL)
    {
     Value = strcat(pch,pch,pch,pch)
     pch = strtok (NULL, " -");
    }
    }
  }

  else {
    Serial.println("invalid request");
   client.stop();
    return;
  }
  // Prepare the response
  String s = "HTTP/1.1 200 OK\r\n";
  s += "Content-Type: text/html\r\n\r\n";
  s += "<!DOCTYPE HTML>\r\n<html>\r\n";
  s += "
<input type=\"button\" name=\"bl\" value=\"Turn LED FADE \" onclick=\"location.href='/FADE'\">";
  s += "


";
  s += "
<input type=\"button\" name=\"bl\" value=\"Turn LED OFF\" onclick=\"location.href='/OFF'\">";
  s += "</html>\n";

  client.flush();

  // Send the response to the client
  client.print(s);
  delay(1);


  
}

Any help is appreciated

Post code that compiles. This single line has several fatal errors.

     Value = strcat(pch,pch,pch,pch)

Your code will never work if you can't take the time to read the most basic documentation for the functions you are using.

PROJECT SOLVED;

Hello everybody sorry for troubling you all but I have fixed then code and I have commented the part which you might need for you project so it is beneficial for everyone:

 if(req.indexOf("/RED") != -1)                //if request contains 'RED'
    {
      analog = true;
      fade = false;                   
      off = false;
      req.toCharArray(str, len );  //converts the string from the request into a char array 
      client.flush();
      String pch;        //starts the string containing the tokens
      pch = strtok (str," -");          //delimiting parameters
      while (pch != NULL)
    {
     pch = strtok (NULL, " -");       //function
     if (pch.toInt() != 0)         //if one the tokens is  a number (.toInt returns 0 if it cannot be converted to an integer)
     {
      RedValue = pch.toInt();  //converts the string to an integer
      Serial.print("Red Analog Values: ");
      Serial.println(RedValue);     //Serial printing for debugging
      break;  //prevents it printing multiple times by breaking out of the while loop
     }
    }
    }

Don't know why I was pretty stupid when starting this post

Full Code:

#include <ESP8266WiFi.h>

const char* ssid = "BTHub6-CWHT";             //both are fake (to simulate real)
const char* password = "UQUedkhwayt9";
const char* host = "****************"; //it will tell you the IP once it starts up //just write it here afterwards and upload

unsigned int len = 27;  //random number
const byte bufferSize=25;           
int RedValue;
int GreenValue;
int BlueValue;

bool off = false;
bool fade = false;
bool analog = false;

WiFiServer server(301); //just pick any port number you like

void setup() {
  Serial.begin(115200);
  delay(10);
  Serial.println(WiFi.localIP());
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);                            //still the same
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  server.begin();
  Serial.println("Server started");
  Serial.println(WiFi.localIP());
  
}

void loop() {
  // Check if a client has connected
  
  WiFiClient client = server.available();
  if (!client) {
    return;
  }

  // Wait until the client sends some data
  while (!client.available()) {
    delay(1);
  }

  // Read the first line of the request
  String req = client.readStringUntil('\r');
  char str[bufferSize] = "";                  //declares the char str
  client.flush();

  // Match the request
  if (req.indexOf("") != -10) {  //checks if you're on the main page

    if (req.indexOf("/OFF") != -1) { //checks if you clicked OFF or inputted the address
      Serial.println("You clicked OFF");
      off = true;
      fade = false;
      analog = false;
    }
    if (req.indexOf("/FADE") != -1) { //checks if you clicked fade or inputted the address
      Serial.println("You clicked FADE");
      fade = true;
      off = false;
      analog = false;
    }
    if(req.indexOf("/RED") != -1)                //if request contains 'RED'
    {
      analog = true;
      fade = false;                   
      off = false;
      req.toCharArray(str, len );  //converts the string from the request into a char array 
      client.flush();
      String pch;        //starts the string containing the tokens
      pch = strtok (str," -");          //delimiting parameters
      while (pch != NULL)
    {
     pch = strtok (NULL, " -");       //function
     if (pch.toInt() != 0)         //if one the tokens is  a number (.toInt returns 0 if it cannot be converted to an integer)
     {
      RedValue = pch.toInt();  //converts the string to an integer
      Serial.print("Red Analog Values: ");
      Serial.println(RedValue);     //Serial printing for debugging
      break;  //prevents it printing multiple times by breaking out of the while loop
     }
    }
    }
    else if(req.indexOf("/BLUE") != -1)
    {
      analog = true;
      fade = false;
      off = false;
      req.toCharArray(str, len );
      client.flush();
      String pch;
      pch = strtok (str," -");
      while (pch != NULL)
    {
     pch = strtok (NULL, " -");
     if (pch.toInt() != 0)
     {
      BlueValue = pch.toInt();
      Serial.print("Blue Analog Values: ");
      Serial.println(BlueValue);  
      break;
     }
    }
    }
    else if(req.indexOf("/GREEN") != -1)
    {
      analog = true;
      fade = false;
      off = false;
      req.toCharArray(str, len );
      client.flush();
      String pch;
      pch = strtok (str," -");
      while (pch != NULL)
    {
     pch = strtok (NULL, " -");
     if (pch.toInt() != 0)
     {
      GreenValue = pch.toInt();
      Serial.print("Green Analog Values: ");
      Serial.println(GreenValue);  
      break;
     }
    }
    }
  }

  else {
    Serial.println("invalid request");
   client.stop();
    return;
  }
  // Prepare the response
  String s = "HTTP/1.1 200 OK\r\n";
  s += "Content-Type: text/html\r\n\r\n";
  s += "<!DOCTYPE HTML>\r\n<html>\r\n";
  s += "
<input type=\"button\" name=\"bl\" value=\"Turn LED FADE \" onclick=\"location.href='/FADE'\">";
  s += "


";
  s += "
<input type=\"button\" name=\"bl\" value=\"Turn LED OFF\" onclick=\"location.href='/OFF'\">";
  s += "</html>\n";

  client.flush();

  // Send the response to the client
  client.print(s);

  delay(1);


  
}

Thanks for all help!

-Ryan

We were all beginners once. We didn't know what we needed to know or how to ask.

Thanks for the feedback.