Pages: [1]   Go Down
Author Topic: WEB SERVER WITH HTML AND ARDUINO  (Read 362 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 122
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 236
Posts: 24267
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Don't SHOUT.

Where is your code?
Logged

"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.

Offline Offline
Full Member
***
Karma: 0
Posts: 122
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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.
« Last Edit: March 13, 2013, 08:12:04 pm by Coding Badly » Logged

UK
Offline Offline
Shannon Member
****
Karma: 183
Posts: 11114
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

0
Offline Offline
Tesla Member
***
Karma: 114
Posts: 8894
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Some simple server test code.

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45935
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
String cad=String(100);
This is equivalent to
Code:
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.
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 122
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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.
Logged

Pages: [1]   Go Up
Jump to: