send commands from esp32 access point to a client?

Hi

I have an esp32 working as an access point
and esp8266 (esp01) as a client

i know i can send data from the client to the server

but after watching hours of tutorials I still do not know how I can send data (particularly simple commands like turn light on) from the server to the client

I dont have any code for now I just need to know how to start

Juraj:
wifi - How to acheive fast and reliable communication between NodeMCUs? - Arduino Stack Exchange

thank you

I tried as you suggest but it does not work please can you help me on this

server code

#include <WiFi.h>



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


char ssid[] = "STC_WiFi_D5EE";
char pass[] = "password";


float  t=14.6;
unsigned long clientTimer = 0;

void setup() {
  Serial.begin(115200);                     // only for debug

                             // starts the WiFi server

    WiFi.config(ip, gateway, subnet);
    WiFi.mode(WIFI_STA);
    //WiFi.softAP(ssid, pass);
    WiFi.begin(ssid,pass);
  while (WiFi.status() != WL_CONNECTED) {

    delay(500);
  }
  server.begin();
  delay(500);
  clientTimer = millis();

  delay(2000);
}

void loop() {


  WiFiClient client = server.available();
    Serial.println("loop");
  if (client) {
    Serial.println("client");
    if (client.connected()) {
      Serial.println("connected");
      String request = client.readStringUntil('\r');    // reads the message from the client
      Serial.println(request);
     // client.flush();
      Serial.println(request);
      client.println(String(t, 1));        // sends the temperature to the client
    client.stop();                         // disconnects the client
  }
}
delay(100);
}

client code

#include <ESP8266WiFi.h>

IPAddress server(192,168,0,80);     // fix IP of the server
WiFiClient client;


//int status = WL_IDLE_STATUS;
char ssid[] = "STC_WiFi_D5EE";
char pass[] = "password";

String answer;


void setup() {
Serial.begin(9600);
  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
}

void loop () {
  
    if (client.connect(server, 80)){             // connects to the server
    client.println("Haliho szerver!\r");    // trigger message to the server, its value is scrapped
      Serial.println("connected");

    answer = client.readStringUntil('\r');  // received the server's answer
        Serial.println(answer);

  client.stop();
    }
}

the server Serial output :
just keep printing “loop”

the client Serial output :
nothing after the “…” stops

Have a look at WebSockets.
Allows for a constantly connected link that data can be sent by either device.
Even allows for multiple connections.

Sorry I know it is for 8266 but I feel it should be simular !!
If not then someone has to rewrite the library for the ESP32.

Hiddenvision:
Have a look at WebSockets.
Allows for a constantly connected link that data can be sent by either device.
Even allows for multiple connections.
GitHub - Links2004/arduinoWebSockets: arduinoWebSockets

Sorry I know it is for 8266 but I feel it should be simular !!
If not then someone has to rewrite the library for the ESP32.

thank you sooo much

it has esp32 version as well !

i will try the library hopefully it will end my misery

Cool,
Glad it was of interest.

I have not looked at the recent version.
I did some little mods (mostly forgot what now).
Darn, I can't even guess now.. it worked fine without them.

But I found the initial library a great stepping point.

Have a look at McLighting library for the WS2812 leds.

That was the first one I looked at I think and that was able to open so many doors using websockets.
It had a very simple, easy to follow flow to it all.

Again I have not looked at their latest offerings but can only imagine it has improved.
I know you may not be playing with the LEDS but the code is still relevant.

There was an example of some websocket stuff using a couple of ESP8266 modules.

Sort of similar enough to be worth a look perhaps unless you have gone passed that point now.

Hiddenvision:
There was an example of some websocket stuff using a couple of ESP8266 modules.

https://forum.arduino.cc/index.php?topic=630014.0

Sort of similar enough to be worth a look perhaps unless you have gone passed that point now.

thank you for your care !!

I used the websocket library

but i’m still stuck they supposed to connect to my router … actually i need them to be connected in a chain… all are in WiFi_AP_STA mode

I tried to connect two esp8266 (esp-01) one as an access point. they connected successfully but the messages are not exchanged and then after seconds… the second esp-01 shows in the serial monitor:
[WSc] Disconnected!

please can you take a look what could be the problem

server code:

#include <Arduino.h>

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <WebSocketsServer.h>
#include <Hash.h>

ESP8266WiFiMulti WiFiMulti;

WebSocketsServer webSocket = WebSocketsServer(81);

#define USE_SERIAL Serial

