Actually, someone suggested to manage all from Arduino, using ESP as a slave.
But now the problem is that if I call a remote server from Arduino client, when it reply (with a webpage) it is interpreted as a new connection to the Arduino server, messing up the request.
Server is listening char-by-char and it thinks the message is finished when it find \r char
On the other side Client response is a web page with a lot of \n and \r
loadServer();
loadClient();
if you comment loadServer() you can see the correct response to the client request.
The Question is: calls and relative requests should be automatically handled with the socket_id inside the WiFiEsp.h class? Am I wrong? Why it is not working?
Do I have to modify my code?
If not possible to distinguish server/client incoming messages using socket_id,
maybe I need to wait the message is finished (how?) and the analyze the content to understand if it for the client or for the server? (poor performance solution, I think)
My actual code is
#include "WiFiEsp.h"
const char* ssid = "ssid";
const char* password = "password";
int status = WL_IDLE_STATUS; // the Wifi radio's status
int reqCount = 0;
char webServer[] = "www.voidbrain.net";
String url = "/temp/grover/ajax/moduli/api/redneck/endpoint";
String params = "";
WiFiEspServer ArduinoServer(80);
WiFiEspClient webClient;
void setup() {
Serial.begin(115200);
Serial1.begin(115200);
WiFi.init(&Serial1);
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
while (true); // don't continue
}
while ( status != WL_CONNECTED) { // attempt to connect to WiFi network
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network
status = WiFi.begin(ssid, password);
}
Serial.println("You're connected to the network");
printWifiStatus();
ArduinoServer.begin();
}
void loop() {
while (webClient.available()) { // reply from remote. if Arduino server is running this is not working
char c = webClient.read();
Serial.write(c);
}
loadServer();
loadClient();
}
void loadServer(){
// listen for incoming clients
WiFiEspClient serverConnection = ArduinoServer.available();
if (serverConnection) {
Serial.println("New client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (serverConnection.connected()) {
if (serverConnection.available()) {
char incomingMessage = serverConnection.read();
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (incomingMessage == '\n' && currentLineIsBlank) {
Serial.println("Sending response");
// send a standard http response header
// use \r\n instead of many println statements to speedup data send
serverConnection.print(
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/html\r\n"
"Connection: close\r\n" // the connection will be closed after completion of the response
"Refresh: 20\r\n" // refresh the page automatically every 20 sec
"\r\n");
serverConnection.print("<!DOCTYPE HTML>\r\n");
serverConnection.print("<html>\r\n");
serverConnection.print("<h1>Hello World!</h1>\r\n");
serverConnection.print("Requests received: ");
serverConnection.print(++reqCount);
serverConnection.print("
\r\n");
serverConnection.print("Analog input A0: ");
serverConnection.print(analogRead(0));
serverConnection.print("
\r\n");
serverConnection.print("</html>\r\n");
break;
}
if (incomingMessage == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (incomingMessage != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(10);
// close the connection:
serverConnection.stop();
Serial.println("Client disconnected");
}
}
void loadClient(){
while (Serial.available()) {
params = Serial.readString(); // "azione=accendi&sensore_id=12"
httpRequestToWebServer();
}
}
// this method makes a HTTP connection to the server
void httpRequestToWebServer() {
webClient.stop(); // close any connection before send a new request, this will free the socket on the WiFi shield
if (webClient.connect(webServer, 80)) {
Serial.println("Connecting...");
params = "azione=accendi&sensore_id=12"; // just for test
webClient.println(String("GET ") + url + String("?") + params + String(" HTTP/1.1"));
webClient.println(String("Host: ")+webServer);
webClient.println("Connection: close");
webClient.println();
Serial.println(String("url: ")+webServer+url+ String("?") + params);
} else {
Serial.println("Connection failed");
}
}
void printWifiStatus() {
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
long rssi = WiFi.RSSI();
Serial.print("Signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}