Pages: 1 [2] 3   Go Down
Author Topic: auto refresh on browser  (Read 3872 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Sr. Member
****
Karma: 0
Posts: 277
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 152
Posts: 6192
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

He meant change it, not remove it. The format is not correct. That is an assignment statement, not a compare. This would be correct:
Code:
if(d == 0)

Two equal signs, not one.
Logged

Offline Offline
Sr. Member
****
Karma: 0
Posts: 277
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 ,
Logged

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

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

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 152
Posts: 6192
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You are not setting the variable 'd' correctly.
Code:
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).

« Last Edit: May 02, 2012, 11:55:22 am by SurferTim » Logged

Offline Offline
Sr. Member
****
Karma: 0
Posts: 277
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
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("<br />");
                         
                 // printing the message
         if (d=0)
        {
          client.print("All Good - No  Movement");
         client.print("<br />");
        }
        else
        {
            client.print("Warning -   Movement");
         client.print("<br />");
        }
       
       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;
                       
         }
 }
Logged

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

Code:
 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.

Quote
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?
Logged

Offline Offline
Sr. Member
****
Karma: 0
Posts: 277
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 :

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("<br />");
                         
                 // printing the message
         if (d=0)
        {
          client.print("All Good - No  Movement");
         client.print("<br />");
        }
        else
        {
            client.print("Warning -   Movement");
         client.print("<br />");
        }
       
       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;
         }
 }

« Last Edit: May 03, 2012, 06:41:01 am by david1234 » Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 152
Posts: 6192
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You have reinserted the previous error into your code.
Code:
                // printing the message
         // this is not correct
         if (d=0)
        {
          client.print("All Good - No  Movement");
         client.print("<br />");
        }
        else
        {
            client.print("Warning -   Movement");
         client.print("<br />");
        }

This is correct.
Code:
if(d == 0)
Logged

Offline Offline
Sr. Member
****
Karma: 0
Posts: 277
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 :

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("<br />");
                         
                 // printing the message
         if
         (d==0)
        {
          client.print("All Good - No  Movement");
         client.print("<br />");
        }
        else
        {
            client.print("Warning -   Movement");
         client.print("<br />");
        }
       
       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
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 152
Posts: 6192
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The millis() function returns the number of milliseconds since the program started.
http://arduino.cc/en/Reference/Millis

Code:
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.

Code:
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.
« Last Edit: May 03, 2012, 07:26:37 am by SurferTim » Logged

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

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

Offline Offline
Sr. Member
****
Karma: 0
Posts: 277
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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;
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("<br />");
                         
                 // printing the message
         if
         (d==0)
        {
          client.print("All Good - No  Movement");
         client.print("<br />");
        }
        else
        {
            client.print("Warning -   Movement");
            client.print("<br />");           
            client.print (millis()/1000);
            client.print("<br />");
        }
       
       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]
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 152
Posts: 6192
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Code:
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;
}
           
  
Logged

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

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.
Logged

Pages: 1 [2] 3   Go Up
Jump to: