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
// defines what the LightsOn, Lights off commands do
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
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
// Setup the Relay Shield pins
// Initialize all relays to an OFF state
// Setup Particle variables
// Setup Particle function for relay control
// 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.