8
8
9
9
#include < WiFiClientSecure.h>
10
10
11
- char ssid[] = " your_network_name " ; // your network SSID (name of wifi network)
12
- char pass[] = " your_password " ; // your network password
11
+ const char * ssid = " your-ssid " ; // your network SSID (name of wifi network)
12
+ const char * password = " your-password " ; // your network password
13
13
14
- char server[] = " www.howsmyssl.com" ; // Server URL
15
- // You can use x.509 certificates if you want
16
- // unsigned char test_ca_cert[] = ""; //For the usage of verifying server
17
- // unsigned char test_client_key[] = ""; //For the usage of verifying client
18
- // unsigned char test_client_cert[] = ""; //For the usage of verifying client
14
+ const char * server = " www.howsmyssl.com" ; // Server URL
15
+
16
+ // www.howsmyssl.com CA certificate, to verify the server
17
+ // change it to your server CA certificate
18
+ // SHA1 fingerprint is broken now!
19
+
20
+ const char * test_ca_cert = \
21
+ " -----BEGIN CERTIFICATE-----\n " \
22
+ " MIIEkjCCA3qgAwIBAgIQCgFBQgAAAVOFc2oLheynCDANBgkqhkiG9w0BAQsFADA/\n " \
23
+ " MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT\n " \
24
+ " DkRTVCBSb290IENBIFgzMB4XDTE2MDMxNzE2NDA0NloXDTIxMDMxNzE2NDA0Nlow\n " \
25
+ " SjELMAkGA1UEBhMCVVMxFjAUBgNVBAoTDUxldCdzIEVuY3J5cHQxIzAhBgNVBAMT\n " \
26
+ " GkxldCdzIEVuY3J5cHQgQXV0aG9yaXR5IFgzMIIBIjANBgkqhkiG9w0BAQEFAAOC\n " \
27
+ " AQ8AMIIBCgKCAQEAnNMM8FrlLke3cl03g7NoYzDq1zUmGSXhvb418XCSL7e4S0EF\n " \
28
+ " q6meNQhY7LEqxGiHC6PjdeTm86dicbp5gWAf15Gan/PQeGdxyGkOlZHP/uaZ6WA8\n " \
29
+ " SMx+yk13EiSdRxta67nsHjcAHJyse6cF6s5K671B5TaYucv9bTyWaN8jKkKQDIZ0\n " \
30
+ " Z8h/pZq4UmEUEz9l6YKHy9v6Dlb2honzhT+Xhq+w3Brvaw2VFn3EK6BlspkENnWA\n " \
31
+ " a6xK8xuQSXgvopZPKiAlKQTGdMDQMc2PMTiVFrqoM7hD8bEfwzB/onkxEz0tNvjj\n " \
32
+ " /PIzark5McWvxI0NHWQWM6r6hCm21AvA2H3DkwIDAQABo4IBfTCCAXkwEgYDVR0T\n " \
33
+ " AQH/BAgwBgEB/wIBADAOBgNVHQ8BAf8EBAMCAYYwfwYIKwYBBQUHAQEEczBxMDIG\n " \
34
+ " CCsGAQUFBzABhiZodHRwOi8vaXNyZy50cnVzdGlkLm9jc3AuaWRlbnRydXN0LmNv\n " \
35
+ " bTA7BggrBgEFBQcwAoYvaHR0cDovL2FwcHMuaWRlbnRydXN0LmNvbS9yb290cy9k\n " \
36
+ " c3Ryb290Y2F4My5wN2MwHwYDVR0jBBgwFoAUxKexpHsscfrb4UuQdf/EFWCFiRAw\n " \
37
+ " VAYDVR0gBE0wSzAIBgZngQwBAgEwPwYLKwYBBAGC3xMBAQEwMDAuBggrBgEFBQcC\n " \
38
+ " ARYiaHR0cDovL2Nwcy5yb290LXgxLmxldHNlbmNyeXB0Lm9yZzA8BgNVHR8ENTAz\n " \
39
+ " MDGgL6AthitodHRwOi8vY3JsLmlkZW50cnVzdC5jb20vRFNUUk9PVENBWDNDUkwu\n " \
40
+ " Y3JsMB0GA1UdDgQWBBSoSmpjBH3duubRObemRWXv86jsoTANBgkqhkiG9w0BAQsF\n " \
41
+ " AAOCAQEA3TPXEfNjWDjdGBX7CVW+dla5cEilaUcne8IkCJLxWh9KEik3JHRRHGJo\n " \
42
+ " uM2VcGfl96S8TihRzZvoroed6ti6WqEBmtzw3Wodatg+VyOeph4EYpr/1wXKtx8/\n " \
43
+ " wApIvJSwtmVi4MFU5aMqrSDE6ea73Mj2tcMyo5jMd6jmeWUHK8so/joWUoHOUgwu\n " \
44
+ " X4Po1QYz+3dszkDqMp4fklxBwXRsW10KXzPMTZ+sOPAveyxindmjkW8lGy+QsRlG\n " \
45
+ " PfZ+G6Z6h7mjem0Y+iWlkYcV4PIWL1iwBi8saCbGS5jN2p8M+X+Q7UNKEkROb3N6\n " \
46
+ " KOqkqm57TH2H3eDJAkSnh6/DNFu0Qg==\n " \
47
+ " -----END CERTIFICATE-----\n " ;
48
+
49
+ // You can use x.509 client certificates if you want
50
+ // const char* test_client_key = ""; //to verify the client
51
+ // const char* test_client_cert = ""; //to verify the client
19
52
20
53
21
54
WiFiClientSecure client;
@@ -27,7 +60,7 @@ void setup() {
27
60
28
61
Serial.print (" Attempting to connect to SSID: " );
29
62
Serial.println (ssid);
30
- WiFi.begin (ssid, pass );
63
+ WiFi.begin (ssid, password );
31
64
32
65
// attempt to connect to Wifi network:
33
66
while (WiFi.status () != WL_CONNECTED) {
@@ -38,39 +71,43 @@ void setup() {
38
71
39
72
Serial.print (" Connected to " );
40
73
Serial.println (ssid);
74
+
75
+ client.setCACert (test_ca_cert);
76
+ // client.setCertificate(certificateBuff); // for client verification
77
+ // client.setPrivateKey(privateKeyBuff); // for client verification
41
78
42
79
Serial.println (" \n Starting connection to server..." );
43
- if (client.connect (server, 443 )) { // client.connect(server, 443, test_ca_cert, test_client_cert, test_client_key)
80
+ if (!client.connect (server, 443 ))
81
+ Serial.println (" Connection failed!" );
82
+ else {
44
83
Serial.println (" Connected to server!" );
45
84
// Make a HTTP request:
46
85
client.println (" GET https://www.howsmyssl.com/a/check HTTP/1.0" );
47
86
client.println (" Host: www.howsmyssl.com" );
48
87
client.println (" Connection: close" );
49
88
client.println ();
50
- }
51
- else
52
- Serial.println (" Connection failed!" );
53
89
54
90
Serial.print (" Waiting for response " ); // WiFiClientSecure uses a non blocking implementation
55
91
while (!client.available ()){
56
92
delay (50 ); //
57
93
Serial.print (" ." );
58
94
}
59
- // if there are incoming bytes available
60
- // from the server, read them and print them:
61
- while (client.available ()) {
62
- char c = client.read ();
63
- Serial.write (c);
64
- }
95
+ // if there are incoming bytes available
96
+ // from the server, read them and print them:
97
+ while (client.available ()) {
98
+ char c = client.read ();
99
+ Serial.write (c);
100
+ }
65
101
66
- // if the server's disconnected, stop the client:
67
- if (!client.connected ()) {
68
- Serial.println ();
69
- Serial.println (" disconnecting from server." );
70
- client.stop ();
102
+ // if the server's disconnected, stop the client:
103
+ if (!client.connected ()) {
104
+ Serial.println ();
105
+ Serial.println (" disconnecting from server." );
106
+ client.stop ();
107
+ }
71
108
}
72
109
}
73
110
74
111
void loop () {
75
112
// do nothing
76
- }
113
+ }
0 commit comments