-->
Page 1 of 2

Multiple ESP8266's on a same network

PostPosted: Thu Aug 30, 2018 7:34 am
by Jovan Jovanovic
Hi. As you can see, I'm a newbie.

I want to connect 2 or more EPS8266 devices to a single network and control their pins for a school project.
One device works perfectly. When I add another one, that's when the problem starts. Seems like one ESP disconnects the other.
I have no idea what causes it.
Also, I'd like to keep static IP address.

(I apologize in advance if this question has been asked. I tried, but was unable to find it)
Appreciate all the help I can get.

This is the code I'm using, it's relatively simple:

Code: Select all#include <ESP8266WiFi.h>
 
const char* ssid = "ESP Test";
const char* password = "88888888";
 
; //
WiFiServer server(80);
 
void setup() {
  Serial.begin(115200);
  delay(10);
  pinMode(0, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(0, OUTPUT);
  pinMode(13, OUTPUT);
  digitalWrite(0, LOW);
  digitalWrite(2, LOW);
  digitalWrite(0, LOW);
  digitalWrite(13, LOW);
 
  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
 
WiFi.begin(ssid, password);
IPAddress ip(192,168,5,199); ////////////////////////////////////////////adresa
IPAddress gateway(192,168,5,1);    ///////////////////////////////////ruter
IPAddress subnet(255,255,255,0);   
WiFi.config(ip, gateway, subnet);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
 
  // Start the server
  server.begin();
  Serial.println("Server started");
 
  // Print the IP address
  Serial.print("Use this URL to connect: ");
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.println("/");
 
}
 
void loop() {
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
 
  // Wait until the client sends some data
  Serial.println("new client");
  while(!client.available()){
    delay(1);
  }
 
  // Read the first line of the request
  String request = client.readStringUntil('\r');
  Serial.println(request);
  client.flush();
 
  // Match the request
 
 
  if (request.indexOf("/light1on") > 0)  {
    digitalWrite(0, HIGH);
   
  }
  if (request.indexOf("/light1off") >0)  {
    digitalWrite(0, LOW);
   
  }

   if (request.indexOf("/light2on") > 0)  {
    digitalWrite(2, HIGH);
   
  }
  if (request.indexOf("/light2off") >0)  {
    digitalWrite(2, LOW);
   
  }
    if (request.indexOf("/light3on") >0)  {
    digitalWrite(0, HIGH);
   
  }
  if (request.indexOf("/light3off") > 0)  {
    digitalWrite(0, LOW);
   
  }
   if (request.indexOf("/light4on") > 0)  {
    digitalWrite(13, HIGH);
   
  }
  if (request.indexOf("/light4off") > 0)  {
    digitalWrite(13, LOW);
   
  }
// Set ledPin according to the request
//digitalWrite(ledPin, value);
 
  // Return the response
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println(""); //  do not forget this one
  client.println("<!DOCTYPE HTML>");
  client.println("<html>");
  client.println("<head>");
  client.println("<meta name='apple-mobile-web-app-capable' content='yes' />");
  client.println("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />");
 client.println("</head>");
  client.println("<body bgcolor = \"#f7e6ec\">");
  client.println("<hr/><hr>");
  client.println("<h4><center> Esp8266 Electrical Device Control </center></h4>");
  client.println("<hr/><hr>");
  client.println("<br><br>");
  client.println("<br><br>");
  client.println("<center>");
  client.println("Device 1");
  client.println("<a href=\"/light1on\"\"><button>Turn On </button></a>");
  client.println("<a href=\"/light1off\"\"><button>Turn Off </button></a><br />"); 
  client.println("</center>");   
  client.println("<br><br>");
   client.println("<center>");
   client.println("Device 2");
  client.println("<a href=\"/light2on\"\"><button>Turn On </button></a>");
  client.println("<a href=\"/light2off\"\"><button>Turn Off </button></a><br />"); 
client.println("</center>");
  client.println("<br><br>");
    client.println("<center>");
   client.println("Device 3");
  client.println("<a href=\"/light3on\"\"><button>Turn On </button></a>");
  client.println("<a href=\"/light3off\"\"><button>Turn Off </button></a><br />"); 
client.println("</center>");
  client.println("<br><br>");
   client.println("<center>");
   client.println("Device 4");
  client.println("<a href=\"/light4on\"\"><button>Turn On </button></a>");
  client.println("<a href=\"/light4off\"\"><button>Turn Off </button></a><br />"); 
client.println("</center>");
  client.println("<br><br>");
  client.println("<center>");
  client.println("<table border=\"5\">");
 client.println("<tr>");
  if (digitalRead(5))
         {
           client.print("<td>Light 1 is ON</td>");
                 }
          else
          {
            client.print("<td>Light 1 is OFF</td>");
              }
     
        client.println("<br />");
             
         if (digitalRead(4))
          {
           client.print("<td>Light 2 is ON</td>");

         }
          else
          {
            client.print("<td>Light 2 is OFF</td>");
          }
          client.println("</tr>");
          client.println("<tr>");
          if (digitalRead(0))
          {
           client.print("<td>Light 3 is ON</td>");
          }

          else
          {
            client.print("<td>Light 3 is OFF</td>");
          }
          if (digitalRead(13))
          {
           client.print("<td>Light 4 is ON</td>");

          }
          else
          {
            client.print("<td>Light 4 is OFF</td>");
         }

          client.println("</tr>");


          client.println("</table>");

          client.println("</center>");
  client.println("</html>");
  delay(1);
  Serial.println("Client disonnected");
  Serial.println("");
 
}

Re: Multiple ESP8266's on a same network

PostPosted: Fri Aug 31, 2018 1:04 pm
by PatrykW
Just a guess, but perhaps your esp is configured as station and ap in which case some of your esp's are connecting to each other instead of your access point.
Try forcing STA mode using
Code: Select allWiFi.mode(WIFI_STA);
might just do the trick.
And of course, make sure every esp has unique IP. Just saying

Re: Multiple ESP8266's on a same network

PostPosted: Fri Aug 31, 2018 4:28 pm
by btidey
I would suggest using the wifiManager library to provide the access to your network. It is very easy to include.

That way the device goes into AP mode to allow the local wifi credentials to be added. These are then saved and from then on it works in STA mode.

I would not set the IP address in the device. That way you can easily get IP clashes which will cause problems.

Just let the devices request an IP address from the DHCP server (often part of the router). By default the DHCP will just issue an available address, It is often convenient to have fixed static IP addresses for each device if you are going to access them directly. This can be achieved by setting up static DHCP entries for each device. This maps the MAC address to your chosen IP. Again this is set up on the DHCP server (router).

Re: Multiple ESP8266's on a same network

PostPosted: Fri Aug 31, 2018 4:48 pm
by Jovan Jovanovic
Patryk, I tried your suggestion, but it didn't work. I jammed your piece of code here:

Code: Select allIPAddress gateway(192, 168, 5, 1);
  IPAddress subnet(255, 255, 255, 0);
  WiFi.config(ip, gateway, subnet);
  WiFi.mode(WIFI_STA);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");


Should it be somewhere else, or is there fine?

Btidey, thanks for answering.
At the moment, I've no idea what wifiManager is, but I'll figure it out tomorrow and get back to you.
I appreciate the rest of the answer.

I'd just like to repeat one again what my issue is:
When I connect one ESP to my network, it works perfectly. When I add another one, on different static address, all hell breaks lose.