Skip to content

Commit 0c60625

Browse files
committed
Ethernet: add WebClientSSL example
Former-commit-id: b876ce2
1 parent 862ceea commit 0c60625

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
TLS Ethernet Web client
3+
4+
Remeber to update the CA certificates using CertificateUploader sketch
5+
before using this sketch.
6+
7+
*/
8+
9+
#include <Ethernet.h>
10+
#include <EthernetSSLClient.h>
11+
12+
// if you don't want to use DNS (and reduce your sketch size)
13+
// use the numeric IP instead of the name for the server:
14+
//IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
15+
char server[] = "www.google.com"; // name address for Google (using DNS)
16+
17+
// Set the static IP address to use if the DHCP fails to assign
18+
IPAddress ip(192, 168, 0, 177);
19+
20+
// Initialize the Ethernet client library
21+
// with the IP address and port of the server
22+
// that you want to connect to (port 80 is default for HTTP):
23+
EthernetSSLClient client;
24+
25+
void setup() {
26+
// Open serial communications and wait for port to open:
27+
Serial.begin(9600);
28+
29+
while (!Serial) {
30+
; // wait for serial port to connect. Needed for native USB port only
31+
}
32+
33+
// start the Ethernet connection:
34+
Serial.println("Starting Ethernet");
35+
if (Ethernet.begin() == 0) {
36+
Serial.println("Failed to configure Ethernet using DHCP");
37+
// try to configure using IP address instead of DHCP:
38+
Ethernet.begin(ip);
39+
}
40+
// give the Ethernet shield a second to initialize:
41+
delay(1000);
42+
Serial.println("Connecting...");
43+
44+
// if you get a connection, report back via serial:
45+
if (client.connect(server, 443)) {
46+
Serial.println("connected");
47+
// Make a HTTP request:
48+
client.println("GET / HTTP/1.1");
49+
client.println("Host: www.google.com");
50+
client.println("Connection: close");
51+
client.println();
52+
} else {
53+
// if you didn't get a connection to the server:
54+
Serial.println("connection failed");
55+
}
56+
}
57+
58+
/* just wrap the received data up to 80 columns in the serial print*/
59+
void read_request() {
60+
uint32_t received_data_num = 0;
61+
while (client.available()) {
62+
/* actual data reception */
63+
char c = client.read();
64+
/* print data to serial port */
65+
Serial.print(c);
66+
/* wrap data to 80 columns*/
67+
received_data_num++;
68+
if(received_data_num % 80 == 0) {
69+
Serial.println();
70+
}
71+
}
72+
}
73+
74+
void loop() {
75+
76+
read_request();
77+
78+
// if the server's disconnected, stop the client:
79+
if (!client.connected()) {
80+
Serial.println();
81+
Serial.println("disconnecting.");
82+
client.stop();
83+
84+
// do nothing forevermore:
85+
while (true);
86+
}
87+
}

0 commit comments

Comments
 (0)