Hi,
Could someone modify below code to get 2 values from a text-field and split them into two(using indexOf to find where the comma is) and assign them into 2 variables and convert the values of 2 variables into int.
assume my string will look something like "145,384".
I want to split it like
int 145
int 384
a function that will take the string and convert it to an INT.
// Function to convert a string into an int
// Accepts a string variable (not CHAR!)
// Returns an INT
int ConvertStringToInt(string convertString){
char convert_buffer[convertString.length() + 1];
convertString.toCharArray(convert_buffer, sizeof(convert_buffer));
int convert_integer_data = atoi(convert_buffer);
return convert_integer_data;
}
Code:
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {
0x5c, 0x26, 0x0a, 0x78, 0x12, 0x85 }; // mac
byte ip[] = {
192, 168, 20 , 2 }; // ip in lan
byte gateway[] = {
192, 168, 20, 1 }; // internet access via router (Gateway)
byte subnet[] = {
255, 255, 255, 0 }; // subnet mask
EthernetServer server(80); // server port
String readString, servo1, servo2;
int lux_v1;
int lux_v2;
void setup(){
//start Ethernet
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
//enable serial data print
Serial.begin(9600);
int lux_v1.attach(7);
int lux_v2.attach(6);
Serial.println("initialize..."); // so I can keep track of what is loaded
}
void loop(){
// Create a client connection
EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
//read char by char HTTP request
if (readString.length() < 100) {
//store characters to string
readString += c;
}
//if HTTP request has ended
if (c == '\n') {
///////////////
Serial.println(readString);
if (readString.length() >0) {
Serial.println(readString);
int lux_v1 = readString.substring(7, 11);
int lux_v1 = readString.substring(12, 16);
Serial.println(lux_v1);
Serial.println(lux_v2);
int n1;
int n2;
char carray1[6];
lux_v1.toCharArray(carray1, sizeof(carray1));
n1 = atoi(carray1);
char carray2[6];
lux_v2.toCharArray(carray2, sizeof(carray2));
n2 = atoi(carray2);
readString="";
}
//now output HTML data header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.print("<html><head>");
client.print("<title></title>");
client.println("</head>");
client.print("<body>");
client.print("<form method=get>LUX:<input type=text size=3 name=L value=");
client.print("> <input name=H type=submit value=submit></form>");
client.print("<body>");
client.println("</html>");
break;
}
}
}
//clearing string for next read
readString="";
client.stop();
Serial.println("client stoped");
}
}
I hope someone will help me on this. Pls I really appreciate your kind help.
Regards,
Udesh