Ayant besoin d'afficher l'heure sur une page web via le protocole NTP, j'ai du me pencher sur la réalisation d'un serveur avec mon arduino Uno + Shield Ethernet, la page web est stocké sur le serveur local, tout va bien, mais dès lors que je veux afficher l'heure, ça ne veut pas.
Voici le code :
/*
Web Server
A simple web server that shows the value of the analog input pins.
using an Arduino Wiznet Ethernet shield.
*/
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192,168,0,42);
IPAddress subnet(255,255,255,0); // subnet and gateway are necessary for NTP
IPAddress gateway(192,168,1,254);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
////////////////////////////////NTP////////////////////////////
unsigned int localPort = 8888;
char timeServer[] = "time.nist.gov";
const int NTP_PACKET_SIZE = 48;
byte packetBuffer[ NTP_PACKET_SIZE];
EthernetUDP Udp;
unsigned long highWord;
unsigned long lowWord;
unsigned long secsSince1900;
const unsigned long seventyYears = 2208988800UL;
unsigned long epoch;
int Heure;
int led=2;
int Minutes;
////////////////////////////////NTP////////////////////////////
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
;
}
// start the Ethernet connection and the server:
Ethernet.begin(mac,ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
void loop() {
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
// 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) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the response
client.println("Refresh: 5"); // refresh the page automatically every 5 sec
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
////////////////////////////////NTP////////////////////////////
if (Udp.parsePacket()){
Serial.println("start ntp");
Udp.read(packetBuffer, NTP_PACKET_SIZE);
highWord = word(packetBuffer[40], packetBuffer[41]);
lowWord = word(packetBuffer[42], packetBuffer[43]);
secsSince1900 = highWord << 16 | lowWord;
epoch = secsSince1900 - seventyYears;
Heure = (((epoch % 86400L) / 3600)+1);
Minutes = ((epoch % 3600) / 60);
client.print("Temps : ");
client.print(Heure);
client.print("h ");
client.print(Minutes);
client.print("min ");
client.print(epoch % 60);
client.print("s");
client.print(" LED : ");
if (Heure > 5 && Heure < 19 ) {
digitalWrite(led, HIGH);
client.println("ON");
}
if (Heure < 6 && Heure > 18 ) {
digitalWrite(led, LOW);
client.println("OFF");
}
}
int photocellPin = A4;
int photocellReading;
photocellReading = analogRead(photocellPin);
client.print("Luminosite = ");
client.println(photocellReading);
delay(1000);
client.println();
//client.println("
");
////////////////////////////////NTP////////////////////////////
client.println("</html>");
break;
}
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;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("client disconnected");
Ethernet.maintain();
}
}
void sendNTPpacket(char* address) {
memset(packetBuffer, 0, NTP_PACKET_SIZE);
packetBuffer[0] = 0b11100011;
packetBuffer[1] = 0;
packetBuffer[2] = 6;
packetBuffer[3] = 0xEC;
packetBuffer[12] = 49;
packetBuffer[13] = 0x4E;
packetBuffer[14] = 49;
packetBuffer[15] = 52;
Udp.beginPacket(address, 123); //NTP requests are to port 123
Udp.write(packetBuffer, NTP_PACKET_SIZE);
Udp.endPacket();
}
Le problème viendrai de la, du moins je pense:
Ethernet.begin(mac,ip);
if (Udp.parsePacket()){
Je pense qu'il doit y avoir un conflit, problème avec l'ip
Merci de vos réponses