Olá pessoal. Estou fazendo um sketch em que meu equipamento acorda como webserver, fornece uma página de configuração, grava os dados em eeprom e a partir de então passa a ser um server logado na rede configurada. Estou com 2 erros no sketch relativos à client.write e server.available. Eu acho que a grande questão deste sketch é o local e a forma correta de abrir e fechar o webserver e depois abrir wifi . Anexo estão os dois erros, poderiam me ajudar?
#include <lwip/priv/tcp_priv.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <EEPROM.h>
#include <ESP8266WebServer.h>
#define APSSID "ESPap"
#define APPSK "thereisnospoon"
#define byte_count 12
#define num_tx 12
#define mod_func 16
const int output2 = 2;
ESP8266WebServer server(80); // server: http://192.168.4.1
void tcpCleanup()
{
while (tcp_tw_pcbs != NULL)
{
tcp_abort(tcp_tw_pcbs);
}
}
String buf_ssid="";
String buf_senha = "";
char flg_dados = 0;
const char *ssid = APSSID;
const char *password = APPSK;
char buffer_rx[90] = {0};
unsigned char buffer_rx2[25] = {0};
unsigned char buffer_hex[35] = {0};
unsigned char buffer_tx[20] = {0};
unsigned char buffer_tx2[20] = {0};
unsigned char LRC;
unsigned char flg_eeprom;
String site = "";
//String bufrx_str = "10,146,170,35,255,255,255,0,10,146,170,1,Mario,24151965";
String bufrx_str = "";
char buf_temp[3];
//***********************************************
// HANDLEROOT
//*************************************************
void handleRoot()
{
char j,k,m,n;
WiFiClient client;
site = "<html>\n";
site += "<head><script>\n";
site += "function EnviaDados(){var link = 'http://192.168.4.1/dados?parametros=' var parsed;\n";
site += "for( var i = 1; i < 15; i++ )\n";
site += "{if (document.getElementById('c' + i).value == '') {alert('Todos os campos precisam ser informados');\n";
site += "return false;}\n";
site += "if (i < 13) {parsed = Number.parseInt(document.getElementById('c' + i).value);\n";
site += "if (Number.isNaN(parsed)){alert('Caractere inválido para campo numérico');\n";
site += "return false;}\n";
site += "if (parsed > 255) {alert('Valores de IP devem ser <= 255');return false;}\n";
site += "link += parsed.toString(16) + ",";}\n";
site += "else {link += document.getElementById('c' + i).value + ',';}}\n";
site += "self.location = link;}\n";
site += "</script>\n";
site += "<meta http-equiv='Content-Type'content='text/html; charset=windows-1252'>\n";
site += "<meta name='viewport' content='width=device-width, initial-scale=1'>\n";
site += "<link rel='icon' href='data:,'>\n";
site += "<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
site += ".btnblue { background-color: #195B6A; border: none; color: white; padding: 16px 24px; width: 150px;\n";
site += "height: 60px; text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}\n";
site += ".btnred { background-color: #FF0000; border: none; color: white; padding: 16px 24px; width: 150px;\n";
site += "height: 60px; text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}\n";
site += ".button2 {background-color: #77878A;}</style></head>\n";
site += "<body><h1>MS Eletrônica</h1>\n";
site += "<p>Ip: <input type='text' id='c' maxlength='3' size='3' required><input type='text' id='c2' maxlength='3' size='3' /><input type='text' id='c3' maxlength='3' size='3'/><input type='text' id='c4' maxlength='3' size='3'/></p>\n";
site += "<p>Gateway Padrão: <input type='text' id='c5' maxlength='3' size='3'/><input type='text' id='c6' maxlength='3' size='3'/><input type='text' id='c7' maxlength='3' size='3'/><input type='text' id='c8' maxlength='3' size='3'/></p>\n";
site += "<p>Máscara Sub Rede: <input type='text' id='c9' maxlength='3' size='3'/><input type='text' id='c10' maxlength='3' size='3'/><input type='text' id='c11' maxlength='3' size='3'/><input type='text' id='c12' maxlength='3' size='3'/></p>\n";
site += "<p>SSid: <input type='text' id='c13' maxlength='32' /></p>\n";
site += "<p>Senha: <input type='text' id='c14' /></p>\n";
site += "<p><a href='#' onClick='EnviaDados()'><button class='btnblue'>ENVIAR</button></a></p>\n";
site += "</body></html>\n";
server.send(2600, "text/html", site);
site = "";
Serial.println ("HTML");
// while (!client.available())
// {
// delay(1);
//
// }
/* if (!client.available)
{
return;
}*/
// char i = client.available();
//for (k=0;k<i; k++)
// {Serial.println("B");
// bufrx_str += client.read();
// if (j = -1)i = 80;
// else buffer_rx[k] = j;
// }
}
//***************************************************************************************************
// Recebe e manipula os dados
//****************************************************************************************************
void handle_dados() // recebeu valor do IP configurado
{
Serial.println("dados");
if (!flg_dados)
{
Serial.println("falso");
bufrx_str = server.arg("parametros"); // armazena os dados recebidos
Serial.println(bufrx_str);
// Serial.println(bufrx_str.length());
bufrx_str.toCharArray(buffer_rx, bufrx_str.length());
// Serial.println (buffer_rx[0] + buffer_rx[1] + buffer_rx[2] + buffer_rx[3] + buffer_rx[4] + buffer_rx[5] + buffer_rx[6] + buffer_rx[7]);
char m=0; // contador dentro de buffer_rx[]
char k=0; // contador dentro do buffer hexa (buffer_hex[])
int p=0;
char i=0;
char j=0;
char r=15;
for (k=0; k<12;k++) // vai puxar 12 parametros de ip, gw e masc
{
if (buffer_rx[m+3] == ',')
{
buffer_hex[k] = ((buffer_rx[m++] & 0x0f)*100) +((buffer_rx[m++] & 0x0f)*10) + ((buffer_rx[m++] & 0x0f));
}
else if (buffer_rx[m+2] == ',')
{
buffer_hex[k] = ((buffer_rx[m++] & 0x0f)*10) + (buffer_rx[m++] & 0x0f);
}
else
{
buffer_hex[k] = (buffer_rx[m++] & 0x0f);
}
m++;
} // agora vai recuperar a porta
if (buffer_rx[m+5] == ',')
{
p = (buffer_rx[m+4] & 0x0f) + ((buffer_rx[m+3]&0x0f)*10)+ ((buffer_rx[m+2]&0x0f)*100)+ ((buffer_rx[m+1]&0x0f)*1000)+ ((buffer_rx[m]&0x0f)*10000);
}
if (buffer_rx[m+4] == ',')
{
p = (buffer_rx[m+3] & 0x0f) + ((buffer_rx[m+2]&0x0f)*10)+ ((buffer_rx[m+1]&0x0f)*100)+ ((buffer_rx[m]&0x0f)*1000);
}
if (buffer_rx[m+3] == ',')
{
p = (buffer_rx[m+2] & 0x0f) + ((buffer_rx[m+1]&0x0f)*10)+ ((buffer_rx[m]&0x0f)*100);
}
if (buffer_rx[m+2] == ',')
{
p = (buffer_rx[m+1] & 0x0f) + ((buffer_rx[m]&0x0f)*10);
}
if (buffer_rx[m+1] == ',')
{
p = (buffer_rx[m] & 0x0f);
}
buffer_hex[k++] = p / 256;
buffer_hex[k] = p % 256;
// buffer hex possui (exemplo): 192 168 10 45 255 255 255 0 192 168 10 1 1 246
// valores em decimal
// os dois íltimos números são a porta: primeiro o mais signif e depois o menos sifnific
// 1d = 1H ; 246d = F6H . Logo 1F6H = 502 , porta 502
m++; // aponta para 1o. caracter ssid
i=0;
k=0;
String buf=("");
while (i)
{
buf = bufrx_str.charAt(m);
if (buf == ",")i=0;
else
{
buf_ssid += buf;
}
m++;
}
buffer_hex[14] = buf.length(); // guarda o no. de caracteres do ssid
m++;
while (i)
{
buf = bufrx_str.charAt(m);
buf = "a";
if (buf == ",")i=0;
else
{
buf_senha += buf;
}
m++;
}
buffer_hex[14] = buf.length(); // guarda o no. de caracteres da senha
for (i=0;i<12;i++)EEPROM.write(i+2, buffer_hex[i]); // guarda parametros na eeprom
for (i=0; i<buffer_hex[13]; i++)EEPROM.write(i+15, bufrx_str.charAt(i)); // guarda ssid
j = 0;
j += buffer_hex[15]; // recupera no. de caracteres
char t=0;
t = t + j;
for (i=0; i<buffer_hex[14]; i++)EEPROM.write(i+15+t, buf_senha.charAt(i)); // guarda senha
EEPROM.commit();
Serial.println(buf_ssid);
Serial.println(buf_senha);
flg_eeprom++;
flg_dados++;
}
}
//*********************************************
// TRATA RECEPÇÃO DO PACOTE
//*********************************************
void trata_rx()
{
//Serial.println("3,");
bufrx_str.toCharArray(buffer_rx, bufrx_str.length());
if ((buffer_rx[5] == byte_count) && (buffer_rx[7] == mod_func)) // buffer_rx[5] = No. de bytes restantes no pacote
// bytes_rx = 12, No. total de bytes no pacote
// buffer_rx[7] = função Modbus: 06 é escrita em um único registrador
{
if (buffer_rx[8] == 0x00 && (buffer_rx[9]) == 1 && buffer_rx[10] == 0 && buffer_rx[11] == 3 && buffer_rx[12] == 6)
// {Serial.println("6,");
{
buffer_rx[5] = 6; // prepara a resposta
client.write(buffer_rx,byte_count); // envia resposta positiva ao server modbus
// prepara buffer de TX para o relógio
buffer_tx[2] = buffer_rx[14] / 10;
buffer_tx[3] = buffer_rx[14] % 10;
buffer_tx[4] = buffer_rx[16] / 10;
buffer_tx[5] = buffer_rx[16] % 10;
buffer_tx[6] = buffer_rx[18] / 10;
buffer_tx[7] = buffer_rx[18] % 10;
buffer_tx[8] = buffer_rx[20] / 10;
buffer_tx[9] = buffer_rx[20] % 10;
LRC = 0;
for (unsigned char i=0; i<num_tx; i++)LRC ^= buffer_tx[i];
buffer_tx[11] = LRC;
for (char i=0;i<12;i++)buffer_tx2[i] = buffer_tx[i] + 0x40;
Serial.write(buffer_tx, num_tx);
// Client.write (buffer_tx, num_tx);
delay(100);
Serial.write(buffer_tx, num_tx); // repete por segurança
}
}
}
//*******************************************************
// SETUP
//*******************************************************
void setup()
{
char teste=0;
char dado1,dado2,dado3;
delay(1000);
Serial.begin(74880);
pinMode(output2,INPUT);
pinMode(0,INPUT);
digitalWrite(0,HIGH); // gpio0 como entrada, com pullup, para ser o reset da configuração
EEPROM.begin(120);
if (!digitalRead(2)) // se tecla apertada, vai desgravar a EEPROM
{
EEPROM.write(0,0);
EEPROM.write(1,0);
}
if (EEPROM.read(0) != 'M' || EEPROM.read(1) != 'S')
{
flg_eeprom = 0;
WiFi.softAP(ssid, password);
IPAddress myIP = WiFi.softAPIP();
server.on("/", handleRoot);
server.on("/dados", handle_dados);
server.begin();
Serial.println("START");
while (flg_eeprom<2)
{
server.handleClient();
}
void stop();
void close();
WiFi.softAPdisconnect(true); // fecha o Access Point
Serial.println("STOP");
EEPROM.write(0,'M');
EEPROM.write(1,'S'); // marca que já foi configurado
EEPROM.commit();
while(1){} // deixa watch dog timer atuar
}
else
{
WiFiServer server((EEPROM.read(12) * 256) + EEPROM.read(13)); //Cria um servidor que escuta as conexões de entrada na porta especificada.
IPAddress local_IP(EEPROM.read(0), EEPROM.read(1), EEPROM.read(2), EEPROM.read(3));
IPAddress gateway(EEPROM.read(4), EEPROM.read(5), EEPROM.read(6), EEPROM.read(7));
IPAddress subnet(EEPROM.read(8), EEPROM.read(9), EEPROM.read(10), EEPROM.read(11));
dado1 = EEPROM.read(14); // recupera no. de caracteres do ssid
for (dado2=0; dado2 < dado1; dado2++)ssid += EEPROM.read(dado2 + 16);
dado3 = EEPROM.read(15); // no. de caracteres da senha
for (dado2 = 0; dado2 < dado3; dado2++)password += EEPROM.read (dado2 + 16 + dado1);
WiFi.begin(ssid, password);
server.begin();
bufrx_str = "";
}
}
//*********************************************
// LOOP PRINCIPAL
//*********************************************
void loop()
{
char dado1=0;
WiFiClient client = server.available(); // Listen for incoming clients
if (client)
{
dado1=0;
while (dado1 != '\n')bufrx_str += dado1;
client.write(buffer_rx, 10);
trata_rx();
bufrx_str = "";
}
}
Estes foram os erros gerados:
C:\Users\x\Documents\0Engenharia\00Mario\Modbus\Modbus-TCP\Wifi\Minas Tenis Clube\ESP8266\teste3\teste3.ino: In function 'void trata_rx()':
teste3:241:11: error: 'client' was not declared in this scope; did you mean 'Client'?
241 | client.write(buffer_rx,byte_count); // envia resposta positiva ao server modbus
| ^~~~~~
| Client
C:\Users\x\Documents\0Engenharia\00Mario\Modbus\Modbus-TCP\Wifi\Minas Tenis Clube\ESP8266\teste3\teste3.ino: In function 'void loop()':
teste3:331:32: error: 'using ESP8266WebServer = class esp8266webserver::ESP8266WebServerTemplate' {aka 'class esp8266webserver::ESP8266WebServerTemplate'} has no member named 'available'
331 | WiFiClient client = server.available(); // Listen for incoming clients
| ^~~~~~~~~
exit status 1
'client' was not declared in this scope; did you mean 'Client'?