It's compiling but not executing.
There's no output.
I've searched everywhere. This is the right syntax.
Also, is there any other way for converting string to integer in arduino?
And please don't say using objects or stoi.. I've tried them.. Not working..
Not even that Int32 thing.. Don't know what it is.. I copied an example from net but it didn't execute either.
Oh yes.. It's working now.. But is there any other simple way of converting a string to integer?? I'm doing in a code which is sort of a patent thing so I'm not able to share it.. And none of the ways given on stack overflow and anywhere else are working..
It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.
Now it works but this still doesn't solve my problem.. Bcoz you see I cannot initialise a char array with a string.. I tried doing it in these two ways but it gave an error.. I need to convert a string variable into an integer.. Not a char array into an integer..
I also tried to break up the string into digits and then make a number out of them in an int variable separately but didn't work either.. Bcoz I had to use the toInt() function in it.. Which is the problem itself.. In this code s3 is the string that has a number I need to convert in the integer.
int sl=s3.length();
int g=0,h=1;
String str="";
for(int k=0;k<=sl;k++)
{
char ch=s3.charAt(k);
str+=ch;
int l=str.toInt();
g=g+10*(l/h);
h*=10;
str="";
}
Serial.println("g: "+g);*/
It's compiling but not executing.
There's no output.
I've searched everywhere. This is the right syntax.
Also, is there any other way for converting string to integer in arduino?
And please don't say using objects or stoi.. I've tried them.. Not working..
Not even that Int32 thing.. Don't know what it is.. I copied an example from net but it didn't execute either.
Please help.
Bcoz I think that you were still thinking that string has no methods, bcoz may be.. I don't know.. You are just starting with the codes.. Or bcoz you are Jon Snow.. Work it up kiddo.. Bcoz you know why? Bcoz this code I just sent you?.. Is running.
CtrlAltElite:
Until you post your code, neither do we.
Okay, so this is the code:
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,137,200); //IP address for your arduino.
char server[] = "192.168.137.1"; //IP address of your computer.
int interrupt=0; //Variable to control the iterations of void loop().
String Name="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.println("Connection established 1");
client.print(String("GET ") + "/tryjson.php/" + " HTTP/1.1\r\n" + "Host: " + server + "\r\n" + "Connection: close\r\n\r\n"); //GET request for server response.
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.println("Connection failed 1");
}
//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.println(s); //Display the exact extracted values.
s3=s;
s="";
}
i++;
}
Serial.println("s3: "+s3);
int conv=s3.toInt();
Serial.println("Conv: "+conv);
}
void loop()
{
if(interrupt==0)
{
httpRequest();
delay(1000);
if (client.connect(server, 81))
{
Serial.println("Connection Established 2");
client.print("GET /info.php?"); //GET request to write data to the database.
client.print("Name=");
client.print(Name);
client.print("&Roll=");
client.print(Roll);
client.print("&Age=");
client.print(Age);
client.println(" HTTP/1.1");
client.println("Host: 192.168.137.1");
client.println("Connection: close");
client.println();
client.println();
client.stop();
}
else
{
Serial.println("Connection failed 2");
}
}
interrupt++;
delay(10000);
}
And this is the output:
Connection established 1
Age
17
s3: 17
Established 2
Connection Established 2
In the second last line, it should print value of conv.. Instead, it prints something which is not even supposed to show on screen..
s3 is the string variable that I need to convert in integer..
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.