Does any of you experienced Arduino professionals have any idea about how to use the Arduino OPTA WiFi connecting via HTTPS to a site?
All examples I have found use port 80, and I have not been able to find out if there is a library (ie. I have tried WiFiNINA without success) for making HTTPS connections (with TLS/SSL).
It is not because of that I want the HTTPS. It is because of that the server we are using have stopped offering HTTP on port 80. We get an error 301 - when trying to access the http://domain.com/opta.php file, we are told that it has moved to https://domain.com/opta.php - in effect meaning that the http on port 80 is unavailable.
So, we would indeed need a port 443 based WiFi library for the Arduino OPTA WiFi model.
My code looks like this here:
#include <WiFi.h>
#include <WiFiClient.h> // For HTTPS connections
const char* ssid = "xxxx"; // Your WiFi network name
const char* password = "xxxx"; // Your WiFi network password
const char* serverHost = "xxxxxx.xxxxxx"; // Your server's host URL (without http:// or https://)
const int serverPort = 443; // Server port (usually 80 for HTTP)
const String postPath = "/xxxx.php"; // The API endpoint path
void setup() {
Serial.begin(115200);
delay(1000);
digitalWrite(LED_D0, HIGH);
delay(1000);
digitalWrite(LED_D0, LOW);
// Connect to WiFi
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi...");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected to WiFi!");
}
void sendPostRequest() {
WiFiClient client;
// client.setInsecure(); // Bypass certificate verification for testing
// Connect to the server with HTTPS
if (!client.connect(serverHost, serverPort)) {
Serial.println("Connection to server failed!");
return;
}
Serial.println("Connected to server");
// Manually create the JSON string
String jsonPayload = "{\"Command\":\"Cadence\"}";
// Construct HTTPS POST request
client.println("POST " + postPath + " HTTP/1.1");
client.println("Host: " + String(serverHost));
client.println("Content-Type: application/json");
client.println("Connection: close");
client.print("Content-Length: ");
client.println(jsonPayload.length());
client.println();
client.println(jsonPayload); // Send JSON payload
// Wait for a response from the server
while (!client.available()) {
delay(10);
}
// Read and display the response
String response = client.readString();
Serial.println("Response:");
Serial.println(response);
// Parse the JSON response manually
parseResponse(response);
// Close the connection
client.stop();
}
void parseResponse(String response) {
int statusIndex = response.indexOf("\"Status\":\"");
int statusEnd = response.indexOf("\"", statusIndex + 10);
String statusValue = response.substring(statusIndex + 9, statusEnd);
int codeIndex = response.indexOf("\"Code\":");
int codeEnd = response.indexOf(",", codeIndex);
String codeValue = response.substring(codeIndex + 7, codeEnd);
int messageIndex = response.indexOf("\"Message\":\"");
int messageEnd = response.indexOf("\"", messageIndex + 11);
String messageValue = response.substring(messageIndex + 10, messageEnd);
Serial.println("Parsed JSON response:");
Serial.println("Status: " + statusValue);
Serial.println("Code: " + codeValue);
Serial.println("Message: " + messageValue);
}
void loop() {
sendPostRequest();
delay(10000); // Send the request every 10 seconds
}
skriv eller indsæt kode her
skriv eller indsæt kode her