-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Closed
Labels
Status: StaleIssue is stale stage (outdated/stuck)Issue is stale stage (outdated/stuck)
Description
Hello guys
am trying to use to use esp32 as a server and two esp32 as a clients,
i have a code for connecting two esp8266 together working but when i use the esp8266 code on esp32 after changing the #include <ESP8266WiFi.h> to #include <WiFi.h> it got uploaded but i am getting a different access point from the one defined in the code.
code for Server
#include <WiFi.h>
//------------------------------------------------------------------------------------
// Define I/O Pins
#define LED0 2 // WIFI Module LED
//------------------------------------------------------------------------------------
// Authentication Variables
char* ssid; // SERVER WIFI NAME
char* password; // SERVER PASSWORD
//------------------------------------------------------------------------------------
// WiFi settings
#define MAXSC 6 // MAXIMUM NUMBER OF CLIENTS
IPAddress APlocal_IP(192, 168, 4, 1);
IPAddress APgateway(192, 168, 4, 1);
IPAddress APsubnet(255, 255, 255, 0);
unsigned int TCPPort = 2390;
WiFiServer TCP_SERVER(TCPPort); // THE SERVER AND THE PORT NUMBER
WiFiClient TCP_Client[MAXSC]; // THE SERVER CLIENTS Maximum number
//------------------------------------------------------------------------------------
// Some Variables
char result[10];
void setup(){
// Setting the serial port
Serial.begin(115200); // Computer Communication
// Setting the mode of the pins
pinMode(LED0, OUTPUT); // WIFI OnBoard LED Light
// setting up a Wifi AccessPoint
SetWifi("DataTransfer","");
}
//====================================================================================
void loop(){
HandleClients();
}
//====================================================================================
void SetWifi(char* NameESP, char* Password123){
// Stop any previous WIFI
WiFi.disconnect();
// Setting The Wifi Mode
WiFi.mode(WIFI_AP_STA);
Serial.println("WIFI Mode : AccessPoint");
// Setting the AccessPoint name & password
ssid = NameESP;
password = Password123;
// Starting the access point
WiFi.softAPConfig(APlocal_IP, APgateway, APsubnet); // softAPConfig (local_ip, gateway, subnet)
WiFi.softAP(ssid, password, 1, 0, MAXSC); // WiFi.softAP(ssid, password, channel, hidden, max_connection)
Serial.println("WIFI < " + String(ssid) + " > ... Started");
// wait a bit
delay(50);
// getting server IP
IPAddress IP = WiFi.softAPIP();
// printing the server IP address
Serial.print("AccessPoint IP : ");
Serial.println(IP);
// starting server
TCP_SERVER.begin(); // which means basically WiFiServer(TCPPort);
Serial.println("Server Started");
}
//====================================================================================
void HandleClients(){
unsigned long tNow;
if(TCP_SERVER.hasClient()){
WiFiClient TCP_Client = TCP_SERVER.available();
TCP_Client.setNoDelay(1); // enable fast communication
while(1){
//---------------------------------------------------------------
// If clients are connected
//---------------------------------------------------------------
if(TCP_Client.available()){
// read the message
String Message = TCP_Client.readStringUntil('\r');
// print the message on the screen
Serial.print("Received packet of size ");
Serial.println(sizeof(Message));
// print who sent it
Serial.print("From ");
Serial.print(TCP_Client.remoteIP());
Serial.print(", port ");
Serial.println(TCP_Client.remotePort());
// content
Serial.print("Content: ");
Serial.println(Message);
// generate a response - current run-time -> to identify the speed of the response
tNow=millis();
dtostrf(tNow, 8, 0, result);
// reply to the client with a message
TCP_Client.println(result); // important to use println instead of print, as we are looking for a '\r' at the client
TCP_Client.flush();
}
//---------------------------------------------------------------
// If clients are disconnected // does not realy work....
//---------------------------------------------------------------
if(!TCP_Client || !TCP_Client.connected()){
// Here We Turn Off The LED To Indicated The Its Disconnectted
digitalWrite(LED0, LOW);
break;
}
}
}
else{
// the LED blinks if no clients are available
digitalWrite(LED0, HIGH);
delay(250);
digitalWrite(LED0, LOW);
delay(250);
}
}
am getting "yourSSIDHere" when i scan for the network on my phone and pc which is not defined in the code.
code for client
#include <ESP8266WiFi.h> // The Basic Function Of The ESP NOD MCU
//------------------------------------------------------------------------------------
// Define I/O Pins
#define LED0 2 // WIFI Module LED
//------------------------------------------------------------------------------------
// Authentication Variables
char* ssid; // Wifi Name
char* password; // Wifi Password
const String Devicename = "Device_1";
//------------------------------------------------------------------------------------
// WIFI Module Role & Port
IPAddress TCP_Server(192, 168, 4, 1);
IPAddress TCP_Gateway(192, 168, 4, 1);
IPAddress TCP_Subnet(255, 255, 255, 0);
unsigned int TCPPort = 2390;
WiFiClient TCP_Client;
//------------------------------------------------------------------------------------
// Some Variables
unsigned char buffer[80];
char result[10];
//====================================================================================
void setup(){
// setting the serial port ----------------------------------------------
Serial.begin(115200);
// setting the mode of pins ---------------------------------------------
pinMode(LED0, OUTPUT); // WIFI OnBoard LED Light
digitalWrite(LED0, !LOW); // Turn WiFi LED Off
// WiFi Connect ----------------------------------------------------
Check_WiFi_and_Connect_or_Reconnect(); // Checking For Connection
}
//====================================================================================
void loop(){
Send_Request_To_Server();
}
//====================================================================================
void Send_Request_To_Server() {
unsigned long tNow;
tNow=millis();
dtostrf(tNow, 8, 0, result); // create a char[] out of the tNow
TCP_Client.println(result); // Send Data
while(1){
int len = TCP_Client.available(); // Check For Reply
if (len > 0) {
if (len > 80){
len = 80;
}
String line = TCP_Client.readStringUntil('\r'); // if '\r' is found
Serial.print("received: "); // print the content
Serial.println(line);
break; // exit
}
if((millis()-tNow)>1000){ // if more then 1 Second No Reply -> exit
Serial.println("timeout");
break; // exit
}
}
TCP_Client.flush(); // Empty Bufffer
Check_WiFi_and_Connect_or_Reconnect();
}
//====================================================================================
void Check_WiFi_and_Connect_or_Reconnect(){
if (WiFi.status() != WL_CONNECTED){
TCP_Client.stop(); //Make Sure Everything Is Reset
WiFi.disconnect();
Serial.println("Not Connected...trying to connect...");
delay(50);
WiFi.mode(WIFI_STA); // station (Client) Only - to avoid broadcasting an SSID ??
WiFi.begin("DataTransfer"); // the SSID that we want to connect to
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("");
}
// stop blinking to indicate if 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(TCP_Server);
Serial.print ("Device IP Address : ");
Serial.println(WiFi.localIP());
// conecting as a client -------------------------------------
Tell_Server_we_are_there();
}
}
//====================================================================================
void Tell_Server_we_are_there(){
// first make sure you got disconnected
TCP_Client.stop();
// if sucessfully connected send connection message
if(TCP_Client.connect(TCP_Server, TCPPort)){
Serial.println ("<" + Devicename + "-CONNECTED>");
TCP_Client.println ("<" + Devicename + "-CONNECTED>");
}
TCP_Client.setNoDelay(1); // allow fast communication?
}
//====================================================================================
PS Both code are working perfect between 2 esp8266 .
Metadata
Metadata
Assignees
Labels
Status: StaleIssue is stale stage (outdated/stuck)Issue is stale stage (outdated/stuck)