1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
| #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 LAN en DHCP ou en IP fixe
* lorsque je me connecte par le WAN sur un entrée redirigée
* 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;
} |
Partager