Growlight control w/ timers


#1

This is a program for controlling my growlight for my plants. It has the option of two timers at 4 hrs or 8 hrs that will automatically turn it off if you want. Its all running on a Wemos D1 Mini what is stacked with a relay shield and a proto board that I have setup to drop the voltage from 12v(*from inside the light) to 5v for the board.

Something to note, CAYENNE_CONNECTED() and CAYENNE_DISCONNECTED() functions have to be enabled by modifying the cayenne library. Easy to do from other posts on here.

Will update pics of completed project in coming days.


**

Code:

#include <Arduino.h>

// #define CAYENNE_DEBUG Serial
// #define CAYENNE_PRINT Serial
#include "CayenneDefines.h"
#include "BlynkSimpleEsp8266.h"
#include "CayenneWiFiClient.h"
#include <SimpleTimer.h>

SimpleTimer timer;


char token[] = "key";
char ssid[] = "devices";
char password[] = "pass";


#define relayPin 5

bool online = false;

int disco = 0;
byte relay;

unsigned long time1;
unsigned long onlineTime;
unsigned long prevTime;
unsigned long eightHrTmr;
unsigned long fourHrTmr;
unsigned long expire;



void setup()
{
	// Serial.begin(9600);
	Cayenne.begin(token, ssid, password);

	eightHrTmr = timer.setTimeout(28800000L, timerDone);
	fourHrTmr = timer.setTimeout(14400000L, timerDone);
	timer.disable(eightHrTmr);
	timer.disable(fourHrTmr);

	pinMode(BUILTIN_LED, OUTPUT);
	pinMode(relayPin, OUTPUT);
}




void loop()
{
	Cayenne.run();
	timer.run();

	time1 = millis();


	if (relay == LOW)
		{
			digitalWrite(relayPin, LOW);
		}

	else
		{
			digitalWrite(relayPin, HIGH);
		}
}




void timerDone()
{
	relay = LOW;
	Cayenne.virtualWrite(V1, LOW);
	Cayenne.virtualWrite(V2, LOW);
	expire = 0;
}



CAYENNE_CONNECTED()
{
	online = true;
	digitalWrite(BUILTIN_LED, LOW);
}


CAYENNE_DISCONNECTED()
{
	online = false;
	digitalWrite(BUILTIN_LED, HIGH);
	prevTime = millis();
	disco++;
}


CAYENNE_IN(V0)
{
	relay = getValue.asInt();
}


CAYENNE_IN(V1)
{
	int tmr4 = getValue.asInt();

	if (tmr4 == 1)
		{
			if (timer.isEnabled(fourHrTmr))
				{
					delay(1);
				}

			else
				{
					timer.enable(fourHrTmr);
					expire = (millis() + 14400000L);
				}
		}

	else
		{
			timer.disable(fourHrTmr);
			expire = 0;
		}
}


CAYENNE_IN(V2)
{
	int tmr8 = getValue.asInt();

	if (tmr8 == 1)
		{
			if (timer.isEnabled(eightHrTmr))
				{
					delay(1);
				}

			else
				{
					timer.enable(eightHrTmr);
					expire = (millis() + 28800000L);
				}
		}

	else
		{
			timer.disable(eightHrTmr);
			expire = 0;
		}
}


CAYENNE_OUT(V10)
{
	if (timer.isEnabled(fourHrTmr) || timer.isEnabled(eightHrTmr))
		{
			Cayenne.virtualWrite(V10, ((expire - time1) / 1000) / 60);
		}

	else
		{
			Cayenne.virtualWrite(V10, 0);
		}

}



CAYENNE_OUT(V23)
{
	Cayenne.virtualWrite(V23, disco);
}

CAYENNE_OUT(V24)
{
	float time2 = (time1 / 1000) / 60;
	Cayenne.virtualWrite(V24, time2);
}

CAYENNE_OUT(V25)
{
	onlineTime = ((time1 - prevTime) / 1000) / 60;
	Cayenne.virtualWrite(V25, onlineTime);
}

**


#2

Post the pics when you get a chance!

-Benny


#3

