Skip to content

Commit ad9ca0a

Browse files
committed
Add WebServer example
1 parent d006dd3 commit ad9ca0a

File tree

1 file changed

+118
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)