Ciao a tutti.
Sottopongo alla vostra "esperta" attenzione un fenomeno strano che mi sta succedendo.
La situazione è questa: ho due arduino (nano e Uno), il primo configurato come web client, il secondo come web server. Non si parlano direttamente, ma attraverso un server Apache con PHP e MySQL.
Il client invia un POST ad uno script PHP sul server Apache, il quale lo interpreta e invia il risultato dell'interpretazione al web server arduino tramite curl.
La stessa operazione si può effettuare da una semplice pagina web che si trova sul server Apache: la pagina web invia i dati del POST allo script PHP e questo li gira al web server arduino con curl.
Fin qui niente di straordinario: sul web si trovano decine di queste cose!
Il fatto strano che si verifica è questo: se il POST arriva al web server arduino tramite pagina web, il web server arduino esegue il comando senza esitazioni, ma, se arriva dal client arduino, 9 volte su 10 si perde per strada.
Ho provato a collegare un arduino per volta: prima il solo client, inviando il comando al server Apache e tutto funziona correttamente. Il comando viene ricevuto dal server.
Poi ho collegato solo il web server arduino (senza client arduino) e riceve correttamente il comando dalla pagina web, eseguendolo senza problemi di nessun tipo.
Il problema si presenta solo quando sono entrambi collegati.
Indagando, ho riscontrato che il web server arduino ha bisogno di circa 60 secondi di stop tra un comando e l'altro (inviato del client arduino): già 55 secondi non vanno bene!
Siccome non ho una grande esperienza, chiedo a voi un parere: è un problema della mia rete lan (che si perde per strada i pacchetti del client arduino)? Un conflitto tra le due schede arduino sulla rete? O forse un problema di codice (anche se, come ho detto, presi singolarmente funzionano senza intoppi...)?
Naturalmente, grazie per ogni suggerimento che vorrete darmi.
qui sotto il codice del web server arduino.
#include <SPI.h>
#include <Ethernet.h>
#include <string.h>
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0xCD, 0xCA };
IPAddress ip(192,168,1,52);
EthernetServer server(80);
char stanza[15];
char opzione[15];
char action[15];
boolean optOn = false;
int optionPin;
int opt_0 = 0;
int opt_1 = 0;
int opt_2 = 0;
void setup() {
Serial.begin(57600);
pinMode(8, OUTPUT);
pinMode(7, OUTPUT);
pinMode(6, OUTPUT);
Serial.println("********************************************");
Serial.println("** Establishing network connection... **");
Ethernet.begin(mac, ip);
delay(300);
server.begin();
Serial.println("** Connected. **");
Serial.print("** Arduino IP address: ");
Serial.print(Ethernet.localIP());
Serial.print(" **");
Serial.println();
Serial.println("********************************************");
}
#define bufferMax 128
int bufferSize;
char buffer[bufferMax];
void loop() {
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println("** Client connected. **");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
bufferSize = 0;
while (client.connected()) {
if(client.available()){
char c = client.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 (c == '\n' && currentLineIsBlank) {
// Here is where the POST data is.
while(client.available()) {
char post = client.read();
if(bufferSize < bufferMax) // if the character is NOT a 'RETURN'
// and the number of characters received
// is les than bufferMax, keep reading
buffer[bufferSize++] = post; // save the new character in buffer and increment bufferSize
}
// Prints the received request to serial monitor
Serial.println("** Received POST request: **");
Serial.println(buffer); // buffer contains the received request
ParseReceivedRequest();
PerformRequestedCommands();
// Sends response to client (browser)
Serial.println("********************************************");
Serial.println("** Sending response **");
Serial.println("********************************************");
// send a standard http response header that cause a redirect to the Apache server
client.print("<html>");
client.print("<head>");
client.print("<meta http-equiv='refresh' content='0; url=http://192.168.1.53/superhouse/rmprocessor.php?stanza=");
client.print(stanza);
client.print("'>");
client.print("</head>");
client.print("</html>");
client.flush();
client.stop();
delay(300);
}
else if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
Serial.println("** Port closed. Waiting for next request. **");
Serial.println("********************************************");
Serial.println();
}
}
void ParseReceivedRequest() {
// Prints message to serial monitor
Serial.println("********************************************");
Serial.println("** Parsing received POST request **");
Serial.println("********************************************");
Serial.println("** Printing parsed strings for debugging **");
Serial.println("********************************************");
// Set some variables to parse the received string
char* p1;
char* p2;
char* p3;
p1 = strstr(buffer, "=") +1;
p2 = strstr(p1, "=") +1;
p3 = strstr(p2, "=") +1;
if(strlen(p3) > 1) {
strncpy(p3, p3, 1);
p3[1] = '\0';
}
// Prints the strings to serial monitor
PrintString("p1", p1);
PrintString("p2", p2);
PrintString("p3", p3);
// Initializes the strings
stanza[0] = 0;
opzione[0] = 0;
action[0] = 0;
// Builds the command and parameter strings
strncat(stanza, p1, p2-p1-12);
strncat(opzione, p2, p3-p2-8);
strncat(action, p3, p2-p3-1);
// Prints the command and the parameter to serial monitor
PrintString("stanza", stanza);
PrintString("opzione", opzione);
PrintString("action", action);
}
void PerformRequestedCommands() {
if(strcmp(opzione, "24") == 0) {
optionPin = 6;
}
if(strcmp(opzione, "100") == 0) {
optionPin = 7;
}
if(strcmp(opzione, "101") == 0) {
optionPin = 8;
}
if(strcmp(action, "1") == 0) {
optOn = true;
RemoteDigitalWrite();
}
if(strcmp(action, "0") == 0) {
optOn = false;
RemoteDigitalWrite();
}
}
void RemoteDigitalWrite() {
if (optOn) {
digitalWrite(optionPin, HIGH);
if(optionPin == 6) {
opt_0 = 24;
} else if(optionPin == 7) {
opt_1 = 100;
} else if(optionPin == 8) {
opt_2 = 101;
}
} else {
digitalWrite(optionPin, LOW);
if(optionPin == 6) {
opt_0 = 0;
} else if(optionPin == 7) {
opt_1 = 0;
} else if(optionPin == 8) {
opt_2 = 0;
}
}
// Information sent to the serial monitor
Serial.println("Remote Digital Write: ");
PrintNumber("optionPin", optionPin);
if (optOn) {
PrintNumber("Option#0", opt_0);
PrintNumber("Option#1", opt_1);
PrintNumber("Option#2", opt_2);
} else {
PrintNumber("Option#0", opt_0);
PrintNumber("Option#1", opt_1);
PrintNumber("Option#2", opt_2);
}
}
void PrintString(char* label, char* str) {
Serial.print(label);
Serial.print("=");
Serial.println(str);
}
void PrintNumber(char* label, int number) {
Serial.print(label);
Serial.print("=");
Serial.println(number, DEC);
}