Pages: [1]   Go Down
Author Topic: Turn on/off pin with Arduino netshield (arduino Coockbook)  (Read 494 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello All,

Thank you for reading this.

I would appreciate a bit of time for some help. I am at entry level. Got the book "Arduino Coockbook" and trying to implement the project 15.10 pg 525. The page is displayed correctly, and i can see some feedback when you push the button. To help out i have put some debugging trace in my code.
From what I can see the "POST" string is not found hence the if statement is not entered...

Please find below the Arduino Code, the monitor output, and the html source code from the browser

Code:

/*
* WebServerPost sketch
 * Turns pin 8 on and off using HTML form
 */
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x98, 0xA2 };
byte ip[] = { 192,168,2,16 };
const int MAX_PAGENAME_LEN = 15; // max characters in page name
char buffer[MAX_PAGENAME_LEN+1]; // additional character for terminating null
EthernetServer server(80);
void setup()
{
  Serial.begin(9600);
  Ethernet.begin(mac, ip);
  server.begin();
  delay(2000);
}
void loop()
{
  EthernetClient client = server.available();
  if (client) {
    int type = 0;
    while (client.connected()) {
      Serial.println("Debug -> Client Connected");
      if (client.available()) {
        Serial.println("Debug -> Available Starting Now...");
        // GET, POST, or HEAD
      //    memset(buffer,0, sizeof(buffer)); // clear the buffer
        if(client.find("/"))
         if(client.readBytesUntil('/', buffer,sizeof(buffer))){
         
            Serial.println("Debug -> Dumping Buffer XX");
            Serial.println(buffer);
           
             
         
            if(strcmp(buffer,"POST") == 1){
              Serial.println("Debug -> Send Hanlder 1");
              client.find("\n\r");
              Serial.println("Debug -> Inside Find");
             
              // skip to the body
             
              // find string starting with "pin", stop on first blank line
              // the POST parameters expected in the form pinDx=Y
              // where x is the pin number and Y is 0 for LOW and 1 for HIGH
              while(client.findUntil("pinD", "\n\r")){
                Serial.println("Debug -> Parsing");
                int pin = client.parseInt(); // the pin number
                int val = client.parseInt(); // 0 or 1
                pinMode(pin, OUTPUT);
                digitalWrite(pin, val);
                Serial.println("Debug -> PIN ");
                Serial.println(pin);
                Serial.println(" ");
                Serial.println(val);
              }

            }
            sendHeader(client,"Post example");
            Serial.println("Debug -> Post example");
            //create HTML button to control pin 8
            client.println("<h2>Click buttons to turn pin 8 on or off</h2>");
            client.print("<form action='/' method='POST'><p><input type='hidden' name='pinD8'");
            client.println("value='0'><input type='submit' value='Off'/></form>");
            //create HTML button to turn on pin 8
            client.print("<form action='/' method='POST'><p><input type='hidden' name='pinD8'");
            client.print("value='1'><input type='submit' value='On'/></form>");
            client.println("</body></html>");
            client.stop();
            Serial.println("Debug -> Client Stop");
          }
        break;
      }
    }
    // give the web browser time to receive the data
    delay(100);
    client.stop();
  }
}
void sendHeader(EthernetClient client, char *title)
{
  // send a standard http response header
  client.println("HTTP/1.1 200 OK");
  client.println("Dino");
  client.println("Dino Content-Type: text/html");
  client.println();
  client.print("<html><head><title>");
  client.print(title);
  Serial.println("Debug -> title");
  Serial.println(title);
  client.println("</title><body>");
}

Monitor Output:

Debug -> Client Connected
Debug -> Available Starting Now...
Debug -> Dumping Buffer XX
 HTTP
Debug -> title
Post example
Debug -> Post example
Debug -> Client Stop
Debug -> Client Connected
Debug -> Available Starting Now...
Debug -> Dumping Buffer XX
 HTTP
Debug -> title
Post example
Debug -> Post example
Debug -> Client Stop
Debug -> Client Connected
Debug -> Available Starting Now...
Debug -> Dumping Buffer XX
 HTTP
Debug -> title
Post example
Debug -> Post example
Debug -> Client Stop
Debug -> Client Connected
Debug -> Available Starting Now...

HTML CODE:

<html><head><title>Post example</title><body>
<h2>Click buttons to turn pin 8 on or off</h2>
<form action='/' method='POST'><p><input type='hidden' name='pinD8'value='0'><input type='submit' value='Off'/></form>
<form action='/' method='POST'><p><input type='hidden' name='pinD8'value='1'><input type='submit' value='On'/></form></body></html>




Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 139
Posts: 5857
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You might want to change this and the others like it:
Code:
client.find("\n\r");
Normally, it is sent the other way around ("\r\n"), but I would search for only one of those ("\n"), and disregard the other ("\r").
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you SurferTim. I will try it tonight and let you know.

Cheers,

lmcdima
Logged

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Simple server code to turn a pin on/off from a web page.

