Bonjour,
j'ai un souci de connexion WAN lorsque je me connecte avec une IP fixe, alors que tout fonctionne bien en DHCP.
J'ai beau tout essayer ça ne veut rien savoir.
Quelqu'un aurait une idée à me soumettre ???
Merci d'avance
Patrick
#include <SPI.h> //For the selection of the key
#include <Ethernet2.h> //gestion réseau WS5500
#define DEBUG
//#define T_DHCP //connection par DHCP sinon IPfixe
#define WAN //CONNECTION WAN sinon LAN
#define SS_IP 53 //connection SS sur mega2560pro
//variables pour la construction de la trame d'interrogation
const char sep = '¤'; //¤ séparateur de trame
const char FT = 253; // fin de trame
int value = 0;
String trame1;
int TR0;
int TR1;
int TR2;
int TR3;
String code;
String carte;
EthernetClient client; //ETHERNET INSTANCE
signed long next;
//adresse mac du module IP
byte mac[6] = {0x00,0x01,0x02,0x03,0x04,0x05};
IPAddress IP(192,168,0,112); //adresse fixe du module enregistrée sur le routeur
IPAddress PAS(192,168,0,1); //passerelle
IPAddress DNS(192,168,0,254); //dns
IPAddress MAS(255,255,255,0); //masque
#ifdef WAN
IPAddress SRV(91,160,17,23); //IP WAN
const long PORT = 33007; //port WAN
#else
IPAddress SRV(192,168,0,65); //IP LAN
const long PORT = 10007; //port LAN
#endif
signed long timeout; //TIMEOUT SO IT DOESN'T SIT THERE FOREVER
/*la connection fonctionne :
* lorsque je me connecte sur le réseau local "PC2017 port 10007" en DHCP ou en IP fixe
* lorsque je me connect par le WAN sur un entrée redirigée "LHD port 30007"
* 33007 est redirigée par le routeur sur 10007
* en DHCP c'est ok mais impossible de se connecter en IP fixe ???
*/
void setup() {
Serial.begin(115200);
digitalWrite(SS_IP, 0);
Ethernet.init(SS_IP);
Serial.println ("***************************************************************");
Serial.println ("********* CONNECTION SERVEUR **********************************");
Serial.println ("***************************************************************");
#ifdef WAN
Serial.print ("connection WAN :");
#else
Serial.print ("connection LAN :");
#endif
Serial.print (SRV);
Serial.print(" - ");
Serial.println (PORT);
#ifdef T_DHCP //CONNECTION QUI FONCTIONNE
int rep=0;
Serial.print("test en DHCP rep= ");
rep=Ethernet.begin(mac); //solution DHCP pour fonctionner sur tous les sites
Serial.println(rep);
#else //CONNECTION QUI NE FONCTIONNE PAS!!!!!!!!
Serial.print("test avec IP fixe : ");
Serial.println(IP);
//Ethernet.begin(mac, IP, DNS, PAS, MAS);
//Ethernet.begin(mac, IP, DNS, PAS);
//Ethernet.begin(mac, IP, DNS);
Ethernet.begin(mac,IP);
#endif
delay(3000);
#ifdef DEBUG
Serial.print("localIP: "); Serial.println(Ethernet.localIP());
Serial.print("subnetMask: "); Serial.println(Ethernet.subnetMask());
Serial.print("gatewayIP: "); Serial.println(Ethernet.gatewayIP());
Serial.print("dnsServerIP: "); Serial.println(Ethernet.dnsServerIP());
Serial.print("Serveur : "); Serial.print (SRV);
Serial.print(" Port : "); Serial.println (PORT);
Serial.println ("=================================================");
#endif
}
void loop() {
Connect();
delay(5000);
}
void Connect() {
++value;
TR0=99;
TR1=2;
TR3=3;
code="OSMOPUR";
#ifdef DEBUG
Serial.print("Connexion : "); Serial.print(SRV);Serial.print(" - Tentative: "); Serial.println(value);
#endif
int rep=0;
rep= (client.connect(SRV,PORT));
if (rep==1)
{
#ifdef DEBUG
Serial.println(" - OK");
value=0;
#endif
// Créer la trame à envoyer
trame1= TR0;
trame1 += sep;trame1 += TR1;
trame1 += sep;trame1 += TR2;
trame1 += sep;trame1 += TR3;
trame1 += sep;trame1 += code;
trame1 += sep;trame1 += carte;
trame1 += FT;
client.print(trame1);
//client.print("Des infos de la part du client - "); client.println(IP);
unsigned long timeout = millis();
while(client.available()==0) {
if (millis() - timeout > 8000) {
//pas de réponse du serveur
#ifdef DEBUG
Serial.print(" >>> Client Timeout ! ");
#endif
goto close;
}
}
int size;
while((size = client.available()) > 0) {
{
uint8_t* msg = (uint8_t*)malloc(size);
size = client.read(msg,size);
#ifdef DEBUG
Serial.print(" - Réponse serveur: ");
Serial.write(msg,size);
#endif
//Serial.println();
free(msg);
}
}
close:
//disconnect client
#ifdef DEBUG
Serial.println(" - Client déconnecté ");
#endif
client.stop();
}else{
#ifdef DEBUG
Serial.println(" - Client connect Erreur ");
#endif
}
delay(1000);
return;
}