| 1 |
|
|---|
| 2 | #include <Ethernet.h>
|
|---|
| 3 | #include <SPI.h>
|
|---|
| 4 | boolean reading = false;
|
|---|
| 5 |
|
|---|
| 6 | ////////////////////////////////////////////////////////////////////////
|
|---|
| 7 | //CONFIGURE
|
|---|
| 8 | ////////////////////////////////////////////////////////////////////////
|
|---|
| 9 | //byte ip[] = { 192, 168, 0, 199 }; //Manual setup only
|
|---|
| 10 | //byte gateway[] = { 192, 168, 0, 1 }; //Manual setup only
|
|---|
| 11 | //byte subnet[] = { 255, 255, 255, 0 }; //Manual setup only
|
|---|
| 12 |
|
|---|
| 13 | // if need to change the MAC address (Very Rare)
|
|---|
| 14 | byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
|
|---|
| 15 |
|
|---|
| 16 | EthernetServer server = EthernetServer(80); //port 80
|
|---|
| 17 | ////////////////////////////////////////////////////////////////////////
|
|---|
| 18 |
|
|---|
| 19 | void setup(){
|
|---|
| 20 | Serial.begin(9600);
|
|---|
| 21 |
|
|---|
| 22 | //Pins 10,11,12 & 13 are used by the ethernet shield
|
|---|
| 23 |
|
|---|
| 24 | pinMode(2, OUTPUT);
|
|---|
| 25 | pinMode(3, OUTPUT);
|
|---|
| 26 | pinMode(4, OUTPUT);
|
|---|
| 27 | pinMode(5, OUTPUT);
|
|---|
| 28 | pinMode(6, OUTPUT);
|
|---|
| 29 | pinMode(7, OUTPUT);
|
|---|
| 30 | pinMode(8, OUTPUT);
|
|---|
| 31 | pinMode(9, OUTPUT);
|
|---|
| 32 |
|
|---|
| 33 | Ethernet.begin(mac);
|
|---|
| 34 | //Ethernet.begin(mac, ip, gateway, subnet); //for manual setup
|
|---|
| 35 |
|
|---|
| 36 | server.begin();
|
|---|
| 37 | Serial.println(Ethernet.localIP());
|
|---|
| 38 |
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | void loop(){
|
|---|
| 42 |
|
|---|
| 43 | // listen for incoming clients, and process qequest.
|
|---|
| 44 | checkForClient();
|
|---|
| 45 |
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | void checkForClient(){
|
|---|
| 49 |
|
|---|
| 50 | EthernetClient client = server.available();
|
|---|
| 51 |
|
|---|
| 52 | if (client) {
|
|---|
| 53 |
|
|---|
| 54 | // an http request ends with a blank line
|
|---|
| 55 | boolean currentLineIsBlank = true;
|
|---|
| 56 | boolean sentHeader = false;
|
|---|
| 57 |
|
|---|
| 58 | while (client.connected()) {
|
|---|
| 59 | if (client.available()) {
|
|---|
| 60 |
|
|---|
| 61 | if(!sentHeader){
|
|---|
| 62 | // send a standard http response header
|
|---|
| 63 | client.println("HTTP/1.1 200 OK");
|
|---|
| 64 | client.println("Content-Type: text/html");
|
|---|
| 65 | client.println();
|
|---|
| 66 | sentHeader = true;
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | char c = client.read();
|
|---|
| 70 |
|
|---|
| 71 | if(reading && c == ' ') reading = false;
|
|---|
| 72 | if(c == '?') reading = true; //found the ?, begin reading the info
|
|---|
| 73 |
|
|---|
| 74 | if(reading){
|
|---|
| 75 | Serial.print(c);
|
|---|
| 76 |
|
|---|
| 77 | switch (c) {
|
|---|
| 78 | case '2':
|
|---|
| 79 | //add code here to trigger on 2
|
|---|
| 80 | triggerPin(2, client);
|
|---|
| 81 | break;
|
|---|
| 82 | case '3':
|
|---|
| 83 | //add code here to trigger on 3
|
|---|
| 84 | triggerPin(3, client);
|
|---|
| 85 | break;
|
|---|
| 86 | case '4':
|
|---|
| 87 | //add code here to trigger on 4
|
|---|
| 88 | triggerPin(4, client);
|
|---|
| 89 | break;
|
|---|
| 90 | case '5':
|
|---|
| 91 | //add code here to trigger on 5
|
|---|
| 92 | triggerPin(5, client);
|
|---|
| 93 | break;
|
|---|
| 94 | case '6':
|
|---|
| 95 | //add code here to trigger on 6
|
|---|
| 96 | triggerPin(6, client);
|
|---|
| 97 | break;
|
|---|
| 98 | case '7':
|
|---|
| 99 | //add code here to trigger on 7
|
|---|
| 100 | triggerPin(7, client);
|
|---|
| 101 | break;
|
|---|
| 102 | case '8':
|
|---|
| 103 | //add code here to trigger on 8
|
|---|
| 104 | triggerPin(8, client);
|
|---|
| 105 | break;
|
|---|
| 106 | case '9':
|
|---|
| 107 | //add code here to trigger on 9
|
|---|
| 108 | triggerPin(9, client);
|
|---|
| 109 | break;
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | if (c == '\n' && currentLineIsBlank) break;
|
|---|
| 115 |
|
|---|
| 116 | if (c == '\n') {
|
|---|
| 117 | currentLineIsBlank = true;
|
|---|
| 118 | }else if (c != '\r') {
|
|---|
| 119 | currentLineIsBlank = false;
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | }
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | delay(1); // give the web browser time to receive the data
|
|---|
| 126 | client.stop(); // close the connection:
|
|---|
| 127 |
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | void triggerPin(int pin, EthernetClient client){
|
|---|
| 133 | //blink a pin - Client needed just for HTML output purposes.
|
|---|
| 134 | client.print("Turning on pin ");
|
|---|
| 135 | client.println(pin);
|
|---|
| 136 | client.print("<br>");
|
|---|
| 137 |
|
|---|
| 138 | digitalWrite(pin, HIGH);
|
|---|
| 139 | delay(25);
|
|---|
| 140 | digitalWrite(pin, LOW);
|
|---|
| 141 | delay(25);
|
|---|
| 142 | }
|
|---|