auto refresh on browser

You've got the logic backwards. There is no need to read the PIR unless a client cares about it's state. The stuff in msg() belongs in loop(). The stuff in loop() should be in a function that loop() calls.

And, get rid of that delay().

still no good
this is the new code , and I have 2 problems with it :

  1. I don't want it to start working only when I enter the card - from what I have seen , just after I enter the IP the sensor begin to work.
  2. now I only get one message. the sensor is doing his work,and I can see that he is waiting 5 sec --> but only the movement massege is showing.

this is the code:

IPAddress ip(10,0,0,155); // ip in lan
EthernetServer server(80); //server is using port 80
int PIRChannel=4;
int GreenLED=9;
int RedLED=7;
int d;
void setup()
{
Ethernet.begin(mac, ip);
server.begin();
pinMode(GreenLED,OUTPUT);
pinMode(RedLED,OUTPUT);
pinMode(PIRChannel,INPUT);
}

void loop()
{
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
// see if HTTP request has ended with blank line
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();

//meta-refresh page every 1 seconds
client.println("");
client.print("");
client.print("<meta http-equiv="refresh" content="1">");
client.print(" Avivim-Test");
client.print("");
client.println("");
client.print("autorefresh test ");
client.println("
");

// printing the message
pir_cheak();
if (d=0)
{
client.print("All Good - No Movement");
client.print("
");
}
else
{
client.print("Warning - Movement");
client.print("
");
}

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();
}
}

void pir_cheak()
{

if (digitalRead(PIRChannel) ==HIGH) // no movement - Green LED
{
digitalWrite(RedLED, LOW);
digitalWrite(GreenLED,HIGH);
d=0;
}
else
{
digitalWrite(GreenLED,LOW); // Movement - Red LED
digitalWrite(RedLED,HIGH);
delay(5000);
}
}

      if (d=0)

Bzzzt. Try again.

but how he will know which massage to print?
there is no condition here.
what do I don't understand?

He meant change it, not remove it. The format is not correct. That is an assignment statement, not a compare. This would be correct:

if(d == 0)

Two equal signs, not one.

still no good.

yet another question - is it possible to make the two run together
I'm mean that the sensor will keep working even if nobody is connected to him?
because now - only after I'm connecting to server at ip 10.0.0.155 it's start working...

Thanks ,

We can't see what code you are running now. Remember to use the icon with the # symbol when posting code.

You are not setting the variable 'd' correctly.

void pir_cheak()
{
   
   if (digitalRead(PIRChannel) ==HIGH) // no movement - Green LED
          {
            digitalWrite(RedLED, LOW);
            digitalWrite(GreenLED,HIGH);
            // If you change d here...
            d=0;
                       }
          else
         {
             digitalWrite(GreenLED,LOW); // Movement - Red LED
            digitalWrite(RedLED,HIGH);
             // ...you need to change d here too.
             d=1;
             // I would do this delay somewhere else in the code, or remove it
             // it can't respond to the web server requests while in this delay
             delay(5000); 
         }
 }

Then move the call to pir_cheak() before the
// listen for incoming clients
remark.

add: I would also add another variable to track the status of the pir pin. If there is motion, set the variable to 1. Don't set that variable to zero until you load the webpage. That way you will know two things. If there is motion now (d), and if there was motion since the last download (your new variable).

try to do what you told me
but now it doesn't work at all
all I get is the Green led (also i have added sireal.print to see where is the problem - and I get junk.....)

this is the code

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(10,0,0,155); // ip in lan
EthernetServer server(80); //server is using port 80
int PIRChannel=4;
int GreenLED=9;
int RedLED=7;
int d;
void setup()
{
  Serial.begin(9600);
    Ethernet.begin(mac, ip);
  server.begin();
  pinMode(GreenLED,OUTPUT);
  pinMode(RedLED,OUTPUT);
  pinMode(PIRChannel,INPUT);
}

void loop()
{
  pir_cheak();
  while (d==1)
  {
    digitalWrite(GreenLED,LOW); // Movement - Red LED
            digitalWrite(RedLED,HIGH);
            Serial.println ("move!!!");
  }
  if (d==0)
  {
       digitalWrite(RedLED, LOW);
            digitalWrite(GreenLED,HIGH);
            Serial.println("No move");
  }
    
    
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    boolean currentLineIsBlank = true;
     while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        // see if HTTP request has ended with blank line
        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();
         
          //meta-refresh page every 1 seconds
           client.println("<HTML>");
          client.print("<HEAD>");
          client.print("<meta http-equiv=\"refresh\" content=\"1\">");
          client.print("<TITLE /> Test</title>");
          client.print("</head>");
          client.println("<BODY>");
          client.print("autorefresh test ");
          client.println("
");
                         
                 // printing the message
         if (d=0)
        {
          client.print("All Good - No  Movement");
         client.print("
");
        }
        else
        {
            client.print("Warning -   Movement");
         client.print("
");
        }
       
       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();
  }
}


int pir_cheak()
{
   
   if (digitalRead(PIRChannel) ==HIGH) // no movement - Green LED
          {
            digitalWrite(RedLED, LOW);
            digitalWrite(GreenLED,HIGH);
            d=0;
          //  Serial.println(d);
                       }
          else
         {
             digitalWrite(GreenLED,LOW); // Movement - Red LED
            digitalWrite(RedLED,HIGH);
            d=1;
                       
         }
 }
  pir_cheak();
  while (d==1)
  {

Inside the while loop, you never change d. If d ever were 1, you'd go into an infinite loop.

but now it doesn't work at all

By now, you'd think that you would have figured out that the code is doing something. You want it to do something. Presumably, "it doesn't work" means that those two somethings are not the same thing. So, if you ever get around to telling us what those two somethings are, then we can get to the point where the two somethings ARE the same thing.

pir_cheak() is defined to return an int. So, where is the necessary return statement? Why don't you expect, and use, the returned value?

Do you even know if you are reading the PIR data correctly?

O.K
this is way I'm asking here , I'm new at this and if I do something wrong I need someone tell that - so I'll learn for the next time.

All I want to do is simple thing
when the sensor is "on" - he will print out a message --> MOVE!!!! , red led will go on and green off
when the sensor is off - he will print out --> ALL GOOD!!!!! , green led will go on and red off

if I do this only with serial.print - it's work fine.

the problem begin when I want to use the card as a server.and start to write HTML code.
I hope now you understand.

this is my last code :

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(10,0,0,155); // ip in lan
EthernetServer server(80); //server is using port 80
int PIRChannel=4;
int GreenLED=7;
int RedLED=9
;
int d;
void setup()
{
  
  Serial.begin(9600);
    Ethernet.begin(mac, ip);
  server.begin();
  pinMode(GreenLED,OUTPUT);
  pinMode(RedLED,OUTPUT);
  pinMode(PIRChannel,INPUT);
}

void loop()
{
  pir_cheak();
  if (d==0)
  {
   digitalWrite(GreenLED,HIGH); // No Movement - green LED
            digitalWrite(RedLED,LOW);
           
  }
  else
  {
       digitalWrite(RedLED, HIGH);
            digitalWrite(GreenLED,LOW);
            
  }
    
    
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    boolean currentLineIsBlank = true;
     while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        // see if HTTP request has ended with blank line
        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();
         
          //meta-refresh page every 1 seconds
           client.println("<HTML>");
          client.print("<HEAD>");
          client.print("<meta http-equiv=\"refresh\" content=\"1\">");
          client.print("<TITLE /> Test</title>");
          client.print("</head>");
          client.println("<BODY>");
          client.print("autorefresh test ");
          client.println("
");
                         
                 // printing the message
         if (d=0)
        {
          client.print("All Good - No  Movement");
         client.print("
");
        }
        else
        {
            client.print("Warning -   Movement");
         client.print("
");
        }
       
       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();
  }
}


int pir_cheak()
{
   
   if (digitalRead(PIRChannel) ==HIGH) // no movement - Green LED
          {
            digitalWrite(RedLED, LOW);
            digitalWrite(GreenLED,HIGH);
             Serial.println ("no move!!!");
            d=0;
          //  Serial.println(d);
                       }
          else
         {
             digitalWrite(GreenLED,LOW); // Movement - Red LED
            digitalWrite(RedLED,HIGH);
             Serial.println ("Move!!!");
            d=1;
                       return d;
         }
 }

You have reinserted the previous error into your code.

                // printing the message
         // this is not correct
         if (d=0)
        {
          client.print("All Good - No  Movement");
         client.print("
");
        }
        else
        {
            client.print("Warning -   Movement");
         client.print("
");
        }

This is correct.

if(d == 0)

you are right , I have noticed it just now and fix it.

now it is almost working like I want to.
2 more things I want to know how to do

  1. I want the redLED will be on from 5 sec. where do I need to put the delay?
    now the led is not stable when there is a movement.
  2. I want that the error message will be written in the side - I need to get the RTC next week and I want to when ever there is a movement it will "save" me the time .

this is the code so far :

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(10,0,0,155); // ip in lan
EthernetServer server(80); //server is using port 80
int PIRChannel=4;
int GreenLED=7;
int RedLED=9
;
int d;
void setup()
{
  
  Serial.begin(9600);
    Ethernet.begin(mac, ip);
  server.begin();
  pinMode(GreenLED,OUTPUT);
  pinMode(RedLED,OUTPUT);
  pinMode(PIRChannel,INPUT);
}

void loop()
{
  pir_cheak();
  if (d==0)
  {
   digitalWrite(GreenLED,HIGH); // No Movement - green LED
            digitalWrite(RedLED,LOW);
    
           
  }
  else 
  {
       digitalWrite(RedLED, HIGH);
            digitalWrite(GreenLED,LOW);
            
  }
    
    
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    boolean currentLineIsBlank = true;
     while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        // see if HTTP request has ended with blank line
        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();
         
          //meta-refresh page every 1 seconds
           client.println("<HTML>");
          client.print("<HEAD>");
          client.print("<meta http-equiv=\"refresh\" content=\"1\">");
          client.print("<TITLE /> Test</title>");
          client.print("</head>");
          client.println("<BODY>");
          client.print("autorefresh test ");
          client.println("
");
                         
                 // printing the message
         if
         (d==0)
        {
          client.print("All Good - No  Movement");
         client.print("
");
        }
        else
        {
            client.print("Warning -   Movement");
         client.print("
");
        }
       
       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();
  }
}


int pir_cheak()
{
   
   if (digitalRead(PIRChannel) ==HIGH) // no movement - Green LED
          {
            digitalWrite(RedLED, LOW);
            digitalWrite(GreenLED,HIGH);
             Serial.println ("no move!!!");
            d=0;
          //  Serial.println(d);
                       }
          else
         {
             digitalWrite(GreenLED,LOW); // Movement - Red LED
            digitalWrite(RedLED,HIGH);
             Serial.println ("Move!!!");
            d=1;
                       return d;
         }
 }

thanks

The millis() function returns the number of milliseconds since the program started.

unsigned long updateTime;

// when there is motion and you turn on the LED, do this
updateTime = millis();
updateTime += 5000;
// Now call millis in the "turn off" section and compare the value returned to updateTime
if(updateTime < millis())
{
   // it has been 5 seconds
   // you can turn off the LED
}

edit: I prefer using the web response to reset the flag for that.

int motionDetect = 0;

// then when you turn on the LED
motionDetect = 1;

// after you read the webpage
// like after client.stop(), then do this
motionDetect = 0;

That way it will store that value in case you don't communicate with the Arduino for a while for some reason or another.

Your pir_cheak() function has two main blocks of code. Only one of the blocks returns a value.

if I'm using the web response option - where do I put the int?
at the pir_cheak function?

and another thing - I'm trying to keep the message at screen as long as the red LED is on.
and it's doesn't doing it - it only write the message once , and then return to "all-good"( this is not good)
I need the message will stay as long as the led is on.
I have change a few thing but still no good

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(10,0,0,155); // ip in lan
EthernetServer server(80); //server is using port 80
int PIRChannel=4;
int GreenLED=7;
int RedLED=9;
unsigned long  updateTime; 
int d;
void setup()
{
  
  Serial.begin(9600);
    Ethernet.begin(mac, ip);
  server.begin();
  pinMode(GreenLED,OUTPUT);
  pinMode(RedLED,OUTPUT);
  pinMode(PIRChannel,INPUT);
}


void loop()
{
  pir_cheak();
   
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    boolean currentLineIsBlank = true;
     while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        // see if HTTP request has ended with blank line
        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();
         
          //meta-refresh page every 1 seconds
           client.println("<HTML>");
          client.print("<HEAD>");
          client.print("<meta http-equiv=\"refresh\" content=\"1\">");
          client.print("<TITLE /> Test</title>");
          client.print("</head>");
          client.println("<BODY>");
          client.print("autorefresh test ");
          client.println("
");
                         
                 // printing the message
         if
         (d==0)
        {
          client.print("All Good - No  Movement");
         client.print("
");
        }
        else
        {
            client.print("Warning -   Movement");
            client.print("
");           
            client.print (millis()/1000);
            client.print("
");
        }
       
       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();
  }
}


int pir_cheak()
{
   
   if (digitalRead(PIRChannel) ==HIGH) // no movement - Green LED
          {
            if (updateTime < millis())
            {
            digitalWrite(RedLED, LOW);
            digitalWrite(GreenLED,HIGH);
             Serial.println ("no move!!!");
             Serial.println(millis()/1000);            
            }
            
            d=0;
            
                       }
          else
          
         {
             digitalWrite(GreenLED,LOW); // Movement - Red LED
            digitalWrite(RedLED,HIGH);
             Serial.println ("Move!!!");
            d=1;
            
             updateTime = millis();
             updateTime +=5000;
            
                 return d;
         }
}


[code]

[/code]

If you are using the variable 'd' for the message send, then don't clear 'd' until the time is up. Move it inside the time compare decision.

if (updateTime < millis())
{
   digitalWrite(RedLED, LOW);
   digitalWrite(GreenLED,HIGH);

   // do not use three of these. That can cause problems
   // Serial.println ("no move!!!");

   Serial.println ("no move!");
   Serial.println(millis()/1000);            
   // this is cleared only when the time is up
   d=0;
}

Your code
running all over
the
page is very
hard to read.

Fortunately, there
is a way
to
fix it.

Use the Tools

  • Auto Format menu
    item to impose
    proper indenting.

Maybe, just maybe, you'll be able to see what is wrong with your code.

SurferTim - Thank you - now it's just like I want it :slight_smile:

PaulS - I didn't know there is an option like this (it's wonderful ,and I'm using this option now - so thank you to!).

the lest thing that I want to do is - how to save the massage in another table ? , I have search the web but didn't find in the HTML basic how to do this.
I want whenever I get a move message - it will also save me the time on the side (meanwhile it will show me the sec using the millis() ).

Thank you!