Pages: [1]   Go Down
Author Topic: Have finally fizzled out working with ethernet...in need of some help  (Read 625 times)
0 Members and 1 Guest are viewing this topic.
East Texas
Offline Offline
Newbie
*
Karma: 0
Posts: 14
It doesn't take a genius to break anything!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

My dad and I are trying to automate the shop using the arduino Uno and the ethernet shield. I've been trying for two days now to figure out why I can't get a reply from "http://.../?firmver" when we request it from the arduino.

Most of the commands won't need to return a value back to the requesting browser, but the firmver and similar commands have places in our web interface that they need to return data. Can someone help and enlighten me as to why this isn't working when "/?firmver" is requested? I'm really new to using the ethernet networking with arduino, but have a few things under my belt, but this isn't quiet making sense to me. smiley-sad-blue

Code:
Code:
//base code from LED ethernet sketch

//modified code by BleedinSarge, customized for ShopMatic application
//NOTE: the web interface is hosted elsewhere due to flash size limits.
#include <SPI.h>
#include <Ethernet.h>
 
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 177 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(80); //http server
 
String httpRequest;
 
//string used to parse command from a browser
 
void setup(){
 
  //am limited on IO due to the network card using 4, 10, 11, 12, and 13
 
  pinMode(2, OUTPUT); //this pin controls central air
  pinMode(3, OUTPUT); //this pin controls outside lights
  pinMode(5, OUTPUT); //this pin controls inside lights
  pinMode(6, OUTPUT); //this pin controls black lights
  pinMode(7, OUTPUT); //not used yet, goes to mood lights...speaking of, one needs to buy more halogen bulbs...
  pinMode(8, OUTPUT); //not used for now
  pinMode(9, OUTPUT); //not used for now
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
  //the pin for the servo co
  //enable serial data print
  Serial.begin(9600);
  Serial.println("ShopMatic testing firmware");
  //need to know which arduino im using, since two are normally connected to my mech
}
 
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 (httpRequest.length() < 80) {
 
        }
 
        //if HTTP request has ended
        if (c == '\n') {
 
          ///////////////
          Serial.println(httpRequest); //print to serial monitor for debuging
 
          client.println("HTTP/1.1 200 OK"); //send new page, i keep this page blank for security reasons...no info, boring target to mess with
          client.println("Content-Type: text/html");
          client.println();
          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<TITLE></TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");
          client.println("<hr />");
          client.println("<br />");
          client.println("</BODY>");
          client.println("</HTML>");
 
          delay(2);
          client.stop();
          //give time for receiving..not like im sending alot

 
          ///////////////////// control arduino pin
          if(httpRequest.indexOf("?lighton") >0)//turn on outside lights
          {
            digitalWrite(3, HIGH);    //9, stand!
            httpRequest="";
          }
          else{
          if(httpRequest.indexOf("?lightoff") >0)//turn off outside lights
          {
            digitalWrite(3, LOW);    //9, duck!
            httpRequest="";
          }
          else{
          if(httpRequest.indexOf("?firmver") >0)//did we get a firmware version request?
          {
            client.connected(); //client gotta connect to get data, right?
          client.println("HTTP/1.1 200 OK"); //send new page, i keep this page blank for security reasons...no info, boring target to mess with
          client.println("Content-Type: text/html");
          client.println();
          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<TITLE></TITLE>");
          client.println("<h2> 0.1.0 </h2>");
          client.println("</HEAD>");
          client.println("<BODY>");
          client.println("<hr />");
          client.println("<br />");
          client.println("</BODY>");
          client.println("</HTML>");
            client.stop();
            httpRequest=""; //make sure to empty this string
          }
          }
          //clearing string for next read
          httpRequest="";

 
        }
      }
    }
  }
}
}
Logged

"Hold my rootbeer and watch this" - Charlie and I

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

Code:
        //read char by char HTTP request
        if (httpRequest.length() < 80) {
 
        }
If the length of the request is less than 80, do nothing. Otherwise do nothing. OK, I see why the length matters. Not.

Where does httpRequest ever get assigned a value other than ""?
Logged

East Texas
Offline Offline
Newbie
*
Karma: 0
Posts: 14
It doesn't take a genius to break anything!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
        //read char by char HTTP request
        if (httpRequest.length() < 80) {
 
        }
If the length of the request is less than 80, do nothing. Otherwise do nothing. OK, I see why the length matters. Not.

Where does httpRequest ever get assigned a value other than ""?

I set httpRequest back to nothing just making sure it is empty for the next request, but I guess that really doesn't matter....

I'm not sure what the length limit is for, the code I posted is a modified sketch from: http://pastebin.com/CZ6J62qJ for controlling an LED via ethernet. I'm trying to modify it to work for my project, and I'm finding a few rough edges along the way.
Logged

"Hold my rootbeer and watch this" - Charlie and I

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

Setting httpRequest back to an empty string at the end is a good thing. Not assigning anything to it anywhere else is not.

In the body of that if statement, there is supposed to be a line that looks like:
Code:
httpRequest += c;
where the character just read is appended onto the string.
Logged

East Texas
Offline Offline
Newbie
*
Karma: 0
Posts: 14
It doesn't take a genius to break anything!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

There was a serial function showing the httpRequest, but for some reason it was using c as the string to print, just something I forgot to strip out.

So only set httpRequest to empty at the end of the code, right? I can understand that, but why is "http://myIP/?firmver" not returning 0.1.0 like it should, that part I'm not understanding...
Logged

"Hold my rootbeer and watch this" - Charlie and I

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

Quote
but why is "http://myIP/?firmver" not returning 0.1.0 like it should, that part I'm not understanding...
Because you are asking if httpRequest ("") contains ?firmver. It doesn't, because httpRequest only contains nothing.

The code you removed did more than just print c. It also appended c onto the end of httpRequest, so that httpRequest contained "", "h", "ht", "htt", "http", ... , "http://yourIP/?firmver".
At some point, then, httpRequest did contain ?firmver.

As it is now, it never does.
Logged

East Texas
Offline Offline
Newbie
*
Karma: 0
Posts: 14
It doesn't take a genius to break anything!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

hmm, give me a bit to wrap my head around this, if i only clear that at the end of the loop, then it would contain the command, given that I possibly reconstruct the appended c part. Alright, given that, why does the LED toggle still?

I'm pretty new to this stuff, but I'm trying, still a few things I don't quiet have a grip on.
Logged

"Hold my rootbeer and watch this" - Charlie and I

0
Offline Offline
Tesla Member
***
Karma: 147
Posts: 9750
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Server test code that you can try.

Code:
//zoomkat 12-8-11
//simple button GET with iframe 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
//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, subnet);
  server.begin();

  myservo.write(0); //set initial servo position if desired
  myservo.attach(7);  //the pin for the servo co
  //enable serial data print
  Serial.begin(9600);
  Serial.println("server LED 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

          //now output HTML data header
             if(readString.indexOf('?') >=0) { //don't send new page
               client.println("HTTP/1.1 204 Zoomkat");
               client.println();
               client.println(); 
             }
             else {
          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\" target=\"inlineframe\">ON</a>");
          client.println("<a href=\"/?off\" target=\"inlineframe\">OFF</a>");

          //client.println("<IFRAME name=inlineframe src=\"res://D:/WINDOWS/dnserror.htm\" width=1 height=1\">");
          client.println("<IFRAME name=inlineframe style=\"display:none\" >");         
          client.println("</IFRAME>");

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

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

OP: Your code is printing stuff. How about showing us that output?
Logged

Pages: [1]   Go Up
Jump to: