hello everyone,
i am new to esp8266's and trying to make a project using esp8266 this is just initial part of it, having two espA and espB, transmitter and receiver. the espA send continuous data to the receiver. So i make espA as server(AP) and espB as client, as i mentioned this code is just a start of my project so i don't want the client(espB) to just stuck in the setup() and wait for the connection establish, it should start executing other function while try to connect with the server or client.
1.now the problem with the client code is if it disconnect with the server then it won't until I reset it
2.problem with the server code is it uses while(){} in the main loop and i don't want the pointer to be busy there bcz i have other function also waiting to execute.
thanks in advance
Client code
#include <ESP8266WiFi.h>
const char *ssid = "ssid"; // SSID of the access point
const char *password = "12345678"; // Password of the access point
WiFiClient client;
void setup() {
Serial.begin(115200);
// Connect to the Wi-Fi network
WiFi.begin(ssid, password);
// Wait for the connection
// while (WiFi.status() != WL_CONNECTED) {
// delay(1000);
// Serial.println("Connecting to WiFi...");
// }
Serial.println("Connected to WiFi");
client.connect("192.168.4.1", 80);
}
void loop() {
Serial.println("Loop start");
// Check if the client is connected to the access point
if (WiFi.status() == WL_CONNECTED) {
// Check if there is data available from the access point
if (client.available()) {
Serial.println("in loop");
// Read data from the access point
//char c = client.read();
String receivedData = client.readStringUntil('\n');
// Print the received data
Serial.print("Received from server: ");
Serial.println(receivedData);
}
} else {
Serial.println("Not connected to the access point. Reconnecting...");
WiFi.begin(ssid, password);
}
delay(100);
}
Server code
#include <ESP8266WiFi.h>
const char *ssid = "ssid"; // SSID of the access point
const char *password = "12345678"; // Password of the access point
int data ;
WiFiServer server(80);
WiFiClient client;
void setup() {
Serial.begin(115200);
// Set up the access point
WiFi.softAP(ssid, password);
server.begin();
Serial.print("Access Point IP address: ");
Serial.println(WiFi.softAPIP());
}
void loop() {
Serial.println("Loop start");
client = server.available();
if (client) {
Serial.println("in if client");
while (client.connected()) {
data++;
Serial.println("New client connected");
// Send data to the client
Serial.print("data: ");
Serial.println(data);
client.println(data);
client.print('\n');
delay(100);
}
}
// Close the connection
// client.stop();
// Serial.println("Client disconnected");
}