아두이노 나노 ENC28J60 이더넷쉴드 릴레이 선풍기 IOT
웹서버 IOT 제어 / 스마트폰 / 피시
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
|
#include <EtherCard.h>
#define STATIC 1 // set to 1 to disable DHCP (adjust myip/gwip values below)
#if STATIC
// ethernet interface ip address
static byte myip[] = { 192,168,0,100 };
// gateway ip address
static byte gwip[] = { 192,168,0,1 };
#endif
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
byte Ethernet::buffer[700];
#define RELAY 5
#define RELAY2 6
bool RELAYStatus = false;
bool RELAY2Status = false;
void setup () {
Serial.begin(57600);
Serial.println("\n[WEBSERVER]");
pinMode(RELAY, OUTPUT);
pinMode(RELAY2, OUTPUT);
ether.begin(sizeof Ethernet::buffer, mymac,10);
#if STATIC
ether.staticSetup(myip, gwip);
#else
if (!ether.dhcpSetup())
Serial.println("DHCP failed");
#endif
ether.printIp("IP: ", ether.myip);
ether.printIp("GW: ", ether.gwip);
ether.printIp("DNS: ", ether.dnsip);
}
const char http_OK[] PROGMEM =
"HTTP/1.0 200 OK\r\n"
"Content-Type: text/html\r\n"
"Pragma: no-cache\r\n\r\n";
const char http_Found[] PROGMEM =
"HTTP/1.0 302 Found\r\n"
"Location: /\r\n\r\n";
const char http_Unauthorized[] PROGMEM =
"HTTP/1.0 401 Unauthorized\r\n"
"Content-Type: text/html\r\n\r\n"
"<h1>401 Unauthorized</h1>";
void loop() {
word len = ether.packetReceive();
word pos = ether.packetLoop(len);
if(pos) {
Serial.print("#1 len=");
Serial.print(len);
Serial.print(",");
Serial.println(pos);
BufferFiller bfill = ether.tcpOffset();
bfill = ether.tcpOffset();
char * data = (char *) Ethernet::buffer + pos;
if (strncmp("GET /", data, 5) != 0) {
}
else {
data += 5;
if (data[0] == ' ') {
}
else if (strncmp("?RELAY=on", data, 9) == 0) {
// Set ledStatus true and redirect to home page
RELAYStatus = true;
digitalWrite(RELAY, RELAYStatus);
}
else if (strncmp("?RELAY=off", data, 10) == 0) {
// Set ledStatus false and redirect to home page
RELAYStatus = false;
digitalWrite(RELAY, RELAYStatus);
}
else if (strncmp("?RELAY2=on", data, 10) == 0) {
// Set ledStatus true and redirect to home page
RELAY2Status = true;
digitalWrite(RELAY2, RELAY2Status);
}
else if (strncmp("?RELAY2=off", data, 11) == 0) {
// Set ledStatus false and redirect to home page
RELAY2Status = false;
digitalWrite(RELAY2, RELAY2Status);
}
else {
}
Serial.print("GET=");
Serial.println(data);
}
bfill.emit_p(PSTR("$F"
"<title>Ethercard RELAY</title>"
"<h1>Ethercard RELAY Control</h1>"
"</br>"
"<h1>RELAY Status: <a href=\"?RELAY=$F\">$F</a></h1>"
"</br>"
"<h1>RELAY2 Status: <a href=\"?RELAY2=$F\">$F</a></h1>"), ///
http_OK,
RELAYStatus?PSTR("off"):PSTR("on"), RELAYStatus?PSTR("ON"):PSTR("OFF"),
RELAY2Status?PSTR("off"):PSTR("on"), RELAY2Status?PSTR("ON"):PSTR("OFF"));
ether.httpServerReply(bfill.position());
}
delay(100);
}
| cs |


댓글 없음