Using an api with Arduino Uno R4 wifi

I want to use the Alpha Vantage api to get stock quotes.
I have the api that works. I can run from my PC
curl -i "https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=IBM&apikey=xxxxxxxxxxxx"

And it returns data. It is the HTTPS connection over port 443

Now I have Arduino Uno R4WIFI and I use WiFiS3.h library, I wrote this code

#include <WiFiS3.h>

char ssid[] = "xxxx"; // your network SSID (name)
char pass[] = "xxxx"; // your network password
char server[] = "www.alphavantage.co"; // Alpha Vantage API server
char apiKey[] = "xxxxxxx";    // your Alpha Vantage API key
char symbol[] = "IBM";                 // Apple stock symbol

WiFiClient client;

void setup() {
  // initialize serial communication
  Serial.begin(9600);
  
  Serial.println("Connecting to WiFi...");
  while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
    Serial.print(".");
    delay(1000);
  } 

  Serial.println("Connected to WiFi");
}

void loop() {
  if (client.connect(server, 443)) {
    Serial.println("Connected to server");
    
    // make a HTTP GET request to Alpha Vantage API
    client.print("GET /query?function=TIME_SERIES_DAILY&symbol=");
    client.print(symbol);
    client.print("&apikey=");
    client.print(apiKey);

    
    // wait for response
    while (client.available()) {
      char c = client.read();
      Serial.print(c);
    }
    
    // close connection
    client.stop();
  } else {
    // if connection failed
    Serial.println("Connection failed");
    // Attempt to reconnect to WiFi
    reconnectWiFi();
  }
  
  // wait before making next request
  delay(60000); // wait 1 minute before making next request
}

void reconnectWiFi() {
  Serial.println("Attempting to reconnect to WiFi...");
  WiFi.disconnect();
  delay(1000);
  while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
    Serial.print(".");
    delay(1000);
  }
  Serial.println("Reconnected to WiFi");

This does not work. I am afraid the connect is not the right methos and connectSSL does not seem to exist for this library. Anybody has a clue how this can be fixed

Yeah, you need to go secure..
did you see any of the examples??
/WiFiS3/examples/WiFiWebClientSSL/WiFiWebClientSSL.ino

seems pretty simple..

good luck.. ~q