The Wemos D1 Mini and components are inside the light fixture so you don’t even see them. Will put up a couple more pics later today when I get the bulk of my plants under it :slight_smile:


#4

Updated code as I was having problems with the simpletimer library for some reason… this new way works better anyway.

#include <Arduino.h>

// #define CAYENNE_DEBUG Serial
// #define CAYENNE_PRINT Serial
#include "CayenneDefines.h"
#include "BlynkSimpleEsp8266.h"
#include "CayenneWiFiClient.h"



char token[] = "zadg3a574d";
char ssid[] = "devices";
char password[] = "open4meplease";


#define relayPin 5

bool online = false;

byte eightHrTimer;
byte fourHrTimer;
unsigned long expireTime;

int disco = 0;
byte relay;

unsigned long time1;
unsigned long onlineTime;
unsigned long prevTime;




void setup()
{
	// Serial.begin(9600);
	Cayenne.begin(token, ssid, password);

	pinMode(BUILTIN_LED, OUTPUT);
	pinMode(relayPin, OUTPUT);
}




void loop()
{
	time1 = millis();

	Cayenne.run();



	if (relay == LOW)
		{
			digitalWrite(relayPin, LOW);
		}

	else
		{
			digitalWrite(relayPin, HIGH);
		}

	if (fourHrTimer == 1 || eightHrTimer == 1)
		{
			if (expireTime < millis())
				{
					timerDone();
				}
		}
}




void timerDone()
{
	relay = LOW;
	Cayenne.virtualWrite(V0, LOW);
	Cayenne.virtualWrite(V1, LOW);
	Cayenne.virtualWrite(V2, LOW);
	fourHrTimer = 0;
	eightHrTimer = 0;
	expireTime = 0;
}



CAYENNE_CONNECTED()
{
	online = true;
	digitalWrite(BUILTIN_LED, LOW);
}


CAYENNE_DISCONNECTED()
{
	online = false;
	digitalWrite(BUILTIN_LED, HIGH);
	prevTime = millis();
	disco++;
}


CAYENNE_IN(V0)
{
	relay = getValue.asInt();
}


CAYENNE_IN(V1)
{
	fourHrTimer = getValue.asInt();

	if (fourHrTimer == 1)
		{
			expireTime = (millis() + 14400000L);
		}

	else
		{
			expireTime = 0;
		}
}


CAYENNE_IN(V2)
{
	eightHrTimer = getValue.asInt();

	if (eightHrTimer == 1)
		{
			expireTime = (millis() + 28800000L);
		}

	else
		{
			expireTime = 0;
		}
}


CAYENNE_OUT(V10)
{
	if (fourHrTimer == 1 || eightHrTimer == 1)
		{
			Cayenne.virtualWrite(V10, ((expireTime - time1) / 1000) / 60);
		}

	else
		{
			Cayenne.virtualWrite(V10, 0);
		}

}



CAYENNE_OUT(V23)
{
	Cayenne.virtualWrite(V23, disco);
}

CAYENNE_OUT(V24)
{
	float time2 = (time1 / 1000) / 60;
	Cayenne.virtualWrite(V24, time2);
}

CAYENNE_OUT(V25)
{
	onlineTime = ((time1 - prevTime) / 1000) / 60;
	Cayenne.virtualWrite(V25, onlineTime);
}

#5

Thanks for the update @vapor83, love it!

-Benny


#6

Hi, can you tell me how much time is your light on/off and how I can modify these values?
I am new to these kind of stuff and try to understand it.
Beside to your project, I want to add an DHT11 shield to my D1 mini.

Thanks!

EDIT: I just saw that you have 4&8 hour timer.


#7

@gaby_korupt this post may help you with your DHT11 code :slight_smile:


#8

@gaby_korupt This is how you change you timer duration. In the code below, there is a number that is added to millis() and that is the duration counting in millis(). So, you can change that number to anything you want you time to be. ((14400000/1000)/60)/60 = 4hrs. So 1 hr would be 1000(millis per second)*60(seconds in a minute)*60(minutes in a hour). You add the “L” to the number because that forces it to be a “long” so the number is calculated correctly.

Does that make sense or were you asking something else. :slight_smile: