- Wed Oct 07, 2015 10:58 am
#30804
I've took your code as is, and I figured out few things that I didn't saw earlier :
- First, the client was a local variable, so it was destroyed on every loop.
- Second "client = server.available()" should only be called when client is null, otherwise the previous client is again destroyed.
- Third, we need to handle the client disconnect with a "client.stop()"
So here is the new version :
Code: Select all/* Create a WiFi access point and provide a web server on it. */
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
/* Set these to your desired credentials. */
const char *ssid = "JP_TestAP";
int status = WL_IDLE_STATUS;
boolean alreadyConnected = false; // whether or not the client was connected previously
WiFiServer server(23);
WiFiClient client;
void setup()
{
delay(1000);
Serial.begin(115200);
Serial.println();
Serial.print("Configuring access point...");
/* You can remove the password parameter if you want the AP to be open. */
WiFi.softAP(ssid);
delay(10000);
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
Serial.println("\nStarting server...");
// start the server:
server.begin();
}
void loop()
{
// wait for a new client:
if (!client) {
client = server.available();
}
else
{
if (client.status() == CLOSED) {
client.stop();
Serial.println("connection closed !");
}
if (!alreadyConnected) // when the client sends the first byte, say hello:
{
// clead out the input buffer:
client.flush();
Serial.println("We have a new client");
client.println("Hello, client!");
alreadyConnected = true;
}
if (client.available() > 0) {
// read the bytes incoming from the client:
char thisChar = client.read();
// echo the bytes back to the client:
server.write(thisChar);
// echo the bytes to the server as well:
Serial.write(thisChar);
}
}
}