How do I make a connection to a host server that has been deployed on a service such as GCP or Railway?

I have used this library using a local IP address and it runs smoothly, but when I change the serverAddress to the API URL that I have hosted on Google Cloud Run, I cannot make an http connection, for example making a get request. I also don't know what the correct port is for the hosted serverAddress.

I am using Arduino Giga R1 Wifi.

char serverAddress[] = "https://safety-driving-backend-j4gottfrqa-et.a.run.app";
const int port = 8080;  // not sure its the correct port for hosted server api
HttpClient httpClient = HttpClient(wifi, serverAddress, port);

I moved your topic to an appropriate forum category @afdulrhmt.

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

the 3 lines of code don't make sense. If you use ArduinoHttpClient library, serverAddress should be something like "myapp.appspot.com", port should be 443 if it is https and wifi I guess is WiFiClient but should be WiFiSSLClient.

1 Like

thank you for your response.

I apologize for the incomplete code, here is my completed code that used to connect localhost server and its works well. And yes, i use the WiFiClient to handle wifi connection.

// M7 Core Sketch
#include <RPC.h>
#include <ArduinoHttpClient.h>
#include <WiFi.h>

char ssid[] = "myWifi;
char password[] = "myPassword";

char serverAddress[] = "192.168.xx.xx"; // localhost
const int port = 3000;
// char serverAddress[] = "https://safety-driving-backend-j4gottfrqa-et.a.run.app";  // my deployed host on gcp cloud run
// const int port = 443;


WiFiClient wifi;
int status = WL_IDLE_STATUS;

HttpClient httpClient = HttpClient(wifi, serverAddress, port);


void setup() {
  Serial.begin(9600);
  // RPC.begin();

  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to Network named: ");
    Serial.println(ssid);

    status = WiFi.begin(ssid, password);
    delay(1000);
  }

  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
}

void loop() {

  sendDataToServer();
  delay(5000);
}

void sendDataToServer() {
  if (WiFi.status() == WL_CONNECTED) {
    Serial.println("Sending data to server...");

    Serial.println("making GET request");

    httpClient.get("/"); // route api for testing the connection purpose. it will just return hello world

    // Read the response
    int statusCode = httpClient.responseStatusCode();
    String response = httpClient.responseBody();

    Serial.print("Status code: ");
    Serial.println(statusCode);
    Serial.print("Response: ");
    Serial.println(response);
  } else {
    Serial.println("WiFi Disconnected");
  }
}

Now I want to use serverHost which I have deployed on GCP, later I will make a post request to send certain data to the server.

I already try to set the serverAddress to

char serverAddress[] = "safety-driving-backend-j4gottfrqa-et.a.run.app"; 
const int port = 443;
HttpClient httpClient = HttpClient(wifi, serverAddress, port);

but still i can't connect. but I haven't tried using WiFiSSLClient before.

// M7 Core Sketch
#include <RPC.h>
#include <ArduinoHttpClient.h>
#include <WiFiSSLClient.h> // using this instead of Wifi.h

char ssid[] = "myWifi;
char password[] = "myPassword";

char serverAddress[] = "safety-driving-backend-j4gottfrqa-et.a.run.app";
const int port = 443;


WiFiSSLClient wifi; // already using WiFiSSLClient 
int status = WL_IDLE_STATUS;

HttpClient httpClient = HttpClient(wifi, serverAddress, port); 


void setup() {
  Serial.begin(9600);
  // RPC.begin();

  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to Network named: ");
    Serial.println(ssid);

    status = WiFi.begin(ssid, password);
    delay(1000);
  }

  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
}

void loop() {

  sendDataToServer();
  delay(10000);
}

void sendDataToServer() {
  if (WiFi.status() == WL_CONNECTED) {
    Serial.println("Sending data to server...");

    Serial.println("making GET request");

    httpClient.get("/");

    //Read the response 
    int statusCode = httpClient.responseStatusCode();
    String response = httpClient.responseBody();

    Serial.print("Status code: ");
    Serial.println(statusCode);
    Serial.print("Response: ");
    Serial.println(response);
  } else {
    Serial.println("WiFi Disconnected");
  }
}

[UPDATE]

I have tried your suggestion, using WiFiSSLClient, and finally successfully connected.

Thank you very much !

I've been debugging this program all day and I thought the problem was with the http library, didn't think at all that the wifi library also needed to be considered for https connections.

above is the updated code