Je viens de faire un rapide teste avec quelque modif et ça marche.
Je me suis d'abord charger du ntp et mis tout ce qui le concerne en local à une fonction comme utile qu'à l'initialisation.
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x6D, 0x86 }; // mac adresse propre à l'arduino
byte ip[] = { 192,168,1,109 }; // ip locale propre à l'arduino
EthernetServer server(1080); // 1080 a remplacer si différent fonctionne également avec ???? => à modifier alors dans php
byte nb = 0;
void setup()
{
Serial.begin(9600);
Ethernet.begin(mac, ip);
print_NTP_Time(NTP()); // on s'occupe d'abord de récupérer l'heure via serveur NTP+affichage
server.begin(); // ensuite du serveur de socket
}
void loop()
{
EthernetClient client = server.available();
if (client)
{
nb++;
if (client.connected())
{
while (client.available())
{
char c = client.read();
Serial.write(c); //on affiche dans le monitor les caractère reçu
if(!client.available())
{
client.write("j'ai bien reçu la requete "); //réponse avec write
client.print(nb, DEC); // réponse avec print (nb = nombre de requêtes)
delay(1);
client.stop();
}
}
Serial.println (); // retour à la ligne dans serial monitor après chaque réception des données
}
}
}
unsigned long NTP() // retourne le nombre de sec. depuis 1 Jan 1970. (Unix time)
{
EthernetUDP Udp;
IPAddress timeServer(192, 43, 244, 18); // time.nist.gov NTP server
const int NTP_PACKET_SIZE= 48; // NTP time stamp is in the first 48 bytes of the message
byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets
memset(packetBuffer, 0, NTP_PACKET_SIZE); //initialise toute les "case" du tableau à 0
//construction de la requete
packetBuffer[0] = 0b11100011; // LI, Version, Mode
packetBuffer[1] = 0; // Stratum, or type of clock
packetBuffer[2] = 6; // Polling Interval
packetBuffer[3] = 0xEC; // Peer Clock Precision
// 8 bytes of zero for Root Delay & Root Dispersion
packetBuffer[12] = 49;
packetBuffer[13] = 0x4E;
packetBuffer[14] = 49;
packetBuffer[15] = 52;
Udp.begin(8888);
Udp.beginPacket(timeServer, 123); //NTP requests are to port 123
Udp.write(packetBuffer,NTP_PACKET_SIZE);
Udp.endPacket();
delay(1000);
if ( Udp.parsePacket() )
{
Udp.read(packetBuffer,NTP_PACKET_SIZE);
Udp.stop();
const unsigned long seventyYears = 2208988800UL;
unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
unsigned long secsSince1900 = highWord << 16 | lowWord;
return secsSince1900 - seventyYears;
}
return 0;
}
void print_NTP_Time(unsigned long epoch) // fonction utilitaire, affiche l'heure UTC
{
Serial.print("Unix time = "); // Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
Serial.println(epoch); // print Unix time:
// print the hour, minute and second:
Serial.print("The UTC time is "); // UTC is the time at Greenwich Meridian (GMT)
Serial.print((epoch % 86400L) / 3600); // print the hour (86400 equals secs per day)
Serial.print(':');
if ( ((epoch % 3600) / 60) < 10 ) // In the first 10 minutes of each hour, we'll want a leading '0'
{
Serial.print('0');
}
Serial.print((epoch % 3600) / 60); // print the minute (3600 equals secs per minute)
Serial.print(':');
if ( (epoch % 60) < 10 ) // In the first 10 seconds of each minute, we'll want a leading '0'
{
Serial.print('0');
}
Serial.println(epoch %60); // print the second
}