Go Down

Topic: WEB SERVER WITH HTML AND ARDUINO (Read 459 times) previous topic - next topic

EJTR

HI
I AM NEW IN ARDUINO AND I AM TRYING TO DEVELOP A PROJECT WITH ARDUINO  ETHERNET BOARD. IT CONSISTS TO DESIGNED WEB IN HTML AND CONTROLLING A LED BUT i DO NOT KNOW WHY IT WORKS GOOD A FEW MINUTES AND  AFTER THAT THE CONNECTION IS BROKEN AND IT IS NOT SERVING UP MORE AND THE CONNECTION IS NOT RESTORE , SO I HAVE TO RESET MY MODEM OR MY PC.

EXCUSE ME MY ENGLISH IS NOT GOOD AND THANKS FOR YOUR ADVICE

AWOL

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

EJTR

#2
Mar 14, 2013, 01:03 am Last Edit: Mar 14, 2013, 02:12 am by Coding Badly Reason: 1
thanks so much  for help me and I am sorry about shout.please help me that is very important for me because that is a project of my university.Only woks a few minutes , after that the conection is broken and do not conect again.


Code: [Select]
#include <SPI.h>
#include <Ethernet.h>

byte mac[] = {0x90, 0xA2, 0xDA, 0x0D, 0x5C, 0x16};
IPAddress ip(192,168,1,5);
byte gateway  [] = {192,168,1, 1};
byte subred  [] = {255, 255, 255, 0};
int led = 7;
EthernetServer server(80);
String cad=String(100);

const char html[] =
//"HTTP/1.1 200 OK"
//"Content-Type: text/html"
"<!DOCTYPE HTML>"
"<html lang='es'>"
"<head><title> ARDUINO </title></head>"
"<body bgcolor='#CCCCCC' text='#FFFFFF' marginheight='125' marginwidth='125' link='#0033CC' vlink='#0033CC' alink='#0033CC'>"
"<center> <font face='Times New Roman, Times, serif' color='#0033CC' size='45'> ARDUINO </center>"
"<center> <font face='Times New Roman, Times, serif' color='#0033CC' size='5'><a href='http://www.arduino.cc'>arduino.cc </a> </center>"
"<br><br>"
"<form method=get><input type=hidden name=apagar value=1><input type=submit value=LED_OFF></form>"
"<form method=get><input type=hidden name=encender value=1><input type=submit value=LED_ON></form>"
"</body></html>";

void setup()
{
  pinMode(led, OUTPUT);
  Ethernet.begin(mac,ip,gateway,subred);
  server.begin();
}

void loop()

  cad="";
  EthernetClient client = server.available();
  if (client) {
    server.print(html);
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        cad=cad+c;
        if (c == '\n')
        {
          if ( cad.lastIndexOf("encender") > -1)
          {
          digitalWrite(led,HIGH);
          }
          client.stop();
          if ( cad.lastIndexOf("apagar") > -1)
          {
          digitalWrite(led,LOW);           
          }                                     
          client.stop();
        }
      }
    }
  }
}



Moderator edit: [code] [/code] tags added.

PeterH


Only woks a few minutes , after that the conection is broken and do not conect again.


Code should be posted inside [ code ] [ /code ] tags to stop the forum software from mangling it. You can go back and edit your post to add these.

I don't know what's causing your problem but I see you're using the String class, which has a known bug that can lead to memory corruption resulting in just this sort of problem. I suggest you get rid of that as the first step. It may cure your problem, but if not then at least you won't have to worry about that biting you in future. The things you're current doing with String can be done just as easily using plain old c-strings (null terminated char arrays). For example, the strstr() function will tell you whether a string contains another string, and strcmp() can be used to find whether two strings are identical. You should have no trouble finding examples of reading characters from a stream and buffering them in a char array.
I only provide help via the forum - please do not contact me for private consultancy.

zoomkat

Some simple server test code.

Code: [Select]

//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="";

        }
      }
    }
  }
}

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

PaulS

Code: [Select]
String cad=String(100);
This is equivalent to
Code: [Select]
String cad = "100";
I fail to see how that is a useful initial value.

What version of the IDE are you using? Anything prior to 1.0.4 has a bug in the free() method that is used to delete String objects when they go out of scope, causing memory corruption and strange crashes.

EJTR


I am using the 1.0.3 version I am going to change of version . About the string I found an example on the internet and I am trying to understand the code .

do you now how I can do to take the strings from web page and save on de SD after that I can  read  and print on the web page , some like the report.

Go Up