Static ip bug? esp32 received his own messages...

martin.ger
Posts: 1
Joined: Tue Jul 09, 2019 12:37 pm

Static ip bug? esp32 received his own messages...

Postby martin.ger » Tue Jul 09, 2019 1:00 pm

Hi i hope someone can help me with my problem. if i use static ips for the esp32 this esp received his own messages( ips all the same that the non static) so i don't know how to solve this problem.

I want to build a communication with around 1000 nodes (First and last node have a distance of around 100Meters) in line one of them is the root node and send every messages to the next esp. so i need every node with ap+sta and with a fixed ip i know which one is where. And i know tcp is slow the code here needs around 700ms to send a message to the next esp/ with udp 5ms.. tcp is only for testing.. if someone had a good idea how to solve the send receiving problem it would be great. and im open for all good ideas to make the communication better. And sorry for my english i hope everybody understand what i want so say :-)



Here my code:


#include <Arduino.h>


#include <WiFi.h>
#include <WiFiMulti.h>

WiFiMulti WiFiMulti;
const char* ssidap = "node2";
const char* passwordap = "12345678";
int counter = 2;
WiFiServer server(80);



void setup()
{
Serial.begin(115200);



//This part of code will try create static IP address subnet, primaryDNS, secondaryDNS)) {
//if (!WiFi.config(local_IP, gateway , subnet)) {
//Serial.println("STA Failed to configure");
//}
//WiFi.mode(WIFI_AP_STA); wenn du das ausführst wars das ^^
// WiFi.mode(WIFI_MODE_APSTA);wenn du das ausführst wars das ^^
delay(10);


if (counter==2){

IPAddress local_IP(192, 168, 4, 2);
// Set your Gateway IP address
IPAddress gateway(192, 168, 4, 1);

IPAddress subnet(255, 255, 255, 0);
IPAddress primaryDNS(192, 168, 4, 1); //dont work!!!
IPAddress secondaryDNS(4, 4, 4, 4); //??
primaryDNS=IPAddress(192,168, 4, 1); works fine:-)
secondaryDNS=IPAddress(4, 4, 4, 4);
//WiFi.softAPConfig(local_IP,local_IP, IPAddress(255, 255, 255, 0));
if (!WiFi.config(local_IP, gateway , subnet, primaryDNS, secondaryDNS)) {
Serial.println("STA Failed to configure");
}

}
WiFi.softAP("node2", "12345678");
WiFiMulti.addAP("node1", "12345678");



Serial.println();
Serial.println();
Serial.print("Waiting for WiFi... ");

while(WiFiMulti.run() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}


Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());

delay(500);
server.begin();
}

int value = 0;

void loop()
{
IPAddress IP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(IP);
Serial.println("");
Serial.println("WiFi connected!");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.print("ESP Mac Address: ");
Serial.println(WiFi.macAddress());
Serial.print("Subnet Mask: ");
Serial.println(WiFi.subnetMask());
Serial.print("Gateway IP: ");
Serial.println(WiFi.gatewayIP());
Serial.print("DNS: ");
Serial.println(WiFi.dnsIP());



//server code!
WiFiClient client = server.available(); // listen for incoming clients

if (client) { // if you get a client,
Serial.println("New Client."); // print a message out the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
String c = client.readStringUntil('\r'); // read a byte, then
Serial.println(c); // print it out the serial monitor
client.printf("hallo vom client");
// client.printf(counter);
client.stop();
Serial.println("Client Disconnected.");
}
}
}

// client code!!
const uint16_t port = 80;
const char * host = "192.168.4.1"; // ip or dns

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

// Use WiFiClient class to create TCP connections
// WiFiClient client;

if (!client.connect(host, port)) {
Serial.println("Connection failed.");
Serial.println("Waiting 5 seconds before retrying...");
delay(5000);
return;
}

// This will send a request to the server
client.print("Send this data to the server n");
//client.printf(counter);
//read back one line from the server
String line = client.readStringUntil('\r');
client.println(line);

Serial.println("Closing connection.");
client.stop();





}

Who is online

Users browsing this forum: No registered users and 9 guests