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