Connection not possible

so on one of your Arduinos you need to use WiFiClient which will require an SSID and password to connect to the WiFi router.

there's no chance of establishing a connection to the other device if it is WiFi and hasn't connected to the WiFi router

again, and again , have you simply tries using the Ethernet or Wifi functions to report you localIP? is it set to what you think it is


here is some older code i used to communicate between WiFi devices (esp32). (i've abandoned this approach for broadcast UDP)


#ifdef ESP32
#include <WiFi.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#endif

#include "eeprom.h"
#include "node.h"
#include "wifi.h"

int dbgWifi = 1;

// -----------------------------------------------------------------------------
// WiFi and JMRI Server Definitions
WiFiClient          wifi;

#define EEPROM_CREDENTIALS
 
#ifndef EEPROM_CREDENTIALS
const char *ssid  = "wally";
const char *pass  = "Pan0rama";

const char *host  = "192.168.0.41";

#else
char host [STR_SIZE] = "";
char ssid [STR_SIZE] = "";
char pass [STR_SIZE] = "";
#endif

int         port  = 4445;

// -------------------------------------
const char *Nstr [] = { "N_NUL", "N_INIT", "N_UP", "N_DOWN" };
enum { N_NUL, N_INIT, N_UP, N_DOWN };

struct Node {
    int        state;
    char       ip [16];
 // IPAddress  ip;
    WiFiClient client;
};

#define MaxClient    10
Node nodes [MaxClient] = {
    { N_INIT, "192.168.0.41" },
};
int nNodes = 1;

// -------------------------------------
void
wifiIpAdd (
    char *ip )
{
    if (MaxClient <= nNodes)  {
        printf ("%s: maxClients, %d, reached\n", __func__, nNodes);
        return;
    }

    Node *p = nodes;
    for (int n = 0; n < nNodes; n++, p++)
        if (! strcmp (p->ip, ip))  {
            printf ("%s: duplicate %s\n", __func__, ip);
            return;
        }

    nodes [nNodes].state = N_INIT;
    strcpy (nodes [nNodes].ip, ip);
    nNodes++;
}

// -------------------------------------
void
wifiIpClr (void)
{
    nNodes = 0;
}

// -------------------------------------
char *
wifiIpGet (
    int idx )
{
    if (nNodes <= idx)
        return NULL;
    return nodes [idx].ip;
}

// -------------------------------------
void
wifiIpList (void)
{
    printf ("%s:\n", __func__);
    Node *p = nodes;
    for (int n = 0; n < nNodes; n++, p++)  {
        printf (" %s: %2d: %d %-8s ", __func__, n, p->state, Nstr [p->state]);
        printf ("%s\n", p->ip);
    }
}

// ---------------------------------------------------------
enum { ST_NUL, ST_INIT, ST_GET_CREDENTIALS, ST_CHK, ST_UP, ST_ERROR };

const char *wifiStStr [] = {
    "ST_NUL",
    "ST_INIT",
    "ST_GET_CREDENTIALS",
    "ST_CHK",
    "ST_UP",
    "ST_ERROR"
};

int state    = ST_NUL;
int stateLst = ST_NUL;

// -------------------------------------
// connect to wifi
void wifiInit (void)
{
    WiFi.mode (WIFI_STA);

#ifdef ESP32
    WiFi.hostname (name);
#else
    printf ("default hostname: %s\n", WiFi.hostname().c_str());
    WiFi.hostname (name);
    printf ("new hostname: %s\n", WiFi.hostname().c_str());
#endif

    printf ("%s: ssid %s, pass %s\n", __func__, ssid, pass);
    WiFi.begin (ssid, pass);
}

// -------------------------------------
bool
wifiCheck (void)
{
    static unsigned long msecLst = 0;

    if ( (msec - msecLst) < 1000)
        return false;
    msecLst = msec;

    printf (" %s:", __func__);

    if (WL_CONNECTED != WiFi.status ())  {
        printf (" not connected\n");
        return false;
   }

   IPAddress ip = WiFi.localIP ();

   printf (" connected %d:%d:%d:%d\n", ip [0], ip[1], ip [2], ip[3]);

   return true;
}

// -------------------------------------------------------------------
// connect to jmri
void nodeConnect (void)
{
    do {
        printf (" ... Node connecting - %s %d\n", host, port);

    } while (! wifi.connect(host, port));
    printf (" connected Node %s %d\n", host, port);
}

// ---------------------------------------------------------
// display wifi responses on serial monitor
void wifiReceive (void)
{
    static char cLst = 0;

    while (wifi.available()) {
        char c = wifi.read ();
        if ('\r' == c)
            continue;

        if ('\n' == cLst && '\n' == c)
            continue;

        Serial.write (c);
        cLst = c;
    }
}

// ---------------------------------------------------------
// common routine for sending strings to wifi and flushing
void
wifiSend (
    const char*  msg )
{
    if (ST_UP != state)
        return;

    if (dbgWifi)  {
        printf ("wifiSend: %s\n", msg);
    }

    Node *p = nodes;
    for (int n = 0; n < nNodes; n++, p++)  {
        // handle re/connect
        if (N_INIT == p->state)  {
            if (p->client.connect (p->ip, port))  {
                p->state = N_UP;
                Serial.print ("  successful connect to ");
            }
            else {
                p->state = N_DOWN;
                Serial.print ("    failed connect to ");
            }
            Serial.println (p->ip);
        }

        // attempt send if connected
        if (N_UP == p->state)  {
            if (! p->client.print (msg)) {
                p->state = N_INIT;      // make one attempt to re-connect
                Serial.print ("    failed to send to  ");
                Serial.println (p->ip);
            }
            else {
                p->client.flush ();
                Serial.print ("  send succeeded to  ");
                Serial.println (p->ip);
            }
        }
    }
}

// ---------------------------------------------------------
void
wifiGetCredentials (void)
{
#ifdef EEPROM_CREDENTIALS
    if (! eepromRead (ID_HOSTNAME, host, sizeof(host)))  {
        printf ("%s: no hostname\n", __func__);
        state = ST_ERROR;
        error = ERR_NO_HOST;
    }

    if (! eepromRead (ID_SSID, ssid, sizeof(host)))  {
        printf ("%s: no ssid\n", __func__);
        state = ST_ERROR;
        error = ERR_NO_SSID;
    }

    if (! eepromRead (ID_PASSWORD, pass, sizeof(host))) {
        printf ("%s: no password\n", __func__);
        state = ST_ERROR;
        error = ERR_NO_PASS;
    }
#endif
}

// -------------------------------------
void
wifiReset (void)
{
    printf ("%s: ssid %s, pass %s, host %s\n", __func__, ssid, pass, host);
    state = ST_NUL;
}

// -------------------------------------
void
wifiMonitor (void)
{
    if (stateLst != state)
        printf ("%s: %d %s\n", __func__, state, wifiStStr [state]);
    stateLst = state;

    switch (state)  {
    case ST_NUL:
        if (ssid [0])
            state = ST_INIT;
        else {
            printf ("%s: no SSID\n", __func__);
            state = ST_ERROR;
        }
        break;

    case ST_INIT:
        wifiInit ();
        state = ST_CHK;
        break;

#if 0
    case ST_GET_CREDENTIALS:
        wifiGetCredentials ();

        if (ST_ERROR != state)  {
            printf ("%s: credentials acquired\n", __func__);
            wifiInit ();
            state = ST_CHK;
        }
        break;
#endif

    case ST_CHK:
        if (wifiCheck ())
            state = ST_UP;
        break;

    case ST_UP:
        break;

    case ST_ERROR:
    default:
        break;
    }
}

so on one of your Arduinos you need to use WiFiClient which will require an SSID and password to connect to the WiFi router.:
Yes exactly thats why Client(instead of wificlient) and ap(instead of wifi router) i have use client here (in the first diagram at the time of starting the topics i have shared).

don't understand what this is trying to say.

a Wifi router is an access point. without a cable, the arduino needs a WiFI board (esp8266), must use WiFi.h, must connect to the access point and must connect to the other device (IP address and port).

if there's an Ethernet board, the Arduino needs a cable connection to the AP/router and needs to connect to the other device (IP address and port)

i think we've been thru this a few times now. not sure what you don't understand

Sir sorry for late response. Thanks for your help and interest to solve the problem.I also found u have shared a code from ur old coding panel to help me out - it is so nice of u(but i think the code is not so useful in that case).

Is it not possible to connect the 2 arduino+ethernetshield without wifi / lan cable(RJ 45). I am trying to do the connection with an wifi router/ access point.
Here i am trying to use a WLAN AP/ Bridge/ Client. Now what will be diagram? May i use the same to communicate between 2 arduino using a addon client (wireless clients). On that case should i need to use the wifi.h or ethernet.h is enough to handle the situation.

If i think that is the right diagram what u want to say then in code for segment 1 and 2 should i use wifi.h or ethernet.h or both in code? Also tell me Is ESP8266 is required to connect in between that?

maybe this will help you Communication between Two Arduino

Yes i tried that one earlier also but not working. It showing "connection not established" and "client unavailable". I use a display to track result in each important line of code.

how exactly are your board connected?

on one side arduino uno with ethernet shield connected or placed just above. From that ethernet shield that connected to router/ap/hub using Rj45 cable/ lan cable. On another side same arduino uno with ethernet shield connected or placed just above. But that side no router/ap/hub are connected. Try to use the ip and mac to connect the other side.
The example which u have shared to me i think that using the lan cable to connect 2 arduino+ethernet shield.

don't you need a cable from the router to each ethernet shield?

from the router, you should see active ethernet connections for each ethernet shield and the IP addresses assigned to them

don't you need a cable from the router to each ethernet shield?
Yes but the in one side(segment 1 where the input is giving) it connected to router but other end if the cable added then it will be difficult to manage as i told u earlier the distance between 2 arduino+ethernet shield or segment 1 and segment 2 is 20 meter or more and the segment 2 will be moving(no scope to adding the cable).

from the router, you should see active ethernet connections for each ethernet shield and the IP addresses assigned to them?
yes i can see the ip address but when using those ip address directly in code then that is not working.

i'm confused

then you need to use WiFi

i'm confused - I do not able to understand why u are getting confused?

then you need to use WiFi - where i will place the ESP8266. in left part or right part or both? Also tell me can i able to add the wifi.h and ethernet.h library in same code. If so then for server and ip which ip address i need to use (ESP8266 one/ ethernet shield one/ ap one)?

because you repeatedly said you couldn't connect an Arduino to a router because of distance but hadn't accepted the need to use WiFi

isn't the esp8266 a standalone processor that can be programmed using the Arduino IDE?