-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement #8: Support SSL/TLS connection by adding TLSClient
We add a new `TLSClient` class as part of the LWiFi library, it * Wraps mbedTLS calls * Provide an example that connects to http://howsmyssl.com Some implementation details: * mbedTLS is already in the liblinkit.a * Switch the default mbedTLS config file to config-mtk-websocket.h which have a larger TLS content buffer. * Increase FreeRTOS heap size to 120KB because increasing the TLS content buffer results in larger heap memory usage. * Also fixes #12 by correct use `const char*` in ssid parameter in `WiFi` APIs.
- Loading branch information
Showing
12 changed files
with
663 additions
and
27 deletions.
There are no files selected for viewing
130 changes: 130 additions & 0 deletions
130
...rd_party/arduino/hardware/arduino/mt7697/libraries/LWiFi/examples/TLSClient/TLSClient.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/* | ||
Web client | ||
This sketch connects to a website (http://download.labs.mediatek.com) | ||
using LinkIt 7697 | ||
This example is written for a network using WPA encryption. For | ||
WEP or WPA, change the Wifi.begin() call accordingly. | ||
Circuit: | ||
* LinkIt 7697 | ||
created 13 July 2010 | ||
by dlf (Metodo2 srl) | ||
modified 31 May 2012 | ||
by Tom Igoe | ||
modified Jan 2017 | ||
by MediaTek Labs | ||
*/ | ||
|
||
#include <LWiFi.h> | ||
|
||
char ssid[] = "your_ap_ssid"; // your network SSID (name) | ||
char pass[] = "your_ap_password"; // your network password (use for WPA, or use as key for WEP) | ||
int keyIndex = 0; // your network key Index number (needed only for WEP) | ||
|
||
int status = WL_IDLE_STATUS; | ||
char server[] = "www.howsmyssl.com"; // This website checks TLS/SSL capabilities | ||
|
||
// This is the root certificate for our host. | ||
// Different host server may have different root CA. | ||
static const char rootCA[] = "-----BEGIN CERTIFICATE-----\r\n" | ||
"MIIDSjCCAjKgAwIBAgIQRK+w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ipxZz\r\n" | ||
"R8srzJmwN0jP41ZL9c8PDHIyh8bwRLtTcm1D9SZImlJnt1ir/md2cXjbDaJWFBM5\r\n" | ||
"JDGFoqgCWjBH4d1QB7wCCZAA62RjYJsWvIjJEubSfZGL+T0yjWW06XyxV3bqxbYo\r\n" | ||
"Ob8VZRzI9neWagqNdwvYkQsEjgfbKbYK7p2CNTUQ\r\n" | ||
"-----END CERTIFICATE-----\r\n"; | ||
|
||
// Initialize the Ethernet client library | ||
// with the IP address and port of the server | ||
// that you want to connect to (port 80 is default for HTTP): | ||
TLSClient client; | ||
|
||
void setup() { | ||
//Initialize serial and wait for port to open: | ||
Serial.begin(9600); | ||
|
||
while (!Serial) { | ||
; // wait for serial port to connect. Needed for native USB port only | ||
} | ||
|
||
// attempt to connect to Wifi network: | ||
while (status != WL_CONNECTED) { | ||
Serial.print("Attempting to connect to SSID: "); | ||
Serial.println(ssid); | ||
// Connect to WPA/WPA2 network. Change this line if using open or WEP network: | ||
status = WiFi.begin(ssid, pass); | ||
// wait 2 seconds for connection: | ||
delay(2000); | ||
} | ||
Serial.println("Connected to wifi"); | ||
printWifiStatus(); | ||
|
||
Serial.println("\nStarting connection to server..."); | ||
// We must set root CA before connecting to host | ||
// Note that the lenght includes the terminating NULL, | ||
// so use sizeof() instead of strlen(). | ||
client.setRootCA(rootCA, sizeof(rootCA)); | ||
if (client.connect(server, 443)) { | ||
Serial.println("connected to server (GET)"); | ||
// Make a HTTP request over SSL (HTTPS) | ||
client.println("GET https://www.howsmyssl.com/a/check HTTP/1.1"); | ||
client.println("Host: www.howsmyssl.com"); | ||
client.println("Accept: */*"); | ||
client.println("Connection: close"); | ||
client.println(); | ||
delay(300); | ||
} | ||
} | ||
|
||
void loop() { | ||
// if there are incoming bytes available | ||
// from the server, read them and print them: | ||
while (client.available()) { | ||
char c = client.read(); | ||
Serial.write(c); | ||
} | ||
|
||
// if the server's disconnected, stop the client: | ||
if (!client.connected()) { | ||
Serial.println(); | ||
Serial.println("disconnecting from server."); | ||
client.stop(); | ||
|
||
// do nothing forevermore: | ||
while (true); | ||
} | ||
} | ||
|
||
|
||
void printWifiStatus() { | ||
// print the SSID of the network you're attached to: | ||
Serial.print("SSID: "); | ||
Serial.println(WiFi.SSID()); | ||
|
||
// print your WiFi shield's IP address: | ||
IPAddress ip = WiFi.localIP(); | ||
Serial.print("IP Address: "); | ||
Serial.println(ip); | ||
|
||
// print the received signal strength: | ||
long rssi = WiFi.RSSI(); | ||
Serial.print("signal strength (RSSI):"); | ||
Serial.print(rssi); | ||
Serial.println(" dBm"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.