-->
Page 1 of 1

Communicate with two ESP8266s

PostPosted: Mon Nov 28, 2022 2:16 am
by YG0820
I would like to connect two ESP8266s to communicate with each other. What should I do? I will post the code and the results of the serial monitor. Please help me.

client-side

//#include<WiFi.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>

const char ssid[]="ESP32-WiFi"; //サーバーのSSID
const char pass[]="esp32wifi"; //サーバーのパスワード
static WiFiClient client; //WiFiClient型でclientと宣言

void setup() {
Serial.begin(115200);
WiFi.begin(ssid,pass); //サーバーに接続
Serial.printf("\n");
while(WiFi.status()!=WL_CONNECTED)
{
Serial.print(".");
delay(1000);
}
Serial.printf("\n");
Serial.println("WiFi Connected");
IPAddress ip(192,168,0,9); //サーバーのIPアドレス
client.connect(ip,80); //IPアドレスとポート番号を指定して接続
}
//単純に一文字送っています
const char value[]="H";
void loop()
{
delay(1000);
if(client.connected()==true)
{
client.write(value,1);

Serial.println(value);


}
}

server-side

//#include<WiFi.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>

const char ssid[]="ESP32-WiFi"; //SSID
const char pass[]="esp32wifi"; //パスワード
const IPAddress ip(192,168,0,9); //サーバーのIPアドレス
const IPAddress subnet(255,255,255,0); //サブネットマスク
WiFiServer server(80);
void setup()
{
Serial.begin(115200);

WiFi.softAP(ssid, pass); //SSIDとパスの設定
delay(100); //接続失敗防止
WiFi.softAPConfig(ip,ip,subnet); //IPアドレス、ゲートウェイ、サブネットマスクの設定
IPAddress myIP = WiFi.softAPIP(); //WiFi.softAPIP()でWiFi起動
server.begin(); //サーバーを起動(htmlを表示させるため)
Serial.println();
/*各種情報を表示*/
Serial.println("WiFi connected");
Serial.print("SSID:");
Serial.println(ssid);
Serial.print("AP IP address:");
Serial.println(myIP);
Serial.println("Server start!");
}

size_t value; //この 型をいろいろ試すと面白いです

void loop()
{
//接続したクライアントの情報を得る。
WiFiClient client = server.available();
if(client){
Serial.println("new client");
while(client.connected()){
if(client.available()){

value=client.read();
Serial.println(value);
delay(1000);
}
}
}
}

client-serial-monitor
.........pm open,type:2 0
....state: 5 -> 2 (6c0)
rm 0
pm close 7
.reconnect
state: 2 -> 0 (0)
scandone
state: 0 -> 2 (b0)
state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 1
cnt

connected with ESP32-WiFi, channel 1
dhcp client start...
..........pm open,type:2 0
.......

server-serial-monitor

SDK:2.2.2-dev(38a443e)/Core:3.0.2=30002000/lwIP:STABLE-2_1_2_RELEASE/glue:1.2-48-g7421258/BearSSL:6105635
fpm close 1
mode : softAP(c6:5b:be:64:89:20)
add if1
dhcp server start:(ip:192.168.4.1,mask:255.255.255.0,gw:192.168.4.1)
bcn 100
bcn 0
del if1
usl
add if1
dhcp server start:(ip:192.168.4.1,mask:255.255.255.0,gw:192.168.4.1)
bcn 100

WiFi connected
SSID:ESP32-WiFi
AP IP address:192.168.0.9
Server start!
add 1
aid 1
station: c4:5b:be:64:51:4f join, AID = 1

Re: Communicate with two ESP8266s

PostPosted: Tue Nov 29, 2022 7:56 am
by AcmeUK
Did you look at the tutorial I pointed you to in your previous post?

Re: Communicate with two ESP8266s

PostPosted: Sun Dec 04, 2022 11:39 pm
by YG0820
AcmeUK wrote:Did you look at the tutorial I pointed you to in your previous post?


Sorry I dont look this. I wrote it again referring to the link, but I still cannot know void loop in client cord . I want to send one character on one ESP and receive it on the other ESP.
What is wrong this time?

server side
#include <ESP8266WiFi.h>
#include <ESPAsyncWebServer.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
// Set your access point network credentials
const char ssid[] = "ESP8266-Access-Point";
const char password[] = "123456789";

AsyncWebServer server(80);

void setup(){
// Serial port for debugging purposes
Serial.begin(115200);
Serial.println();

// Setting the ESP as an access point
Serial.print("Setting AP (Access Point)…");
// Remove the password parameter, if you want the AP (Access Point) to be open
WiFi.softAP(ssid, password);

IPAddress IP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(IP);

// Start server
Server.begin();
}

void loop()
{
if(AsyncWebServer.available){
char value=client.read;
Serial.println(value);
delay(1000);
}
}

client side
#include <ESP8266WiFi.h>
#include "ESPAsyncWebServer.h"

// Set your access point network credentials
const char* ssid = "ESP8266-Access-Point";
const char* password = "123456789";

// Create AsyncWebServer object on port 80
AsyncWebServer server(80);

void setup(){
// Serial port for debugging purposes
Serial.begin(115200);
Serial.println();

// Setting the ESP as an access point
Serial.print("Setting AP (Access Point)…");
// Remove the password parameter, if you want the AP (Access Point) to be open
WiFi.softAP(ssid, password);

IPAddress IP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(IP);

// Start server
server.begin();
}

void loop(){

}

Re: Communicate with two ESP8266s

PostPosted: Mon Dec 05, 2022 5:40 am
by AcmeUK
You say
I want to send one character on one ESP and receive it on the other ESP.

Then there is a simpler way Communication between two ESP8266s via UDP