Hi,
I ve bought a ESP8266-01 module, and I’m using the WiFiEsp library.
I am comunicating with the WiFiEspServer on the 80 port.
Everything works, but when i load the page from it, the tcp packets arrive really slow.
I am using an arduino nano.
Any ideas to make the sending of the packets faster?
Thank you.
I got another problem. It takes a lot of time to disconnect the client at the command “client.stop();” (like 10 seconds) and it stucks the loop of arduino; i even set the closing of the connection in the html header.
Thank you again.
Alessandro Amedei from Florence.
#include "WiFiEsp.h"
#include <SoftwareSerial.h>
SoftwareSerial ESP(9, 10); //TX, RX
char ssid[] = "FASTWEB********"; // your network SSID (name)
char pass[] = "***********"; // your network password
WiFiEspServer server(80); //Start the server
int status = WL_IDLE_STATUS;
RingBuffer buf(8);
int rele=12; //!!
int switch_=3;
int microphone=2;
int led=4;
int state=false;
int debug=0; //!!!
void setup () {
pinMode(microphone,INPUT_PULLUP);
pinMode(switch_,INPUT_PULLUP);
pinMode(rele,OUTPUT);
pinMode(led,4);
digitalWrite(rele,LOW);
digitalWrite(led,LOW);
ESP.begin(9600);
Serial.begin(9600);
setupWifi();
}
int i=0;
void loop () {
switchListener();
hear();
clientManager();
}
void switchLight(){
state=!digitalRead(rele);
digitalWrite(rele,state);
debug++; // !!!
delay(1000);
}
void switchListener(){
if(!digitalRead(switch_)){ //!!!!
switchLight();
}
}
int pos=0;
unsigned long t=0;
unsigned long t2=0;
void hear(){
if(!digitalRead(microphone) && pos==0){
Serial.print("pos=1 ");
Serial.println(millis());
digitalWrite(led,HIGH);
t= millis();
pos=1;
delay(10);
}
t2=millis();
if((!digitalRead(microphone)) && pos==1){
Serial.print("pos=2_NOIN ");
Serial.println(millis());
if(t2-t>400 && t2-t<1000){
Serial.println("Switch");
digitalWrite(led,LOW);
switchLight();
t=0;
pos=0;
t2=0;
}
}
if(t2-t>1000 && pos==1){
t=0;
pos=0;
t2=0;
Serial.println("reset2");
digitalWrite(led,LOW);
}
}
void setupWifi(){
WiFi.init(&ESP); // initialize ESP module
// check for the presence of the shield
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue
while (true);
}
// attempt to connect to WiFi network
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network
status = WiFi.begin(ssid, pass);
}
Serial.println("You're connected to the network");
printWifiStatus();
// start the web server on port 80
server.begin();
}
void printWifiStatus()
{
// print the SSID of the network you're attached to
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print where to go in the browser
Serial.println();
Serial.print("To see this page in action, open a browser to http://");
Serial.println(ip);
Serial.println();
}
void clientManager()
{
WiFiEspClient client = server.available(); // listen for incoming clients
if (client) { // if you get a client,
Serial.println("New client"); // print a message out the serial port
buf.init(); // initialize the circular buffer
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
buf.push(c);
Serial.print(c);// push it to the ring buffer
// printing the stream to the serial monitor will slow down
// the receiving of data from the ESP filling the serial buffer
//Serial.write(c);
// you got two newline characters in a row
// that's the end of the HTTP request, so send a response
if (buf.endsWith("\r\n\r\n")) {
sendHttpResponse(client);
break;
}
// Check to see if the client request was "GET /H" or "GET /L":
if (buf.endsWith("GET /H")) {
Serial.println("Turn led ON");
state=1;
digitalWrite(rele, HIGH);
}
else if (buf.endsWith("GET /L")) {
Serial.println("Turn led OFF");
state=0;
digitalWrite(rele, LOW);
}
}
}
// close the connection
client.stop();
Serial.println("Client disconnected");
}
}
void sendHttpResponse(WiFiEspClient client)
{
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println(F("HTTP/1.1 200 OK"));
client.println(F("Content-type:text/html\r\n"
"Connection: close\r\n"));
client.println();
client.println(F("<html><head><style>"));
client.println(F(".button{"));
client.println(F("background-color: green; border: none; color: white;"));
client.println(F("padding: 15px 32px; text-align: center; text-decoration: none;"));
client.println(F("display: inline-block; font-size: 16px;margin: 4px 2px; cursor: pointer;}"));
client.println(F("</style></head>"));
// the content of the HTTP response follows the header:
client.println(F("<body><table align='center'>"));
client.println(F("<tr align='center'><td> <h1 style='color:blue; font-style:bold; '>HOMOTION</h1> </td></tr>"));
client.println(F("<tr align='center'><td> <p style='color:green'>Controllo Luce</p> </td></tr>"));
if(state)
client.println(F("<tr align='center'><td> <h2 style='color:green'>Luce ACCESA</h2> </td></tr>"));
else
client.println(F("<tr align='center'><td> <h2 style='color:red'>Luce SPENTA</h2> </td></tr>"));
client.println(F("<tr align='center'><td>"));
client.println(F("<a href='/H'><button style='width:200px; height:100px; padding: 5px 10px' type='button'>Accendi</button></a>"));
client.println(F("<a href='/L'><button style='width:200px; height:100px; padding: 5px 10px' type='button'>Spegni</button></a>"));
client.println(F("</td></tr>"));
String debug_string= String(debug);
client.println("<tr align='center'><td>
<p style='color:blue;'>Accensioni/Spegnimenti casuali: "+debug_string+" </p> </td></tr>");
client.println(F("<tr align='center'><td>
<p style='color:blue; styel:italic;'>Alessandro Amedei</p></td></tr>"));
client.println(F("
"));
client.println(F("
"));
// client.println("Click <a href=\"/H\">here</a>LightOn
");
// client.println("Click <a href=\"/L\">here</a>LightOff
");
client.println(F("</body></html>"));
// The HTTP response ends with another blank line:
client.println();
}