Go Down

Topic: Have finally fizzled out working with ethernet...in need of some help (Read 649 times) previous topic - next topic

timberwolf9

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: [Select]

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


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

PaulS

Code: [Select]
        //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 ""?

timberwolf9


Code: [Select]
        //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.
"Hold my rootbeer and watch this" - Charlie and I

PaulS

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: [Select]
httpRequest += c;
where the character just read is appended onto the string.

timberwolf9

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...
"Hold my rootbeer and watch this" - Charlie and I

PaulS

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.

timberwolf9

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.
"Hold my rootbeer and watch this" - Charlie and I

zoomkat

Server test code that you can try.

Code: [Select]

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

        }
      }
    }
  }
}

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

PaulS

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

Go Up