ESP32 Won't Connect To Host/Server

Hi, I'm trying to send the serial communication output of Arduino UNO - ESP32 to 000Webhost, but it always failed.
This is the command I use. I hope someone can help me :slight_smile:

#include <SoftwareSerial.h>

#include "WiFi.h"
#include "HTTPClient.h"
//variabel software serial
SoftwareSerial DataSerial (18, 19);

//milis
unsigned long previousMillis = 0;
const long interval = 3000;

//variabel array  pharsing
String arrData[32];

//var for wifi and pss
const char* ssid = "WHERE";
const char* pass = "Exercise-ok-";

//variabel server
const char* host = "faotdaud27.000webhostapp.com";

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  DataSerial.begin(9600);

  //connect to wifi
  WiFi.begin(ssid, pass);
  Serial.println("Connecting....");
  while(WiFi.status() != WL_CONNECTED)
  {
    Serial.print(".");
    delay(500);
  }
  //if connected to wifi
  Serial.println("Connected");
}

void loop() {
  // conf millis
  unsigned long currentMillis = millis ();
  if(currentMillis - previousMillis >= interval)
  {
    //update premillis
    previousMillis =currentMillis;

 
    //read dataserial
    String data = "";
    while(DataSerial.available()>0)
    {
      data += char(DataSerial.read());
    }
    data.trim();
    
    if(data != "")
        {
          // format data "122#52.00#223"
          //pharsing
          int index =0;
          for(int i=0; i<= data.length() ; i++)
          {
            char delimiter = '#' ;
            if(data[i] != delimiter)
            arrData[index] += data[i];
            else 
            index++; //batasi klo tidak variabel + 1
          }

          if(index == 2)
          {
            Serial.println(arrData[0]);
            Serial.println(arrData[1]);
            Serial.println(arrData[2]);
          }
          String tds = arrData[0];
          String ph = arrData[1];
          String aliran = arrData[2];

            arrData[0] = "";
          arrData[1] = "";
          arrData[2] = "";
          
          // send data to server
          WiFiClient client ;
          // inisialisasi port

          const int httpPort = 80;
          if( !client.connect(host, httpPort) );
          {
            Serial.println("Connection Failed");
            return;
          }
          //send to web

          String Link;
          HTTPClient http;

          Link ="http://" + String(host) + "/senddata.php?tds=" + String(tds) + "&ph=" + String(ph) + "&aliran=" + String(aliran);
          
          http.begin(Link);
          http.GET();

          // respon 
          String respon = http.getString();
          Serial.println(respon);
          http.end();

        }
      
      DataSerial.println("Yes");
      
  }
}

this program succeeds when connecting to the router, but fails when connecting to the server/host

if the code in post #1 is for an ESP32 why are you using SoftwareSerial - the ESP32 has hardware serial ports
can you upload (as text not a screen image) of the program serial monitor output?
are the ESP32 and server on the same network subnet?

To be honest, I'm learning, and I'm following the tutorial videos I found on YouTube.

for serial monitor output is
"......Connected
Connection Failed"

For ".....Connected" successfully connected to the Router/hotspot
And "Connection failed" is a failure to connect to the host

As for the network subnet, I don't know yet. how to check it?

Hi @arcvinci,

would you mind to try a

from your computer just to make sure that the host is online and reachable from your network ...?

Yes, I've tried it. The host is online and reachable from my network.

if I try it it displays

ping hydroponicsen.000webhostapp.com

Pinging us-east-1.route-1.000webhost.awex.io [145.14.144.21] with 32 bytes of data:
Request timed out.
Request timed out.
Request timed out.
Request timed out.

Ping statistics for 145.14.144.21:
    Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),

sorry it is an old link. here is the new link i am using now faotdaud27.000webhostapp.com

Ok. I can see that page in my browser (but there is still no ping returned, maybe the server does block ICMP ...).

My suggestion is to

  • strap down your software just to the parts required for connecting to the WiFi and the host
  • try to connect with other hosts

e.g. like this example

https://www.arduino.cc/reference/en/libraries/wifi/client.connect/

This way you might come closer to the reason (e.g. a problem with a firewall or your internet gateway).

Good luck!

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