I made changes to the code `#include <WiFiS3.h>

#include <WiFiS3.h>
#include "WiFiSSLClient.h"

char ssid[] = "xxxxxx"; // your network SSID (name)
char pass[] = "xxxxx"; // your network password
char server[] = "www.alphavantage.co"; // Alpha Vantage API server
char apiKey[] = "xxxxx";    // your Alpha Vantage API key
char symbol[] = "IBM";                 // Apple stock symbol

WiFiSSLClient client;



void setup() {
  Serial.begin(115200);
  while (!Serial);

  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    while (true);
  }

  String fv = WiFi.firmwareVersion();
  if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
    Serial.println("Please upgrade the firmware");
  }

  int status = WL_IDLE_STATUS;
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    status = WiFi.begin(ssid, pass);
    delay(10000);
  }

  Serial.println("Starting connection to server...");
  if (client.connect(server, 443)) {
    Serial.println("Connected to server");
    // Make a HTTPS request
    client.print("GET /query?function=TIME_SERIES_DAILY&symbol=");
    client.print(symbol);
    client.print("&apikey=");
    client.print(apiKey);
    client.println(" HTTP/1.1");
    client.print("Host: ");
    client.println(server);
    client.println("Connection: close");
    client.println();
  }
}

void read_response() {
  while (client.available()) {
    char c = client.read();
    Serial.print(c);
  }
}

void loop() {
  read_response();
  if (!client.connected()) {
    Serial.println("disconnecting from server.");
    client.stop();
    while (true);
  }
}

But stil does not work

I've been looking at this too..
I'm finding the documentation lacking..
Usually with SSL you have certs to deal with or you go inSecure and except anything..
There's 2 links at the top of the demo, one of them is talking about certs that seem to be preloaded..
curious though..

can you elaborate some on this??
does it connect to the server??
i whois'd your link, it's not a godaddy, you may need to get a cert..
or maybe there is a way to except any cert thrown at you but I don't see this..
Looking at the source I do see..

 void setCACert(const char* root_ca);

curious, does the example work??
it just connects to google, which I see thay have a cert for..

~q

When I run this example I get

Attempting to connect to SSID: Dziubym
SSID: Dziubym
IP Address: 192.168.68.117
signal strength (RSSI):-82 dBm

Starting connection to server...

disconnecting from server.

When using curl on my PC I do not need any certificate to get fetch data so here this should also not be required.
I am stuck I am afraid.

So i guess i need to use a different board:(

I'm still thinking might just need a cert..
Does the example code linked too work??
It tries to connect to google..
~q

curious..
what does this do??

#include <WiFiS3.h>
#include "WiFiSSLClient.h"

const char ISRG_ROOT_X2[] = R"(
-----BEGIN CERTIFICATE-----
MIICGzCCAaGgAwIBAgIQQdKd0XLq7qeAwSxs6S+HUjAKBggqhkjOPQQDAzBPMQsw
CQYDVQQGEwJVUzEpMCcGA1UEChMgSW50ZXJuZXQgU2VjdXJpdHkgUmVzZWFyY2gg
R3JvdXAxFTATBgNVBAMTDElTUkcgUm9vdCBYMjAeFw0yMDA5MDQwMDAwMDBaFw00
MDA5MTcxNjAwMDBaME8xCzAJBgNVBAYTAlVTMSkwJwYDVQQKEyBJbnRlcm5ldCBT
ZWN1cml0eSBSZXNlYXJjaCBHcm91cDEVMBMGA1UEAxMMSVNSRyBSb290IFgyMHYw
EAYHKoZIzj0CAQYFK4EEACIDYgAEzZvVn4CDCuwJSvMWSj5cz3es3mcFDR0HttwW
+1qLFNvicWDEukWVEYmO6gbf9yoWHKS5xcUy4APgHoIYOIvXRdgKam7mAHf7AlF9
ItgKbppbd9/w+kHsOdx1ymgHDB/qo0IwQDAOBgNVHQ8BAf8EBAMCAQYwDwYDVR0T
AQH/BAUwAwEB/zAdBgNVHQ4EFgQUfEKWrt5LSDv6kviejM9ti6lyN5UwCgYIKoZI
zj0EAwMDaAAwZQIwe3lORlCEwkSHRhtFcP9Ymd70/aTSVaYgLXTWNLxBo1BfASdW
tL4ndQavEi51mI38AjEAi/V3bNTIZargCyzuFJ0nN6T5U6VR5CmD1/iQMVtCnwr1
/q4AaOeMSQ+2b1tbFfLn
-----END CERTIFICATE-----
)";


char ssid[] = "xxxxxx"; // your network SSID (name)
char pass[] = "xxxxx"; // your network password
char server[] = "www.alphavantage.co"; // Alpha Vantage API server
char apiKey[] = "xxxxx";    // your Alpha Vantage API key
char symbol[] = "IBM";                 // Apple stock symbol

WiFiSSLClient client;



void setup() {
  Serial.begin(115200);
  while (!Serial);

  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    while (true);
  }

  String fv = WiFi.firmwareVersion();
  if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
    Serial.println("Please upgrade the firmware");
  }

  int status = WL_IDLE_STATUS;
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    status = WiFi.begin(ssid, pass);
    delay(10000);
  }
  
  client.setCaCert(ISRG_ROOT_X2);

  Serial.println("Starting connection to server...");
  if (client.connect(server, 443)) {
    Serial.println("Connected to server");
    // Make a HTTPS request
    client.print("GET /query?function=TIME_SERIES_DAILY&symbol=");
    client.print(symbol);
    client.print("&apikey=");
    client.print(apiKey);
    client.println(" HTTP/1.1");
    client.print("Host: ");
    client.println(server);
    client.println("Connection: close");
    client.println();
  }
}

void read_response() {
  while (client.available()) {
    char c = client.read();
    Serial.print(c);
  }
}

void loop() {
  read_response();
  if (!client.connected()) {
    Serial.println("disconnecting from server.");
    client.stop();
    while (true);
  }
}

untested sorry..

~q

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.