Hello guys
I am trying to establish a client server communication with esp32 as server and 2 esp8266 as clients, i have tried multiple codes but i cant get esp8266 to connect to esp32,
here is my code below
#include <WiFi.h>
const char* ssid = "ESP32";
const char* password = "123";
WiFiServer server(80);
void setup() {
Serial.begin(115200);
Serial.println("Setting AP (Access Point)…");
WiFi.softAP(ssid, password);
IPAddress IP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(IP);
Serial.print("MAC address: ");
Serial.println(WiFi.softAPmacAddress());
server.begin();
}
void loop(){
WiFiClient client = server.available(); // Listen for incoming clients
if (client)
{ Serial.println("New Client.");
while (client.connected())
{
Serial.println(client.connected());
Serial.println("Client connected.");
Serial.println("");
}
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}
}
for esp32 Server side
/*
This sketch sends a message to a TCP server
*/
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
ESP8266WiFiMulti WiFiMulti;
void setup() {
Serial.begin(115200);
delay(10);
// We start by connecting to a WiFi network
WiFi.mode(WIFI_STA);
WiFiMulti.addAP("ESP32", "123");
Serial.println();
Serial.println();
Serial.print("Wait 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);
}
void loop() {
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("wait 5 sec...");
delay(5000);
return;
}
// This will send the request to the server
client.println("Send this data to server");
//read back one line from server
String line = client.readStringUntil('\r');
Serial.println(line);
Serial.println("closing connection");
client.stop();
Serial.println("wait 5 sec...");
delay(5000);
}
Code for Client side.
The Client is not able to connect with the server it just keeps connecting.
The final goal of this is to send temperature sensor from two client to the server and a push button from client a to server.
Thanks
Yeah i made the same mistake too once.
const char* password = "123";
this is not a valid password. your password needs to be at 8 characters long. (and there are some rules as for what characters can be used)
Deva_Rishi:
Yeah i made the same mistake too once.
const char* password = "123";
this is not a valid password. your password needs to be at 8 characters long. (and there are some rules as for what characters can be used)
i tried that now but still not getting connected
Can you connect to the ESP32 using another device ? (a phone fi)
Yes
Deva_Rishi:
Can you connect to the ESP32 using another device ? (a phone fi)
Yes i get connected with my phone but not with esp8266.
I deside to use esp8266 for everything now, for both Server and Client.
I am able to establish communication and send data between all three of them but i can only get data from one client at a time even when both are connected to the server.
here's the Server
//-- Libraries Included --------------------------------------------------------------
#include <ESP8266WiFi.h>
//------------------------------------------------------------------------------------
// Define I/O Pins
#define LED0 D2 // WIFI Module LED
#define LED1 D0 // Connectivity With Client #1
#define LED2 D2 // Connectivity With Client #2
#define BUTTON D1 // Connectivity ReInitiate Button
//------------------------------------------------------------------------------------
// Authentication Variables
char* TKDssid; // SERVER WIFI NAME
char* TKDpassword; // SERVER PASSWORD
//------------------------------------------------------------------------------------
#define MAXSC 2 // MAXIMUM NUMBER OF CLIENTS
WiFiServer TKDServer(9001); // THE SERVER AND THE PORT NUMBER
WiFiClient TKDClient[MAXSC]; // THE SERVER CLIENTS
void setup()
{
// Setting The Serial Port
Serial.begin(115200); // Computer Communication
// Setting The Mode Of Pins
pinMode(LED0, OUTPUT); // WIFI OnBoard LED Light
pinMode(LED1, OUTPUT); // Indicator For Client #1 Connectivity
pinMode(LED2, OUTPUT); // Indicator For Client #2 Connectivity
pinMode(BUTTON, INPUT_PULLUP); // Initiate Connectivity
// Print Message Of I/O Setting Progress
Serial.println();
Serial.println("I/O Pins Modes Set .... Done");
// Setting Up A Wifi Access Point
SetWifi("TAKEONE", "");
}
void loop()
{
IsClients2();
}
void SetWifi(char* Name, char* Password)
{
// Stop Any Previous WIFI
WiFi.disconnect();
// Setting The Wifi Mode
WiFi.mode(WIFI_AP_STA);
Serial.println("WIFI Mode : AccessPoint Station");
// Setting The Access Point
TKDssid = Name;
TKDpassword = Password;
// Starting The Access Point
WiFi.softAP(TKDssid, TKDpassword);
Serial.println("WIFI < " + String(TKDssid) + " > ... Started");
// Wait For Few Seconds
delay(1000);
// Getting Server IP
IPAddress IP = WiFi.softAPIP();
// Printing The Server IP Address
Serial.print("AccessPoint IP : ");
Serial.println(IP);
// Starting Server
TKDServer.begin();
Serial.println("Server Started");
}
void IsClients()
{
if(TKDServer.hasClient())
{
WiFiClient TKDClient = TKDServer.available();
if(digitalRead(LED0) == HIGH) digitalWrite(LED0, LOW);
digitalWrite(LED1, HIGH);
while(1)
{
//--[ Draft ] ---------------------------------------------------
//---------------------------------------------------------------
// If Clients Are Connected
//---------------------------------------------------------------
if(TKDClient.available())
{
// Here We Read The Message
String Message = TKDClient.readStringUntil('\r');
// Here We Print The Message On The Screen
Serial.println(Message);
// Here We Reply To The Client With A Message
TKDClient.print("\nWe Got Your Message");
TKDClient.flush();
}
//---------------------------------------------------------------
// If Clients Are Disconnected
//---------------------------------------------------------------
if(!TKDClient || !TKDClient.connected())
{
// Here We Turn Off The LED To Indicated The Its Disconnectted
digitalWrite(LED1, LOW);
// Here We Jump Out Of The While Loop
break;
}
//----------------------------------------------------------------
}
}
else
{
// This LED Blinks If No Clients Where Available
digitalWrite(LED0, HIGH);
delay(250);
digitalWrite(LED0, LOW);
delay(250);
}
}
void IsClients2()
{
if (TKDServer.hasClient())
{
for(int i = 0; i < MAXSC; i++)
{
//find free/disconnected spot
if (!TKDClient[i] || !TKDClient[i].connected())
{
if(TKDClient[i]) TKDClient[i].stop();
TKDClient[i] = TKDServer.available();
Serial.print("New Client : "); Serial.print(String(i+1) + " - ");
continue;
}
}
// no free / disconnected spot so reject
digitalWrite(LED1, HIGH);
WiFiClient TKDClient = TKDServer.available();
TKDClient.stop();
}
//check clients for data -------------------------------------------------------
for(int i = 0; i < MAXSC; i++)
{
if (TKDClient[i] && TKDClient[i].connected())
{
if(TKDClient[i].available())
{
// If Any Data Was Available We Read IT
while(TKDClient[i].available())
{
// Read From Client
Serial.println(TKDClient[i].readStringUntil('\n'));
// Reply To Client
TKDClient[i].println("<OK>");
}
}
}
}
}
Client 1 and Client 2
//------------------------------------------------------------------------------------
#include <Wire.h>
#include <ESP8266WiFi.h>
#include "DHT.h"
//------------------------------------------------------------------------------------
// Defining I/O Pins
//------------------------------------------------------------------------------------
#define LED0 2 // WIFI Module LED
#define LED1 D0 // Connectivity With Client #1
#define LED2 D2 // Connectivity With Client #2
#define BUTTON D1 // Connectivity ReInitiate Button
#define TWI_FREQ 400000L // I2C Frequency Setting To 400KHZ
#define DHTPIN D7
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
//------------------------------------------------------------------------------------
// BUTTON Variables
//------------------------------------------------------------------------------------
int ButtonState;
int LastButtonState = LOW;
int LastDebounceTime = 0;
int DebounceDelay = 50;
const String ButtonColor = "BLU";
float temperature ;
float humidity ;
String t, h;
//------------------------------------------------------------------------------------
// LED Delay Variables
//------------------------------------------------------------------------------------
int LEDState = LOW;
unsigned long CurrMillis = 0;
unsigned long PrevMillis = 0;
unsigned long Interval = 1000;
//------------------------------------------------------------------------------------
// Authentication Variables
//------------------------------------------------------------------------------------
char* TKDssid;
char* TKDpassword;
IPAddress TKDServer(192,168,4,1);
WiFiClient TKDClient;
void setup()
{
/* ----------------------------------------------------------------------
* Setting The I2C Pins SDA, SCL
* Because We Didnt Specify Any Pins The Defult
* SDA = D4 <GPIO2>, SCL = D5 <GPIO14> For ESP8266 Dev Kit Node MCU v3
--------------------------------------------------------------------- */
Wire.begin(); // Begginning The I2C
dht.begin();
// Setting Up The I2C Of The MPU9250 ------------------------------------
Wire.setClock(TWI_FREQ); // Setting The Frequency MPU9250 Require
// Setting The Serial Port ----------------------------------------------
Serial.begin(115200); // Computer Communication
// Setting The Mode Of Pins ---------------------------------------------
pinMode(LED0, OUTPUT); // WIFI OnBoard LED Light
pinMode(LED1, OUTPUT); // Indicator For Client #1 Connectivity
pinMode(LED2, OUTPUT); // Indicator For Client #2 Connectivity
pinMode(BUTTON, INPUT_PULLUP); // Initiate Connectivity
digitalWrite(LED0, !LOW); // Turn WiFi LED Off
// Print Message Of I/O Setting Progress --------------------------------
Serial.println("\nI/O Pins Modes Set .... Done");
// Starting To Connect --------------------------------------------------
WiFi.mode(WIFI_STA); // To Avoid Broadcasting An SSID
WiFi.begin("TAKEONE"); // The SSID That We Want To Connect To
// Printing Message For User That Connetion Is On Process ---------------
Serial.println("!--- Connecting To " + WiFi.SSID() + " ---!");
// WiFi Connectivity ----------------------------------------------------
CheckConnectivity(); // Checking For Connection
// Stop Blinking To Indicate Connected ----------------------------------
digitalWrite(LED0, !HIGH);
Serial.println("!-- Client Device Connected --!");
// Printing IP Address --------------------------------------------------
Serial.println("Connected To : " + String(WiFi.SSID()));
Serial.println("Signal Strenght : " + String(WiFi.RSSI()) + " dBm");
Serial.print ("Server IP Address : ");
Serial.println(TKDServer);
Serial.print ("Device IP Address : ");
Serial.println(WiFi.localIP());
// Conecting The Device As A Client -------------------------------------
TKDRequest();
}
void loop()
{
//ReadButton();
senddata();
}
void ReadButton()
{
// Reading The Button
int reading = digitalRead(BUTTON);
// If It Doest Match The Previous State
if(reading != LastButtonState)
{
LastDebounceTime = millis();
}
// To Iliminate Debounce
if((millis() - LastDebounceTime) > DebounceDelay)
{
if(reading != ButtonState)
{
ButtonState = reading;
if(ButtonState == LOW)
{
LEDState = !digitalRead(LED1);
digitalWrite(LED1, LEDState);
Serial.println("<" + ButtonColor + "-SCORED>");
TKDClient.println("<" + ButtonColor + "-SCORED>");
TKDClient.flush();
}
}
}
// Last Button State Concidered
LastButtonState = reading;
}
void senddata() {
delay(2000);
humidity = dht.readHumidity();
temperature = dht.readTemperature();
t = String(temperature);
h = String(humidity);
// Serial.println("Temperature" + t );
// Serial.println("Humidity" + h );
TKDClient.println("Client1Temp" + t );
TKDClient.println("Client1Hum" + h );
TKDClient.flush();
if(TKDClient.available())
{
// Here We Read The Message
String Message = TKDClient.readStringUntil('\r');
// Here We Print The Message On The Screen
Serial.println(Message);
// Here We Reply To The Client With A Message
TKDClient.print("\nWe Got Your Message");
TKDClient.flush();
}
}
void CheckConnectivity()
{
while(WiFi.status() != WL_CONNECTED)
{
for(int i=0; i < 10; i++)
{
digitalWrite(LED0, !HIGH);
delay(250);
digitalWrite(LED0, !LOW);
delay(250);
Serial.print(".");
}
Serial.println("");
}
}
void TKDRequest()
{
// First Make Sure You Got Disconnected
TKDClient.stop();
// If Sucessfully Connected Send Connection Message
if(TKDClient.connect(TKDServer, 9001))
{
// Serial.println ("<" + ButtonColor + "-CONNECTED>");
// TKDClient.println ("<" + ButtonColor + "-CONNECTED>");
Serial.println ("Client Connected");
TKDClient.println ("Client Connected");
}
}
I am able to establish communication and send data between all three of them but i can only get data from one client at a time even when both are connected to the server.
But both clients should be able to get info from the server.
How about you use ESPwebserver for the 'stations' so you can let the 'AP' send a request for data to each of them. You can set up a webpage on the AP as well that you can log into so you can access the data gathered from a different device.
In other words, your stations should be you servers, and you access-point should be a client and a server.