Pages: [1]   Go Down
Author Topic: Need internet help  (Read 718 times)
0 Members and 1 Guest are viewing this topic.
Buena Vista, CO
Offline Offline
Full Member
***
Karma: 0
Posts: 180
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm trying to set up a webserver which accepts comands fron the internet. I used
as an example the program at
http://www.charmcitynetworks.com/techblog/web-controlled-window-air-conditioner-using-and-arduino/
my code:
Code:
#include <[color=#CC6600]SPI[/color].h>
#include <[color=#CC6600]Ethernet[/color].h>
#define BUFSIZ 100
[color=#CC6600]byte[/color] mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
[color=#CC6600]byte[/color] ip[] = {192,168,0,14};
[color=#CC6600]Server[/color] server(80);
[color=#CC6600]void[/color] [color=#CC6600][b]setup[/b][/color]()
{
  [color=#CC6600]Ethernet[/color].[color=#CC6600]begin[/color](mac, ip);
  server.[color=#CC6600]begin[/color]();
  [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]begin[/color](9600);
}
[color=#CC6600]void[/color] [color=#CC6600][b]loop[/b][/color]()
{
  [color=#CC6600]char[/color] clientline[BUFSIZ];
  [color=#CC6600]char[/color] c;
  [color=#CC6600]int[/color] index = 0;
  [color=#CC6600]int[/color] stat = 0;
  [color=#7E7E7E]// listen for incoming clients[/color]
  [color=#CC6600]Client[/color] client = server.[color=#CC6600]available[/color]();
  [color=#CC6600]if[/color] (client)
  {
    [color=#CC6600]boolean[/color] currentLineIsBlank = [color=#CC6600]true[/color];
    [color=#CC6600]while[/color] (client.[color=#CC6600]connected[/color]())
    {
      [color=#CC6600]if[/color] (client.[color=#CC6600]available[/color]())
      {
        [color=#CC6600]char[/color] c = client.[color=#CC6600]read[/color]();
        [color=#CC6600]if[/color] (c != [color=#006699]'\n'[/color] && c != [color=#006699]'\r'[/color]) {
          clientline[index] = c;
          index++;
          [color=#CC6600]if[/color] (index >= BUFSIZ)
            index = BUFSIZ -1;
          [color=#CC6600]continue[/color];
        }
        clientline[index] = 0;
        [color=#CC6600]if[/color] (strstr(clientline, [color=#006699]"GET / "[/color]) != 0)
        {
          [color=#7E7E7E]// send a standard http response header[/color]
          client.[color=#CC6600]println[/color]([color=#006699]"HTTP/1.1 200 OK"[/color]);
          client.[color=#CC6600]println[/color]([color=#006699]"Content-Type: text/html"[/color]);
          client.[color=#CC6600]println[/color]();
          client.[color=#CC6600]println[/color]([color=#006699]"<html>"[/color]);
          client.[color=#CC6600]println[/color]([color=#006699]"<body>"[/color]);
          client.[color=#CC6600]print[/color]([color=#006699]"<FORM action=\"http://192.168.0.14/\" >"[/color]);
          client.[color=#CC6600]print[/color]([color=#006699]"<INPUT type=\"hidden\" name=\"status1\" value=\"1\">"[/color]);
          client.[color=#CC6600]print[/color]([color=#006699]"<INPUT type=\"submit\" value=\"Led On\"> </FORM>"[/color]);
          client.[color=#CC6600]print[/color]([color=#006699]"<FORM action=\"http://192.168.0.14/\" >"[/color]);
          client.[color=#CC6600]print[/color]([color=#006699]"<INPUT type=\"hidden\" name=\"status0\" value=\"0\">"[/color]);
          client.[color=#CC6600]print[/color]([color=#006699]"<INPUT type=\"submit\" value=\"Led Off\"> </FORM>"[/color]);
          [color=#CC6600]break[/color];
        }         
        [color=#CC6600]else[/color] [color=#CC6600]if[/color] (strstr(clientline, [color=#006699]"status0"[/color]) != 0)
        {
         [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color](clientline);
         doZero;
          [color=#CC6600]break[/color];
        }
        [color=#CC6600]else[/color] [color=#CC6600]if[/color] (strstr(clientline, [color=#006699]"status1"[/color]) != 0)
        {
         [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color](clientline);
          doOne;
          [color=#CC6600]break[/color];
        }
         [color=#CC6600]else[/color]
        {
          client.[color=#CC6600]println[/color]([color=#006699]"HTTP/1.1 404 Not Found"[/color]);
          client.[color=#CC6600]println[/color]([color=#006699]"Content-Type: text/html"[/color]);
          client.[color=#CC6600]println[/color]();
        }
      }
    }
    [color=#CC6600]delay[/color](1);
    client.[color=#CC6600]stop[/color]();
  }
}
[color=#CC6600]void[/color] doZero()
{
}
[color=#CC6600]void[/color] doOne()
{
}

it works somewhat, using my test program shows two buttons. The Serial monitor shows that clicking
the on button gives:
GET /?status1=1 HTTP/1.1
and clicking the off button gives:
GET /?status1=0 HTTP/1.1
The problem is that the browser then gives error message
The connection was reset
and browser address line is
http://192.168.0.14/?status1=1  or 0
what I need is to reload 192.168.0.14, how do I do that?
Jim
Logged

8000ft above the average

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

I'm nearly positive that your code doesn't really look like that. Try again.
Logged

Buena Vista, CO
Offline Offline
Full Member
***
Karma: 0
Posts: 180
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm nearly positive that your code doesn't really look like that. Try again.
Sorry, I used "copy for forum" and that is what I got. Try this
Code:
#include <SPI.h>
#include <Ethernet.h>
#define BUFSIZ 100
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = {192,168,0,14};
Server server(80);
void setup()
{
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.begin(9600);
}
void loop()
{
  char clientline[BUFSIZ];
  char c;
  int index = 0;
  int stat = 0;
  // listen for incoming clients
  Client client = server.available();
  if (client)
  {
    boolean currentLineIsBlank = true;
    while (client.connected())
    {
      if (client.available())
      {
        char c = client.read();
        if (c != '\n' && c != '\r') {
          clientline[index] = c;
          index++;
          if (index >= BUFSIZ)
            index = BUFSIZ -1;
          continue;
        }
        clientline[index] = 0;
        if (strstr(clientline, "GET / ") != 0)
        {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          client.println("<html>");
          client.println("<body>");
          client.print("<FORM action=\"http://192.168.0.14/\" >");
          client.print("<INPUT type=\"hidden\" name=\"status1\" value=\"1\">");
          client.print("<INPUT type=\"submit\" value=\"Led On\"> </FORM>");
          client.print("<FORM action=\"http://192.168.0.14/\" >");
          client.print("<INPUT type=\"hidden\" name=\"status0\" value=\"0\">");
          client.print("<INPUT type=\"submit\" value=\"Led Off\"> </FORM>");
          break;
        }
        else if (strstr(clientline, "status0") != 0)
        {
         Serial.print(clientline);
         doZero;
         client.print ("<a href=\"http://192.168.0.14\" ></a>");
          break;
        }
        else if (strstr(clientline, "status1") != 0)
        {
         Serial.print(clientline);
          doOne;
          break;
        }
         else
        {
          client.println("HTTP/1.1 404 Not Found");
          client.println("Content-Type: text/html");
          client.println();
        }
      }
    }
    delay(1);
    client.stop();
  }
}
void doZero()
{
}
void doOne()
{
}

Jim
Logged

8000ft above the average

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

Code:
         doZero;
          doOne;
These are not function calls. They do nothing.

Code:
         client.print ("<a href=\"http://192.168.0.14\" ></a>");
What's this doing?

Code:
          client.println("<html>");
          client.println("<body>");
There are no matching close tags.

Logged

Buena Vista, CO
Offline Offline
Full Member
***
Karma: 0
Posts: 180
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
         doZero;
          doOne;
These are not function calls. They do nothing.
They were stubs of code to be added
Code:
They were stubs of code to be added
        client.print ("<a href=\"http://192.168.0.14\" ></a>");
What's this doing?
a attempt o force page to refresh that I forgot to delete
Code:
a attempt o force page to refresh that I forgot to delete          client.println("<html>");
          client.println("<body>");
There are no matching close tags.
I wondered about that, they are not in the sample code either. He said that it worked on the web page


Thanks for looking! Jim
Logged

8000ft above the average

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

Simple server control 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 4 high
            Serial.println("Led On");
          }
          if(readString.indexOf("off") >0)//checks for off
          {
            myservo.write(140);
            digitalWrite(5, LOW);    // set pin 4 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

Buena Vista, CO
Offline Offline
Full Member
***
Karma: 0
Posts: 180
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks, zoomkat I'm studying that!

Jim
Logged

8000ft above the average

Pages: [1]   Go Up
Jump to: