Ethernet shield and a led...

Hello guys. As you know i am new in world of arduino.
So my next idea (after i try the light senson example with success) is that, i want to make that example with ethernet shield...
i want the day the led Off and the night On and when is off in my webserver to say "LOW" and when is night the webserver to say "ON" here is my code but i dont know what i am doing wrong cause the led is all the time ON

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,177);

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);

void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);

pinMode(13, OUTPUT);

while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}

// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}

void loop() {
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the response
client.println("Refresh: 5"); // refresh the page automatically every 5 sec
client.println();
client.println("");
client.println("");
//

int lightPin = 0; //define a pin for Photo resistor
int threshold = 50;

client.println(analogRead(lightPin));

if(analogRead(lightPin) > threshold ){
digitalWrite(13, HIGH);
client.println("high");
}
else{
digitalWrite(13, LOW);
client.println("low");
}

delay(100);

client.println("
");
}
client.println("");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("client disonnected");
}
}

any idea ? :slight_smile:
thanks

If your Arduino is an Uno, the led is on D13, and it is also the clock line for the SPI. The ethernet shield uses the SPI, so you can't use D13 for the led.

so the solution is to change the pin ?

The LED's pin, yep, which means of course an external LED with a resistor, since only D13 has the on-board LED.

yes i know i use 10k ohm resistor but the problem is the same... the led is open always....is my code right/?

tsakaritas:
yes i know i use 10k ohm resistor but the problem is the same... the led is open always....is my code right/?

Not unless you changed the pin number...

 pinMode(13, OUTPUT);

 if(analogRead(lightPin) > threshold ){    
    digitalWrite(13, HIGH);
    client.println("high"); 
  }
  else{
    digitalWrite(13, LOW);
    client.println("low"); 
  }

..... those lines all refer to pin 13.

But what does "open" mean?

And not 10k.... about 200-300 ohms in series with an LED on a 5V supply . 10k will give about half a milliAmp which is just about nothing.

open means is all the time lighting....
when is day is lighting....
when is dark is lighting....

it must close in day and lighting at night.

btw i change 13 to 10

With a 10k resistor I'm surprised you can see that it's on....

i still have 10k and is on :slight_smile: so my code now is

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,177);

// Initialize the Ethernet server library
// with the IP address and port you want to use 
// (port 80 is default for HTTP):
EthernetServer server(80);

void setup() {
  
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
  pinMode(10, OUTPUT);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}


void loop() {
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");  // the connection will be closed after completion of the response
	  client.println("Refresh: 5");  // refresh the page automatically every 5 sec
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          // output the value of each analog input pin
          
          int lightPin = 0;  //define a pin for Photo resistor
int threshold = 50;
          client.println(analogRead(lightPin)); 

  if(analogRead(lightPin) > threshold ){    
    digitalWrite(10, HIGH);
    client.println("high"); 
  }
  else{
    digitalWrite(10, LOW);
    client.println("low"); 
  }

  delay(100);
   
          client.println("</html>");
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        } 
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
    Serial.println("client disonnected");
  }
}

but the led is all the time lighting... :frowning: nd in my web server it says 5low, 6low after a minute i say said 57 high 60 high what is this ? :smiley:

and my connections in breadboard are....

from pin 10 -> resistor -> +led and from -led goes to ground
+5v -> resistor --> light sensor ---> ground
^
|
A0

Please put your code in tags... use the # above the :wink: and :sweat_smile:, so that

it looks like this

It's so difficult to read other wise.

What do you mean, the webserver says what? Maybe post a screen dump.

but i fix it cause
i had

client.println(analogRead(lightPin));
and make it Serial.println(analogRead(lightPin)); and know it says only HIGH

So you went from using the SPI clock (D13) to using the SPI default slave select pin (D10)? You can't use D10 to D13 for anything but the SPI bus on an Uno. Pick another pin, like D9.

Annnnnnnnnndddddddddddd yes it works :slight_smile: thank you all guys :wink: