Skip to content

Commit e8d76e4

Browse files
committed
WiFi: add WebClientSSL example
Former-commit-id: 4137256
1 parent c511839 commit e8d76e4

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
TLS WiFi Web client
3+
4+
Remeber to update the CA certificates using CertificateUploader sketch
5+
before using this sketch.
6+
7+
*/
8+
9+
#include "CWifi.h"
10+
#include "WiFiSSLClient.h"
11+
#include "IPAddress.h"
12+
13+
#include "arduino_secrets.h"
14+
15+
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
16+
char ssid[] = SECRET_SSID; // your network SSID (name)
17+
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
18+
19+
int status = WL_IDLE_STATUS;
20+
// if you don't want to use DNS (and reduce your sketch size)
21+
// use the numeric IP instead of the name for the server:
22+
//IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
23+
char server[] = "www.google.com"; // name address for Google (using DNS)
24+
25+
// Initialize the Ethernet client library
26+
// with the IP address and port of the server
27+
// that you want to connect to (port 80 is default for HTTP):
28+
WiFiSSLClient client;
29+
30+
/* -------------------------------------------------------------------------- */
31+
void setup() {
32+
/* -------------------------------------------------------------------------- */
33+
//Initialize serial and wait for port to open:
34+
Serial.begin(115200);
35+
while (!Serial) {
36+
; // wait for serial port to connect. Needed for native USB port only
37+
}
38+
39+
// check for the WiFi module:
40+
if (WiFi.status() == WL_NO_MODULE) {
41+
Serial.println("Communication with WiFi module failed!");
42+
// don't continue
43+
while (true);
44+
}
45+
46+
String fv = WiFi.firmwareVersion();
47+
if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
48+
Serial.println("Please upgrade the firmware");
49+
}
50+
51+
// attempt to connect to WiFi network:
52+
while (status != WL_CONNECTED) {
53+
Serial.print("Attempting to connect to SSID: ");
54+
Serial.println(ssid);
55+
// Connect to WPA/WPA2 network.
56+
status = WiFi.begin(ssid, pass);
57+
58+
// wait 10 seconds for connection:
59+
delay(10000);
60+
}
61+
62+
printWifiStatus();
63+
64+
Serial.println("\nStarting connection to server...");
65+
// if you get a connection, report back via serial:
66+
if (client.connect(server, 443)) {
67+
Serial.println("connected to server");
68+
// Make a HTTP request:
69+
client.println("GET /search?q=arduino HTTP/1.1");
70+
client.println("Host: www.google.com");
71+
client.println("Connection: close");
72+
client.println();
73+
}
74+
}
75+
76+
/* just wrap the received data up to 80 columns in the serial print*/
77+
/* -------------------------------------------------------------------------- */
78+
void read_response() {
79+
/* -------------------------------------------------------------------------- */
80+
uint32_t received_data_num = 0;
81+
while (client.available()) {
82+
/* actual data reception */
83+
char c = client.read();
84+
/* print data to serial port */
85+
Serial.print(c);
86+
/* wrap data to 80 columns*/
87+
received_data_num++;
88+
if(received_data_num % 80 == 0) {
89+
Serial.println();
90+
}
91+
}
92+
}
93+
94+
/* -------------------------------------------------------------------------- */
95+
void loop() {
96+
/* -------------------------------------------------------------------------- */
97+
read_response();
98+
99+
// if the server's disconnected, stop the client:
100+
if (!client.connected()) {
101+
Serial.println();
102+
Serial.println("disconnecting from server.");
103+
client.stop();
104+
105+
// do nothing forevermore:
106+
while (true);
107+
}
108+
}
109+
110+
/* -------------------------------------------------------------------------- */
111+
void printWifiStatus() {
112+
/* -------------------------------------------------------------------------- */
113+
// print the SSID of the network you're attached to:
114+
Serial.print("SSID: ");
115+
Serial.println(WiFi.SSID());
116+
117+
// print your board's IP address:
118+
IPAddress ip = WiFi.localIP();
119+
Serial.print("IP Address: ");
120+
Serial.println(ip);
121+
122+
// print the received signal strength:
123+
long rssi = WiFi.RSSI();
124+
Serial.print("signal strength (RSSI):");
125+
Serial.print(rssi);
126+
Serial.println(" dBm");
127+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#define SECRET_SSID ""
2+
#define SECRET_PASS ""

0 commit comments

Comments
 (0)