Freaking strange thing happening with this simple code

CtrlAltElite:
No, s3 is a String variable - if you want to make any headway programming, you will have to start paying attention to details, and using language properly.

You'll discover that folks around here don't like Strings very much.

Then what should I do? Using the lowercase (s) string gives an error that it is not a datatype...

“Freaking”

Everyone knows what this is.
This is okay in the bar, but come on, this is a family form, are you not better than this?

.

larryd:
“Freaking”

Everyone knows what this is.
This is okay in the bar, but come on, this is a family form, are you not better than this?

.

You're not a Dr Hook fan then?

larryd:
“Freaking”

Everyone knows what this is.
This is okay in the bar, but come on, this is a family form, are you not better than this?

.

Okay.. Grandpa..

Using the lowercase (s) string gives an error that it is not a datatype

That's because it is not a datatype. Did anyone in this thread say that it was or contribute an example where a variable was declared as a string (lowercase s) ?

The suggestion is that you use zero terminated arrays of chars (aka strings) throughout your program but you will need to stop trying to use the String (uppercase S).

udbhav95:
Okay.. Grandpa..

Something I've noticed about this forum, is that generally, the people who can best help you don't like:
txt speak.
Strings.
Bad manners.

As the Americans say, "Way to go!"

udbhav95:
Then what should I do? Using the lowercase (s) string gives an error that it is not a datatype…

you are mixing apples with oranges. Here is an approach to simplify your writing to client and Serial, but you still have to come up with a way to stop using String class to receive text:

#include <SPI.h>
#include <Ethernet.h>

template <class T> inline Print& operator <<(Print& obj, T arg)
{
  obj.print(arg);
  return obj;
}

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

IPAddress ip(192, 168, 137, 200); //IP address for your arduino.

const char server[] PROGMEM = "192.168.137.1"; //IP address of your computer.

int interrupt = 0; //Variable to control the iterations of void loop().

const char Name[] PROGMEM = "Udbhav"; //Variable to be written on the database.

int Roll = 51;
int Age = 17;

String rcv = ""; //Variable in which the server response is recorded.

EthernetClient client;

void setup()
{

  Serial.begin(9600);

  Ethernet.begin(mac, ip);

  delay(5000); //Wait for ethernet to connect.

}

void httpRequest()
{
  if (client.connect(server, 81))
  {
    Serial << (F("Connection established 1\n"));
    
    client << (F("GET")) << (F("/tryjson.php/")) << (F(" HTTP/1.1\r\n")) << (F("Host: ")) << server << (F("\r\n")) << (F("Connection: close\r\n\r\n"));

    unsigned long timeout = millis();
    while (client.available() == 0)
    {
      if (millis() - timeout > 25000) //If nothing is available on server for 25 seconds, close the connection.
      {
        return;
      }
    }
    while (client.available())
    {
      String line = client.readStringUntil('\r'); //Read the server response line by line..
      rcv += line; //And store it in rcv.
    }
    client.stop(); // Close the connection.
  }
  else
  {
    Serial << (F("Connection failed 1\n"));
  }
  //Serial.println("Received string: ");
  //Serial.println(rcv); //Display the server response.
  //Serial.println();

  String s2 = rcv.substring((rcv.indexOf('[')), rcv.indexOf(']')); // Extract the line returned by JSON object.

  int l = s2.length();
  String s = "", s3;
  int m, i = 0;
  while (i < l)
  {
    m = s2.indexOf('"', i);
    if (m != -1)
    {
      for (int j = m + 1; j < l; j++)
      {
        if (s2.charAt(j) != '"')
        {
          s += s2.charAt(j);
        }
        else
        {
          i = j;
          break;
        }
      }
      Serial << (s); //Display the exact extracted values.
      s3 = s;
      s = "";
    }
    i++;
  }
  Serial << (F("s3: "))  << (s3);
  int conv = s3.toInt();
  Serial << (F("Conv: ")) << (conv);
}

