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.

124 lines
3.8KB

  1. /*
  2. Web Server
  3. A simple web server that shows the value of the analog input pins.
  4. using an Arduino Wiznet Ethernet shield.
  5. Circuit:
  6. * Ethernet shield attached to pins 10, 11, 12, 13
  7. * Analog inputs attached to pins A0 through A5 (optional)
  8. created 18 Dec 2009
  9. by David A. Mellis
  10. modified 9 Apr 2012
  11. by Tom Igoe
  12. modified 02 Sept 2015
  13. by Arturo Guadalupi
  14. */
  15. #include <SPI.h>
  16. #include <Ethernet.h>
  17. // Enter a MAC address and IP address for your controller below.
  18. // The IP address will be dependent on your local network:
  19. byte mac[] = {
  20. 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
  21. };
  22. IPAddress ip(192, 168, 1, 177);
  23. // Initialize the Ethernet server library
  24. // with the IP address and port you want to use
  25. // (port 80 is default for HTTP):
  26. EthernetServer server(80);
  27. void setup() {
  28. // You can use Ethernet.init(pin) to configure the CS pin
  29. //Ethernet.init(10); // Most Arduino shields
  30. //Ethernet.init(5); // MKR ETH shield
  31. //Ethernet.init(0); // Teensy 2.0
  32. //Ethernet.init(20); // Teensy++ 2.0
  33. //Ethernet.init(15); // ESP8266 with Adafruit Featherwing Ethernet
  34. //Ethernet.init(33); // ESP32 with Adafruit Featherwing Ethernet
  35. // Open serial communications and wait for port to open:
  36. Serial.begin(9600);
  37. while (!Serial) {
  38. ; // wait for serial port to connect. Needed for native USB port only
  39. }
  40. Serial.println("Ethernet WebServer Example");
  41. // start the Ethernet connection and the server:
  42. Ethernet.begin(mac, ip);
  43. // Check for Ethernet hardware present
  44. if (Ethernet.hardwareStatus() == EthernetNoHardware) {
  45. Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
  46. while (true) {
  47. delay(1); // do nothing, no point running without Ethernet hardware
  48. }
  49. }
  50. if (Ethernet.linkStatus() == LinkOFF) {
  51. Serial.println("Ethernet cable is not connected.");
  52. }
  53. // start the server
  54. server.begin();
  55. Serial.print("server is at ");
  56. Serial.println(Ethernet.localIP());
  57. }
  58. void loop() {
  59. // listen for incoming clients
  60. EthernetClient client = server.available();
  61. if (client) {
  62. Serial.println("new client");
  63. // an http request ends with a blank line
  64. boolean currentLineIsBlank = true;
  65. while (client.connected()) {
  66. if (client.available()) {
  67. char c = client.read();
  68. Serial.write(c);
  69. // if you've gotten to the end of the line (received a newline
  70. // character) and the line is blank, the http request has ended,
  71. // so you can send a reply
  72. if (c == '\n' && currentLineIsBlank) {
  73. // send a standard http response header
  74. client.println("HTTP/1.1 200 OK");
  75. client.println("Content-Type: text/html");
  76. client.println("Connection: close"); // the connection will be closed after completion of the response
  77. client.println("Refresh: 5"); // refresh the page automatically every 5 sec
  78. client.println();
  79. client.println("<!DOCTYPE HTML>");
  80. client.println("<html>");
  81. // output the value of each analog input pin
  82. for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
  83. int sensorReading = analogRead(analogChannel);
  84. client.print("analog input ");
  85. client.print(analogChannel);
  86. client.print(" is ");
  87. client.print(sensorReading);
  88. client.println("<br />");
  89. }
  90. client.println("</html>");
  91. break;
  92. }
  93. if (c == '\n') {
  94. // you're starting a new line
  95. currentLineIsBlank = true;
  96. } else if (c != '\r') {
  97. // you've gotten a character on the current line
  98. currentLineIsBlank = false;
  99. }
  100. }
  101. }
  102. // give the web browser time to receive the data
  103. delay(1);
  104. // close the connection:
  105. client.stop();
  106. Serial.println("client disconnected");
  107. }
  108. }