Am have been working on a control for my pool. After much cursing, i have it worked out where i can control i t manually with 3 buttons (Pump, Heat, Light) or with a web page. It is working quite well in fact.
My next hurdle is making a timer. To do this, i will need set the time in Arduino which i have been able to do using NTP time sync.
I have the time.h library installed and from what i have read, there should be a function called setTime(). I don't seem to be able to get this to work. When i try to use it the IDE throws and error "no matching call".
My issue is this: I want to store the current time and start time in a variable so that i can set the pump to start a 6am and shut off at 1pm. For the sake of argument, lets say we call the variables "startTime" "and curTime"
if((curTime)>=(startTime) && (curTime)<(endTime))
digitalWrite(5, HIGH); // pump is running on D5
So what i am trying to do figure out how to set the value of (curTime) to the current time.
As far as the setTime function, i am using the updated version of time.h Apparently i am not the only one who has had this issue however I can't seem to find a solution for it.
That's a problem. Use the time_t data type from the Time.h library. This is a 32-bin unsigned integer, basically Unix epoch time, seconds since 01Jan1970.
When i add what you suggested, i still get the same error indicating that time_t was not declared.
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
#include <time.h>
// size of buffer used to capture HTTP requests
#define REQ_BUF_SZ 60
// MAC address from Ethernet shield sticker under board
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 0, 24); // IP address, may need to change depending on network
EthernetServer server(80); // create a server at port 80
File webFile; // the web page file on the SD card
char HTTP_req[REQ_BUF_SZ] = {0}; // buffered HTTP request stored as null terminated string
char req_index = 0; // index into HTTP_req buffer
boolean LED_state[4] = {0}; // stores the states of the LEDs
int pumpSw = (31);
int lightSw = (32);
int heatSw = (33);
//TIMER VARIABLES=============================================================
/*
int curTime;
int startTime;
int endTime;
*/
//============================================================================
void setup()
{
// disable Ethernet chip
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);
Serial.begin(9600); // for debugging
// initialize SD card
Serial.println("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("ERROR - SD card initialization failed!");
return; // init failed
}
Serial.println("SUCCESS - SD card initialized.");
// check for index.htm file
if (!SD.exists("index.htm")) {
Serial.println("ERROR - Can't find index.htm file!");
return; // can't find index file
}
Serial.println("SUCCESS - Found index.htm file.");
// switches on pins 2, 3 and 5
pinMode(2, INPUT);
pinMode(3, INPUT);
//pinMode(4, INPUT);
//pinMode(5, INPUT);
pinMode(31,INPUT);
pinMode(32,INPUT);
pinMode(33,INPUT);
// LEDs
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(50,OUTPUT);
pinMode(22,OUTPUT); //to power breadboard
Ethernet.begin(mac, ip); // initialize Ethernet device
server.begin(); // start to listen for clients
Serial.print("IP Address is");
Serial.println(Ethernet.localIP ());
Serial.println(digitalRead(2));
digitalWrite(22, HIGH);
}
void loop()
{
//TIMER ON/OFF CODE===========================================================
/* if((curTime)>=(startTime) && (curTime)<(endTime))
digitalWrite(5, HIGH); // pump is running on D5
*/
// ===========================================================================
EthernetClient client = server.available(); // try to get client
if (client) { // got client?
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) { // client data available to read
char c = client.read(); // read 1 byte (character) from client
// limit the size of the stored received HTTP request
// buffer first part of HTTP request in HTTP_req array (string)
// leave last element in array as 0 to null terminate string (REQ_BUF_SZ - 1)
if (req_index < (REQ_BUF_SZ - 1)) {
HTTP_req[req_index] = c; // save HTTP request character
req_index++;
}
// last line of client request is blank and ends with \n
// respond to client only after last line received
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
// remainder of header follows below, depending on if
// web page or XML page is requested
// Ajax request - send XML file
if (StrContains(HTTP_req, "ajax_inputs")) {
// send rest of HTTP header
client.println("Content-Type: text/xml");
client.println("Connection: keep-alive");
client.println();
SetLEDs();
// send XML file containing input states
XML_response(client);
}
else { // web page request
// send rest of HTTP header
client.println("Content-Type: text/html");
client.println("Connection: keep-alive");
client.println();
// send web page
webFile = SD.open("index.htm"); // open web page file
if (webFile) {
while(webFile.available()) {
client.write(webFile.read()); // send web page to client
}
webFile.close();
}
}
// display received HTTP request on serial port
Serial.print(HTTP_req);
// reset buffer index and all buffer elements to 0
req_index = 0;
StrClear(HTTP_req, REQ_BUF_SZ);
break;
}
// every line of text received from the client ends with \r\n
if (c == '\n') {
// last character on line of received text
// starting new line with next character read
currentLineIsBlank = true;
}
else if (c != '\r') {
// a text character was received from client
currentLineIsBlank = false;
}
} // end if (client.available())
} // end while (client.connected())
delay(1); // give the web browser time to receive the data
client.stop(); // close the connection
} // end if (client)
// Manual pump control-----------------------------------------------------
if (digitalRead(pumpSw)==1)
if(digitalRead(5)==LOW)
{
LED_state[0] = 1; // save LED state
digitalWrite(5, HIGH);
delay(500);
}
else if (digitalRead(5)==HIGH)
{
LED_state[0] = 0; // save LED state
digitalWrite(5, LOW);
delay(500);
}
// Manual light control-----------------------------------------------------
if (digitalRead(lightSw)==1)
if(digitalRead(6)==LOW)
{
LED_state[1] = 1; // save LED state
digitalWrite(6, HIGH);
delay(500);
}
else if (digitalRead(lightSw)==1)
{
LED_state[1] = 0; // save LED state
digitalWrite(6, LOW);
delay(500);
}
// Manual heat control-----------------------------------------------------
if(digitalRead(5)==HIGH)
{
if (digitalRead(heatSw)==1)
if(digitalRead(7)==LOW)
{
LED_state[2] = 1; // save LED state
digitalWrite(7, HIGH);
delay(500);
}
else if (digitalRead(heatSw)==1)
{
LED_state[2] = 0; // save LED state
digitalWrite(7, LOW);
delay(500);
}
}
}
// checks if received HTTP request is switching on/off LEDs
// also saves the state of the LEDs
==============REMAINDER OF CODE DELETED DUE TO SIZE > 9500 CHARACTERS=========================
needahobby:
When i add what you suggested, i still get the same error indicating that time_t was not declared.
Impossible to check your code, it was truncated. It's also a lot of code to check
Have you tried one of the simple examples that come with the Time library? Are you sure the Time library is installed correctly? Seems I remember that the way it is zipped, it may end up down one folder level from where it should be.
It should be in
sketchbook\libraries\Time
and should contain an Examples folder, DateStrings.cpp, Time.cpp, Time.h, keywords.txt and Readme.txt.
Be sure it's not in
sketchbook\libraries\Time\Time ... or something similar
That could be the problem but if i try to call the setTime() function in the time example, I still get an error.
question:
If i add a library such as time into the libraries. This contains time.h. Does this now mean that time.h is available for any sketch or just those that are in the libraries>time folder?
needahobby:
That could be the problem but if i try to call the setTime() function in the time example, I still get an error.
If the examples give the same error, that sure sounds like the library is not installed correctly.
If i add a library such as time into the libraries. This contains time.h. Does this now mean that time.h is available for any sketch or just those that are in the libraries>time folder?
Any sketch. Your sketches don't belong in the libraries folder. If you write libraries, then they can go in the libraries folder.