-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnect-arduinoshiled-post-dataonserver.ino
73 lines (63 loc) · 2.08 KB
/
connect-arduinoshiled-post-dataonserver.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
#include<SPI.h> //Connect Arduino to Sheild
#include<Ethernet.h> //Ethernet Connection
int led=8;
byte mac[]={0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; //Mac address of ethernet sheild
EthernetServer server(80);
String readString;
void setup() {
Serial.begin(9600);
pinMode(led,OUTPUT);
Serial.println("Initialize Ethernet with DHCP:");
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
} else if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
// no point in carrying on, so do nothing forevermore:
while (true) {
delay(1);
}
}
server.begin();
Serial.print("Server is at ");
Serial.print(Ethernet.localIP());
}
void loop() {
EthernetClient client = server.available();
if(client){
while(client.connected()){
if(client.available()){
char c = client.read();
Serial.print(c);
if(readString.length()<100){
readString += c;
}
//check if new data read
if(c=='\n'){
Serial.print(readString);
client.println("<HTTP/1.1 200 OK>");
client.println("<Connection-Type: text/html>");
client.println("<Connection: close>");
client.println("");
client.println("<!DOCTYPE html");
client.println("<html><head><title>Webserver</title></head>");
client.println("<body>");
client.println("<a href=\"/?button1on\"\"<button>LED ON</button></a>");
client.println("<a href=\"/?button2off\"\"<button>LED OFF</button></a>");
client.println("<body style=background-color:powderblue>");
delay(1);
client.stop();
if(readString.indexOf("?button1on")>0){
digitalWrite(led, HIGH);
}
if(readString.indexOf("?button2off")>0){
digitalWrite(led, LOW);
}
readString="";
}
}
}
}
}