WebServer.ino 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. Description: Start the Web Server in the local network and the user can obtain the page response by accessing the assigned IP address.
  3. Please install library before compiling:
  4. Ethernet2: file in M5stack lib examples -> modules -> W5500 -> Ethernet2.zip (unzip the lib zip file to the Arduino Lib path)
  5. */
  6. #include <M5Stack.h>
  7. #include <SPI.h>
  8. #include <Ethernet2.h>
  9. #define SCK 18
  10. #define MISO 19
  11. #define MOSI 23
  12. #define CS 26
  13. // 01 05 00 01 02 00 9d 6a
  14. char uart_buffer[8] = {0x01, 0x05, 0x00, 0x01, 0x02, 0x00, 0x9d, 0x6a};
  15. char uart_rx_buffer[8] = {0};
  16. char Num = 0;
  17. char stringnum = 0;
  18. unsigned long W5500DataNum = 0;
  19. unsigned long Send_Num_Ok = 0;
  20. unsigned long Rec_Num = 0;
  21. unsigned long Rec_Num_Ok = 0;
  22. // Enter a MAC address and IP address for your controller below.
  23. // The IP address will be dependent on your local network:
  24. byte mac[] = {
  25. 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
  26. };
  27. IPAddress ip(192, 168, 1, 177);
  28. // Initialize the Ethernet server library
  29. // with the IP address and port you want to use
  30. // (port 80 is default for HTTP):
  31. EthernetServer server(80);
  32. void setup() {
  33. // Open serial communications and wait for port to open:
  34. M5.begin(true, false, true);
  35. M5.Power.begin();
  36. while (!Serial) {
  37. ; // wait for serial port to connect. Needed for Leonardo only
  38. }
  39. SPI.begin(SCK, MISO, MOSI, -1);
  40. Ethernet.init(CS);
  41. // start the Ethernet connection and the server:
  42. Ethernet.begin(mac, ip);
  43. server.begin();
  44. Serial.print("server is at ");
  45. Serial.println(Ethernet.localIP());
  46. M5.Lcd.println("M5Stack W5500 Test");
  47. M5.Lcd.println(" ");
  48. M5.Lcd.print(Ethernet.localIP());
  49. }
  50. void loop() {
  51. // listen for incoming clients
  52. EthernetClient client = server.available();
  53. if (client) {
  54. Serial.println("new client");
  55. // an http request ends with a blank line
  56. boolean currentLineIsBlank = true;
  57. while (client.connected()) {
  58. if (client.available()) {
  59. char c = client.read();
  60. Serial.write(c);
  61. // if you've gotten to the end of the line (received a newline
  62. // character) and the line is blank, the http request has ended,
  63. // so you can send a reply
  64. if (c == '\n' && currentLineIsBlank) {
  65. // send a standard http response header
  66. client.println("HTTP/1.1 200 OK");
  67. client.println("Content-Type: text/html");
  68. client.println("Connection: close"); // the connection will be closed after completion of the response
  69. client.println("Refresh: 5"); // refresh the page automatically every 5 sec
  70. client.println();
  71. client.println("<!DOCTYPE HTML>");
  72. client.println("<html>");
  73. client.println("<body>");
  74. client.println("<h1>M5Stack W5500 Test</h1>");
  75. client.println("<br />");
  76. client.println("<p>Please click here</p>");
  77. client.println("<a href=\"http://www.M5Stack.com\">M5Stack</a>");
  78. client.println("<br />");
  79. client.println("<br />");
  80. client.println("<br />");
  81. client.print("W5500 Counter Num :");
  82. client.print(W5500DataNum);
  83. client.println("<br />");
  84. client.println("<br />");
  85. W5500DataNum ++;
  86. client.print("Rec_Num_Ok Counter :");
  87. client.print(Rec_Num_Ok);
  88. client.println("<br />");
  89. client.println("<br />");
  90. client.print("Rec_Num Counter :");
  91. client.print(Rec_Num);
  92. client.println("<br />");
  93. client.println("<br />");
  94. client.println("</body>");
  95. client.println("</html>");
  96. break;
  97. }
  98. if (c == '\n') {
  99. // you're starting a new line
  100. currentLineIsBlank = true;
  101. }
  102. else if (c != '\r') {
  103. // you've gotten a character on the current line
  104. currentLineIsBlank = false;
  105. }
  106. }
  107. }
  108. // give the web browser time to receive the data
  109. delay(1);
  110. // close the connection:
  111. client.stop();
  112. Serial.println("client disconnected");
  113. }
  114. }