#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <Time.h>
#include <TimeAlarms.h>
//RELAY BOARD CONFIGURATION
#define RELAY_ON 0
#define RELAY_OFF 1
#define Relay_1 3 // Relay 1
#define Relay_2 5 // Relay 2
#define Relay_3 6 // Relay 3
#define Relay_4 8 // Relay 4
#define Relay_5 9 // Relay 5
#define Relay_6 4 // Relay 6
#define Relay_7 7 // Relay 7
#define Relay_M 2 // Relay 8 (Master Valve)
// Network and NTP Configuration
byte mac[] = { 0x90, 0xA2, 0xDA, 0x06, 0x00, 0x5A }; //Mac Adress Arduino
unsigned int localPort = 8888; // UDP Port
IPAddress timeServer(199,167,198,163); // pool.ntp.org 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;
int timeZoneHour = +2; // Timezone
long timeZoneOffset = (timeZoneHour * -1) * 60 * 60 ;
int NTP_Update_Interval = 86400; // NTP Update Interval
// Watering Program
int execution; //
int active_stations; // Active Watering stations (used to calculate the pause period)
int period1 = 10; // Station 1 Watering period (in seconds)
int period2 = 20; // Station 2 Watering period (in seconds)
int period3 = 20; // Station 3 Watering period (in seconds)
int period4 = 20; // Station 4 Watering period (in seconds)
int period5 = 10; // Station 5 Watering period (in seconds)
int period6 = 10; // Station 6 Watering period (in seconds)
int period7 = 10; // Station 7 Watering period (in seconds)
long pause_time = 120; // Pause Time (in seconds)
// Start and Stop Configuration
int orapartenza = 8; // Start time (Morning Alarm) - Hour
int minutopartenza = 0; // Start time (Morning Alarm) - Minute
int orafine = 19; // Stop time (Evening Alarm) - Hour
int minutofine = 0; // Stop time (Evening Alarm) - Minute
// Void Setup - Executed only once
void setup()
{
digitalWrite(Relay_1, RELAY_OFF);
digitalWrite(Relay_2, RELAY_OFF);
digitalWrite(Relay_3, RELAY_OFF);
digitalWrite(Relay_4, RELAY_OFF);
digitalWrite(Relay_5, RELAY_OFF);
digitalWrite(Relay_6, RELAY_OFF);
digitalWrite(Relay_7, RELAY_OFF);
digitalWrite(Relay_M, RELAY_OFF);
Serial.begin(9600);
Serial.println("Starting");
// 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);
Serial.print("Got IP:");
Serial.println(Ethernet.localIP());
setSyncProvider(getNTPTime);
Serial.println("Looking for a time");
while(timeStatus()== timeNotSet);
Serial.println("Got a Time");
setSyncInterval(NTP_Update_Interval);
// ALARM
Alarm.alarmRepeat(orapartenza,minutopartenza,0, MorningAlarm); // START - Morning Alarm
Alarm.alarmRepeat(orafine,minutofine,0,EveningAlarm); // STOP - Evening Alarm
//RELAY
pinMode(Relay_1, OUTPUT);
pinMode(Relay_2, OUTPUT);
pinMode(Relay_3, OUTPUT);
pinMode(Relay_4, OUTPUT);
pinMode(Relay_5, OUTPUT);
pinMode(Relay_6, OUTPUT);
pinMode(Relay_7, OUTPUT);
pinMode(Relay_M, OUTPUT);
}
//void loop
void loop(){
if (execution == 1)
{
Azione();
}
else
{
print_time();
Alarm.delay(1000);
}
}
// Printdigits (only used for serial debug)
void printDigits(int digits){
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}
//Accende i LED
void Azione(){
// MASTER
if ( period1 > 0 || period2 > 0 || period3 > 0 || period4 > 0 || period5 > 0 || period6 > 0 || period7 > 0 )
{
Serial.print("MASTER VALVE START: ");
print_time();
digitalWrite(Relay_M, RELAY_ON);
Alarm.delay(1000);
active_stations++;
}
// Stazione 1
if ( period1 > 0 ){
Serial.print("RELAY 1 START: ");
print_time();
digitalWrite(Relay_1, RELAY_ON);
Alarm.delay(period1*1000);
digitalWrite(Relay_1, RELAY_OFF);
Alarm.delay(1000);
active_stations++;
}
//Stazione 2
if ( period2 > 0 ){
Serial.print("RELAY 2 START: ");
print_time();
digitalWrite(Relay_2, RELAY_ON);
Alarm.delay(period2*1000);
digitalWrite(Relay_2, RELAY_OFF);
Alarm.delay(1000);
active_stations++;
}
//Stazione 3
if ( period3 > 0 ){
Serial.print("RELAY 3 START: ");
print_time();
digitalWrite(Relay_3, RELAY_ON);
Alarm.delay(period3*1000);
digitalWrite(Relay_3, RELAY_OFF);
Alarm.delay(1000);
active_stations++;
}
// Stazione 4
if ( period4 > 0 ){
Serial.print("RELAY 4 START: ");
print_time();
digitalWrite(Relay_4, RELAY_ON);
Alarm.delay(period4*1000);
digitalWrite(Relay_4, RELAY_OFF);
Alarm.delay(1000);
active_stations++;
}
// Stazione 5
if ( period5 > 0 ){
Serial.print("RELAY 5 START: ");
print_time();
digitalWrite(Relay_5, RELAY_ON);
Alarm.delay(period5*1000);
digitalWrite(Relay_5, RELAY_OFF);
Alarm.delay(1000);
active_stations++;
}
// Stazione 6
if ( period6 > 0 ){
Serial.print("RELAY 6 START: ");
print_time();
digitalWrite(Relay_6, RELAY_ON);
Alarm.delay(period6*1000);
digitalWrite(Relay_6, RELAY_OFF);
Alarm.delay(1000);
active_stations++;
}
// Stazione 7
if ( period7 > 0 ){
Serial.print("RELAY 7 START: ");
print_time();
digitalWrite(Relay_7, RELAY_ON);
Alarm.delay(period7*1000);
digitalWrite(Relay_7, RELAY_OFF);
Alarm.delay(1000);
active_stations++;
}
// Fine MASTER e Pausa
if ( period1 > 0 || period2 > 0 || period3 > 0 || period4 > 0 || period5 > 0 || period6 > 0 || period7 > 0 ) {
digitalWrite(Relay_M, RELAY_OFF);
Serial.print("Begin pause: ");
print_time();
Serial.print("Pause duration: ");
Serial.print(pause_time-period1-period2-period3-period4-period5-period6-period7-active_stations);
Serial.print(" Seconds ");
Serial.println();
Alarm.delay(pause_time*1000-period1*1000-period2*1000-period3*1000-period4*1000-period5*1000-period6*1000-period7*1000-1000*active_stations);
active_stations=0;
}
}
// print_time - Used to print time (only used in serial debug)
void print_time() {
printDigits(hour());
Serial.print(":");
printDigits(minute());
Serial.print(":");
printDigits(second());
Serial.print(" ");
printDigits(day());
Serial.print("/");
printDigits(month());
Serial.print("/");
Serial.print(year());
Serial.println();
}
// functions to be called when an alarm triggers:
void MorningAlarm(){
execution = 1;
}
void EveningAlarm(){
execution = 0;
}
//NTP Code
unsigned long getNTPTime()
{
Serial.println("In getNTPTime");
sendNTPpacket(timeServer);
Alarm.delay(1000);
if ( Udp.parsePacket() ) {
Serial.println("Got Time Packet");
Udp.read(packetBuffer,NTP_PACKET_SIZE);
unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
unsigned long secsSince1900 = highWord << 16 | lowWord;
const unsigned long seventyYears = 2208988800UL + timeZoneOffset;
unsigned long epoch = secsSince1900 - seventyYears;
return epoch;
}
Serial.println("No Time Packet Found");
return 0;
}
// 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();
}
Here you are... i know i can change period1, period2, period3 etc. to change the watering time (in the code I posted now station 2, 3 and 4 have 20 seconds of activation, all the other stations have 10 seconds), but the pause will always be the same for every valve.
I would like to really have an independent control of the valves (for example: valve 1 waters 10 seconds and pause for 20 minutes, valve 2 waters 20 seconds and pause for 3 hours etc. etc.) and independent morning and evening alarm for each valve.
I don't want anybody to write me the code XD , i'm just asking for suggestions on how you would do it.