Code:
//zoomkat 4-1-12
//simple button GET for servo and pin 5
//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 ' instead of "
//address will look like http://192.168.1.102:84 when submited
//for use with W5100 based ethernet shields

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

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

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();

  myservo.write(90); //set initial servo position if desired
  myservo.attach(7);  //the pin for the servo control
  //enable serial data print
  Serial.begin(9600);
  Serial.println("server servo/pin 5 test 1.0"); // 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); //print to serial monitor for debuging

          client.println("HTTP/1.1 200 OK"); //send new page
          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>Zoomkat's simple Arduino button</H1>");
         
          client.println("<a href=\"/?on\">ON</a>");
          client.println("<a href=\"/?off\">OFF</a>");

          client.println("</BODY>");
          client.println("</HTML>");
 
          delay(1);
          //stopping client
          client.stop();

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

        }
      }
    }
  }
}

Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Guys,

The trick of modifying the client.find did not work... I'm learning wire shark tracing to see exactly explorer is sending ...but...i have modified the code by removing the "post" check and it works...

Zoomkat, thank your your reply. I will try your code later today and let you know. at first glance it looks way less cumbersom than the cookbook one....

Code is below...


/*
* WebServerPost sketch
 * Turns pin 8 on and off using HTML form
 */
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x98, 0xA2 };
byte ip[] = { 192,168,2,16 };
const int MAX_PAGENAME_LEN = 15; // max characters in page name
char buffer[MAX_PAGENAME_LEN+1]; // additional character for terminating null
EthernetServer server(80);
void setup()
{
  Serial.begin(9600);
  Ethernet.begin(mac, ip);
  server.begin();
  delay(2000);
}
void loop()
{
  EthernetClient client = server.available();
  if (client) {
    int type = 0;
    while (client.connected()) {
      Serial.println("Debug -> Client Connected");
      if (client.available()) {
        Serial.println("Debug -> Available Starting Now...");
        // GET, POST, or HEAD
      //    memset(buffer,0, sizeof(buffer)); // clear the buffer
        if(client.find("/"))
         if(client.readBytesUntil('/', buffer,sizeof(buffer))){
         
            Serial.println("Debug -> Dumping Buffer XX");
            Serial.println(buffer);
           
             
         
         //   if(strcmp(buffer,"POST ") == 0){
              Serial.println("Debug -> Send Hanlder 1");
              client.find("\n\r");
              Serial.println("Debug -> Inside Find");
             
              // skip to the body
             
              // find string starting with "pin", stop on first blank line
              // the POST parameters expected in the form pinDx=Y
              // where x is the pin number and Y is 0 for LOW and 1 for HIGH
              while(client.findUntil("pinD", "\n\r")){
                Serial.println("Debug -> Parsing");
                int pin = client.parseInt(); // the pin number
                int val = client.parseInt(); // 0 or 1
                pinMode(pin, OUTPUT);
                digitalWrite(pin, val);
                Serial.println("Debug -> PIN ");
                Serial.println(pin);
                Serial.println(" ");
                Serial.println(val);
              }
           // }
            sendHeader(client,"Post example");
            Serial.println("Debug -> Post example");
            //create HTML button to control pin 8
            client.println("<h2>Click buttons to turn pin 8 on or off</h2>");
            client.print("<form action='/' method='POST'><p><input type='hidden' name='pinD8'");
            client.println("value='0'><input type='submit' value='Off'/></form>");
            //create HTML button to turn on pin 8
            client.print("<form action='/' method='POST'><p><input type='hidden' name='pinD8'");
            client.print("value='1'><input type='submit' value='On'/></form>");
            client.println("</body></html>");
            client.stop();
            Serial.println("Debug -> Client Stop");
          }
        break;
      }
    }
    // give the web browser time to receive the data
    delay(100);
    client.stop();
  }
}
void sendHeader(EthernetClient client, char *title)
{
  // send a standard http response header
  client.println("HTTP/1.1 200 OK");
  client.println("Dino");
  client.println("Dino Content-Type: text/html");
  client.println();
  client.print("<html><head><title>");
  client.print(title);
  Serial.println("Debug -> title");
  Serial.println(title);
  client.println("</title><body>");
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Guys,

I am also attaching this so that when other people is in need of help can understand a bit more...

This is how a POST message looks like....


POST / HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Referer: http://192.168.2.16/
Accept-Language: en-CA
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
Host: 192.168.2.16
Content-Length: 7
Connection: Keep-Alive
Cache-Control: no-cache

pinD8=1HTTP/1.1 200 OK

Dino
Dino Content-Type: text/html

<html><head><title>Post example</title><body>
<h2>Click buttons to turn pin 8 on or off</h2>
<form action='/' method='POST'><p><input type='hidden' name='pinD8'value='0'><input type='submit' value='Off'/></form>
<form action='/' method='POST'><p><input type='hidden' name='pinD8'value='1'><input type='submit' value='On'/></form></body></html>
Logged

Pages: [1]   Go Up
Jump to: