I have no clue what I am doing wrong lol

Hello everyone! I am new to arduinos and the arduino IDE, I do have years of programming knowledge behind me and I am developing a program with the UNO R4 WIFI board. I am wanting to send heart rate/pulse data to a remote python script that will send data into VRChat for a 3D avatar's pulsating glow effect. The connections needed are mostly there. However, I am having trouble connecting the arduino to a domain IP address via TCP communication. I have tried both HTTP and TCP and I have gotten NOTHING to connect to my domain IP from the arduino. The arduino can connect to the internet strangely enough though. The hosting software that acts as an API gateway for my program is ngrok. I have done projects with python in the past using the same setup with ngrok and have not had issues. I can only reason something is wrong on the arduino side. I have copy and pasted official arduino sample code and have run it and it still has not worked. No dependency issues either, just the script does not connect and I do not know why... I have pictures of my ngrok setup and the code that is in my sketch in the arduino IDE. Would anybody know at all what to do or what I am incorrectly doing? Thank you all for your time!


const char* WIFI_SSID = "SSID";          // CHANGE TO YOUR WIFI 
const char* WIFI_PASSWORD = "PASSWORD";       // CHANGE TO YOUR WIFI 
const char* TCP_SERVER_ADDR = "0.tcp.ngrok.io";  // CHANGE TO TCP SERVER'S IP ADDRESS
const int TCP_SERVER_PORT = 12865;

WiFiClient TCP_client;

void setup() {
  Serial.begin(115200);
  while (!(Serial)){
    ;
  }
  Serial.println("Arduino: TCP CLIENT");

  // check for the WiFi module:
  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    while (true)
      ;
  }

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

  Serial.print("Attempting to connect to SSID: ");
  Serial.println(WIFI_SSID);
  // attempt to connect to WiFi network:
  int status = 0;
  status = WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  while (status == 0) {
    delay(1000);  // wait 10 seconds for connection:
    status = WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  }

  Serial.print("Connected to WiFi ");
  Serial.println(WIFI_SSID);

  // connect to TCP server
  if (TCP_client.connect(TCP_SERVER_ADDR, TCP_SERVER_PORT)) {
    Serial.println("Connected to TCP server");
    TCP_client.write("Hello!");  // send to TCP Server
    TCP_client.flush();
  } else {
    Serial.println("Failed to connect to TCP server");
  }
}

void loop() {
  // Read data from server and print them to Serial
  if (TCP_client.available()) {
    char c = TCP_client.read();
    Serial.print(c);
  }

  if (!TCP_client.connected()) {
    Serial.println("Connection is disconnected");
    TCP_client.stop();

    // reconnect to TCP server
    if (TCP_client.connect(TCP_SERVER_ADDR, TCP_SERVER_PORT)) {
      Serial.println("Reconnected to TCP server");
      TCP_client.write("Hello!");  // send to TCP Server
      TCP_client.flush();
    } else {
      Serial.println("Failed to reconnect to TCP server");
      delay(1000);
    }
  }
}

Since your Arduino can connect to the internet but fails to connect to your ngrok-hosted API, the issue is likely with how the connection is being established.

If you check the ngrok logs (ngrok http --log=stdout), do see if any connection attempts from the Arduino are reaching ngrok?

if you run ngrok tcp <your-local-port> do you get tcp://0.tcp.ngrok.io:12865

Well, i have not tried the ngrok http --log command yet. However, to run ngrok to be a API gateway the command is ngrok tcp and the port i put is 730 bc it sets it up so a generated ngrok address sends data to localhost at port 730. In rephrased wording, the 0.tcp.ngrok.io and port of 12865 is auto generated by ngrok. The ngrok tcp 730 command just sets it up to receive data at localhost 730. I might try the http --log command but for this project, i just want to send pulse readings to a remote python script so i don't really need all the fancy setups that comes from doing HTTP requests. I was just trying to make a connection period. Ideally, finding a way to establish a TCP connection would be great but i dunno lol.

Exactly my question.

So if you run ngrok tcp 730 you should see tcp://0.tcp.ngrok.io:12865 which is what you are using in your code.

(I was trying to validate that this information is correct)

Oh, lol sorry its like 3 AM for me i was up late working on this. Yes, that information is correct.

OK so looking at the logs would provide some clue if any connection attempt was made.

I'll try that tomorrow and update this thread. Thank you so much!

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