Using IFTTT with my Photon

I found a photo resistor (it the thing with a wavy red line on the top of it) and I would like to use it to control some smart light bulbs though a smart hub. I figured out how to do the THEN THAT part of the IF THIS THEN THAT statement bulb how do I use a photo resistor and a resistor (so I don’t kill my Photon) with IFTT?

If you could explain in a bit more detail what the IF THIS scenario would look like, we might be able to give you some clues. You mention you’d “like to control smart lights with a photo-resistor”, how exactly have you imagined that?

At my house we have a wink hub that controls object like light bulbs in our house and I have two cree bulbs that are controlled at I have setup a short cut in the wink app that hopefully will get activated and turn on the lightbulb when the photo resistor changes it’s value because the room will get darker and then I want the lights to turn on.

That sounds very do-able. So basically, you want to let IFTTT switch some light bulb if your value drops below a certain treshold? There’s a nice tutorial on LDRs on Adafruit, if you don’t yet know how they work. I’d advice you to read that, and get it working locally (printing values over Serial). Once you’ve got that working, I’d suggest implementing logic in your code that sends a SSE once the value drops below your threshold.
Since the variable tracking system on IFTTT is a polling process, which can take several minutes, I’d rather use SSEs. In your application it might suffice, since it isn’t anything critical (like an alarm). Anyhow, try to get it working locally, then we’ll proceed from there.

Should I use a resistor with my photo resistor?

Most definitely. Please do take a look at the link provided in my comment above. This will show you how to properly use a photoresistor. Rather than having to explain every step, that should get you up and running in no time :smile: Take in mind that some elements are different on the Photon in comparison with a ’ regular’ Arduino. analogRead() will go to 4095, and the voltage should be taken from 3.3V, rather than 5V.

Here is how I have my Photon setup on my bread board. The Picture is from my computer’s webcam.

I think I have it setup right but I got stuck on how to setup on how to setup the code for the project.

Unfortunately, that picture isn’t clear enough to see whether or not it’s hooked up correctly. If you’ve followed the steps in the Adafruit guide, you should have something along the lines of: 3.3V -> LDR (*) -> Resistor -> GND. Between the LDR and the resistor, where the (*) is, should be a wire going to one of the analog pins.

As far as code goes. The Adafruit guide has a nice example, which is worthwhile trying out.

You can try this code to see the raw values. You’ll have to use a serial monitor to see them. When using USB, you can use the Particle-CLI with particle serial monitor to see the output. Give it a try, and let me know if that worked for you.

#define LDRPin A0
int reading;

void setup() {
    pinMode(LDRPin, INPUT);
    Serial.begin(9600);
    delay(7500);
    Serial.println("Well, hello there, I'm going to show you the LDR values");
}

void loop() {
    reading = analogRead(LDRPin);
    Serial.println(reading);
    delay(1000);
}
1 Like

I don’t have enough jumper wires but I guess I can make some! I lost mine and I can’t seam to find them.

I put my hand in front of a light and the numbers dropped. Here is what I got
Opening serial monitor for com port: “/dev/cu.usbmodem1421”

153
152
152
152
153
152
152
152
153
153
153
153
152
152
152
153
154
154
154
154
154
31
162
165
168
108
90
168
172
172
154
169
172
171
171
162
175
176
177
177
178
177
178
178
177
178
178
178
178
107
134
171
170
128
32
28
123
172
172
176
119
30
30
24
31
176
177
176
54
43
35
27
32
175
177

Looks Iike you’ve got it working! The values you get are pretty small. What value resistor are you using? If you increase that value, you should get a bigger range.
You should then decide on a treshold value you want to use as a trigger. If possible, put your sensor in the place you want it to be when finished. Then, use your smart lights to dim the brightness to where you think the lights should turn on. Check what value it’s outputting at that time, and note that. Let me know when you’ve done that, and we’ll proceed from there.

