#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()) { // risposta dal server remoto. se il server Arduino è attivo non funziona
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"; // temporaneo
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");
}
Se loadServer(); è commentato vedo le risposte alle chiamate del client.
Se invece accendo anche il server, ogni messaggio viene dirottato a lui come nuova connessione, anche se era una risposta al client.
Altra domanda, se posso: vorrei passare una querystring, ma se la scrivo a mano funziona, se invece provo a passarla dall'input del serial monitor no. C'è forse un qualche carattere invisibile NL CR che scombina la querystring? O è perchè il server riceve carattere per carattere e non aspetto che Serial1 abbia finito?
Se nello stesso punto stampo la stringa vedo il contenuto correttamente
Anche il Server Arduino deve gestire dati passati in querystring, vedo che se chiamo http://192.168.1.4/?azione=accendi&motore=2 il server risponde, devo ora capire come estrapolarmi chiavi e valori