I have an Arduino Opta, the one with the ethernet port. I'm trying to communicate with my server and it works fine over HTTP, but I can't get the SSL client to work for HTTPS.
For demonstration, I'm using the example files which are listed under "Examples for Opta > Ethernet". For starters, I'm using the following WebClientRepeating file:
/*
Repeating Web client
This sketch connects to a a web server and makes a request
using a Wiznet Ethernet shield. You can use the Arduino Ethernet shield, or
the Adafruit Ethernet shield, either one will work, as long as it's got
a Wiznet Ethernet module on board.
This example uses DNS, by assigning the Ethernet client with a MAC address,
IP address, and DNS address.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
created 19 Apr 2012
by Tom Igoe
modified 21 Jan 2014
by Federico Vanzati
http://www.arduino.cc/en/Tutorial/WebClientRepeating
This code is in the public domain.
*/
#include <SPI.h>
#include <PortentaEthernet.h>
#include <Ethernet.h>
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 177);
IPAddress myDns(192, 168, 0, 1);
// initialize the library instance:
EthernetClient client;
char server[] = "www.arduino.cc"; // also change the Host line in httpRequest()
//IPAddress server(64,131,82,241);
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
const unsigned long postingInterval = 10*1000; // delay between updates, in milliseconds
void setup() {
// start serial port:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// start the Ethernet connection:
Serial.println("Initialize Ethernet with DHCP:");
if (Ethernet.begin() == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
// try to congifure using IP address instead of DHCP:
Ethernet.begin(ip, myDns);
Serial.print("My IP address: ");
Serial.println(Ethernet.localIP());
} else {
Serial.print(" DHCP assigned IP ");
Serial.println(Ethernet.localIP());
}
// give the Ethernet shield a second to initialize:
delay(1000);
}
void loop() {
// if there's incoming data from the net connection.
// send it out the serial port. This is for debugging
// purposes only:
if (client.available()) {
char c = client.read();
Serial.write(c);
}
// if ten seconds have passed since your last connection,
// then connect again and send data:
if (millis() - lastConnectionTime > postingInterval) {
httpRequest();
}
}
// this method makes a HTTP connection to the server:
void httpRequest() {
// close any connection before send a new request.
// This will free the socket on the WiFi shield
client.stop();
// if there's a successful connection:
if (client.connect(server, 80)) {
Serial.println("connecting...");
// send the HTTP GET request:
client.println("GET /latest.txt HTTP/1.1");
client.println("Host: www.arduino.cc");
client.println("User-Agent: arduino-ethernet");
client.println("Connection: close");
client.println();
// note the time that the connection was made:
lastConnectionTime = millis();
} else {
// if you couldn't make a connection:
Serial.println("connection failed");
}
}
Which correctly outputs:
Initialize Ethernet with DHCP:
DHCP assigned IP 192.168.137.87
connecting...
HTTP/1.1 403 Forbidden
Date: Wed, 04 Sep 2024 08:43:34 GMT
Content-Type: text/html
(Just more fetched web content here)
The 403 should not matter, what I care about is that it succesfully connected and received a response from the server.
Now if I'm using the other example code, WebClientRepeatingSSL;
/*
Repeating TLS/SSL Ethernet Web client.
Remeber to update the CA certificates using WiFiFirmwareUpdater sketch
before using this sketch.
*/
#include <Ethernet.h>
#include <EthernetSSLClient.h>
#include <PortentaEthernet.h>
// initialize the library instance:
EthernetSSLClient client;
char server[] = "www.arduino.cc";
int port = 443;
// IPAddress server(64,131,82,241);
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 177);
IPAddress myDns(192, 168, 0, 1);
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
const unsigned long postingInterval = 10 * 1000; // delay between updates, in milliseconds
void setup()
{
// start serial port:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// start the Ethernet connection:
Serial.println("Initialize Ethernet with DHCP:");
if (Ethernet.begin() == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
// try to congifure using IP address instead of DHCP:
Ethernet.begin(ip, myDns);
Serial.print("My IP address: ");
Serial.println(Ethernet.localIP());
} else {
Serial.print(" DHCP assigned IP ");
Serial.println(Ethernet.localIP());
}
// give the Ethernet shield a second to initialize:
delay(1000);
}
void loop()
{
// if there's incoming data from the net connection.
// send it out the serial port. This is for debugging
// purposes only:
if (client.available()) {
char c = client.read();
Serial.write(c);
}
// if ten seconds have passed since your last connection,
// then connect again and send data:
if (millis() - lastConnectionTime > postingInterval) {
httpRequest();
}
}
// this method makes a HTTP connection to the server:
void httpRequest()
{
// close any connection before send a new request.
// This will free the socket on the WiFi shield
client.stop();
// if there's a successful connection:
if (client.connect(server, port)) {
Serial.println("connecting...");
// send the HTTP GET request:
client.println("GET /latest.txt HTTP/1.1");
client.print("Host: ");
client.println(server);
client.println("User-Agent: arduino-ethernet");
client.println("Accept: *");
client.println("Connection: close");
client.println();
// note the time that the connection was made:
lastConnectionTime = millis();
} else {
// if you couldn't make a connection:
Serial.println("connection failed");
}
}
Which creates the following output:
Initialize Ethernet with DHCP:
DHCP assigned IP 192.168.137.41
connection failed
connection failed
connection failed
connection failed
(And so forth)
I have tried messing around with EthernetSSLClient a fair bit but client.connect() just always fails and I don't know why. It's very strange to me that even the example code does not work. I've tried on several different networks and methods of connecting to make sure it's nothing being blocked outside of the PLC.
From what I've seen online I see a lot of updating the wifi firmware (but I'm using ethernet) or uploading certificates, but I don't know how to do that or where to get those or anything, does that only apply to hosting? I only want a client.
Super long post I'm sorry, but I hope being comprehensive helped make my issue clear!