Salve a tutti, ho un problema con lo sketch di un progetto e dopo che l'ho guardato e riguardato non riesco a venire a capo di una soluzione.
Ora vi spiego il progetto:
Ho due arduini Uno con ethernet shield W5100 collegati tra di loro con un router dedicato su una lan solo per loro, il primo trasmette: un carattere di controllo per identificare l'inizio della tresmissione, 3 variabili float e un ultimo carattere di controllo per chiudere la trasmissione. il secondo riceve solamente per poi scrivere ciò che riceve su un display (anche se per adesso lo fa sul monitor seriale).
Allego gli sketch
TX (premetto che per cercare di risolvere il problema trasmette sempre gli stessi valori 11.11, 22.22, 33.33):
char nomeprog[]="LAN COM TX TEST";
char versione[]="v0 ser.";
#define LanID 'P' // id di riconoscimento
#define LanBreak 'p' // separatore variabili trasmesse
//parte Ethernet
#include <Ethernet.h>
#include <SPI.h>
// ****** PARAMETRI ETHERNET ******
// assign a MAC address for the Ethernet controller.
// fill in your address here:
byte mac[] = {
0xDE, 0xED, 0xBE, 0xEF, 0xFE, 0xED
};
// assign an IP address for the controller:
IPAddress PowerIp (132, 180, 0, 3); // indirizzo ufficiale
//IPAddress ip(192, 168, 1, 182); // test lan casa
IPAddress serverMAIN(132,180,0,2);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
//EthernetServer server(80);
EthernetClient POWERclient;
int port = 80;
void setup()
{
Serial.begin(9600);
// PRESENTAZIONE
Serial.println(nomeprog);
Serial.println(versione);
Serial.print("\n");
// BOOT PARTE ETHERNET
// start the SPI library:
SPI.begin();
Serial.println("SPI ok");
// start the Ethernet connection and the server:
Ethernet.begin(mac, PowerIp);
Serial.println("ethernet ok");
//server.begin();
Serial.println("server ok");
Serial.println(LanID);
// Fine Boot Ethernet
Serial.println(" \nBOOT COMPLETO\n \n \n");
delay(1500);
//Fine boot
//FINE_SETUP
}
void loop()
{
POWERclient.connect (serverMAIN, port);
if (POWERclient.connected() == true)
{
Serial.println("connected");
POWERclient.println(LanID);
POWERclient.println("11.11");
//POWERclient.println(LanBreak);
POWERclient.println("22.22");
//POWERclient.println(LanBreak);
POWERclient.println("33.33");
POWERclient.println(LanBreak);
//POWERclient.println(LanBreak);
POWERclient.stop();
delay(1500);
}
else
{
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
}
RX:
char nomeprog[]="LAN COM RX TEST";
char versione[]="v0 ser.";
//parte Ethernet
#include <Ethernet.h>
#include <SPI.h>
String LanIn = "", LanIn2= "", LanIn3= "";
#define PwrID 'P' // id di riconoscimento
#define PwrEND 'p' // chiusura paccheto variabili trasmesse PWR
// ****** PARAMETRI ETHERNET ******
// assign a MAC address for the Ethernet controller.
// fill in your address here:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
// assign an IP address for the controller:
IPAddress ip(132, 180, 0, 2);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
float Voltbatt1, Voltbatt2, Voltbatt3;
void setup()
{
Serial.begin(9600);
// PRESENTAZIONE
Serial.println(nomeprog);
Serial.println(versione);
Serial.print("\n");
// BOOT PARTE ETHERNET
// start the SPI library:
SPI.begin();
Serial.println("SPI ok");
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
Serial.println("ethernet ok");
server.begin();
server.available();
Serial.println("server ok");
// Fine Boot Ethernet
Serial.println(" \nBOOT COMPLETO\n \n \n");
delay(1500);
//FINE_SETUP
}
void loop()
{
EthernetClient client = server.available();
if (client)
{
char c;
Serial.println("Got a client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected())
{
if (client.available())
{
c = client.read();
//Serial.print(c);
char ver=' ';
int b;
if (c==PwrID)
{
ver=c;
b=1;
}
while (ver==PwrID)
{
c = client.read();
//Serial.print(c);
if (c != '\n') // ricezione batt1
{
if (b=1)
{
LanIn += (char)c;
}
if (b=2)
{
LanIn2 += (char)c;
}
if (b=3)
{
LanIn3 += (char)c;
}
}
else
{
if (b=1)
{
Voltbatt1=LanIn.toFloat();
//Serial.println(LanIn);
//Serial.println(Voltbatt1);
LanIn = "";
b=2;
}
if (b=2)
{
Voltbatt2=LanIn2.toFloat();
//Serial.println(LanIn2);
//Serial.println(Voltbatt2);
LanIn2 = "";
b=3;
}
if (b=3)
{
Voltbatt3=LanIn3.toFloat();
//Serial.println(LanIn3);
//Serial.println(Voltbatt3);
LanIn3 = "";
b=4;
}
}// fine ricezione 1
if(c==PwrEND) //controllo per uscita while
{
ver='x';
}
}// Fine While PWR
Serial.println("****");
Serial.println(Voltbatt1);
Serial.println(Voltbatt2);
Serial.println(Voltbatt3);
}
}
}
}
Il problema è che così comè il tutto mi sembra corretto, ma correggetemi se sbaglio, solamente che ottengo come risultato:
Got a client
****
33.33
33.33
33.33
quale può essere il problema?
Grazie a tutti per la pazienza e per le eventuali risposte.