Description
connect()
/ connectSSL()
– function establishes a connection to a TCP listening server (e.g. a webserver). The connectSSL() function works the same way as the “classic” connect() function of the various technology classes but uses the SSL / TLS security encryption for the data transfer. E.g. WiFiClient’s connect() and connetSSL() functions.
Syntax
myClient.connect( IPAddress ip, uint16_t port);
myClient.connect( const char *host, uint16_t port);
myClient.connectSSL( IPAddress ip, uint16_t port );
myClient.connectSSL( const char *host, uint16_t port );
*note the SSL equivalent is not available on dice-ETH and the CAN2SER monitor in CANIP provider
Parameters
IPAddress ip: an IP address (e.g. IPAddress(80, 237, 128, 56))
const char *host: pointer to an array of characters / URL of the server
optional: uint16_t ou16_port: TCP port of the request
Returns
int:
0 – error no connection could be established
1 – OK. Connection could be established
Example
WiFiHTTPClient myClient;
myClient.connect( "myserver.com", 30000 );
Example Code
The following example can be used on a dice-WiFi because it makes use of the HTTP_Client type: WiFiHTTPClient
. If you like to use the code on a different device, replace it by any of the following #includes & classes: CANIP_Client, EthernetHTTPClient or GSMHTTPClient
To test the example please replace the server and port with your own server URL. Then open a listening port on the server e.g. by using the netcat command: nc -l -p30000
#include <WiFiNINA.h>
char ssid[] = "mySSID";
char pass[] = "myWiFiPassword";
char server[] = "MyServer.com";
uint16_t serverPort = 30000;
WiFiHTTPClient myClient;
void setup()
{
while(!Serial);
Serial.println("WiFiHTTPClient Connect Example");
while(WiFi.begin(ssid, pass) != WL_CONNECTED)
delay(5000);
if(!myClient.connect(server, serverPort))
Serial.println("Failed to connect to server");
}
void loop() {
while((myClient.connected()) || myClient.available())
{
if(Serial.peek() != -1) myClient.write(Serial.read());
if(myClient.peek() != -1) Serial.write(myClient.read());
}
Serial.println("Connection closed");
while(1){yield();};
}
Notes and Warnings
The SSL functionality depends on the capabilities of the modules and transmission technology that is used (e.g. WiFi, LTE etc.). The communication devices (WiFi, IoT) come delivered with a number of preinstalled certificates (note that dice-eth and the can2ser monitor-can-ip provider do not yet support SSL). If you need to connect to any specific SSL/TLS-host that is not covered, please install the appropriate root certificate first. This can be achieved with the help of the CAN2SER monitor:
- Menu command:
misc -> Generate a certificates file
(filename must end with .pem) - Complie and flash the sketch Console_Dev.ino on the target (
Files -> Example -> Console - > Console_Dev
) - Download the generated certificate (xyz.pem) file onto the target (
misc -> send file to device
) - In the C2S console use the commands
gsm_cert_def
(dice-IoT) orwifi_cert
(dice-WiFi) to flash the new certificates onto the modem.
Alternatively, you can use the corresponding C functions of the libraries to build your own certificate-update system within your application. Use the Cosole_Dev source code (consoleWiFiDev.cpp / consoleGSMCertConfig.cpp) and the library code (WiFiNina / DICENB) as reference:
The source code of the libraries can be found locally on your computer here:
C:\Users\yourName\AppData\Local\Arduino15\packages\dice\hardware\samc\1.0.0\libraries
Examples can be found in the Arduino IDE menu:
File -> Examples -> DICE -> HTTP_Client
See also
Find more details here: