Skip to content

Commit 995b7dd

Browse files
authored
Create my19-ethernet-webserver.ino
1 parent 9979867 commit 995b7dd

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed

Diff for: my19-ethernet-webserver.ino

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
2+
#include <Portenta_Ethernet.h>
3+
#include <Ethernet.h>
4+
5+
// Enter a MAC address and IP address for your controller below.
6+
// The IP address will be dependent on your local network:
7+
byte mac[] = {
8+
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
9+
};
10+
IPAddress ip(192, 168, 1, 177); // what are these for??
11+
12+
// Initialize the Ethernet server library
13+
// with the IP address and port you want to use
14+
// (port 80 is default for HTTP):
15+
EthernetServer server(80);
16+
17+
18+
19+
void setup() {
20+
21+
// Open serial communications and wait for port to open:
22+
Serial.begin(115200);
23+
pinMode(LED_BUILTIN, OUTPUT); // set the LED Green pin mode
24+
pinMode(LEDB, OUTPUT);
25+
digitalWrite(LEDB, HIGH);
26+
digitalWrite(LED_BUILTIN, LOW);
27+
28+
delay(5000); // time to get serial monitor working if needed
29+
Serial.println("Wait a few seconds to plug in serial");
30+
delay(5000);
31+
Serial.println(".");
32+
delay(5000);
33+
Serial.println(".");
34+
35+
36+
Serial.println("Ethernet WebServer Example");
37+
38+
// start the Ethernet connection and the server:
39+
Ethernet.begin(mac);
40+
41+
// Check for Ethernet hardware present
42+
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
43+
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
44+
while (true) {
45+
delay(1); // do nothing, no point running without Ethernet hardware
46+
}
47+
}
48+
if (Ethernet.linkStatus() == LinkOFF) {
49+
Serial.println("Ethernet cable is not connected.");
50+
}
51+
52+
// start the server
53+
server.begin();
54+
Serial.print("server is at ");
55+
Serial.println(Ethernet.localIP());
56+
digitalWrite(LEDB, LOW);
57+
digitalWrite(LED_BUILTIN, HIGH);
58+
}
59+
60+
61+
void loop() {
62+
// listen for incoming clients
63+
EthernetClient client = server.available();
64+
if (client) { // if you get a client,
65+
66+
Serial.print("server is at ");
67+
Serial.println(Ethernet.localIP());
68+
69+
Serial.println("new client"); // print a message out the serial port
70+
String currentLine = ""; // make a String to hold incoming data from the client
71+
while (client.connected()) { // loop while the client's connected
72+
if (client.available()) { // if there's bytes to read from the client,
73+
char c = client.read(); // read a byte, then
74+
Serial.write(c); // print it out the serial monitor
75+
if (c == '\n') { // if the byte is a newline character
76+
77+
// if the current line is blank, you got two newline characters in a row.
78+
// that's the end of the client HTTP request, so send a response:
79+
if (currentLine.length() == 0) {
80+
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
81+
// and a content-type so the client knows what's coming, then a blank line:
82+
client.println("HTTP/1.1 200 OK");
83+
client.println("Content-type:text/html");
84+
//client.println("{answer:42}");
85+
client.println();
86+
87+
// the content of the HTTP response follows the header:
88+
client.print("<input type=button value='LED BLUE Off' onclick='{location=\"/H\"}'>");
89+
client.print("<input type=button value='LED BLUE On' onclick='{location=\"/L\"}'>");
90+
91+
// The HTTP response ends with another blank line:
92+
client.println();
93+
// break out of the while loop:
94+
break;
95+
} else { // if you got a newline, then clear currentLine:
96+
currentLine = "";
97+
}
98+
} else if (c != '\r') { // if you got anything else but a carriage return character,
99+
currentLine += c; // add it to the end of the currentLine
100+
}
101+
102+
// Check to see if the client request was "GET /H" or "GET /L":
103+
if (currentLine.endsWith("GET /H")) {
104+
digitalWrite(LEDB, HIGH); // GET /H turns the LED on
105+
}
106+
if (currentLine.endsWith("GET /L")) {
107+
digitalWrite(LEDB, LOW); // GET /L turns the LED off
108+
}
109+
}
110+
}
111+
// close the connection:
112+
client.stop();
113+
114+
}
115+
}
116+

0 commit comments

Comments
 (0)