I was using a dim light. I can’t really get that much light where I am right now. Does the Photon have to be connected to my computer for this to work?
Based on the values I gave you what do you think is a good thresh hold?

In order to get the best values for your Smart Light scenario, you should try placing it where you want it to be when finished. This will ensure that the values match to ‘real life’.

For a Serial connection to work, you will indeed need a USB connection. You could substitute this with a Spark.publish();, which will publish your value to the cloud. Keep in mind that this is limited to 1 per second (the same as your Serial prints were in this code). If you use the Particle-cli, you can subscribe to these events by executing particle subscribe mine. That way you can place your Photon away from your computer, and still be able to see the values. Hopefully, you can then place it at the ‘final destination’.

I can’t really comment on that, since that’ll be a personal preference. The value drops according to how much light it gets. Less light is a lower value. You have to decide at what brightness you want your lights to turn on, and then find the matching value. Like I mentioned in my previous post, by increasing the resistor value, you should get a greater range in the values, which makes it easier to determine a good one to be used. Please give that a try, and you’ll notice the values increase.

One event per second is fine for what I am using it for. Can you add Spark.publish(reading); to the code? Then I will set the IFTTT trigger to new event published.

In ‘production’ I wouldn’t publish every second. I would only publish if the treshold is met. The once-per-second publish I’d only use to figure out where I’d put the treshold at. So basically, it’s like the Serial.println(), but wireless.
No need for me to add that to the code, you’re more than capable to do that yourself. There’s no fun in it if I do everything, now is there ;)?
I wouldn’t yet involve IFTTT until you’ve got the code on the Photon exactly the way you want it. Including it now is too much of a hassle, and not really useful at the moment, other than add confusion.

Alright but where do I put Spark.publish(reading);?

Jumping in on advice for the photo-resistor circuit. The circuit is a two part resistor divider circuit. Without going into the theory or the math, here are two rules of thumb. First, you will get the largest range of values from the circuit if the value of the two resistors is exactly equal. The range drops off on either side if the resistor is more or fewer ohms than the photo-resistor. The second rule of thumb is that the largest Vout of the circuit will be 1/2 of the Vsupply. So for a 3.3V supply the highest Vout will be 1.65V, or half the range of the analog input of a Core/Photon.

I’ve used the +5 from the USB (what powers the core) as the Vsupply for the circuit. The range of the resistor is then R > 0 to R = 2.5V. I’ve yet to see a photo-resistor go to 0 Ohms in pitch black, so you will not get 0 volts at the dark end.

I replaced Serial.println(reading); with Spark.publish(reading); and I got this error.

In file included from ../inc/spark_wiring.h:29:0,
from ../inc/application.h:29,
from photo_resistor.cpp:2:
../../core-common-lib/SPARK_Firmware_Driver/inc/config.h:12:2: warning: #warning "Defaulting to Release Build" [-Wcpp]
#warning "Defaulting to Release Build"
^
photo_resistor.cpp: In function 'void loop()':
photo_resistor.cpp:13:26: error: invalid conversion from 'int' to 'const char*' [-fpermissive]
Serial.println("Well, hello there, I'm going to show you the LDR values");
^
In file included from ../inc/spark_wiring.h:33:0,
from ../inc/application.h:29,
from photo_resistor.cpp:2:
../inc/spark_utilities.h:110:14: error: initializing argument 1 of 'static void SparkClass::publish(const char*)' [-fpermissive]
static void publish(const char *eventName);
^
make: *** [photo_resistor.o] Error 1

The error was to be expected, since it doesn’t follow the right syntax. It was more of a general idea of how it could be implemented, rather than a copy&paste example. That said, this should work: Spark.publish("LDR", String(reading)); Do take a look at the docs to get an impression of why this work the way it does, and why it threw you that error.


@Bendrix

Assuming the setup from above is being used, and the LDR resistance goes towards 0, your voltage would increase, rather than decrease?