Thanks! That really got me a good start. I looked at the Timealarms library and it seems to fit the bill at this stage.
For now, I setup a single timer to turn on relay 1 and relay 2, as both are connected to lights. I want the relays on for 20 hours, and off for 4. So I used one alarm that will turn them on at 04:00 (4AM), and another to turn them off at 00:00 (midnight).
Additionally, I included the relayControl command out of the particle documentation, so that I can send an API call to turn on or off any of the relays remotely. Since I already have this app communicating values to Ubidots, I hope to use buttons on the ubidots dashboard to send these API calls (will that work?). This is a simple over-ride, so that I can turn the lights off if I need to change a bulb or something like that.
I started with this up top:
//setup the relays
int RELAY1 = D3;
int RELAY2 = D4;
int RELAY3 = D5;
int RELAY4 = D6;
// function for relay control from particle documentation
// command format r1,HIGH
int relayControl(String command)
{
int relayState = 0;
// parse the relay number
int relayNumber = command.charAt(1) - '0';
// do a sanity check
if (relayNumber < 1 || relayNumber > 4) return -1;
// find out the state of the relay
if (command.substring(3,7) == "HIGH") relayState = 1;
else if (command.substring(3,6) == "LOW") relayState = 0;
else return -1;
// write to the appropriate relay
digitalWrite(relayNumber+2, relayState);
return 1;
}
// defines what the LightsOn, Lights off commands do
void LightsOn()
{
digitalWrite(RELAY1, HIGH); // turn on the lights connected to relay 1
digitalWrite(RELAY2, HIGH); // turn on the lights connected to relay 2
Serial.println("Lights are turned on"); // print the status of the lights
}
void LightsOff()
{
digitalWrite(RELAY1,LOW); // turn off lights on relay 1
digitalWrite(RELAY2,LOW); // turn off lights on relay 2
Serial.println("Lights are turned off"); // print the status of the lights
}
then in my setup:
// set the time zone
Time.zone(-8);
// Setup the Relay Shield pins
pinMode(RELAY1, OUTPUT);
pinMode(RELAY2, OUTPUT);
pinMode(RELAY3, OUTPUT);
pinMode(RELAY4, OUTPUT);
// Initialize all relays to an OFF state
digitalWrite(RELAY1, LOW);
digitalWrite(RELAY2, LOW);
digitalWrite(RELAY3, LOW);
digitalWrite(RELAY4, LOW);
// Setup Particle variables
Particle.variable("i2cdevice", "SHT31");
Particle.variable("cTemp", cTemp);
Particle.variable("humidity", humidity);
Particle.variable("lux", tLux);
Particle.variable("Lights", LightsOn);
// Setup Particle function for relay control
Particle.function("relay", relayControl);
// create the alarms
Alarm.alarmRepeat(4,00,0, LightsOn); // 4:00am every day
Alarm.alarmRepeat(0,00,10,LightsOff); // 00:00am every day
nothing related to the timer goes into the loop afaik.
The code builds without errors. Can you help me improve my code and skills by suggesting where you would do things differently? I am not very confident with what order everything should be in.
My next step is to setup an over-ride button in Ubidots to manually turn the lights on and off. This should not mess with or disable the alarms, so if I over-ride the lights on, when the alarm comes, they should just stay on.
The path I am paving here is hopefully leading to an android homescreen widget that displays my garden parameters and allows some basic control of lights, fans, and solenoids. I am not sure if ubidots is the way to this goal, but for now, it seems to be an easy way to bring the data to life.