-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ino
97 lines (87 loc) · 2.51 KB
/
server.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
void runServer()
{
/* SERVER */
String HTTPRequest;
char req_index = 0;
// 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();
HTTPRequest += c;
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();
if (HTTPRequest.indexOf("LEDOn") > -1)
{
digitalWrite(lightPin, HIGH);
saveLightStatus(1);
}
else if (HTTPRequest.indexOf("LEDOff") > -1)
{
digitalWrite(lightPin, LOW);
saveLightStatus(0);
}
if (HTTPRequest.indexOf("openDoor") > -1)
{
doorStatus = 1;
saveDoorStatus();
}
else if (HTTPRequest.indexOf("closeDoor") > -1)
{
doorStatus = 0;
saveDoorStatus();
}
else if (HTTPRequest.indexOf("feed") > -1)
{
feedStatus = 1;
/*Serial.print("FEEDSTAT: ");
Serial.print(feedStatus);
Serial.println();*/
}
req_index = 0; // reset request index
// finished with the HTTP request so clear it
HTTPRequest = "";
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 disconnected");
}
}
void saveLightStatus(int status)
{
FILE *lightStatusFile;
lightStatusFile = fopen("/home/root/lightStatus.txt", "w");
fprintf(lightStatusFile, "%d", status);
fclose(lightStatusFile);
}