2

I am new to arduino programming and nodeMcu boards.I have two ESP8266 bards as access points and one board get some data form WiFi stations and resend that data to other access point. but the problem is second access point didn't receive any data packet from first access point.

This is the code of Access point which collect data and resend it to upper level Access point. This Access point successfully received the data from the WiFi stations.

const char *ssid1 ="SecondAP"; 
const char *pwd1 = "12345678910";

const char *ssid2 ="FirstAP"; 
const char *pwd2 = "123456789101";

ESP8266WiFiMulti WiFiMulti;

IPAddress local_IP(192,168,4,10); 
IPAddress gateway(192,168,4,9); 
IPAddress subnet(255,255,255,0);
IPAddress NextAP(192,168,4,9);
StaticJsonDocument<200> doc;
unsigned int locPort1 = 1000;
unsigned int locPort2 = 2000;
WiFiUDP udp1; 
WiFiUDP udp2; 
char packetBuffer[100];
IPAddress addrs;

void setup() {

  Serial.begin(9600);
  delay(10);
  Serial.println('\n');

  ConnectToAP();
  delay(1000);
  EnableAP();
  delay(1000);

  udp1.begin(locPort1);
  udp2.begin(locPort2);
}

void loop() {


int Icb = 0;

  Icb = udp1.parsePacket();

  if(Icb > 0){
      GetSensorDataPacket();
      Icb = 0;
    }
}

void EnableAP(){

  WiFi.softAPConfig(local_IP, gateway, subnet);
  WiFi.softAP(ssid2 , pwd2);
  delay(1000);

  Serial.print("Access Point \"");
  Serial.print(ssid2);
  Serial.println("\" started");
  Serial.print("IP address:\t");
  Serial.println(WiFi.softAPIP()); 
}

void ConnectToAP(){

  WiFi.mode(WIFI_STA);
  WiFiMulti.addAP(ssid1, pwd1);

  Serial.print("Connecting to ...........");
  Serial.print(ssid1); Serial.println("...");

  int i = 0;
  while (WiFiMulti.run() != WL_CONNECTED) {    

   delay(1000);
   Serial.print(++i); Serial.print(' ');
  }

  Serial.println('\n');
  Serial.println("Connection established!");  
  Serial.print("IP address:\t");
  Serial.println(WiFi.localIP());
}

void GetSensorDataPacket(){

  Serial.println("GetSensorDataPacket Function");
  udp1.read(packetBuffer,100);
  DeserializationError error = deserializeJson(doc, packetBuffer);

  IPAddress ClientAddrs = udp1.remoteIP();

  double SensorData = doc["SensorData"];
  int ClientID = doc["ID"];

  delay(1000);
  Serial.println(); 
  Serial.print("Sensor reading = ");
  Serial.println(SensorData);
  Serial.print("Client ID = ");
  Serial.println(ClientID);

  SendDataToNextAP(SensorData,ClientID); 
}

void SendDataToNextAP(double Sensor_Data,int Client_ID){

  StaticJsonDocument<500> jsonBuffr;
  Serial.println("I'm in SendDataToNextAP Function");

  udp2.beginPacket(NextAP, locPort1); //gateway
  JsonObject Sob1 = jsonBuffr.to<JsonObject>();
  Sob1["SensorData"] = Sensor_Data;
  Sob1["ID"] = Client_ID;
  serializeJson(jsonBuffr, udp2);

  udp2.println();
  udp2.endPacket();

  delay(1000);
}

This is the code of upper Access point which should receive data packet from first access point but didn't receive any data from the first access point.

const char *ssid = "SecondAP";
const char *pwd = "12345678910";
ESP8266WiFiMulti WiFiMulti;

IPAddress local_IP(192,168,4,9); // Ip address of this node
IPAddress gateway(192,168,3,1); 
IPAddress subnet(255,255,255,0);


StaticJsonDocument<200> doc;
unsigned int locPort1 = 1000;
WiFiUDP udp1;
char packetBuffer[100];

void setup() {

  Serial.begin(9600);
  delay(10);
  Serial.println('\n');

  EnableAP(); 
  udp1.begin(locPort1);
  delay(1000);
}

void loop() {

int Icb = 0;

  Icb = udp1.parsePacket();  
  if(Icb > 0){
      Serial.println("Got a Udp packet");
      GetSensorDataPacket();
    }
}

void EnableAP(){

  WiFi.softAPConfig(local_IP, gateway, subnet);
  WiFi.softAP(ssid,pwd);
  delay(1000);

  Serial.print("Access Point \"");
  Serial.print(ssid);
  Serial.println("\" started");
  Serial.print("IP address:\t");
  Serial.println(WiFi.softAPIP()); 
}

void GetSensorDataPacket(){

  Serial.println("GetSensorDataPacket Function");
  udp1.read(packetBuffer,100);
  DeserializationError error = deserializeJson(doc, packetBuffer);

  IPAddress ClientAddrs = udp1.remoteIP();

  double SensorData = doc["SensorData"];
  int ClientID = doc["ID"];

  delay(1000);
  Serial.println(); 
  Serial.print("Sensor reading = ");
  Serial.println(SensorData);
  Serial.print("Client ID = ");
  Serial.println(ClientID);    
}

I couldn't find the reason for that. If anyone knows the solution for that please help me?

Thanks!

  • you have two networks with the same IP range. and gateway(192,168,3,1); is what? – Juraj Aug 2 at 10:57
  • Sorry It's my mistake. it should be gateway(192,168,4,8). – CKJ Aug 2 at 18:04
  • Thank you very much..It works after change IP address. – CKJ Aug 3 at 3:21
0

The two network interfaces (station and SoftAP) of the middle esp8266 must have different IP address range. If the top AP has 192.168.4.x, set for the AP of the middle IP for example 192.168.5.1. Because with same IP address ranges the esp8266 doesn't know to which network do you want to send the UDP message.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.