Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Demo using DHCP and DNS to perform a web client request.
- // 2011-06-08 <jc@wippler.nl>
- //
- // License: GPLv2
- #include <EtherCard.h>
- // ethernet interface mac address, must be unique on the LAN
- static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
- byte Ethernet::buffer[700];
- static uint32_t timer;
- const char website[] PROGMEM = "192.168.1.1";
- const int buttonPin = 2;
- const int ledPin = 3;
- int ledState = HIGH;
- // called when the client request is complete
- static void my_callback (byte status, word off, word len) {
- Serial.println(">>>");
- Ethernet::buffer[off+300] = 0;
- Serial.print((const char*) Ethernet::buffer + off);
- Serial.println("...");
- }
- void setup () {
- pinMode(buttonPin, INPUT);
- pinMode(ledPin, OUTPUT);
- digitalWrite(ledPin, ledState);
- Serial.begin(9600);
- Serial.println(F("\n[webClient]"));
- // Change 'SS' to your Slave Select pin, if you arn't using the default pin
- if (ether.begin(sizeof Ethernet::buffer, mymac, SS) == 0)
- Serial.println(F("Failed to access Ethernet controller"));
- if (!ether.dhcpSetup())
- Serial.println(F("DHCP failed"));
- ether.printIp("IP: ", ether.myip);
- ether.printIp("GW: ", ether.gwip);
- ether.printIp("DNS: ", ether.dnsip);
- #if 1
- // use DNS to resolve the website's IP address
- if (!ether.dnsLookup(website))
- Serial.println("DNS failed");
- #elif 2
- // if website is a string containing an IP address instead of a domain name,
- // then use it directly. Note: the string can not be in PROGMEM.
- char websiteIP[] = "192.168.1.1";
- ether.parseIp(ether.hisip, websiteIP);
- #else
- // or provide a numeric IP address instead of a string
- byte hisip[] = { 192,168,88,100 };
- ether.copyIp(ether.hisip, hisip);
- #endif
- byte hisip[] = { 10,1,23,108 };
- ether.copyIp(ether.hisip, hisip);
- ether.printIp("SRV: ", ether.hisip);
- }
- int buttonState = HIGH;
- unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
- unsigned long debounceDelay = 50;
- int lastButtonState = LOW;
- void loop () {
- ether.packetLoop(ether.packetReceive());
- int reading = digitalRead(buttonPin);
- if (reading != lastButtonState) {
- // reset the debouncing timer
- lastDebounceTime = millis();
- }
- if ((millis() - lastDebounceTime) > debounceDelay) {
- // whatever the reading is at, it's been there for longer than the debounce
- // delay, so take it as the actual current state:
- // if the button state has changed:
- if (reading != buttonState) {
- buttonState = reading;
- // only toggle the LED if the new button state is HIGH
- if (buttonState == LOW) {
- Serial.println();
- Serial.print("<<< REQ ");
- ether.browseUrl(PSTR("/"), "", website, my_callback);
- }
- }
- }
- lastButtonState = reading;
- digitalWrite(ledPin, lastButtonState);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement