I have a project that I’m working on. I have an Adafruit light sensor TSL2651, I also have an Iot relay I bought. I want to hook the sensor up with the relay so essentially when someone turns a light on, it trips the relay. When someone turns the light off, it shuts the relay off.
I have been able to get the two working independently, whenever I try to join both functionalities I can’t figure it out. I thought it would be as simple as, I need to create a new app, import the TSL2651 library, import the relay library and can do a couple if else statements that if the light levels drop below X turn the relay on (which i have plugged into pin D7)
Any help, or links to any project you have come across in the past please let me know!
I want to include the tsl2651 library, I’m trying to compute illuminance into a value. Take the value and say, if it is greater than 5 enable pin D7, if it is less than 5 disable pin D7. Right now, it only works in the sense that there is a parse function that I can type in Enable (pinnumber) and it will turn my relay on. Would it be better to get rid of the check for which pin it’s in and specifiy D7? Obviously i’m new to this, trying to teach myself more things!
Thanks
#include "tsl2561.h"
double illuminance;
void setup() {
Spark.function("parse", parse);
Particle.variable("illuminance", illuminance);
// 0-7 maps to D0-D7
for (int i = 0; i <= 7; i++) {
pinMode(i, OUTPUT);
}
}
void loop() {
}
if(!tsl.getLux(integrationTime,broadband,ir,illuminance))
{
error_code = tsl.getError();
strcpy(tsl_status,"getLuxError");
operational = false;
}
int parse(String cmd) {
cmd.trim();
cmd.toUpperCase();
if (cmd.startsWith("ENABLE")) {
int pin = cmd.substring(6).toInt();
digitalWrite(pin, HIGH);
}
if (cmd.startsWith("DISABLE")) {
int pin = cmd.substring(7).toInt();
digitalWrite(pin, LOW);
}
}
Hmm, considering you're new to this, why not take some steps back and start simple?
Try making the comparison in the Particle function for example. Send it a value, and check whether or not it's greater/smaller than the one you need. Then, have it act accordingly. Once you've got that going, you can start to merge it with the sensor values.
void setup() {
Spark.function("parse", parse);
//declare D7 as my pin for the relay
int pin = D7;
}
void loop() {
}
int parse(String cmd) {
cmd.trim();
cmd.toUpperCase();
if (cmd.startsWith("ENABLE")) {
int pin = D7;
digitalWrite(D7, HIGH);
}
if (cmd.startsWith("DISABLE")) {
int pin = D7;
digitalWrite(D7, LOW);
}
}
Ahh okay, yes very simple. After I write this, to proceed would I then worry about the sensor library and value would be whatever reading I get from my LUX sensor?
So here’s how I have my sensor connected. I was digging through the particle documentation, and it looks like i’ll want to create a variable, lets say Light in the cloud, assign the variable to the pins D0, D1 as analogRead and then take that value and use it to trip my relay on or off?
I think this project can help you here, if I understand your needs: depending on the available light, an LED strip is switched on via relay or mosfets (or even your normal lamp)
Instead of your sensor I´m using a photo resistor.
That depends entirely on where you want to use that ‘if’ statement. You can write a fully functional application without using the loop. (Might not be ideal, but possible)
Ended up getting it working. I have a 12" Traffic light hanging in the warehouse at work now, turning on when someone flips the bathroom light on. For the interim it is awesome, but I have to come up with a fail-safe in case someone leaves the light on.