// Ethercard REL example #include #include #include #define STATIC 0 // set to 1 to disable DHCP (adjust myip/gwip values below) #if STATIC // ethernet interface ip address static byte myip[] = { 192,168,0,200 }; // gateway ip address static byte gwip[] = { 192,168,0,1 }; #endif // ethernet mac address - must be unique on your network static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 }; #define BUFFER_SIZE 500 byte Ethernet::buffer[BUFFER_SIZE]; BufferFiller bfill; #define REL 5 // define REL pin bool RELStatus = false; float temp; char temp_str[10]; #define ONE_WIRE_BUS 4 OneWire oneWire(ONE_WIRE_BUS); DallasTemperature sensors(&oneWire); 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" "

401 Unauthorized

"; void homePage() { String entete = "" "Temp Server" "\n"; String table = entete + ""; table += "\n"; for (int i = 0; i < 2; i++) { table += "\n"; } table += "<\/table>\n"; //table += ""; String pied = "<\/body>"; // html += ""; Serial.println(table); //bfill.emit_p(String.toCharArray(html)); int len = table.length() + 1; char tmp[len]; //Serial.println(tmp); table.toCharArray(tmp, len); bfill.emit_p(PSTR("$F$F"),http_OK,tmp); // bfill.emit_p(PSTR("$F" // "" // "Temp Server" // "" // "
" // "
" // "" // "Temperature: " // "$S °C" // "
" // "
" // "" // "
" // "Relay Status: $F"), http_OK, temp_str, RELStatus?PSTR("off"):PSTR("on"), RELStatus?PSTR("ON"):PSTR("OFF")); } void setup() { Serial.begin(9600); pinMode(REL, OUTPUT); if (ether.begin(BUFFER_SIZE, mymac) == 0) Serial.println("Cannot initialise ethernet."); else Serial.println("Ethernet initialised."); //ether.staticSetup(myip); #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); } void loop() { digitalWrite(REL, RELStatus); // write to REL digital output delay(1); // necessary for my system word len = ether.packetReceive(); // check for ethernet packet word pos = ether.packetLoop(len); // check for tcp packet sensors.requestTemperatures(); temp = (sensors.getTempCByIndex(0)); dtostrf(temp, 6, 2, temp_str); if (pos) { bfill = ether.tcpOffset(); char *data = (char *) Ethernet::buffer + pos; if (strncmp("GET /", data, 5) != 0) { // Unsupported HTTP request // 304 or 501 response would be more appropriate bfill.emit_p(http_Unauthorized); } else { data += 5; if (data[0] == ' ') { Serial.println("Homepage"); // Return home page homePage(); } else if (strncmp("?REL=on ", data, 8) == 0) { // Set RELStatus true and redirect to home page RELStatus = true; bfill.emit_p(http_Found); } else if (strncmp("?REL=off ", data, 9) == 0) { // Set RELStatus false and redirect to home page RELStatus = false; bfill.emit_p(http_Found); } else { // Page not found bfill.emit_p(http_Unauthorized); } } ether.httpServerReply(bfill.position()); // send http response } }
PinValue
"; table += String(i); table += ""; table += String(digitalRead(i)); table += "