void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {

    switch(type) {
        case WStype_DISCONNECTED:
            USE_SERIAL.printf("[%u] Disconnected!\n", num);
            break;
        case WStype_CONNECTED:
            {
                IPAddress ip = webSocket.remoteIP(num);
                USE_SERIAL.printf("[%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload);
				
				// send message to client
				webSocket.sendTXT(num, "message from the server");
            }
            break;
        case WStype_TEXT:
            USE_SERIAL.printf("[%u] get Text: %s\n", num, payload);

            // send message to client
            // webSocket.sendTXT(num, "message here");

            // send data to all connected clients
            // webSocket.broadcastTXT("message here");
            break;
        case WStype_BIN:
            USE_SERIAL.printf("[%u] get binary length: %u\n", num, length);
            hexdump(payload, length);

            // send message to client
            // webSocket.sendBIN(num, payload, length);
            break;
    }

}

void setup() {
    // USE_SERIAL.begin(921600);
    USE_SERIAL.begin(115200);

    //Serial.setDebugOutput(true);
    USE_SERIAL.setDebugOutput(true);

    USE_SERIAL.println();
    USE_SERIAL.println();
    USE_SERIAL.println();

    for(uint8_t t = 4; t > 0; t--) {
        USE_SERIAL.printf("[SETUP] BOOT WAIT %d...\n", t);
        USE_SERIAL.flush();
        delay(1000);
    }
    WiFi.mode(WIFI_AP_STA);
    WiFi.softAP("SSID", "passpasspass");


        delay(300);
    

    webSocket.begin();
    webSocket.onEvent(webSocketEvent);
}

void loop() {
    webSocket.loop();
}

client code:

#include <Arduino.h>

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>

#include <WebSocketsClient.h>

#include <Hash.h>

ESP8266WiFiMulti WiFiMulti;
WebSocketsClient webSocket;

#define USE_SERIAL Serial

void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {

	switch(type) {
		case WStype_DISCONNECTED:
			USE_SERIAL.printf("[WSc] Disconnected!\n");
			break;
		case WStype_CONNECTED: {
			USE_SERIAL.printf("[WSc] Connected to url: %s\n", payload);

			// send message to server when Connected
			webSocket.sendTXT("Connected");
		}
			break;
		case WStype_TEXT:
			USE_SERIAL.printf("[WSc] get text: %s\n", payload);

			// send message to server
			// webSocket.sendTXT("message here");
			break;
		case WStype_BIN:
			USE_SERIAL.printf("[WSc] get binary length: %u\n", length);
			hexdump(payload, length);

			// send data to server
			// webSocket.sendBIN(payload, length);
			break;
        case WStype_PING:
            // pong will be send automatically
            USE_SERIAL.printf("[WSc] get ping\n");
            break;
        case WStype_PONG:
            // answer to a ping we send
            USE_SERIAL.printf("[WSc] get pong\n");
            break;
    }

}

void setup() {
	// USE_SERIAL.begin(921600);
	USE_SERIAL.begin(115200);

	//Serial.setDebugOutput(true);
	USE_SERIAL.setDebugOutput(true);

	USE_SERIAL.println();
	USE_SERIAL.println();
	USE_SERIAL.println();

	for(uint8_t t = 4; t > 0; t--) {
		USE_SERIAL.printf("[SETUP] BOOT WAIT %d...\n", t);
		USE_SERIAL.flush();
		delay(1000);
	}

	WiFiMulti.addAP("SSID", "passpasspass");

	//WiFi.disconnect();
	while(WiFiMulti.run() != WL_CONNECTED) {
    USE_SERIAL.print("..");
		delay(100);
	}

	// server address, port and URL
	webSocket.begin("192.168.4.2", 81, "/");

	// event handler
	webSocket.onEvent(webSocketEvent);

	// use HTTP Basic Authorization this is optional remove if not needed
	// webSocket.setAuthorization("user", "Password");

	// try ever 5000 again if connection has failed
	webSocket.setReconnectInterval(5000);
  
  // start heartbeat (optional)
  // ping server every 15000 ms
  // expect pong from server within 3000 ms
  // consider connection disconnected if pong is not received 2 times
  webSocket.enableHeartbeat(15000, 3000, 2);

}

void loop() {
	webSocket.loop();
}

have another look at the thread mentioned before.

Start at the post that starts "This is easier" post #11 if your numbered the same.

I have updated the example I think

There is two examples now,

One is Ino project that is both client and server code.
and another samename.h that was more for adding into that persons existing code.

Both files are fully working but I think you will need the INO to try first.
Read the posts on that thread for setup and using.

I suspect that it is just the client not finding the server in your example.
Check the IP.

Also perhaps you can look at the WIFIMESH stuff I also mention, this may suit your needs closer.
Two ESP8266 on one Fastled webserver - Home Automation and Networked Objects - Arduino Forum Post #17