Ethernet sheild : How to read int value in html form.... Need Help.

Hi guys,

I am new to arduino.

Can you somebody tell how how to make simple html program to control dc motor speed.

Objective of program is to make text box on html page. User will enter the number (Motor speed) and

how to convert that entered number into int variable to program it using c language.

I am stuck here i know how to control but dont know about how to read user entered value in textbox on html page.

Please see the void ProcessCheckbox(EthernetClient cl) function . In that how to take int value from user i.e. text box and assign it int variable t2.


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

// MAC address from Ethernet shield sticker under board
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 0, 100); // IP address, may need to change depending on network
EthernetServer server(80); // create a server at port 80
String HTTP_req; // stores the HTTP request
boolean LED_status = 0; // state of LED, off by default
void setup()
{
Ethernet.begin(mac, ip); // initialize Ethernet device
server.begin(); // start to listen for clients
Serial.begin(9600); // for diagnostics
pinMode(12, OUTPUT); // declares pin 12 as output

}
void loop()
{
EthernetClient client = server.available(); // try to get client

if (client) { // got client?
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) { // client data available to read
char c = client.read(); // read 1 byte (character) from client
HTTP_req += c; // save the HTTP request 1 char at a time
// last line of client request is blank and ends with \n
// respond to client only after last line received
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
// send web page
client.println("");
client.println("");
client.println("");
client.println(" ");
client.println("");
client.println("");
client.println("

ECT MOTOR SPED CONTROL

");
client.println("

ENTER THE SPEED.

");
client.println("<form method="get">");
ProcessCheckbox(client);
client.println("");
client.println("");
client.println("");
Serial.print(HTTP_req);
HTTP_req = ""; // finished with request, empty string
break;
}
// every line of text received from the client ends with \r\n
if (c == '\n') {
// last character on line of received text
// starting new line with next character read
currentLineIsBlank = true;
}
else if (c != '\r') {
// a text character was received from client
currentLineIsBlank = false;
}
} // end if (client.available())
} // end while (client.connected())
delay(1); // give the web browser time to receive the data
client.stop(); // close the connection
} // end if (client)
}

void ProcessCheckbox(EthernetClient cl)

{
int t2 = 0;
pinMode(13, OUTPUT);

{

cl.println("<input type="text" name="t1"");

cl.println ("<input type="submit"value="ENTER"onclick=fun()>");

function fun()
{
alert("Number is :+t1.value")
t1=t2;
if(t2=1)
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
else
if(t2=0)
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
}
}
}

Below is web server code that produces a web page with a text box. Bottom is simple servo test code that demonstrates how to change a character String into an integer (number)..

//zoomkat 12-08-12
//get submit box code
//for use with IDE 1.0
//open serial monitor to see what the arduino receives
//use the \ slash to escape the " in the html or use a '
//address will look like http://192.168.1.102:84 when submited
//for use with W5100 based ethernet shields
//note that the below bug fix may be required
// http://code.google.com/p/arduino/issues/detail?id=605 

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port

String readString; 

//////////////////////

void setup(){

  pinMode(5, OUTPUT); //pin selected to control
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  server.begin();

  //enable serial data print 
  Serial.begin(9600); 
  Serial.println("server text box test1"); // 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; 
          //Serial.print(c);
        } 

        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.println(readString); //see what was captured

          //now output HTML data header

          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();

          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<TITLE>Arduino GET test page</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");

          client.println("<H1>HTML form GET example</H1>");

          client.println("<FORM ACTION='/' method=get >"); //uses IP/port of web page

          client.println("Pin 5 'on5' or 'off5': <INPUT TYPE=TEXT NAME='LED' VALUE='' SIZE='25' MAXLENGTH='50'>
");

          client.println("<INPUT TYPE=SUBMIT NAME='submit' VALUE='Change Pin 5!'>");

          client.println("</FORM>");

          client.println("
");

          client.println("</BODY>");
          client.println("</HTML>");

          delay(1);
          //stopping client
          client.stop();

          /////////////////////
          if(readString.indexOf("on5") >0)//checks for on
          {
            digitalWrite(5, HIGH);    // set pin 5 high
            Serial.println("Led On");
          }
          if(readString.indexOf("off5") >0)//checks for off
          {
            digitalWrite(5, LOW);    // set pin 5 low
            Serial.println("Led Off");
          }
          //clearing string for next read
          readString="";

        }
      }
    }
  }
}
//zoomkat 7-30-10 serial servo test
//type servo position 0 to 180 in serial monitor
// Powering a servo from the arduino usually *DOES NOT WORK*.

String readString;
#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

void setup() {
  Serial.begin(9600);
  myservo.attach(9);
  Serial.println("servo-test"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the String readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured String 
    int n = readString.toInt();  //convert readString into a number
    myservo.write(n);
    readString="";
  } 
}

Sir,

Thanks for the valuable reply.

I am new to arduino.

