My Esp8266 receive some data from serial port that is sended every 10sec.
First work as web server to show this data locally.
Every xx minutes, data is send to thinkspeak.
Well all works but web page is most of time trying to connect, or incomplete, or drawing items slowly.
It's possible that serial port is slowing web server?
Web page don't will be show entirelly at the end of "client.stop()"
// RESUME CODE
#include <ESP8266WiFi.h>
WiFiServer server(80);
WiFiClient client;
// ************************** SETUP
void setup() {
Serial.begin(9600);
// reserve 80 bytes for the inputString:
inputString.reserve(80);
valor.reserve(20);
delay(10);
totalineas=0;
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
line=0;
postdata="";
} /// end setup
//****************** LOOP **************************
void loop() {
webserver();
if (serialEvent()){ //read an entire line from seria port
parser();
if (tsloop++ > MAX_TS_LOOP){ thinkspeaksend(); }
}//serial event end
delay(300);
}
// *************** thinkspeak *******************
void thinkspeaksend(){
if (client.connect(APIserver, 80)) { // "184.106.153.149" or api.thingspeak.com
String postStr = apiKey;
postStr += postdata;
postStr += "\r\n\r\n";
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);
}else { client.stop();}
}
// *************** webserver *******************
//Tutorial en: http://diymakers.es/crear-servidor-web-con-arduino/
void webserver(){
int linea=0;
server.begin();
// you're connected now, so print out the status:
// printWifiStatus();
// listen for incoming clients
WiFiClient client = server.available(); //Creamos un cliente Web
//Cuando detecte un cliente a través de una petición HTTP
if (client) {
Serial.println("**** new client");
boolean currentLineIsBlank = true; // an http request ends with a blank line
String cadena="";
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c); //Visualizamos la petición HTTP por el Monitor Serial
cadena.concat(c);//Unimos el String 'cadena' con la petición HTTP (c). De esta manera convertimos la petición HTTP a un String
//Ya que hemos convertido la petición HTTP a una cadena de caracteres, ahora podremos buscar partes del texto.
int posicion=cadena.indexOf("EDIT="); //Guardamos la posición de la instancia "EDIT=" a la variable 'posicion'
if(cadena.substring(posicion)=="EDIT=")//Si a la posición 'posicion' hay "EDIT"
{
posicion=posicion+5; //lo que viene despues de edit
Serial.println(cadena.charAt(posicion)) ;
}
//Cuando reciba una línea en blanco, quiere decir que la petición HTTP ha acabado y
//el servidor Web está listo para enviar una respuesta
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the response
client.println("Refresh: 15"); // refresh the page automatically every 5 sec
client.println();
client.println("<!DOCTYPE HTML>");
//Página web en formato HTML
client.println("<html>");
client.println("<head>");
client.println("</head>");
client.println("<BASEFONT SIZE=5>");
client.println("<body bgcolor=\"black\" text= \"yellow\">");
client.println("<div style=\"width:600px\">");
client.println("<hr>");
client.println("SSID: ");
client.println(WiFi.SSID());
// print the received signal strength:
long rssi = WiFi.RSSI();
client.print("RSSI:");
client.print(rssi);
client.println(" dBm");
client.println("<br>");
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
client.print("IP Add: ");
client.println(ip);
client.println("<br>");
client.println("<table border=\"1\"> <tr><td>");
do{
client.print(temp[linea].indice);
client.println("</td><td>");
client.print(temp[linea].nombre);
client.println("</td><td>");
client.print(temp[linea].temp);
client.println("</td><td>");
client.print(temp[linea].th);
client.println("</td><td>");
client.print(temp[linea].tl);
client.println("</td><td>");
client.print(temp[linea].act);
client.println("</td><td>");
client.print("<button onClick=location.href='./?EDIT=");client.print(linea);
client.print("\' style='margin:auto;border-radius: 8px;padding: 10px;'>");
client.println("EDIT");
client.println("</button>");
linea++;
client.println("</td></tr><tr><td>");
}while (temp[linea].nombre!="");
client.println("</td></tr></table>");
client.println("<hr>");
client.println("</div>");
client.println("</body>");
client.println("</html>");
break;
}
if (c == '\n') { currentLineIsBlank = true;
} else if (c != '\r') {currentLineIsBlank = false; }
}
}
// give the web browser time to receive the data
delay(2);
// close the connection:
client.stop();
Serial.println("******client disonnected");
}
}