void loop()
{
  if (interrupt == 0)
  {
    httpRequest();
    delay(1000);
    if (client.connect(server, 81))
    {
      Serial <<(F("Connection Established 2"));
      client << (F("GET /info.php?"));
      client << ("Name=") << Name << (F("&Roll=")) << Roll << (F("&Age=")) << Age;
      client << (F(" HTTP/1.1")) << (F("Host: 192.168.137.1\n")) << (F("Connection: close\n")) << (F("\n\n"));
      client.stop();
    }
    else
    {
      Serial << (F("Connection failed 2\n"));
    }
  }
  interrupt++;
  delay(10000);  // Why is this here???
}
long stringToLong(String value)
{
     long outLong=0;
     long inLong=1;
     int c = 0;
     int idx=value.length()-1;
     for(int i=0;i<=idx;i++)
     {
          c=(int)value[idx-i];
          outLong+=inLong*(c-48);
          inLong*=10;
     }
     return outLong;
}

Even this is not working…

@UKHeliBob

You yourself said it…

Do you need to convert a string (lowercase s) or a String (uppercase S) ?

If the latter then could you use the former then use atoi() to do the conversion ?

‘You could use the former’

The suggestion is that you use zero terminated arrays of chars (aka strings) throughout your program but you will need to stop trying to use the String (uppercase S).

…I already tried using char arrays and it’s not working… Let me try it one more time…

@BulldogLowell

Alright, thanks. I’ll try this one too.

Alright guys, I have extracted the string s3 into a char array.. Now what to do?.. In order to make an integer out of it..

int someInt = atoi (s3_is_a_char_array_with_a_zero_terminator);
 int len=s3.length();
                while(w<len)
                {
                  ch[w]=s3.charAt(w);
                  w++;
                }
                Serial.println("ch:");
                for(int i=0;i<10;i++)
                Serial.print(ch[i]);
                Serial.println();
                int fin=atoi(ch);

I fetched the s3 String in the ch array and applied atoi() to it but the fin variable remains null… Any idea why?

CtrlAltElite:
s3_is_a_char_array_with_a_zero_terminator

What does this mean?

I fetched the s3 String in the ch array

But you didn't add a zero terminator to the array.

You need to find a C string handling basics tutorial somewhere online.

CtrlAltElite:
You need to find a C string handling basics tutorial somewhere online.

Alright... One more thing, the black space just below my sketch in Arduino IDE is showing this..

Sketch uses 14448 bytes (44%) of program storage space. Maximum is 32256 bytes.
Global variables use 1129 bytes (55%) of dynamic memory, leaving 919 bytes for local variables. Maximum is 2048 bytes.

Is 919 bytes too low for the code to get stuck while executing? Is this what's causing these 'Established 2' unknown statements to appear on my serial monitor? Is this the main problem?

No (probably), no (The connection thing is in your sketch) and no.

CtrlAltElite:
The connection thing is in your sketch

I know but that statement is to print 'Connection established 2'.

Instead, it is printing 'established 2'... which is weird.

I'm gonna sleep now... It's almost early morning here... Yet I'm not able to resolve the issue. I'll look into it tomorrow.

udbhav95:
Is 919 bytes too low for the code to get stuck while executing? Is this what’s causing these ‘Established 2’ unknown statements to appear on my serial monitor? Is this the main problem?

probably…

Move your string constants to Program Space with the F() macro, as I showed you.

stop concatenating Strings… it is VERY memory hungry, and YOU DON’T NEED TO DO THAT!!!

Instead, it is printing ‘established 2’… which is weird.

It’s only odd if your code isn’t writing all over random access memory at random, but only you know whether that is true.

This has almost reached (bar sport) !

BulldogLowell:
probably...

Move your string constants to Program Space with the F() macro, as I showed you.

stop concatenating Strings... it is VERY memory hungry, and YOU DON'T NEED TO DO THAT!!!!!

Alright... I'll get back to you once I do it.

And thanks, everyone who helped.

BulldogLowell:
probably...

Move your string constants to Program Space with the F() macro, as I showed you.

stop concatenating Strings... it is VERY memory hungry, and YOU DON'T NEED TO DO THAT!!!!!

Earlier I was using String variables to store the server response, so I was doing this...

String rcv="";
while(client.available)
{
     String line=client.readStringUntil('\r')
     rcv+=line;
}

But now I'm using char arrays, so I did this...

char rcv[]="";
int x=0;
while(client.available)
{
     rcv+=client.charAt(x);
     x++;
}

But it says that class Ethernetclient doesn't have a method named charAt()...

Is there any other way to store the server response in a char array?