This repository has been archived by the owner on Jun 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ino
157 lines (131 loc) · 4.08 KB
/
main.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {0xDe, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
unsigned short int ldrValor = 0;
//IPAddress ip(10, 33, 250, 117);
IPAddress ip(192, 168, 0, 105); // Here, you must put the Arduino's IP
#define Led1 7
#define ldrPin A0
EthernetServer server(80);
int pinLed1 = 8;
int led = 0;
void setup() {
pinMode(Led1, OUTPUT);
pinMode(ldrPin, INPUT);
digitalWrite(Led1, LOW);
Serial.begin(9600);
while (!Serial) {
;
}
if (digitalRead(pinLed1) == HIGH) led = 1;
else if (digitalRead(pinLed1) == LOW) led = 0;
pinMode(pinLed1, OUTPUT);
digitalWrite(pinLed1, LOW);
Ethernet.begin(mac, ip);
server.begin();
Serial.print("Server is at ");
Serial.println(Ethernet.localIP());
}
void loop()
{
EthernetClient client = server.available();
if (client) {
Serial.println("New client");
boolean currentLineIsBlank = true;
String DatEth;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
DatEth.concat(c);
if (DatEth.endsWith("/&ON")) {
led = 1;
}
if (DatEth.endsWith("/&OFF")) {
led = 0;
}
if (c == '\n' && currentLineIsBlank) {
ldrValor = analogRead(ldrPin);
Serial.println(ldrValor);
if (ldrValor <= 50 && digitalRead(Led1) == LOW)
{
digitalWrite(Led1, HIGH);
}
delay (2);
if (ldrValor > 50 && digitalRead(Led1) == HIGH)
{
digitalWrite(Led1, LOW);
}
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println("Refresh: 1");
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
if (digitalRead(Led1) == HIGH)
{
client.println("<center><h1>\n"
"Estado do LED SENSOR <span style='background: #00FF00; border-radius:5px; padding:5px'>"
"ON</span>.\n"
"</h1></center>\n");
}
else
{
client.println("<center><h1>\n"
"Estado do LED SENSOR <span style='background: #FF0000 ; border-radius:5px; padding:5px'>"
"OFF</span>.\n"
"</h1></center>\n");
}
if (led == 1)
{
client.println("</br><center><h1>\n"
"Estado do LED HTML <span style='background: #00FF00; border-radius:5px; padding:5px'>"
"ON</span>.\n"
"</h1></center>\n");
}
else
{
client.println("</br><center><h1>\n"
"Estado do LED HTML <span style='background: #FF0000 ; border-radius:5px; padding:5px'>"
"OFF</span>.\n"
"</h1></center>\n");
}
client.println("<center><form method='get' action='/&ON'><button type='submit'>Ligar LED HTML</button></form></center>"
"</br>"
"<center><form method='get' action='/&OFF'><button type='submit'>Desligar LED HTML</button></form></center>"
""
"");
client.println("</html>");
if (led == 1) {
digitalWrite(pinLed1, HIGH);
}
if (led == 0) {
digitalWrite(pinLed1, LOW);
}
break;
}
if (c == '\n') {
currentLineIsBlank = true;
}
else if (c != '\r') {
currentLineIsBlank = false;
}
}
}
delay(1);
client.stop();
Serial.println("Client disconnected");
}
ldrValor = analogRead(ldrPin);
Serial.println(ldrValor);
if (ldrValor <= 50 && digitalRead(Led1) == LOW)
{
digitalWrite(Led1, HIGH);
}
delay (2);
if (ldrValor > 50 && digitalRead(Led1) == HIGH)
{
digitalWrite(Led1, LOW);
}
}