PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

131 lines
3.8KB

  1. /*
  2. Repeating Web client
  3. This sketch connects to a a web server and makes a request
  4. using a Wiznet Ethernet shield. You can use the Arduino Ethernet shield, or
  5. the Adafruit Ethernet shield, either one will work, as long as it's got
  6. a Wiznet Ethernet module on board.
  7. This example uses DNS, by assigning the Ethernet client with a MAC address,
  8. IP address, and DNS address.
  9. Circuit:
  10. * Ethernet shield attached to pins 10, 11, 12, 13
  11. created 19 Apr 2012
  12. by Tom Igoe
  13. modified 21 Jan 2014
  14. by Federico Vanzati
  15. http://www.arduino.cc/en/Tutorial/WebClientRepeating
  16. This code is in the public domain.
  17. */
  18. #include <SPI.h>
  19. #include <NativeEthernet.h>
  20. // assign a MAC address for the ethernet controller.
  21. // fill in your address here:
  22. byte mac[] = {
  23. 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
  24. };
  25. // Set the static IP address to use if the DHCP fails to assign
  26. IPAddress ip(192, 168, 0, 177);
  27. IPAddress myDns(192, 168, 0, 1);
  28. // initialize the library instance:
  29. EthernetClient client;
  30. char server[] = "www.arduino.cc"; // also change the Host line in httpRequest()
  31. //IPAddress server(64,131,82,241);
  32. unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
  33. const unsigned long postingInterval = 10*1000; // delay between updates, in milliseconds
  34. void setup() {
  35. // You can use Ethernet.init(pin) to configure the CS pin
  36. //Ethernet.init(10); // Most Arduino shields
  37. //Ethernet.init(5); // MKR ETH shield
  38. //Ethernet.init(0); // Teensy 2.0
  39. //Ethernet.init(20); // Teensy++ 2.0
  40. //Ethernet.init(15); // ESP8266 with Adafruit Featherwing Ethernet
  41. //Ethernet.init(33); // ESP32 with Adafruit Featherwing Ethernet
  42. // start serial port:
  43. Serial.begin(9600);
  44. while (!Serial) {
  45. ; // wait for serial port to connect. Needed for native USB port only
  46. }
  47. // start the Ethernet connection:
  48. Serial.println("Initialize Ethernet with DHCP:");
  49. if (Ethernet.begin(mac) == 0) {
  50. Serial.println("Failed to configure Ethernet using DHCP");
  51. // Check for Ethernet hardware present
  52. if (Ethernet.hardwareStatus() == EthernetNoHardware) {
  53. Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
  54. while (true) {
  55. delay(1); // do nothing, no point running without Ethernet hardware
  56. }
  57. }
  58. if (Ethernet.linkStatus() == LinkOFF) {
  59. Serial.println("Ethernet cable is not connected.");
  60. }
  61. // try to congifure using IP address instead of DHCP:
  62. Ethernet.begin(mac, ip, myDns);
  63. Serial.print("My IP address: ");
  64. Serial.println(Ethernet.localIP());
  65. } else {
  66. Serial.print(" DHCP assigned IP ");
  67. Serial.println(Ethernet.localIP());
  68. }
  69. // give the Ethernet shield a second to initialize:
  70. delay(1000);
  71. }
  72. void loop() {
  73. // if there's incoming data from the net connection.
  74. // send it out the serial port. This is for debugging
  75. // purposes only:
  76. if (client.available()) {
  77. char c = client.read();
  78. Serial.write(c);
  79. }
  80. // if ten seconds have passed since your last connection,
  81. // then connect again and send data:
  82. if (millis() - lastConnectionTime > postingInterval) {
  83. httpRequest();
  84. }
  85. }
  86. // this method makes a HTTP connection to the server:
  87. void httpRequest() {
  88. // close any connection before send a new request.
  89. // This will free the socket on the WiFi shield
  90. client.stop();
  91. // if there's a successful connection:
  92. if (client.connect(server, 80)) {
  93. Serial.println("connecting...");
  94. // send the HTTP GET request:
  95. client.println("GET /latest.txt HTTP/1.1");
  96. client.println("Host: www.arduino.cc");
  97. client.println("User-Agent: arduino-ethernet");
  98. client.println("Connection: close");
  99. client.println();
  100. // note the time that the connection was made:
  101. lastConnectionTime = millis();
  102. } else {
  103. // if you couldn't make a connection:
  104. Serial.println("connection failed");
  105. }
  106. }