Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <EtherCard.h>
- /* https://github.com/jcw/ethercard */
- /*
- http://www.lucadentella.it/2012/08/10/enc28j60-e-arduino-7/
- https://github.com/lucadentella/enc28j60_tutorial/tree/master/_7_WebLed
- */
- #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,1,44 };
- // gateway ip address
- static byte gwip[] = { 192,168,1,1 };
- #endif
- //static byte mymac[] = {0xDD,0xDD,0xDD,0x00,0x00,0x01};
- static byte mymac[] = { 0x00, 0x00, 0x6C, 0x01, 0x02, 0x03 };
- byte Ethernet::buffer[700];
- const int vstup = 7;
- const int ledPin = 2;
- boolean ledStatus;
- char* on = "ON";
- char* off = "OFF";
- char* statusLabel;
- char* buttonLabel;
- void setup () {
- Serial.begin(9600);
- Serial.println("WebLed Demo");
- if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
- Serial.println( "Failed to access Ethernet controller");
- #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);
- Serial.println();
- pinMode(vstup, 7);
- pinMode(ledPin, OUTPUT);
- digitalWrite(ledPin, LOW);
- ledStatus = false;
- }
- void loop() {
- word len = ether.packetReceive();
- word pos = ether.packetLoop(len);
- int buttonState = digitalRead(vstup);
- //Serial.println("Input state: " + buttonState);
- if(pos) {
- if(strstr((char *)Ethernet::buffer + pos, "GET /?status=ON") != 0) {
- Serial.println("Received ON command");
- ledStatus = true;
- }
- if(strstr((char *)Ethernet::buffer + pos, "GET /?status=OFF") != 0) {
- Serial.println("Received OFF command");
- ledStatus = false;
- }
- if(ledStatus) {
- digitalWrite(ledPin, HIGH);
- statusLabel = on;
- buttonLabel = off;
- } else {
- digitalWrite(ledPin, LOW);
- statusLabel = off;
- buttonLabel = on;
- }
- Serial.println("Input state: " + buttonState);
- BufferFiller bfill = ether.tcpOffset();
- bfill.emit_p(PSTR("HTTP/1.0 200 OK\r\n"
- "Content-Type: text/html\r\nPragma: no-cache\r\n\r\n"
- "<html><head><title>WebLed</title></head>"
- "<body style=\"font-size: 200%\">LED Status: $S "
- "<br><a href=\"/?status=$S\"><strong><input style=\"font-size: 200%\" type=\"button\" value=\"$S\"><strong></a>"
- "</body></html>"
- ), statusLabel, buttonLabel, buttonLabel, buttonState);
- ether.httpServerReply(bfill.position());
- }
- }
- void showString (PGM_P s) {
- char c;
- while ((c = pgm_read_byte(s++)) != 0)
- Serial.print(c);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement