Advertisement
C1sc0

Untitled

Apr 25th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. // Demo using DHCP and DNS to perform a web client request.
  2. // 2011-06-08 <jc@wippler.nl>
  3. //
  4. // License: GPLv2
  5.  
  6. #include <EtherCard.h>
  7.  
  8. // ethernet interface mac address, must be unique on the LAN
  9. static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
  10.  
  11. byte Ethernet::buffer[700];
  12. static uint32_t timer;
  13.  
  14. const char website[] PROGMEM = "192.168.1.1";
  15. const int buttonPin = 2;
  16. const int ledPin = 3;
  17. int ledState = HIGH;
  18.  
  19. // called when the client request is complete
  20. static void my_callback (byte status, word off, word len) {
  21. Serial.println(">>>");
  22. Ethernet::buffer[off+300] = 0;
  23. Serial.print((const char*) Ethernet::buffer + off);
  24. Serial.println("...");
  25. }
  26.  
  27. void setup () {
  28. pinMode(buttonPin, INPUT);
  29. pinMode(ledPin, OUTPUT);
  30. digitalWrite(ledPin, ledState);
  31. Serial.begin(9600);
  32. Serial.println(F("\n[webClient]"));
  33.  
  34. // Change 'SS' to your Slave Select pin, if you arn't using the default pin
  35. if (ether.begin(sizeof Ethernet::buffer, mymac, SS) == 0)
  36. Serial.println(F("Failed to access Ethernet controller"));
  37. if (!ether.dhcpSetup())
  38. Serial.println(F("DHCP failed"));
  39.  
  40. ether.printIp("IP: ", ether.myip);
  41. ether.printIp("GW: ", ether.gwip);
  42. ether.printIp("DNS: ", ether.dnsip);
  43.  
  44. #if 1
  45. // use DNS to resolve the website's IP address
  46. if (!ether.dnsLookup(website))
  47. Serial.println("DNS failed");
  48. #elif 2
  49. // if website is a string containing an IP address instead of a domain name,
  50. // then use it directly. Note: the string can not be in PROGMEM.
  51. char websiteIP[] = "192.168.1.1";
  52. ether.parseIp(ether.hisip, websiteIP);
  53. #else
  54. // or provide a numeric IP address instead of a string
  55. byte hisip[] = { 192,168,88,100 };
  56. ether.copyIp(ether.hisip, hisip);
  57. #endif
  58. byte hisip[] = { 10,1,23,108 };
  59. ether.copyIp(ether.hisip, hisip);
  60. ether.printIp("SRV: ", ether.hisip);
  61. }
  62.  
  63. int buttonState = HIGH;
  64. unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
  65. unsigned long debounceDelay = 50;
  66. int lastButtonState = LOW;
  67. void loop () {
  68. ether.packetLoop(ether.packetReceive());
  69.  
  70.  
  71. int reading = digitalRead(buttonPin);
  72.  
  73. if (reading != lastButtonState) {
  74. // reset the debouncing timer
  75. lastDebounceTime = millis();
  76.  
  77. }
  78.  
  79. if ((millis() - lastDebounceTime) > debounceDelay) {
  80. // whatever the reading is at, it's been there for longer than the debounce
  81. // delay, so take it as the actual current state:
  82.  
  83. // if the button state has changed:
  84. if (reading != buttonState) {
  85. buttonState = reading;
  86.  
  87. // only toggle the LED if the new button state is HIGH
  88. if (buttonState == LOW) {
  89.  
  90. Serial.println();
  91. Serial.print("<<< REQ ");
  92. ether.browseUrl(PSTR("/"), "", website, my_callback);
  93.  
  94. }
  95. }
  96. }
  97. lastButtonState = reading;
  98. digitalWrite(ledPin, lastButtonState);
  99.  
  100.  
  101.  
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement