Hello,
i'm making a project to control application via Ethernet. The project using Atmega 2560, Ethernet Shield, PIR Sensor and LED. The problem is i couldnt turn off the PIR via ethernet. I didnt how to control the button OFF in the webpage i have created. I hope someone could give advice on this. Thanks in advance.
Below is the coding :
#include <SPI.h>
#include <Ethernet.h>
// ethernet configuration
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 178); // P1 --> { 10, 1, 1, 5 };
EthernetServer server(80); // port 80 is default for HTTP
const int PIR = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// initial
int LED = 13; // led is connected to digital pin 13
int PIRstate = 0; // variable for PIR sensor status
// variables will change:
char c = 0; // received data
char command[2] = "\0"; // command
void setup()
{
Ethernet.begin(mac, ip);
server.begin();
pinMode(ledPin, OUTPUT);
pinMode(PIR, INPUT);
}
void loop()
{
EthernetClient client = server.available();
// detect if current is the first line
boolean current_line_is_first = true;
PIRstate = digitalRead(PIR);
if (client) {
// an http request ends with a blank line
boolean current_line_is_blank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
// if we've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so we can send a reply
if (c == '\n' && current_line_is_blank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
// auto reload webpage every 5 second
client.println("<META HTTP-EQUIV=REFRESH CONTENT=5 URL=>");
// webpage title
client.println("<center><p><h1>Motion Detector Security System</h1></p><center><hr>
");
// read digital pin 13 for the state of PIR sensor
PIRstate = digitalRead(2);
if (PIRstate == HIGH) { // PIR sensor detected movement
client.println("<p><h2><font color=red>Motion Detected!</font></h2></p>");
digitalWrite(LED, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
else { // No movement is detected
client.println("<p><h2><font color=green>No Movement</font></h2></p>");
// turn LED off:
digitalWrite(LED, LOW);
}
// button functions
client.println("<form method=get name=form>");
client.println("<button name=b value=1 type=submit style=height:80px;width:150px>Turn On</button>");
client.println("<button name=b value=2 type=submit style=height:80px;width:150px>Turn Off</button>");
client.println("</form>
");
// webpage footer
client.println("<hr><center><a href=http://www.robothead2toe.com.my>Robot.Head to Toe</a>
");
client.println("<p>P.S.: This page will automatically refresh every 5 seconds.</p></center>");
break;
}
if (c == '\n') {
// we're starting a new line
current_line_is_first = false;
current_line_is_blank = true;
}
else if (c != '\r') {
// we've gotten a character on the current line
current_line_is_blank = false;
}
// get the first http request
if (current_line_is_first && c == '=') {
for (int i = 0; i < 1; i++) {
c = client.read();
command[i] = c;
}
// LED control
if (!strcmp(command, "1")) {
digitalWrite(LED, HIGH);
}
else if (!strcmp(command, "2")) {
digitalWrite(LED, LOW);
}
}
}
}
// give the web browser time to receive the data
delay(1);
client.stop();
}
}