Ben Dixon
Published © MIT

ESP32 Server and Many ESP8266 Clients Controlled Over WiFi

This project shows how to use an ESP32 and ESP8266 to create a WiFi network to sense and control things around your home.

IntermediateFull instructions provided1 hour9,961
ESP32 Server and Many ESP8266 Clients Controlled Over WiFi

Things used in this project

Hardware components

IP01
XinaBox IP01
×3
CW01
XinaBox CW01
×2
CW02
XinaBox CW02
×1
OC03
XinaBox OC03
×1
XinaBox SW10
×1
XC10
XinaBox XC10
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

Server

Arduino
#include "WiFi.h"

#define SET_POINT 30
#define NUM_CLIENTS 2

#define LED_GREEN 26

WiFiServer server(80);              // launches the server
IPAddress ip(192, 168, 4, 1);      // fix IP of the server
IPAddress gateway(192, 168, 4, 1);  // WiFi router's IP
IPAddress subnet(255, 255, 255, 0);

const char *ssid = "IoTDemo";
const char *pass = "gKWaqUmkfNMusGfV";

String data;
String CLIENT;
String ACTION;


void setup() {
  pinMode(LED_GREEN, OUTPUT);
  Serial.begin(115200);                     // only for debug
  //WiFi.config(ip, gateway, subnet); //You may want to define your own variable at the top and config your server here by uncommenting this line. I used the defaults that my ESP32 set itself to.
  WiFi.mode(WIFI_AP);
  WiFi.softAP(ssid, pass);           // starts the WiFi server
  delay(2000);
  Serial.println();
  Serial.println("WiFi Connected");
  Serial.print("IP address: ");
  Serial.println(WiFi.softAPIP());
  delay(500);
  server.begin();
  Serial.println("Server Started");
  digitalWrite(LED_GREEN, HIGH); //Indicate Server has started
}

void loop() {
  if((clientRequest("SENSOR1:TEMPERATURE?").toInt()) > SET_POINT) //Request the temperature from SENSOR1
  {
    clientCommand("OUTPUT1:ON"); //Sets the output on OUTPUT1
  }
  else
  {
    clientCommand("OUTPUT1:OFF"); //Sets the output on OUTPUT1
  }
  delay(100);
}

void clientCommand(String input)
{
  for (int i = 0; i < NUM_CLIENTS; i++)
  {
    WiFiClient client = server.available();
    if (client) {
      if (client.connected()) {
        client.println(input);
        delay(10);
      }
    }
  }
  delay(100);
}

String clientRequest(String input)
{
  String response = "\0";
  for (int i = 0; i < NUM_CLIENTS; i++)
  {
    WiFiClient client = server.available();
    client.setTimeout(50);
    if (client) {
      if (client.connected()) {
        client.println(input);

        data = client.readStringUntil('\r');  // received the server's answer
        if (data != "\0")
        {
          int Index = data.indexOf(':');

          CLIENT = data.substring(0, Index);
          ACTION = data.substring(Index + 1);

          if (CLIENT == "ACK")
          {
            response = ACTION;
          }

          client.flush();
          data = "\0";
        }
      }
    }
  }
  delay(100);
  return response;
}

Sensor

Arduino
#include <ESP8266WiFi.h>
#include <xCore.h>
#include <xSW10.h>

#define LED_GREEN 13

String CLIENT_NAME = "SENSOR1";

IPAddress server(192, 168, 4, 1);  // fix IP of the server
WiFiClient client;

xSW10 SW10;

const char *ssid = "IoTDemo";
const char *pass = "gKWaqUmkfNMusGfV";
unsigned long askTimer = 0;
String answer;
String data;
String CLIENT;
String ACTION;

void setup() {

  pinMode(LED_GREEN, OUTPUT);

  Serial.begin(115200);

#ifdef ESP8266
  Wire.pins(2, 14);
  Wire.setClockStretchLimit(15000);
#endif

  // Start the I2C Comunication
  Wire.begin();

  // Start the  SW10 Sensor
  SW10.begin();

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
  delay(2000);
  Serial.println();
  Serial.println("WiFi Connected");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop () {

  SW10.poll();

  if (millis() - askTimer > 10) {
    if (!client.connect(server, 80)) {
      while (WiFi.status() != WL_CONNECTED) {
        Serial.print(".");
        digitalWrite(LED_GREEN, LOW); //Indicate wifi is not connected
        delay(500);
      }
      Serial.print("+");
      digitalWrite(LED_GREEN, LOW); //Indicate client is not connected
      delay(100);
      return;
    }
    else
    {
      digitalWrite(LED_GREEN, HIGH); //Indicate client is connected
    }

    data = client.readStringUntil('\r');  // received the server's answer
    if (data != "\0")
    {
      int Index = data.indexOf(':');

      CLIENT = data.substring(0, Index);
      ACTION = data.substring(Index + 1);

      if (CLIENT == CLIENT_NAME)
      {
        if (ACTION == "TEMPERATURE?")
        {
          client.println("ACK:" + (String(SW10.getTempC())));
        }
      }

      client.flush();
      askTimer = millis();
      data = "\0";
    }
  }
}

Output

Arduino
#include <ESP8266WiFi.h>
#include <xCore.h>
#include <xOC03.h>

#define LED_GREEN 13

String CLIENT_NAME = "OUTPUT1";

IPAddress server(192, 168, 4, 1);  // fix IP of the server
WiFiClient client;

xOC03 OC03;

//int status = WL_IDLE_STATUS;
const char *ssid = "IoTDemo";
const char *pass = "gKWaqUmkfNMusGfV";
unsigned long askTimer = 0;
String answer;
String data;
String CLIENT;
String ACTION;

void setup() {

  pinMode(LED_GREEN, OUTPUT);

#ifdef ESP8266
  Wire.pins(2, 14);
#endif

  // Start the I2C Communication
  Wire.begin();

  // Start the OC03 port expander
  OC03.begin();

  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
  delay(2000);
  Serial.println();
  Serial.println("WiFi Connected");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop () {
  if (millis() - askTimer > 10) {
    if (!client.connect(server, 80)) {
      while (WiFi.status() != WL_CONNECTED) {
        Serial.print(".");
        digitalWrite(LED_GREEN, LOW); //Indicate wifi is not connected
        delay(500);
      }
      Serial.print("+");
      digitalWrite(LED_GREEN, LOW); //Indicate client is not connected
      delay(100);
      return;
    }
    else
    {
      digitalWrite(LED_GREEN, HIGH); //Indicate client is connected
    }

    answer = client.readStringUntil('\r');  // received the server's answer
    if (answer != "\0")
    {
      client.flush();
      askTimer = millis();
      data = answer;
      answer = "\0";
    }

    if (data != "\0")
    {
      int Index = data.indexOf(':');

      CLIENT = data.substring(0, Index);
      ACTION = data.substring(Index + 1);

      if (CLIENT == CLIENT_NAME)
      {
        //client.println("ACK:" + CLIENT_NAME);
        if (ACTION == "ON")
        {
          OC03.write(HIGH);
        }
        else if (ACTION == "OFF")
        {
          OC03.write(LOW);
        }
      }
      data = "\0";
    }
  }
}

Credits

Ben Dixon

Ben Dixon

4 projects • 11 followers
I am an electronic engineer with a true love for the art of electronics. I live for anything technology. Yes, I'm a geek just like you!

Comments