Receiving information from 2 remote wireless arduinos at ESP8266 Wifi AP

Hi, I am using ESP8266 as my Access point and want to receive information from 2 remote arduino devices simultaneously. I am just able to get the information from one of those remote arduinos. My code at the access point is given as under:

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <WiFiUdp.h>
#include <TimeLib.h>

//These are the avariables you can change for your project. For example, change the SSID (network ID), change the ports on both machines.
//The most useful one is the LOOP_DELAY which changes how quicly it refreshes. set to 100ms be default.
char ssid[] = "ESPAP";      //  your network SSID (name)
char password[] = "mPass@54321";  //Set the AP's password
unsigned int localPort = 2390;        // local port to listen on
#define LOOP_DELAY    10             //how fast we check for new data
#define ARRAYSIZE     255             //size of message array (255bytes)
#define BAUD_RATE     115200          //serial comunication speed
//global variables
IPAddress remoteIp;
int remoteUDPPort;
char packetBuffer[ARRAYSIZE]; //buffer to hold incoming packet
int arraySize = ARRAYSIZE;
char inData[ARRAYSIZE]; // Allocate some space for the string
char inChar; // Where to store the character read
byte aindex = 0; // Index into array; where to store the character
boolean dataToSend = false;
WiFiUDP Udp;
void setup()
{
  Serial.begin(BAUD_RATE);
  /* You can remove the password parameter if you want the AP to be open. */
  WiFi.softAP(ssid, password);    //Start AP
  // IPAddress myIP = WiFi.softAPIP(); if ever in doubt on this devices IP address you can get it here. Should be 192.168.4.1
  Udp.begin(localPort);
  WiFi.setSleepMode(WIFI_NONE_SLEEP);
}
void loop() {
 while(Serial.available() > 0) //check for something to read from serial port
  {
      if(aindex < (arraySize-1)) // One less than the size of the array
      {
          inChar = Serial.read();   // Read a character
          inData[aindex] = inChar;  // Store it
          aindex++;                 // Increment where to write next
          inData[aindex] = '\0';    // Null terminate the string
      }
      dataToSend = true;
       }
  //check for incoming data via UDP
  int packetSize = Udp.parsePacket();
  if (packetSize)
  {
    remoteIp = Udp.remoteIP();
    remoteUDPPort = Udp.remotePort();
    int len = Udp.read(packetBuffer, 255);  //read in the packet
    if (len > 0)
    {
      packetBuffer[len] = 0;
    }
     Serial.println(packetBuffer);
  }
  // send UDP Data - this sends data to the last device it receives data from. For a one to one link this is fine. If there are multiple clients then this may need to be adjusted to store and send to all clients.
  if(dataToSend)
  {
    if(Udp.beginPacket(remoteIp, remoteUDPPort))
    {
      Udp.write(inData);
      Udp.endPacket();
      //Serial.println(inData); //Uncomment this line for a local echo of the data sent
      }
    else
    {
      Serial.println("No connection");
    }
    dataToSend = false; //set the flag to false, ie only send when you need to.
    for (aindex=0; aindex < arraySize; aindex++)  //wipe the array
    {
       inData[aindex] = '\0'; // Null terminate the string
    }
    aindex = 0; //reset the index
     }
   
}

The information sent by remote devices are preceded by an identifier A01, B01. The information received from one remote device is :

B01 23.00 2384 916.35 5.14 0.000 5.00 -23.00
B01 23.00 2412 916.35 5.14 0.000 5.00 -23.00
B01 23.00 2466 916.35 5.15 0.000 5.00 -23.00

whereas i do not receive from A01. Kindly suggest. Many thanks

What do you receive when two people are talking to you at the same time?

Paul

Thanks Paul for your response. But ESP supports 4 clients when acting as an AP, isnt that so ?

So the ESP does connect to more than 1 clients but displays only for 1 client. Can anyone please suggest how to get the data displayed from the other clients as well ?

Thanks

sal_murd:
whereas i do not receive from A01. Kindly suggest. Many thanks

Do you get the data from A01 when you disable B01 ?

...R

Robin2:
Do you get the data from A01 when you disable B01 ?

...R

Yes. Actually its not "disabling" B01,i just attempt to send from A01 which results in no data from B01

Can tell how esp at firmware is doing that might help. I have same system running in my house 4 esp arduinos and one main unit. All done via at commands. Im using tcp instead of udp.
Yes esp does support 4 clients but that doesnt mean that it can handle 4 even 2 at once. Client connects to esp got its ip then at tcp request server applies identifier and status of client i.e. 0 Connect. Which means client 0 connected then second client request will get 1 connect and so on. So im saving id and what was requested via +ipd identifier to reply back to same id. Or else it will reply to the last device which sends request. Kinda ids will be overwritten.
Example:
0, connect asking for something
1, connect asking for something

First request is getting processed while receiving 2nd request from second device. When reply for 1st device is ready esp sends back using id that was saved for that particular request. Then deals with second devices request.

And they are not getting mixed up cos of saving id at request time. If id will not be saved it will send reply for the first device to the last requested device-sender.

Again im not using library only at commands all parsing processing are done on unos. Works flawlessly without restarts or halts ~ 2 months already.

Thanks surepic. My question is do you have to create a dhcp pool to allocate IP addresses to different remote devices. The remote arduino always gets 192.168.4.2 when it connects to AP. That is, when the 2nd device attempts to send its udp packets, the first one disconnects and also gets the same IP Address.

Also, how would you use TCP as there is no library as there is for UDP?

Appreciate your response.

Yes you have to activate dhcp pool on main unit giving ip range . And on clients too.

As i said im not using library only at commands found it easier to use and very reliable.

surepic:
Yes you have to activate dhcp pool on main unit giving ip range . And on clients too.

As i said im not using library only at commands found it easier to use and very reliable.

If you are not using UDP, how would you send information over the wireless network, as I am doing it through

Udp.beginPacket(remoteIp, remoteUDPPort)
Udp.write(inData);
Udp.endPacket();
? Any idea on that ?

Thanks a lot

I think you dont get me. I am using AT firmware not a library thats the first point.
Second:
Esp main unit is running as softap and webserver. Also dhcp server.

Clients are in client modes. At first power up they are connecting to softap and getting their ip. Meanwhile softap is registering which ips have been reserved and is assigning id to them.
Then clients want to send something to server so its opening tcp connection via cipstart and sending whatever it want. At same time server is getting request with +ipd identifier . And replying back if needed. Why client requests arent getting mixed up because with each request server is saving id and processing that request separately from lets say request from another device.

Hope you got it now.