Hello,
I am new in programming Arduino and I can`t find a solution for this Problem. I hope you can help me!
I want control extrenal devices via a Realis at a specifik time. To get the correct time I use the eathernet schield. This works fine.
The Problem is, that the digitalWrite doesn´t work.
Here is the code:
#include <TimeAlarms.h>
#include <Time.h>
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
//Ethernet Shield Settings
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x0F, 0xD0 }; // Mac Adresse des Ethernet Shields
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
EthernetUDP Udp; // A UDP instance to let us send and receive packets over UDP
//Programm Settings
int NestAufPin = 13; // Nest-Auf auf PIN 13 legen
int NestZuPin = 12;
int t = 3000; // Impulsdauer
int T = 6000; // Periodendauer
long Zeitstempel = 0; // Zeitstempel für Delay
int ledState = LOW; // Initial kein Strom
void setup()
{
Serial.begin(9600);
// Alarm Settings
Alarm.alarmRepeat(5,0,0,NestAuf); // Nest auf um 5:00 Uhr
Alarm.alarmRepeat(21,0,0,NestZu); // Nest schließen um 21:00 Uhr
Alarm.timerRepeat(5, Repeats);
// 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);
ZeitSync();
pinMode(NestAufPin, OUTPUT);
pinMode(NestZuPin, OUTPUT);
digitalWrite(NestAufPin, ledState);
digitalWrite(NestZuPin, ledState);
}
void loop(){
if (millis() - Zeitstempel > 1000){ // eine Sekunde warten vor der aktualisierung der Zeit ohne D
Alarm.delay(0);
Zeitstempel = millis();
digitalClockDisplay();
}
}
void ZeitSync(){
Serial.println("Zeit sync");
sendNTPpacket(timeServer); // send an NTP packet to a time server
delay(1000); // wait to see if a reply is available
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
unsigned long highWord = word(packetBuffer[40], packetBuffer[41]); // 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 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;
const unsigned long seventyYears = 2208988800UL;
unsigned long epoch = secsSince1900 - seventyYears;
unsigned long std = ((epoch % 86400L) / 3600) + 1; // UTC Zeit + 1 = Winterzeit
unsigned long minu = ((epoch % 3600) / 60);
unsigned long sek = (epoch %60);
setTime(std,minu,sek,1,1,11);
}
}
void NestAuf(){
Serial.println("Nest auf");
digitalWrite(NestAufPin, HIGH);
digitalWrite(NestZuPin, LOW);
}
void NestZu(){
Serial.println("Nest schließen");
digitalWrite(NestAufPin, LOW);
for(int i = 0; i < 10; i++) {
digitalWrite(NestZuPin, HIGH);
delay(t);
digitalWrite(NestZuPin, LOW);
delay(T - t);
}
}
void Repeats(){
Serial.println("15 second timer");
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
digitalWrite(NestAufPin, ledState);
Serial.println(ledState);
}
void digitalClockDisplay() // digital clock display of the time
{
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.println();
}
void printDigits(int digits)
{
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}
// 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();
}
To see if the TimeEvent works there is the "Repeats" function.
Here is the result:
Zeit sync
15 second timer
1
20:23:01
20:23:02
20:23:03
20:23:04
20:23:05
15 second timer
0
20:23:06
20:23:07
20:23:08
20:23:09
20:23:10
15 second timer
1
20:23:11
20:23:12
20:23:13
20:23:14
20:23:15
15 second timer
0
20:23:16
20:23:17
20:23:18
20:23:19
20:23:20
15 second timer
1
20:23:21
20:23:22
20:23:23
You can see, that the event works fine, but the "NestAufPin 13" doesn´t change the voltage. I can not unterstand why?
If I test the board with the "blink"-Example everything works fine.
I use the Arduino 1.0.1 and the sketch use 16.924 Bytes.
Many thanks for your help!