Skip to content

Commit 5a18521

Browse files
committed
Refactor EthernetSSLClient
1 parent ddf0a93 commit 5a18521

File tree

3 files changed

+104
-41
lines changed

3 files changed

+104
-41
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
Repeating TLS/SSL Ethernet Web client.
3+
4+
Remeber to update the CA certificates using WiFiFirmwareUpdater sketch
5+
before using this sketch.
6+
7+
*/
8+
9+
#include <Ethernet.h>
10+
#include <EthernetSSLClient.h>
11+
#include <PortentaEthernet.h>
12+
13+
// initialize the library instance:
14+
EthernetSSLClient client;
15+
16+
char server[] = "www.arduino.cc";
17+
int port = 443;
18+
// IPAddress server(64,131,82,241);
19+
20+
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
21+
const unsigned long postingInterval = 10 * 1000; // delay between updates, in milliseconds
22+
23+
void setup()
24+
{
25+
26+
// start serial port:
27+
Serial.begin(9600);
28+
while (!Serial) {
29+
; // wait for serial port to connect. Needed for native USB port only
30+
}
31+
32+
// start the Ethernet connection:
33+
Serial.println("Initialize Ethernet with DHCP:");
34+
if (Ethernet.begin() == 0) {
35+
Serial.println("Failed to configure Ethernet using DHCP");
36+
// Check for Ethernet hardware present
37+
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
38+
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
39+
while (true) {
40+
delay(1); // do nothing, no point running without Ethernet hardware
41+
}
42+
}
43+
if (Ethernet.linkStatus() == LinkOFF) {
44+
Serial.println("Ethernet cable is not connected.");
45+
}
46+
// try to congifure using IP address instead of DHCP:
47+
Ethernet.begin(ip, myDns);
48+
Serial.print("My IP address: ");
49+
Serial.println(Ethernet.localIP());
50+
} else {
51+
Serial.print(" DHCP assigned IP ");
52+
Serial.println(Ethernet.localIP());
53+
}
54+
// give the Ethernet shield a second to initialize:
55+
delay(1000);
56+
}
57+
58+
void loop()
59+
{
60+
// if there's incoming data from the net connection.
61+
// send it out the serial port. This is for debugging
62+
// purposes only:
63+
if (client.available()) {
64+
char c = client.read();
65+
Serial.write(c);
66+
}
67+
68+
// if ten seconds have passed since your last connection,
69+
// then connect again and send data:
70+
if (millis() - lastConnectionTime > postingInterval) {
71+
httpRequest();
72+
}
73+
}
74+
75+
// this method makes a HTTP connection to the server:
76+
void httpRequest()
77+
{
78+
// close any connection before send a new request.
79+
// This will free the socket on the WiFi shield
80+
client.stop();
81+
82+
// if there's a successful connection:
83+
if (client.connect(server, port)) {
84+
Serial.println("connecting...");
85+
// send the HTTP GET request:
86+
client.println("GET /latest.txt HTTP/1.1");
87+
client.print("Host: ");
88+
client.println(server);
89+
client.println("User-Agent: arduino-ethernet");
90+
client.println("Accept: *");
91+
client.println("Connection: close");
92+
client.println();
93+
94+
// note the time that the connection was made:
95+
lastConnectionTime = millis();
96+
} else {
97+
// if you couldn't make a connection:
98+
Serial.println("connection failed");
99+
}
100+
}

libraries/Ethernet/src/EthernetSSLClient.cpp

Lines changed: 0 additions & 5 deletions
This file was deleted.

libraries/Ethernet/src/EthernetSSLClient.h

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -21,48 +21,16 @@
2121
#define ETHERNETSSLCLIENT_H
2222

2323
#include "EthernetClient.h"
24-
25-
#include <FATFileSystem.h>
26-
#include <MBRBlockDevice.h>
27-
#include <QSPIFBlockDevice.h>
24+
#include "MbedSSLClient.h"
2825

2926
extern const char CA_CERTIFICATES[];
3027

3128
namespace arduino {
3229

33-
class EthernetSSLClient : public arduino::EthernetClient {
34-
35-
public:
36-
EthernetSSLClient();
37-
virtual ~EthernetSSLClient() {
38-
stop();
39-
}
40-
41-
int connect(IPAddress ip, uint16_t port) {
42-
return connectSSL(ip, port);
43-
}
44-
int connect(const char* host, uint16_t port) {
45-
return connectSSL(host, port, _disableSNI);
46-
}
47-
void disableSNI(bool statusSNI) {
48-
_disableSNI = statusSNI;
49-
}
50-
51-
private:
52-
int setRootCA() {
53-
54-
QSPIFBlockDevice root;
55-
mbed::MBRBlockDevice wifi_data(&root, 1);
56-
mbed::FATFileSystem wifi("wlan");
57-
58-
int err = wifi.mount(&wifi_data);
59-
if (err)
60-
return err;
61-
62-
return ((TLSSocket*)sock)->set_root_ca_cert_path("/wlan/");
30+
class EthernetSSLClient : public arduino::MbedSSLClient {
31+
NetworkInterface *getNetwork() {
32+
return Ethernet.getNetwork();
6333
}
64-
65-
bool _disableSNI;
6634
};
6735

6836
}

0 commit comments

Comments
 (0)