Hi everyone,
I wrote some code for an automated sprinkler system using an Arduino Uno, ethernet shield, and an 8 relay board. I would like to be able to set a weekly schedule for it and am planning on using the timeAlarms library and NTP time. I'm just wondering what the best way is to implement the NTP into my existing code (I'm using the example from http://arduino.cc/en/Tutorial/UdpNtpClient). I'm trying to figure out when to send and read the packet from the time server...this is just confusing to me since I am already reading information from a client using the ethernet shield. Should I send the NTP packet before I connect to the client, or after the client is available?
Aso, all of the output in the NTP example is done in an if() statement:
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;
Serial.print("Seconds since Jan 1 1900 = " );
Serial.println(secsSince1900);
...
...
...
}
(I will be using client.print in place of serial.print)
Do I need to put my existing void loop() code inside this if() statement? Or should it be vice versa?
This is my existing code:
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <LiquidCrystal.h>
#include <Time.h>
#include <TimeAlarms.h>
// General
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x98, 0x26 };
IPAddress ip(192,168,1,134);
EthernetServer server(80);
int pin[8] = {2,3,4,5,6,7,8,9};
static char readString[30];
static char *state[8] = {"OFF","OFF","OFF","OFF","OFF","OFF","OFF","OFF"};
char trueFalse;
int val;
int cursorPin;
// NTP
unsigned int localPort = 8888;
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];
EthernetUDP Udp; // A UDP instanve to let us send and receibe packets over UDP
//LCD
LiquidCrystal lcd(14,15,16,17,18,19);
int backLight = 1;
void setup()
{
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
//Sets the LEDpin as an output
for(int i = 0; i < 8; i++){
pinMode(pin[i],OUTPUT);
digitalWrite(pin[i],HIGH);
}
//LCD Setup
pinMode(backLight, OUTPUT);
digitalWrite(backLight, HIGH);
lcd.begin(20,4);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("SprinkDuino");
lcd.setCursor(0,1);
lcd.print("Version 1.0");
delay(5000);
lcd.clear();
//TIME Setup
Udp.begin(localPort);
}
void loop()
{
EthernetClient client = server.available();
if (client) {
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
int pos = strlen(readString);
if(pos < 30){
readString[pos++] = c;
readString[pos] = '\0';
}
if (c == '\n' && currentLineIsBlank) {
char *response = strtok(readString, "?");
response = strtok(NULL, "=");
if(strcmp (response, "A") == 0){
val = 1;
}
else {
val = atoi(response);
}
response = strtok(NULL, ";");
if(strcmp (response, "T") == 0){
trueFalse = 'T';
}
else {
trueFalse = 'F';
}
client.println("<html>");
//Refresh page every 5 seconds to keep page live
client.print("<meta http-equiv=\"refresh\" content=\"5\">");
client.println("<h1>SprinkDuino</h1>");
client.println("
");
client.print("<table border=1><tr>");
client.print("<td><a href=\"./?A=T;\"><button>Turn All On</button></a>");
client.print("<a href=\"./?A=F;\"><button>Turn All Off</button></a>
");
//Individual pin control
for(int i = 0; i < 8; i++){
if ((val == (i + 2)) && (trueFalse == 'T')) {
digitalWrite(pin[i],LOW);
}
else if ((val == (i + 2)) && (trueFalse == 'F')) {
digitalWrite(pin[i],HIGH);
}
//Turn all on or off
else if ((val == 1) && (trueFalse == 'T')) {
for(int i = 0; i < 8; i++){
digitalWrite(pin[i],LOW);
}
}
else if ((val == 1) && (trueFalse == 'F')) {
for(int i = 0; i < 8; i++){
digitalWrite(pin[i],HIGH);
}
}
//Changes state[] depending on pin status
for(int h = 0; h < 8; h++){
if(digitalRead(pin[h]) == LOW){
state[h] = "ON";
}
else{
state[h] = "OFF";
}
}
client.print("Sprinkler ");
client.print(pin[i]);
client.print(" is ");
client.print(state[i]);
if (state[i] == "ON") {
client.print("<a href=\"./?");
client.print(pin[i]);
client.print("=F;\"><button>Turn Off</button><a>");
}
else {
client.print("<a href=\"./?");
client.print(pin[i]);
client.print("=T;\"><button>Turn On</button><a>");
}
client.print("
");
}
client.print("</td><td>");
client.print("<h1>Schedule</h1>");
client.print("</td></tr>");
client.print("</table>");
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);
readString[0] = '\0';
// close the connection:
client.stop();
}
}
// 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();
}
Thanks for any help!