Hier der sketch. Noch ein wenig durcheinander.
/*
Udp NTP Client
Get the time from a Network Time Protocol (NTP) time server
Demonstrates use of UDP sendPacket and ReceivePacket
For more on NTP time servers and the messages needed to communicate with them,
see http://en.wikipedia.org/wiki/Network_Time_Protocol
created 4 Sep 2010
by Michael Margolis
modified 9 Apr 2012
by Tom Igoe
This code is in the public domain.
*/
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <Time.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SD.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
unsigned int localPort = 8888; // local port to listen for UDP packets
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
boolean printed;
unsigned long epoch;
EthernetUDP Udp;
// SEnsoren
const int chipSelect = 4;
// unsigned long epoch;
#define ONE_WIRE_BUS 7
float Temperatures[5]; // 5 Sensoren
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// start Ethernet and UDP
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
for(;;)
;
}
Udp.begin(localPort);
// GHier startet das Auslesen der Sensoren
pinMode(10, OUTPUT);
Serial.begin(9600);
Serial.println("Dallas Temperature IC Control Library Demo");
// Start up the library
sensors.begin(); // IC Default 9 bit. If you have troubles consider upping it 12.
if (!SD.begin(chipSelect))
{
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
/*
// delete the file:
Serial.println("Entferne Datalog.txt");
SD.remove("Datalog.txt");
if (SD.exists("Datalog.txt"))
{
Serial.println("Datalog.txt konnte nicht geloescht werden.");
}
else
{
Serial.println("Datalog.txt erfolgreich geloescht.");
}
*/
}
void loop()
{
sendNTPpacket(timeServer); // send an NTP packet to a time server
// wait to see if a reply is available
delay(1000);
if ( Udp.parsePacket() ) {
// We've received a packet, read the data from it
Udp.read(packetBuffer,NTP_PACKET_SIZE); // read the packet into the buffer
//the timestamp starts at byte 40 of the received packet and is four bytes,
// or two words, long. First, esxtract the two words:
unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
// combine the four bytes (two words) into a long integer
// this is NTP time (seconds since Jan 1 1900):
unsigned long secsSince1900 = highWord << 16 | lowWord;
// now convert NTP time into everyday time (Unix-Time):
// Serial.print("Unix time = ");
// Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
const unsigned long seventyYears = 2208988800UL;
// subtract seventy years:
unsigned long epoch = secsSince1900 - seventyYears + 7200; // 7200 = Zeitzonenanpassung
// print Unix time:
// Serial.println(epoch);
time_t t=epoch;
if (minute(t)%2 == 0){
if (printed == false) {
if (day(t)<10)
{
Serial.print('0');
}
Serial.print(day(t));
Serial.print(".");
if (month(t)<10)
{
Serial.print('0');
}
Serial.print(month(t));
Serial.print(".");
Serial.print(year(t));
Serial.print(", ");
if (hour(t)<10)
{
Serial.print('0');
}
Serial.print(hour(t));
Serial.print(":");
if (minute(t)<10)
{
Serial.print('0');
}
Serial.print(minute(t));
/*EW Serial.print(":");
if (second(t)<10)
{
Serial.print('0');
}
Serial.println(second(t));*/
Serial.print(", ");
Serial.println();
printed =true;
}
}
else (printed = false);
}
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
sensors.requestTemperatures(); // Send the command to get temperatures
Serial.print("Temperatures: ");
for (int DScount = 0; DScount < 5; DScount++)
{
// EW Temperatures[DScount] = 0;
Temperatures[DScount] =(sensors.getTempCByIndex(DScount));
Serial.print(Temperatures[DScount]);
Serial.print(", ");
}
Serial.println();
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("Datalog.txt", FILE_WRITE);
if (dataFile) // if the file is available, write to it:
{
time_t t=epoch;
if (day(t)<10)
{
dataFile.print('0');
}
dataFile.print(day(t));
dataFile.print(".");
if (month(t)<10)
{
dataFile.print('0');
}
dataFile.print(month(t));
dataFile.print(".");
dataFile.print(year(t));
dataFile.print(", ");
if (hour(t)<10)
{
dataFile.print('0');
}
dataFile.print(hour(t));
dataFile.print(":");
if (minute(t)<10)
{
dataFile.print('0');
}
dataFile.print(minute(t));
/*EW Serial.print(":");
if (second(t)<10)
{
Serial.print('0');
}
Serial.println(second(t));*/
dataFile.print(", ");
for (int DScount = 0; DScount < 5; DScount++)
{
dataFile.print(Temperatures[DScount]);
dataFile.print(", ");
dataFile.print("Sensor ");
dataFile.print(DScount +1);
if (DScount==4)
{
dataFile.println();
}
}
dataFile.close();
}
// if the file isn't open, pop up an error:
else
{
Serial.println("error opening datalog.txt");
}
// wait ten seconds before asking for the time again
delay(10000);
}
// send an NTP request to the time server at the given address
unsigned long sendNTPpacket(IPAddress& address)
{
// set all bytes in the buffer to 0
memset(packetBuffer, 0, NTP_PACKET_SIZE);
// Initialize values needed to form NTP request
// (see URL above for details on the packets)
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;
// all NTP fields have been given values, now
// you can send a packet requesting a timestamp:
Udp.beginPacket(address, 123); //NTP requests are to port 123
Udp.write(packetBuffer,NTP_PACKET_SIZE);
Udp.endPacket();
}