I am writing code to control dc motor using pwm output.

PWM output should vary according to user defined speed ( For which html text box will be used).

Can you tell me how read int which is entered in html text box on website and assign that value to int variable in program. So by reading that value i can control pwm output of motor.

Program flow will be like this.

User enter int value in Html text box >> Program will read it>> assign it to user defined int variable in program.

Using the text box code I posted, if you send the value 240 in the text box, the arduino will receive the below line. I would suggest you use readString.indexOf('=') and readString.indexOf('&') to locate the position of the 240 in the captured String, then use readString.substring(x, y) to capture the 240 characters, then convert those characters into an integer for PWM use.

GET /?LED=240&submit=Change+Pin+5%21 HTTP/1.1

I tried but not getting result. :slightly_frowning_face: can you give me the complete code.

I tried but not getting result. :slightly_frowning_face: can you give me the complete code.

I don't have time now, but the below shows the concept of parsing desired data out of a captured String.

//zoomkat 03-28-15 simple parse test
//send "GET /?LED=240&submit=Change+Pin+5%21 HTTP/1.1"
//from serial monitor
//and result returned to serial monitor

String readString, newString;

void setup() {
  Serial.begin(9600);
  Serial.println("parse test"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the String readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }
  if (readString.length() >0) {
    Serial.println(readString); //prints string to serial port out
    int pos1 = readString.indexOf('=');
    int pos2 = readString.indexOf('&');
    newString = readString.substring(pos1+1, pos2);
    Serial.print("newString is: ");
    Serial.println(newString);
    int val = newString.toInt();
    Serial.print("The value sent is: ");
    Serial.println(val);
    readString=""; //clears variable for new input    
    newString=""; //clears variable for new input
  } 
}

Please help me. I want to control servo from web server(localhost), the arduino and ethernet as a client.

#include <Servo.h>

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

// this must be unique
byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// change to your network settings
IPAddress ip(192,168,1,177);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);

// change to your server
IPAddress server(192,168,1,178); // Google

char serverName[] = "192,168,1,178";

// change to your server's port
int serverPort = 80;

EthernetClient client;
int totalCount = 0;
char pageAdd[64];
Servo microservo;
int pos = 0;
String readString;
// set this to the number of milliseconds delay
// this is 30 seconds
#define delayMillis 30000UL

unsigned long thisMillis = 0;
unsigned long lastMillis = 0;


void setup() {
 Serial.begin(9600);

 // disable SD SPI
 pinMode(4,OUTPUT);
 digitalWrite(4,HIGH);

 // Start ethernet
 Serial.println(F("Starting ethernet..."));
 Ethernet.begin(mac, ip, gateway, gateway, subnet);

 // If using dhcp, comment out the line above 
 // and uncomment the next 2 lines

 // if(!Ethernet.begin(mac)) Serial.println(F("failed"));
 // else Serial.println(F("ok"));

 Serial.println(Ethernet.localIP());

 delay(2000);
 Serial.println(F("Ready"));
}

void loop()
{
 microservo.write(90);
 delay(10);
  microservo.write(pos);
 thisMillis = millis();

 if(thisMillis - lastMillis > delayMillis)
 {
   lastMillis = thisMillis;

   // Modify next line to load different page
   // or pass values to server
   sprintf(pageAdd,"/",totalCount);

   // sprintf(pageAdd,"/arduino.php?test=%u",totalCount);

   if(!getPage(server,serverPort,pageAdd)) Serial.print(F("Fail "));
   else Serial.print(F("Pass "));
   totalCount++;
   Serial.println(totalCount,DEC);
 }    
}

byte getPage(IPAddress ipBuf,int thisPort, char *page)
{
 int inChar;
 char outBuf[128];

 Serial.print(F("connecting..."));

 if(client.connect(ipBuf,thisPort) == 1)
 {
   Serial.println(F("connected"));

   sprintf(outBuf,"GET %s HTTP/1.1", "/PhpProject1/index.php" );
   client.println(outBuf);
   sprintf(outBuf,"Host: %s"," localhost");
   client.println(outBuf);
   client.println(F("Connection: close\r\n"));
 } 
 else
 {
   Serial.println(F("failed"));
   return 0;
 }

 // connectLoop controls the hardware fail timeout
 int connectLoop = 0;

 while(client.connected())
 {
   while(client.available())
   {
     inChar = client.read();
     Serial.write(inChar);
     // set connectLoop to zero if a packet arrives
     connectLoop = 0;
         }

   }

   connectLoop++;

   // if more than 10000 milliseconds since the last packet
   if(connectLoop > 10000)
   {
     // then close the connection from this end.
     Serial.println();
     Serial.println(F("Timeout"));
     client.stop();

   // this is a delay for the connectLoop timing
   delay(1);
 }

 Serial.println();

 Serial.println(F("disconnecting."));
 // close client end
 client.stop();

 return 1;
}